Skip to content

Commit

Permalink
Use assembly hooks for view and url context
Browse files Browse the repository at this point in the history
  • Loading branch information
amortemousque committed Jan 21, 2025
1 parent 675aaab commit 1192e1d
Show file tree
Hide file tree
Showing 11 changed files with 201 additions and 145 deletions.
18 changes: 14 additions & 4 deletions packages/rum-core/src/boot/startRum.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,14 @@ import { startViewCollection } from '../domain/view/viewCollection'
import type { RumEvent, RumViewEvent } from '../rumEvent.types'
import type { LocationChange } from '../browser/locationChangeObservable'
import { startLongAnimationFrameCollection } from '../domain/longAnimationFrame/longAnimationFrameCollection'
import type { RumSessionManager } from '..'
import { startViewHistory, type RumSessionManager } from '..'
import type { RumConfiguration } from '../domain/configuration'
import { RumEventType } from '../rawRumEvent.types'
import { startFeatureFlagContexts } from '../domain/contexts/featureFlagContext'
import type { PageStateHistory } from '../domain/contexts/pageStateHistory'
import { createCustomVitalsState } from '../domain/vital/vitalCollection'
import { startHooks } from '../hooks'
import { startUrlContexts } from '../domain/contexts/urlContexts'
import { startRum, startRumEventCollection } from './startRum'

