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

✨ [RUM-2203] Forward replay records to the bridge #2470

Merged
merged 17 commits into from Mar 5, 2024
Merged
46 changes: 41 additions & 5 deletions packages/core/src/transport/eventBridge.spec.ts
@@ -1,4 +1,6 @@
import { deleteEventBridgeStub, initEventBridgeStub } from '../../test'
import { DefaultPrivacyLevel } from '../domain/configuration'
import type { DatadogEventBridge } from './eventBridge'
import { getEventBridge, canUseEventBridge } from './eventBridge'

describe('canUseEventBridge', () => {
Expand All @@ -9,22 +11,22 @@ describe('canUseEventBridge', () => {
})

it('should detect when the bridge is present and the webView host is allowed', () => {
initEventBridgeStub(allowedWebViewHosts)
initEventBridgeStub({ allowedWebViewHosts })
expect(canUseEventBridge('foo.bar')).toBeTrue()
expect(canUseEventBridge('baz.foo.bar')).toBeTrue()
expect(canUseEventBridge('www.foo.bar')).toBeTrue()
expect(canUseEventBridge('www.qux.foo.bar')).toBeTrue()
})

it('should not detect when the bridge is present and the webView host is not allowed', () => {
initEventBridgeStub(allowedWebViewHosts)
initEventBridgeStub({ allowedWebViewHosts })
expect(canUseEventBridge('foo.com')).toBeFalse()
expect(canUseEventBridge('foo.bar.baz')).toBeFalse()
expect(canUseEventBridge('bazfoo.bar')).toBeFalse()
})

it('should not detect when the bridge on the parent domain if only the subdomain is allowed', () => {
initEventBridgeStub(['baz.foo.bar'])
initEventBridgeStub({ allowedWebViewHosts: ['baz.foo.bar'] })
expect(canUseEventBridge('foo.bar')).toBeFalse()
})

Expand All @@ -33,7 +35,7 @@ describe('canUseEventBridge', () => {
})
})

