Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

✨ [POC] Before error session replay recording mode #3038

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 13 additions & 7 deletions packages/rum/src/boot/recorderApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ export function makeRecorderApi(
})

let cachedDeflateEncoder: DeflateEncoder | undefined
let flushCachedRecords: () => void = noop

function getOrCreateDeflateEncoder() {
if (!cachedDeflateEncoder) {
Expand All @@ -144,12 +145,20 @@ export function makeRecorderApi(

startStrategy = (options?: StartRecordingOptions) => {
const session = sessionManager.findTrackedSession()
if (!session || (session.sessionReplay === SessionReplayState.OFF && (!options || !options.force))) {
if (!session) {
state = { status: RecorderStatus.IntentToStart }
return
}

if (state.status === RecorderStatus.Starting || state.status === RecorderStatus.Started) {
if (state.status === RecorderStatus.Starting) {
return
}

if (state.status === RecorderStatus.Started) {
if (options && options.force && session.sessionReplay === SessionReplayState.OFF) {
flushCachedRecords()
sessionManager.setForcedReplay()
}
return
}

Expand All @@ -168,7 +177,7 @@ export function makeRecorderApi(
return
}

const { stop: stopRecording } = startRecordingImpl(
const { stop: stopRecording, flushCachedRecords: stopCaching } = startRecordingImpl(
lifeCycle,
configuration,
sessionManager,
Expand All @@ -179,11 +188,8 @@ export function makeRecorderApi(
status: RecorderStatus.Started,
stopRecording,
}
flushCachedRecords = stopCaching
})

if (options && options.force && session.sessionReplay === SessionReplayState.OFF) {
sessionManager.setForcedReplay()
}
}

stopStrategy = () => {
Expand Down
29 changes: 24 additions & 5 deletions packages/rum/src/boot/startRecording.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import type { RawError, HttpRequest, DeflateEncoder } from '@datadog/browser-core'
import { createHttpRequest, addTelemetryDebug, canUseEventBridge } from '@datadog/browser-core'
import { createHttpRequest, addTelemetryDebug, canUseEventBridge, noop } from '@datadog/browser-core'
import type { LifeCycle, ViewHistory, RumConfiguration, RumSessionManager } from '@datadog/browser-rum-core'
import { LifeCycleEventType } from '@datadog/browser-rum-core'
import { LifeCycleEventType, SessionReplayState } from '@datadog/browser-rum-core'

import { record } from '../domain/record'
import { startSegmentCollection, SEGMENT_BYTES_LIMIT } from '../domain/segmentCollection'
import type { BrowserRecord } from '../types'
import { startRecordBridge } from '../domain/startRecordBridge'
import { startRecordsCaching } from '../domain/recordsCaching'

export function startRecording(
lifeCycle: LifeCycle,
Expand All @@ -28,8 +29,9 @@ export function startRecording(
createHttpRequest(configuration, configuration.sessionReplayEndpointBuilder, SEGMENT_BYTES_LIMIT, reportError)

let addRecord: (record: BrowserRecord) => void
let flushCachedRecords: () => void = noop

if (!canUseEventBridge()) {
const initSegmentCollection = () => {
const segmentCollection = startSegmentCollection(
lifeCycle,
configuration,
Expand All @@ -38,21 +40,38 @@ export function startRecording(
replayRequest,
encoder
)
addRecord = segmentCollection.addRecord
cleanupTasks.push(segmentCollection.stop)
return { addRecord: segmentCollection.addRecord }
}

if (!canUseEventBridge()) {
const session = sessionManager.findTrackedSession()!
if (session.sessionReplay === SessionReplayState.OFF) {
const cacheInitResult = startRecordsCaching()
addRecord = cacheInitResult.addRecord

flushCachedRecords = () => {
;({ addRecord } = initSegmentCollection())
const records = cacheInitResult.getRecords()
records.forEach((record: BrowserRecord) => addRecord(record))
}
} else {
;({ addRecord } = initSegmentCollection())
}
} else {
;({ addRecord } = startRecordBridge(viewHistory))
}

const { stop: stopRecording } = record({
emit: addRecord,
emit: (record) => addRecord(record),
configuration,
lifeCycle,
viewHistory,
})
cleanupTasks.push(stopRecording)

return {
flushCachedRecords,
stop: () => {
cleanupTasks.forEach((task) => task())
},
Expand Down
1 change: 1 addition & 0 deletions packages/rum/src/domain/recordsCaching/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './recordsCaching'
18 changes: 18 additions & 0 deletions packages/rum/src/domain/recordsCaching/recordsCaching.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import type { BrowserRecord } from '../../types'

export function startRecordsCaching() {
const records: BrowserRecord[] = []

function addRecord(record: BrowserRecord) {
records.push(record)
}

function getRecords() {
return records
}

return {
addRecord,
getRecords,
}
}