function collectServerEvents(lifeCycle: LifeCycle) {
Expand All @@ -66,15 +68,19 @@ function startRumStub(
pageStateHistory: PageStateHistory,
reportError: (error: RawError) => void
) {
const hooks = startHooks()
const viewHistory = startViewHistory(lifeCycle)
const urlContexts = startUrlContexts(lifeCycle, hooks, locationChangeObservable, location)

const { stop: rumEventCollectionStop } = startRumEventCollection(
lifeCycle,
hooks,
configuration,
location,
sessionManager,
pageStateHistory,
locationChangeObservable,
domMutationObservable,
windowOpenObservable,
viewHistory,
() => ({
context: {},
user: {},
Expand All @@ -84,19 +90,23 @@ function startRumStub(
)
const { stop: viewCollectionStop } = startViewCollection(
lifeCycle,
hooks,
configuration,
location,
domMutationObservable,
windowOpenObservable,
locationChangeObservable,
startFeatureFlagContexts(lifeCycle, createCustomerDataTracker(noop)),
pageStateHistory,
noopRecorderApi
noopRecorderApi,
viewHistory
)

startLongAnimationFrameCollection(lifeCycle, configuration)
return {
stop: () => {
viewHistory.stop()
urlContexts.stop()
rumEventCollectionStop()
viewCollectionStop()
},
Expand Down
30 changes: 15 additions & 15 deletions packages/rum-core/src/boot/startRum.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import { createWindowOpenObservable } from '../browser/windowOpenObservable'
import { startRumAssembly } from '../domain/assembly'
import { startInternalContext } from '../domain/contexts/internalContext'
import { LifeCycle, LifeCycleEventType } from '../domain/lifeCycle'
import type { ViewHistory } from '../domain/contexts/viewHistory'
import { startViewHistory } from '../domain/contexts/viewHistory'
import { startRequestCollection } from '../domain/requestCollection'
import { startActionCollection } from '../domain/action/actionCollection'
Expand All @@ -34,7 +35,6 @@ import { startRumSessionManager, startRumSessionManagerStub } from '../domain/ru
import { startRumBatch } from '../transport/startRumBatch'
import { startRumEventBridge } from '../transport/startRumEventBridge'
import { startUrlContexts } from '../domain/contexts/urlContexts'
import type { LocationChange } from '../browser/locationChangeObservable'
import { createLocationChangeObservable } from '../browser/locationChangeObservable'
import type { RumConfiguration } from '../domain/configuration'
import type { ViewOptions } from '../domain/view/trackViews'
Expand All @@ -50,6 +50,8 @@ import { startCiVisibilityContext } from '../domain/contexts/ciVisibilityContext
import { startLongAnimationFrameCollection } from '../domain/longAnimationFrame/longAnimationFrameCollection'
import { RumPerformanceEntryType } from '../browser/performanceObservable'
import { startLongTaskCollection } from '../domain/longTask/longTaskCollection'
import type { Hooks } from '../hooks'
import { startHooks } from '../hooks'
import type { RecorderApi } from './rumPublicApi'

export type StartRum = typeof startRum
Expand All @@ -71,6 +73,7 @@ export function startRum(
) {
const cleanupTasks: Array<() => void> = []
const lifeCycle = new LifeCycle()
const hooks = startHooks()

lifeCycle.subscribe(LifeCycleEventType.RUM_EVENT_COLLECTED, (event) => sendToExtension('rum', event))

Expand Down Expand Up @@ -127,24 +130,25 @@ export function startRum(
const domMutationObservable = createDOMMutationObservable()
const locationChangeObservable = createLocationChangeObservable(configuration, location)
const pageStateHistory = startPageStateHistory(configuration)
const viewHistory = startViewHistory(lifeCycle)
const urlContexts = startUrlContexts(lifeCycle, hooks, locationChangeObservable, location)

const { observable: windowOpenObservable, stop: stopWindowOpen } = createWindowOpenObservable()
cleanupTasks.push(stopWindowOpen)

const {
viewHistory,
urlContexts,
actionContexts,
addAction,
stop: stopRumEventCollection,
} = startRumEventCollection(
lifeCycle,
hooks,
configuration,
location,
session,
pageStateHistory,
locationChangeObservable,
domMutationObservable,
windowOpenObservable,
viewHistory,
getCommonContext,
reportError
)
Expand All @@ -162,6 +166,7 @@ export function startRum(
stop: stopViewCollection,
} = startViewCollection(
lifeCycle,
hooks,
configuration,
location,
domMutationObservable,
Expand All @@ -170,8 +175,10 @@ export function startRum(
featureFlagContexts,
pageStateHistory,
recorderApi,
viewHistory,
initialViewOptions
)

cleanupTasks.push(stopViewCollection)

const { stop: stopResourceCollection } = startResourceCollection(lifeCycle, configuration, pageStateHistory)
Expand Down Expand Up @@ -234,19 +241,16 @@ function startRumTelemetry(configuration: RumConfiguration) {

export function startRumEventCollection(
lifeCycle: LifeCycle,
hooks: Hooks,
configuration: RumConfiguration,
location: Location,
sessionManager: RumSessionManager,
pageStateHistory: PageStateHistory,
locationChangeObservable: Observable<LocationChange>,
domMutationObservable: Observable<void>,
windowOpenObservable: Observable<void>,
viewHistory: ViewHistory,
getCommonContext: () => CommonContext,
reportError: (error: RawError) => void
) {
const viewHistory = startViewHistory(lifeCycle)
const urlContexts = startUrlContexts(lifeCycle, locationChangeObservable, location)

const actionCollection = startActionCollection(
lifeCycle,
domMutationObservable,
Expand All @@ -261,9 +265,9 @@ export function startRumEventCollection(
startRumAssembly(
configuration,
lifeCycle,
hooks,
sessionManager,
viewHistory,
urlContexts,
actionCollection.actionContexts,
displayContext,
ciVisibilityContext,
Expand All @@ -272,17 +276,13 @@ export function startRumEventCollection(
)

return {
viewHistory,
pageStateHistory,
urlContexts,
addAction: actionCollection.addAction,
actionContexts: actionCollection.actionContexts,
stop: () => {
actionCollection.stop()
ciVisibilityContext.stop()
displayContext.stop()
urlContexts.stop()
viewHistory.stop()
pageStateHistory.stop()
},
}
Expand Down
129 changes: 36 additions & 93 deletions packages/rum-core/src/domain/assembly.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import {
createRumSessionManagerMock,
createRawRumEvent,
mockRumConfiguration,
mockUrlContexts,
mockActionContexts,
mockDisplayContext,
mockViewHistory,
Expand All @@ -24,6 +23,7 @@ import type { RumEventDomainContext } from '../domainContext.types'
import type { RawRumActionEvent, RawRumEvent } from '../rawRumEvent.types'
import { RumEventType } from '../rawRumEvent.types'
import type { RumActionEvent, RumErrorEvent, RumEvent, RumResourceEvent } from '../rumEvent.types'
import { HookNames, startHooks } from '../hooks'
import { startRumAssembly } from './assembly'
import type { RawRumEventCollectedData } from './lifeCycle'
import { LifeCycle, LifeCycleEventType } from './lifeCycle'
Expand Down Expand Up @@ -389,43 +389,6 @@ describe('rum assembly', () => {
})
})

describe('priority of rum context', () => {
it('should prioritize view customer context over global context', () => {
const { lifeCycle, serverRumEvents, commonContext } = setupAssemblyTestWithDefaults({
findView: () => ({
id: '7890',
name: 'view name',
startClocks: {} as ClocksState,
context: { foo: 'baz' },
}),
})
commonContext.context = { foo: 'bar' }

notifyRawRumEvent(lifeCycle, {
rawRumEvent: createRawRumEvent(RumEventType.VIEW),
})

expect(serverRumEvents[0].context!.foo).toBe('baz')
})

it('should prioritize child customer context over inherited view context', () => {
const { lifeCycle, serverRumEvents } = setupAssemblyTestWithDefaults({
findView: () => ({
id: '7890',
name: 'view name',
startClocks: {} as ClocksState,
context: { foo: 'bar' },
}),
})
notifyRawRumEvent(lifeCycle, {
customerContext: { foo: 'baz' },
rawRumEvent: createRawRumEvent(RumEventType.ACTION),
})

expect(serverRumEvents[0].context!.foo).toBe('baz')
})
})

describe('rum global context', () => {
it('should be merged with event attributes', () => {
const { lifeCycle, serverRumEvents, commonContext } = setupAssemblyTestWithDefaults()
Expand Down Expand Up @@ -569,36 +532,6 @@ describe('rum assembly', () => {
})
})

describe('view context', () => {
it('should be merged with event attributes', () => {
const { lifeCycle, serverRumEvents } = setupAssemblyTestWithDefaults()
notifyRawRumEvent(lifeCycle, {
rawRumEvent: createRawRumEvent(RumEventType.ACTION),
})
expect(serverRumEvents[0].view).toEqual(
jasmine.objectContaining({
id: '7890',
name: 'view name',
})
)
})

it('child event should have view customer context', () => {
const { lifeCycle, serverRumEvents } = setupAssemblyTestWithDefaults({
findView: () => ({
id: '7890',
name: 'view name',
startClocks: {} as ClocksState,
context: { foo: 'bar' },
}),
})
notifyRawRumEvent(lifeCycle, {
rawRumEvent: createRawRumEvent(RumEventType.ACTION),
})
expect(serverRumEvents[0].context).toEqual({ foo: 'bar' })
})
})

describe('service and version', () => {
const extraConfigurationOptions = { service: 'default service', version: 'default version' }

Expand All @@ -614,24 +547,6 @@ describe('rum assembly', () => {
expect(serverRumEvents[0].version).toEqual('default version')
})

it('should be overridden by the view context', () => {
const { lifeCycle, serverRumEvents } = setupAssemblyTestWithDefaults({
partialConfiguration: extraConfigurationOptions,
findView: () => ({
service: 'new service',
version: 'new version',
id: '1234',
startClocks: {} as ClocksState,
}),
})

notifyRawRumEvent(lifeCycle, {
rawRumEvent: createRawRumEvent(RumEventType.ACTION),
})
expect(serverRumEvents[0].service).toEqual('new service')
expect(serverRumEvents[0].version).toEqual('new version')
})

describe('fields service and version', () => {
it('it should be modifiable', () => {
const { lifeCycle, serverRumEvents } = setupAssemblyTestWithDefaults({
Expand All @@ -656,14 +571,41 @@ describe('rum assembly', () => {
})
})

describe('url context', () => {
it('should be merged with event attributes', () => {
const { lifeCycle, serverRumEvents } = setupAssemblyTestWithDefaults()
describe('assemble hook', () => {
it('should add and override common properties', () => {
const { lifeCycle, hooks, serverRumEvents, commonContext } = setupAssemblyTestWithDefaults({
partialConfiguration: { service: 'default service', version: 'default version' },
})
commonContext.context = { foo: 'global context' }

hooks.register(HookNames.Assemble, () => ({
service: 'new service',
version: 'new version',
context: { foo: 'bar' },
view: { id: 'new view id', url: '' },
}))

notifyRawRumEvent(lifeCycle, {
rawRumEvent: createRawRumEvent(RumEventType.ACTION),
})
expect(serverRumEvents[0].service).toEqual('new service')
expect(serverRumEvents[0].version).toEqual('new version')
expect(serverRumEvents[0].context).toEqual({ foo: 'bar' })
expect(serverRumEvents[0].view.id).toEqual('new view id')
})

it('should not override customer context', () => {
const { lifeCycle, hooks, serverRumEvents } = setupAssemblyTestWithDefaults()

hooks.register(HookNames.Assemble, () => ({
context: { foo: 'bar' },
}))

notifyRawRumEvent(lifeCycle, {
rawRumEvent: createRawRumEvent(RumEventType.ACTION),
customerContext: { foo: 'customer context' },
})
expect(serverRumEvents[0].view.url).toBe(location.href)
expect(serverRumEvents[0].view.referrer).toBe(document.referrer)
expect(serverRumEvents[0].context).toEqual({ foo: 'customer context' })
})
})

Expand Down Expand Up @@ -1015,6 +957,7 @@ function setupAssemblyTestWithDefaults({
findView = () => ({ id: '7890', name: 'view name', startClocks: {} as ClocksState }),
}: AssemblyTestParams = {}) {
const lifeCycle = new LifeCycle()
const hooks = startHooks()
const reportErrorSpy = jasmine.createSpy('reportError')
const rumSessionManager = sessionManager ?? createRumSessionManagerMock().setId('1234')
const commonContext = {
Expand All @@ -1031,9 +974,9 @@ function setupAssemblyTestWithDefaults({
startRumAssembly(
mockRumConfiguration(partialConfiguration),
lifeCycle,
hooks,
rumSessionManager,
{ ...mockViewHistory(), findView: () => findView() },
mockUrlContexts(),
mockActionContexts(),
mockDisplayContext(),
{ get: () => ciVisibilityContext } as CiVisibilityContext,
Expand All @@ -1045,5 +988,5 @@ function setupAssemblyTestWithDefaults({
subscription.unsubscribe()
})

return { lifeCycle, reportErrorSpy, serverRumEvents, commonContext }
return { lifeCycle, hooks, reportErrorSpy, serverRumEvents, commonContext }
}
Loading

0 comments on commit 1192e1d

Please sign in to comment.