diff --git a/packages/core/src/index.ts b/packages/core/src/index.ts index fc9af50cdb..bd23294a04 100644 --- a/packages/core/src/index.ts +++ b/packages/core/src/index.ts @@ -54,6 +54,7 @@ export { Payload, createHttpRequest, canUseEventBridge, + isBridgeForRecordsSupported, getEventBridge, startBatchWithReplica, createFlushController, diff --git a/packages/core/src/transport/eventBridge.ts b/packages/core/src/transport/eventBridge.ts index 793b1329ef..764fb1dd65 100644 --- a/packages/core/src/transport/eventBridge.ts +++ b/packages/core/src/transport/eventBridge.ts @@ -32,6 +32,11 @@ export function getEventBridge() { } } +export function isBridgeForRecordsSupported(): boolean { + const bridge = getEventBridgeGlobal() + return !!bridge && 'getPrivacyLevel' in bridge +} + export function canUseEventBridge(currentHost = getGlobalObject().location?.hostname): boolean { const bridge = getEventBridge() return ( diff --git a/packages/core/src/transport/index.ts b/packages/core/src/transport/index.ts index 97de422d2f..5cd46bce91 100644 --- a/packages/core/src/transport/index.ts +++ b/packages/core/src/transport/index.ts @@ -1,4 +1,10 @@ export { HttpRequest, createHttpRequest, Payload, RetryInfo } from './httpRequest' -export { canUseEventBridge, getEventBridge, BrowserWindowWithEventBridge } from './eventBridge' +export { + canUseEventBridge, + isBridgeForRecordsSupported, + getEventBridge, + BrowserWindowWithEventBridge, + DatadogEventBridge, +} from './eventBridge' export { startBatchWithReplica } from './startBatchWithReplica' export { createFlushController, FlushController, FlushEvent, FlushReason } from './flushController' diff --git a/packages/core/test/emulate/eventBridge.ts b/packages/core/test/emulate/eventBridge.ts index ab52954388..f590f34de7 100644 --- a/packages/core/test/emulate/eventBridge.ts +++ b/packages/core/test/emulate/eventBridge.ts @@ -1,16 +1,20 @@ import { DefaultPrivacyLevel } from '../../src/domain/configuration' -import type { BrowserWindowWithEventBridge } from '../../src/transport' +import type { BrowserWindowWithEventBridge, DatadogEventBridge } from '../../src/transport' import { registerCleanupTask } from '../registerCleanupTask' export function initEventBridgeStub({ allowedWebViewHosts = [window.location.hostname], privacyLevel = DefaultPrivacyLevel.MASK, -}: { allowedWebViewHosts?: string[]; privacyLevel?: DefaultPrivacyLevel } = {}) { - const eventBridgeStub = { + bridgeForRecordsSupported = true, +}: { allowedWebViewHosts?: string[]; privacyLevel?: DefaultPrivacyLevel; bridgeForRecordsSupported?: boolean } = {}) { + const eventBridgeStub: DatadogEventBridge = { send: (_msg: string) => undefined, getAllowedWebViewHosts: () => JSON.stringify(allowedWebViewHosts), - getPrivacyLevel: () => privacyLevel, } + if (bridgeForRecordsSupported) { + eventBridgeStub.getPrivacyLevel = () => privacyLevel + } + ;(window as BrowserWindowWithEventBridge).DatadogEventBridge = eventBridgeStub registerCleanupTask(() => { diff --git a/packages/rum-core/src/domain/assembly.ts b/packages/rum-core/src/domain/assembly.ts index 182efbb477..8213b143cc 100644 --- a/packages/rum-core/src/domain/assembly.ts +++ b/packages/rum-core/src/domain/assembly.ts @@ -117,7 +117,6 @@ export function startRumAssembly( const syntheticsContext = getSyntheticsContext() const ciTestContext = getCiTestContext() - lifeCycle.subscribe( LifeCycleEventType.RAW_RUM_EVENT_COLLECTED, ({ startTime, rawRumEvent, domainContext, savedCommonContext, customerContext }) => { diff --git a/packages/rum-core/src/domain/rumSessionManager.spec.ts b/packages/rum-core/src/domain/rumSessionManager.spec.ts index 560c6e0b36..e821d07795 100644 --- a/packages/rum-core/src/domain/rumSessionManager.spec.ts +++ b/packages/rum-core/src/domain/rumSessionManager.spec.ts @@ -10,12 +10,17 @@ import { DOM_EVENT, } from '@datadog/browser-core' import type { Clock } from '@datadog/browser-core/test' -import { createNewEvent, mockClock } from '@datadog/browser-core/test' +import { createNewEvent, initEventBridgeStub, mockClock } from '@datadog/browser-core/test' import type { RumConfiguration } from './configuration' import { validateAndBuildRumConfiguration } from './configuration' import { LifeCycle, LifeCycleEventType } from './lifeCycle' -import { RUM_SESSION_KEY, RumTrackingType, startRumSessionManager } from './rumSessionManager' +import { + RUM_SESSION_KEY, + RumTrackingType, + startRumSessionManager, + startRumSessionManagerStub, +} from './rumSessionManager' describe('rum session manager', () => { const DURATION = 123456 @@ -221,3 +226,15 @@ describe('rum session manager', () => { ) }) }) + +describe('rum session manager stub', () => { + it('should return a tracked session with replay allowed when the event bridge support records', () => { + initEventBridgeStub({ bridgeForRecordsSupported: true }) + expect(startRumSessionManagerStub().findTrackedSession()!.sessionReplayAllowed).toEqual(true) + }) + + it('should return a tracked session without replay allowed when the event bridge support records', () => { + initEventBridgeStub({ bridgeForRecordsSupported: false }) + expect(startRumSessionManagerStub().findTrackedSession()!.sessionReplayAllowed).toEqual(false) + }) +}) diff --git a/packages/rum-core/src/domain/rumSessionManager.ts b/packages/rum-core/src/domain/rumSessionManager.ts index 3a07ccfba5..1d6e43d475 100644 --- a/packages/rum-core/src/domain/rumSessionManager.ts +++ b/packages/rum-core/src/domain/rumSessionManager.ts @@ -1,5 +1,5 @@ import type { RelativeTime } from '@datadog/browser-core' -import { Observable, noop, performDraw, startSessionManager } from '@datadog/browser-core' +import { Observable, isBridgeForRecordsSupported, noop, performDraw, startSessionManager } from '@datadog/browser-core' import type { RumConfiguration } from './configuration' import type { LifeCycle } from './lifeCycle' import { LifeCycleEventType } from './lifeCycle' @@ -58,7 +58,7 @@ export function startRumSessionManager(configuration: RumConfiguration, lifeCycl export function startRumSessionManagerStub(): RumSessionManager { const session: RumSession = { id: '00000000-aaaa-0000-aaaa-000000000000', - sessionReplayAllowed: true, + sessionReplayAllowed: isBridgeForRecordsSupported(), } return { findTrackedSession: () => session, diff --git a/packages/rum/src/boot/recorderApi.spec.ts b/packages/rum/src/boot/recorderApi.spec.ts index 4f7d5a0ba9..efcb0360d7 100644 --- a/packages/rum/src/boot/recorderApi.spec.ts +++ b/packages/rum/src/boot/recorderApi.spec.ts @@ -184,14 +184,21 @@ describe('makeRecorderApi', () => { }) describe('if event bridge present', () => { - beforeEach(() => { - initEventBridgeStub() - }) + it('should start recording when the bridge supports records', () => { + initEventBridgeStub({ bridgeForRecordsSupported: true }) - it('does not start recording', () => { setupBuilder.build() + rumInit() recorderApi.start() + expect(startRecordingSpy).toHaveBeenCalled() + }) + + it('should not start recording when the bridge does not support records', () => { + initEventBridgeStub({ bridgeForRecordsSupported: false }) + + setupBuilder.build() rumInit() + recorderApi.start() expect(startRecordingSpy).not.toHaveBeenCalled() }) }) diff --git a/packages/rum/src/boot/recorderApi.ts b/packages/rum/src/boot/recorderApi.ts index 6e65fb2457..05730f8a5b 100644 --- a/packages/rum/src/boot/recorderApi.ts +++ b/packages/rum/src/boot/recorderApi.ts @@ -1,5 +1,11 @@ import type { DeflateEncoder } from '@datadog/browser-core' -import { DeflateEncoderStreamId, noop, runOnReadyState } from '@datadog/browser-core' +import { + DeflateEncoderStreamId, + canUseEventBridge, + isBridgeForRecordsSupported, + noop, + runOnReadyState, +} from '@datadog/browser-core' import type { LifeCycle, ViewContexts, @@ -53,7 +59,7 @@ export function makeRecorderApi( startRecordingImpl: StartRecording, createDeflateWorkerImpl?: CreateDeflateWorker ): RecorderApi { - if (!isBrowserSupported()) { + if ((canUseEventBridge() && !isBridgeForRecordsSupported()) || !isBrowserSupported()) { return { start: noop, stop: noop, diff --git a/test/e2e/lib/framework/pageSetups.ts b/test/e2e/lib/framework/pageSetups.ts index 2cbcf8a20f..5b8e0d4a3c 100644 --- a/test/e2e/lib/framework/pageSetups.ts +++ b/test/e2e/lib/framework/pageSetups.ts @@ -206,6 +206,9 @@ function setupEventBridge(servers: Servers) { return html`