Skip to content

Commit

Permalink
⚗✨ [RUMF-819] postpone recording
Browse files Browse the repository at this point in the history
  • Loading branch information
BenoitZugmeyer committed Jan 20, 2021
1 parent 5fa8698 commit bee9dd5
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 4 deletions.
50 changes: 48 additions & 2 deletions packages/rum-recorder/src/boot/rumRecorderPublicApi.spec.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,25 @@
import { Configuration } from '@datadog/browser-core'
import { RumPublicApi, StartRum } from '@datadog/browser-rum-core'
import { makeRumRecorderPublicApi, StartRecording } from './rumRecorderPublicApi'

const DEFAULT_INIT_CONFIGURATION = { applicationId: 'xxx', clientToken: 'xxx' }

describe('makeRumRecorderPublicApi', () => {
let rumGlobal: RumPublicApi
let rumGlobal: RumPublicApi & { startSessionRecord?(): void }
let startRecordingSpy: jasmine.Spy<StartRecording>
let startRumSpy: jasmine.Spy<StartRum>
let enabledFlags: string[] = []

beforeEach(() => {
enabledFlags = []
startRecordingSpy = jasmine.createSpy()
startRumSpy = jasmine.createSpy().and.callFake(() => {
return ({} as unknown) as ReturnType<StartRum>
const configuration: Partial<Configuration> = {
isEnabled(flag: string) {
return enabledFlags.includes(flag)
},
}
return ({ configuration } as unknown) as ReturnType<StartRum>
})
rumGlobal = makeRumRecorderPublicApi(startRumSpy, startRecordingSpy)
})
Expand All @@ -38,4 +46,42 @@ describe('makeRumRecorderPublicApi', () => {
expect(getCommonContext().hasReplay).toBe(true)
})
})

describe('experimental flag postpone_start_recording', () => {
it('if disabled, startSessionRecord should not be defined', () => {
expect(rumGlobal.startSessionRecord).toBeUndefined()
rumGlobal.init(DEFAULT_INIT_CONFIGURATION)
expect(rumGlobal.startSessionRecord).toBeUndefined()
})

it('if enabled, recording should not start when calling init()', () => {
enabledFlags = ['postpone_start_recording']
rumGlobal.init(DEFAULT_INIT_CONFIGURATION)
expect(startRecordingSpy).not.toHaveBeenCalled()
})

it('if enabled, startSessionRecord should be defined', () => {
enabledFlags = ['postpone_start_recording']
expect(rumGlobal.startSessionRecord).toBeUndefined()
rumGlobal.init(DEFAULT_INIT_CONFIGURATION)
expect(rumGlobal.startSessionRecord).toEqual(jasmine.any(Function))
})

it('if enabled, commonContext.hasReplay should be true only if startSessionRecord is called', () => {
enabledFlags = ['postpone_start_recording']
rumGlobal.init(DEFAULT_INIT_CONFIGURATION)
expect(getCommonContext().hasReplay).toBe(false)
rumGlobal.startSessionRecord!()
expect(getCommonContext().hasReplay).toBe(true)
})

it('if enabled, calling startSessionRecord multiple times should only start recording once', () => {
enabledFlags = ['postpone_start_recording']
rumGlobal.init(DEFAULT_INIT_CONFIGURATION)
rumGlobal.startSessionRecord!()
rumGlobal.startSessionRecord!()
rumGlobal.startSessionRecord!()
expect(startRecordingSpy).toHaveBeenCalledTimes(1)
})
})
})
21 changes: 19 additions & 2 deletions packages/rum-recorder/src/boot/rumRecorderPublicApi.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { monitor } from '@datadog/browser-core'
import { makeRumPublicApi, StartRum } from '@datadog/browser-rum-core'

import { startRecording } from './recorder'
Expand All @@ -6,13 +7,29 @@ export type StartRecording = typeof startRecording

export function makeRumRecorderPublicApi(startRumImpl: StartRum, startRecordingImpl: StartRecording) {
const rumRecorderGlobal = makeRumPublicApi((userConfiguration, getCommonContext) => {
let isRecording = false

const startRumResult = startRumImpl(userConfiguration, () => ({
...getCommonContext(),
hasReplay: true,
hasReplay: isRecording,
}))

const { lifeCycle, parentContexts, configuration, session } = startRumResult
startRecordingImpl(lifeCycle, userConfiguration.applicationId, configuration, session, parentContexts)

if (configuration.isEnabled('postpone_start_recording')) {
;(rumRecorderGlobal as any).startSessionRecord = monitor(startSessionRecord)
} else {
startSessionRecord()
}

function startSessionRecord() {
if (isRecording) {
return
}

isRecording = true
startRecordingImpl(lifeCycle, userConfiguration.applicationId, configuration, session, parentContexts)
}

return startRumResult
})
Expand Down

0 comments on commit bee9dd5

Please sign in to comment.