From bee9dd505f5700a95560087edee4f6a7e1221829 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Beno=C3=AEt=20Zugmeyer?= Date: Wed, 13 Jan 2021 10:57:16 +0100 Subject: [PATCH] =?UTF-8?q?=E2=9A=97=E2=9C=A8=20[RUMF-819]=20postpone=20re?= =?UTF-8?q?cording?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/boot/rumRecorderPublicApi.spec.ts | 50 ++++++++++++++++++- .../src/boot/rumRecorderPublicApi.ts | 21 +++++++- 2 files changed, 67 insertions(+), 4 deletions(-) diff --git a/packages/rum-recorder/src/boot/rumRecorderPublicApi.spec.ts b/packages/rum-recorder/src/boot/rumRecorderPublicApi.spec.ts index 3a5c3b552c..360c2985c4 100644 --- a/packages/rum-recorder/src/boot/rumRecorderPublicApi.spec.ts +++ b/packages/rum-recorder/src/boot/rumRecorderPublicApi.spec.ts @@ -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 let startRumSpy: jasmine.Spy + let enabledFlags: string[] = [] beforeEach(() => { + enabledFlags = [] startRecordingSpy = jasmine.createSpy() startRumSpy = jasmine.createSpy().and.callFake(() => { - return ({} as unknown) as ReturnType + const configuration: Partial = { + isEnabled(flag: string) { + return enabledFlags.includes(flag) + }, + } + return ({ configuration } as unknown) as ReturnType }) rumGlobal = makeRumRecorderPublicApi(startRumSpy, startRecordingSpy) }) @@ -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) + }) + }) }) diff --git a/packages/rum-recorder/src/boot/rumRecorderPublicApi.ts b/packages/rum-recorder/src/boot/rumRecorderPublicApi.ts index 75d3c48527..79163fda19 100644 --- a/packages/rum-recorder/src/boot/rumRecorderPublicApi.ts +++ b/packages/rum-recorder/src/boot/rumRecorderPublicApi.ts @@ -1,3 +1,4 @@ +import { monitor } from '@datadog/browser-core' import { makeRumPublicApi, StartRum } from '@datadog/browser-rum-core' import { startRecording } from './recorder' @@ -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 })