describe('getEventBridge', () => {
describe('event bridge send', () => {
let sendSpy: jasmine.Spy<(msg: string) => void>

beforeEach(() => {
Expand All @@ -45,11 +47,45 @@ describe('getEventBridge', () => {
deleteEventBridgeStub()
})

it('event bridge should serialize sent events', () => {
it('should serialize sent events without view', () => {
const eventBridge = getEventBridge()!

eventBridge.send('view', { foo: 'bar' })

expect(sendSpy).toHaveBeenCalledOnceWith('{"eventType":"view","event":{"foo":"bar"}}')
})

it('should serialize sent events with view', () => {
const eventBridge = getEventBridge()!

eventBridge.send('view', { foo: 'bar' }, '123')

expect(sendSpy).toHaveBeenCalledOnceWith('{"eventType":"view","event":{"foo":"bar"},"view":{"id":"123"}}')
})
})

describe('event bridge getPrivacyLevel', () => {
let eventBridgeStub: DatadogEventBridge
const bridgePrivacyLevel = DefaultPrivacyLevel.MASK

beforeEach(() => {
eventBridgeStub = initEventBridgeStub({ privacyLevel: bridgePrivacyLevel })
})

afterEach(() => {
deleteEventBridgeStub()
})
amortemousque marked this conversation as resolved.
Show resolved Hide resolved

it('should return the privacy level', () => {
const eventBridge = getEventBridge()!

expect(eventBridge.getPrivacyLevel()).toEqual(bridgePrivacyLevel)
})

it('should return undefined if getPrivacyLevel not present in the bridge', () => {
delete eventBridgeStub.getPrivacyLevel
const eventBridge = getEventBridge()!

expect(eventBridge.getPrivacyLevel()).toBeUndefined()
})
})
9 changes: 7 additions & 2 deletions packages/core/src/transport/eventBridge.ts
Expand Up @@ -6,6 +6,7 @@ export interface BrowserWindowWithEventBridge extends Window {
}

export interface DatadogEventBridge {
getPrivacyLevel?(): string
getAllowedWebViewHosts(): string
send(msg: string): void
}
Expand All @@ -18,11 +19,15 @@ export function getEventBridge<T, E>() {
}

return {
getPrivacyLevel() {
return eventBridgeGlobal.getPrivacyLevel?.()
},
getAllowedWebViewHosts() {
return JSON.parse(eventBridgeGlobal.getAllowedWebViewHosts()) as string[]
},
send(eventType: T, event: E) {
eventBridgeGlobal.send(JSON.stringify({ eventType, event }))
send(eventType: T, event: E, viewId?: string) {
const view = viewId ? { id: viewId } : undefined
eventBridgeGlobal.send(JSON.stringify({ eventType, event, view }))
},
}
}
Expand Down
7 changes: 6 additions & 1 deletion packages/core/test/emulate/eventBridge.ts
@@ -1,9 +1,14 @@
import { DefaultPrivacyLevel } from '../../src/domain/configuration'
import type { BrowserWindowWithEventBridge } from '../../src/transport'

export function initEventBridgeStub(allowedWebViewHosts: string[] = [window.location.hostname]) {
export function initEventBridgeStub({
allowedWebViewHosts = [window.location.hostname],
privacyLevel = DefaultPrivacyLevel.MASK,
}: { allowedWebViewHosts?: string[]; privacyLevel?: DefaultPrivacyLevel } = {}) {
const eventBridgeStub = {
send: (_msg: string) => undefined,
getAllowedWebViewHosts: () => JSON.stringify(allowedWebViewHosts),
getPrivacyLevel: () => privacyLevel,
}
;(window as BrowserWindowWithEventBridge).DatadogEventBridge = eventBridgeStub
return eventBridgeStub
Expand Down
23 changes: 20 additions & 3 deletions packages/rum-core/src/boot/rumPublicApi.spec.ts
Expand Up @@ -104,26 +104,43 @@ describe('rum public api', () => {
})

describe('if event bridge present', () => {
const bridgePrivacyLevel = DefaultPrivacyLevel.ALLOW
beforeEach(() => {
initEventBridgeStub()
initEventBridgeStub({ privacyLevel: bridgePrivacyLevel })
})

afterEach(() => {
deleteEventBridgeStub()
})

it('init should accept empty application id and client token', () => {
it('should accept empty application id and client token', () => {
const hybridInitConfiguration: HybridInitConfiguration = {}
rumPublicApi.init(hybridInitConfiguration as RumInitConfiguration)
expect(display.error).not.toHaveBeenCalled()
})

it('init should force session sample rate to 100', () => {
it('should force session sample rate to 100', () => {
const invalidConfiguration: HybridInitConfiguration = { sessionSampleRate: 50 }
rumPublicApi.init(invalidConfiguration as RumInitConfiguration)
expect(rumPublicApi.getInitConfiguration()?.sessionSampleRate).toEqual(100)
})

it('should set the default privacy level received from the bridge if the not provided in the init configuration', () => {
const hybridInitConfiguration: HybridInitConfiguration = {}
rumPublicApi.init(hybridInitConfiguration as RumInitConfiguration)
expect((rumPublicApi.getInitConfiguration() as RumInitConfiguration)?.defaultPrivacyLevel).toEqual(
bridgePrivacyLevel
)
})

it('should set the default privacy level from the init configuration if provided', () => {
const hybridInitConfiguration: HybridInitConfiguration = { defaultPrivacyLevel: DefaultPrivacyLevel.MASK }
rumPublicApi.init(hybridInitConfiguration as RumInitConfiguration)
expect((rumPublicApi.getInitConfiguration() as RumInitConfiguration)?.defaultPrivacyLevel).toEqual(
hybridInitConfiguration.defaultPrivacyLevel
)
})

it('should initialize even if session cannot be handled', () => {
spyOnProperty(document, 'cookie', 'get').and.returnValue('')
const rumPublicApi = makeRumPublicApi(startRumSpy, noopRecorderApi, {})
Expand Down
4 changes: 3 additions & 1 deletion packages/rum-core/src/boot/rumPublicApi.ts
Expand Up @@ -31,6 +31,7 @@ import {
CustomerDataCompressionStatus,
createCustomerDataTrackerManager,
storeContextManager,
getEventBridge,
} from '@datadog/browser-core'
import type { LifeCycle } from '../domain/lifeCycle'
import type { ViewContexts } from '../domain/contexts/viewContexts'
Expand Down Expand Up @@ -341,11 +342,12 @@ export function makeRumPublicApi(
return true
}

function overrideInitConfigurationForBridge<C extends InitConfiguration>(initConfiguration: C): C {
function overrideInitConfigurationForBridge<C extends RumInitConfiguration>(initConfiguration: C): C {
return assign({}, initConfiguration, {
applicationId: '00000000-aaaa-0000-aaaa-000000000000',
clientToken: 'empty',
sessionSampleRate: 100,
defaultPrivacyLevel: initConfiguration.defaultPrivacyLevel ?? getEventBridge()?.getPrivacyLevel(),
})
}
}
5 changes: 4 additions & 1 deletion packages/rum-core/src/boot/startRum.spec.ts
Expand Up @@ -381,7 +381,10 @@ describe('view events', () => {
clock.tick(VIEW_DURATION)
window.dispatchEvent(createNewEvent('beforeunload'))

const lastBridgeMessage = JSON.parse(sendSpy.calls.mostRecent().args[0]) as { eventType: 'rum'; event: RumEvent }
const lastBridgeMessage = JSON.parse(sendSpy.calls.mostRecent().args[0]) as {
eventType: 'rum'
event: RumEvent
}
expect(lastBridgeMessage.event.type).toBe('view')
expect(lastBridgeMessage.event.view.time_spent).toBe(toServerDuration(VIEW_DURATION))
})
Expand Down
2 changes: 1 addition & 1 deletion packages/rum-core/src/domain/rumSessionManager.ts
Expand Up @@ -58,7 +58,7 @@ export function startRumSessionManager(configuration: RumConfiguration, lifeCycl
export function startRumSessionManagerStub(): RumSessionManager {
const session: RumSession = {
id: '00000000-aaaa-0000-aaaa-000000000000',
sessionReplayAllowed: false,
sessionReplayAllowed: true,
}
return {
findTrackedSession: () => session,
Expand Down
4 changes: 2 additions & 2 deletions packages/rum/src/boot/recorderApi.ts
@@ -1,5 +1,5 @@
import type { DeflateEncoder } from '@datadog/browser-core'
import { DeflateEncoderStreamId, canUseEventBridge, noop, runOnReadyState } from '@datadog/browser-core'
import { DeflateEncoderStreamId, noop, runOnReadyState } from '@datadog/browser-core'
import type {
LifeCycle,
ViewContexts,
Expand Down Expand Up @@ -53,7 +53,7 @@ export function makeRecorderApi(
startRecordingImpl: StartRecording,
createDeflateWorkerImpl?: CreateDeflateWorker
): RecorderApi {
if (canUseEventBridge() || !isBrowserSupported()) {
if (!isBrowserSupported()) {
amortemousque marked this conversation as resolved.
Show resolved Hide resolved
return {
start: noop,
stop: noop,
Expand Down
24 changes: 23 additions & 1 deletion packages/rum/src/boot/startRecording.spec.ts
Expand Up @@ -3,7 +3,12 @@ import { PageExitReason, DefaultPrivacyLevel, noop, isIE, DeflateEncoderStreamId
import type { LifeCycle, ViewCreatedEvent, RumConfiguration } from '@datadog/browser-rum-core'
import { LifeCycleEventType } from '@datadog/browser-rum-core'
import type { Clock } from '@datadog/browser-core/test'
import { collectAsyncCalls, createNewEvent } from '@datadog/browser-core/test'
import {
collectAsyncCalls,
createNewEvent,
deleteEventBridgeStub,
initEventBridgeStub,
} from '@datadog/browser-core/test'
import type { RumSessionManagerMock, TestSetupBuilder } from '../../../rum-core/test'
import { appendElement, createRumSessionManagerMock, setup } from '../../../rum-core/test'

Expand Down Expand Up @@ -81,6 +86,7 @@ describe('startRecording', () => {
setupBuilder.cleanup()
clock?.cleanup()
resetDeflateWorkerState()
deleteEventBridgeStub()
})

it('sends recorded segments with valid context', async () => {
Expand Down Expand Up @@ -209,6 +215,22 @@ describe('startRecording', () => {
})
})

it('should send records through the bridge when it is present', () => {
const eventBridgeStub = initEventBridgeStub()
setupBuilder.build()
const sendSpy = spyOn(eventBridgeStub, 'send')

document.body.dispatchEvent(createNewEvent('click', { clientX: 1, clientY: 2 }))

const lastBridgeMessage = JSON.parse(sendSpy.calls.mostRecent().args[0])

expect(lastBridgeMessage).toEqual({
eventType: 'record',
event: jasmine.objectContaining({ type: RecordType.IncrementalSnapshot }),
view: { id: viewId },
})
})

function changeView(lifeCycle: LifeCycle) {
lifeCycle.notify(LifeCycleEventType.VIEW_ENDED, {} as any)
viewId = 'view-id-2'
Expand Down
27 changes: 18 additions & 9 deletions packages/rum/src/boot/startRecording.ts
@@ -1,10 +1,12 @@
import type { RawError, HttpRequest, DeflateEncoder } from '@datadog/browser-core'
import { createHttpRequest, addTelemetryDebug } from '@datadog/browser-core'
import { createHttpRequest, addTelemetryDebug, noop, canUseEventBridge } from '@datadog/browser-core'
import type { LifeCycle, ViewContexts, RumConfiguration, RumSessionManager } from '@datadog/browser-rum-core'
import { LifeCycleEventType } 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'

export function startRecording(
lifeCycle: LifeCycle,
Expand All @@ -23,14 +25,21 @@ export function startRecording(
httpRequest ||
createHttpRequest(configuration, configuration.sessionReplayEndpointBuilder, SEGMENT_BYTES_LIMIT, reportError)

const { addRecord, stop: stopSegmentCollection } = startSegmentCollection(
lifeCycle,
configuration,
sessionManager,
viewContexts,
replayRequest,
encoder
)
let addRecord = (_record: BrowserRecord) => {}
amortemousque marked this conversation as resolved.
Show resolved Hide resolved
let stopSegmentCollection = noop
amortemousque marked this conversation as resolved.
Show resolved Hide resolved

if (!canUseEventBridge()) {
;({ addRecord, stop: stopSegmentCollection } = startSegmentCollection(
lifeCycle,
configuration,
sessionManager,
viewContexts,
replayRequest,
encoder
))
} else {
;({ addRecord } = startRecordBridge(viewContexts))
}

const { stop: stopRecording } = record({
emit: addRecord,
Expand Down
3 changes: 2 additions & 1 deletion packages/rum/src/domain/record/record.ts
@@ -1,3 +1,4 @@
import type { RelativeTime } from '@datadog/browser-core'
import { sendToExtension, timeStampNow } from '@datadog/browser-core'
import type { LifeCycle, RumConfiguration, ViewContexts } from '@datadog/browser-rum-core'
import type {
Expand Down Expand Up @@ -44,7 +45,7 @@ export function record(options: RecordOptions): RecordAPI {
const emitAndComputeStats = (record: BrowserRecord) => {
emit(record)
sendToExtension('record', { record })
const view = options.viewContexts.findView()
const view = options.viewContexts.findView(record.timestamp as RelativeTime)
if (view) {
replayStats.addRecord(view.id)
}
Expand Down
18 changes: 18 additions & 0 deletions packages/rum/src/domain/startRecordBridge.ts
@@ -0,0 +1,18 @@
import type { RelativeTime } from '@datadog/browser-core'
import { getEventBridge } from '@datadog/browser-core'
import type { ViewContexts } from '@datadog/browser-rum-core'
import type { BrowserRecord } from '../types'

export function startRecordBridge(viewContexts: ViewContexts) {
const bridge = getEventBridge<'record', BrowserRecord>()!

return {
addRecord: (record: BrowserRecord) => {
const view = viewContexts.findView(record.timestamp as RelativeTime)
if (!view) {
return
}
amortemousque marked this conversation as resolved.
Show resolved Hide resolved
bridge.send('record', record, view.id)
},
}
}
4 changes: 4 additions & 0 deletions test/e2e/lib/framework/intakeRegistry.ts
Expand Up @@ -121,6 +121,10 @@ export class IntakeRegistry {
get replaySegments() {
return this.replayRequests.map((request) => request.segment)
}

get replayRecords() {
return this.replayRequests.flatMap((request) => request.segment.records)
}
}

function isLogsIntakeRequest(request: IntakeRequest): request is LogsIntakeRequest {
Expand Down