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-7572] Add get api of view specific context #3266

Merged
merged 4 commits into from
Jan 14, 2025
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions packages/rum-core/src/boot/preStartRum.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -567,6 +567,19 @@ describe('preStartRum', () => {
})
})

describe('getViewContext', () => {
it('returns empty object', () => {
const strategy = createPreStartStrategy(
{},
getCommonContextSpy,
createTrackingConsentState(),
createCustomVitalsState(),
doStartRumSpy
)
expect(strategy.getViewContext()).toEqual({})
})
})

describe('stopSession', () => {
it('does not buffer the call before starting RUM', () => {
const strategy = createPreStartStrategy(
Expand Down
6 changes: 5 additions & 1 deletion packages/rum-core/src/boot/preStartRum.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import {
addTelemetryConfiguration,
initFetchObservable,
} from '@datadog/browser-core'
import type { TrackingConsentState, DeflateWorker } from '@datadog/browser-core'
import type { TrackingConsentState, DeflateWorker, Context } from '@datadog/browser-core'
import {
validateAndBuildRumConfiguration,
type RumConfiguration,
Expand Down Expand Up @@ -53,6 +53,8 @@ export function createPreStartStrategy(

const trackingConsentStateSubscription = trackingConsentState.observable.subscribe(tryStartRum)

const noopContext: Context = {}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🥜 nitpick:emptyContext is more correct I think in this case (even if indeed there is no operation too)


function tryStartRum() {
if (!cachedInitConfiguration || !cachedConfiguration || !trackingConsentState.isGranted()) {
return
Expand Down Expand Up @@ -204,6 +206,8 @@ export function createPreStartStrategy(
bufferApiCalls.add((startRumResult) => startRumResult.setViewContextProperty(key, value))
},

getViewContext: () => noopContext,

addAction(action, commonContext = getCommonContext()) {
bufferApiCalls.add((startRumResult) => startRumResult.addAction(action, commonContext))
},
Expand Down
31 changes: 31 additions & 0 deletions packages/rum-core/src/boot/rumPublicApi.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ const noopStartRum = (): ReturnType<StartRum> => ({
setViewContext: () => undefined,
setViewContextProperty: () => undefined,
setViewName: () => undefined,
getViewContext: () => ({}),
getInternalContext: () => undefined,
lifeCycle: {} as any,
viewHistory: {} as any,
Expand Down Expand Up @@ -891,4 +892,34 @@ describe('rum public api', () => {
expect(setViewContextPropertySpy).toHaveBeenCalledWith('foo', 'bar')
})
})

describe('getViewContext', () => {
let getViewContextSpy: jasmine.Spy<ReturnType<StartRum>['getViewContext']>
let rumPublicApi: RumPublicApi

beforeEach(() => {
getViewContextSpy = jasmine.createSpy().and.callFake(() => ({
foo: 'bar',
}))
rumPublicApi = makeRumPublicApi(
() => ({
...noopStartRum(),
getViewContext: getViewContextSpy,
}),
noopRecorderApi
)
})

it('should return the view context after init', () => {
rumPublicApi.init(DEFAULT_INIT_CONFIGURATION)

expect(rumPublicApi.getViewContext()).toEqual({ foo: 'bar' })
expect(getViewContextSpy).toHaveBeenCalled()
})

it('should return an empty object before init', () => {
expect(rumPublicApi.getViewContext()).toEqual({})
expect(getViewContextSpy).not.toHaveBeenCalled()
})
})
})
9 changes: 9 additions & 0 deletions packages/rum-core/src/boot/rumPublicApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,12 @@ export interface RumPublicApi extends PublicApi {
* @param value value of the property
*/
setViewContextProperty: (key: string, value: any) => void

/**
* Get View Context.
*/
getViewContext: () => Context

/**
* Set the global context information to all events, stored in `@context`
*
Expand Down Expand Up @@ -352,6 +358,7 @@ export interface Strategy {
setViewName: StartRumResult['setViewName']
setViewContext: StartRumResult['setViewContext']
setViewContextProperty: StartRumResult['setViewContextProperty']
getViewContext: StartRumResult['getViewContext']
addAction: StartRumResult['addAction']
addError: StartRumResult['addError']
addFeatureFlagEvaluation: StartRumResult['addFeatureFlagEvaluation']
Expand Down Expand Up @@ -453,6 +460,8 @@ export function makeRumPublicApi(
strategy.setViewContextProperty(key, value)
}),

getViewContext: monitor(() => strategy.getViewContext()),

setGlobalContext: monitor((context) => {
globalContextManager.setContext(context)
addTelemetryUsage({ feature: 'set-global-context' })
Expand Down
2 changes: 2 additions & 0 deletions packages/rum-core/src/boot/startRum.ts
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,7 @@ export function startRum(
setViewName,
setViewContext,
setViewContextProperty,
getViewContext,
stop: stopViewCollection,
} = startViewCollection(
lifeCycle,
Expand Down Expand Up @@ -207,6 +208,7 @@ export function startRum(
startView,
setViewContext,
setViewContextProperty,
getViewContext,
setViewName,
lifeCycle,
viewHistory,
Expand Down
22 changes: 12 additions & 10 deletions packages/rum-core/src/domain/view/setupViewTest.specHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,21 +39,23 @@ export function setupViewTest({ lifeCycle, initialLocation }: ViewTrackingContex
} = spyOnViews<ViewEndedEvent>()
lifeCycle.subscribe(LifeCycleEventType.VIEW_ENDED, viewEndHandler)

const { stop, startView, setViewName, setViewContext, setViewContextProperty, addTiming } = trackViews(
location,
lifeCycle,
domMutationObservable,
windowOpenObservable,
configuration,
locationChangeObservable,
!configuration.trackViewsManually,
initialViewOptions
)
const { stop, startView, setViewName, setViewContext, setViewContextProperty, getViewContext, addTiming } =
trackViews(
location,
lifeCycle,
domMutationObservable,
windowOpenObservable,
configuration,
locationChangeObservable,
!configuration.trackViewsManually,
initialViewOptions
)
return {
stop,
startView,
setViewContext,
setViewContextProperty,
getViewContext,
changeLocation,
setViewName,
addTiming,
Expand Down
8 changes: 8 additions & 0 deletions packages/rum-core/src/domain/view/trackViews.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -932,6 +932,14 @@ describe('view event count', () => {
setViewContextProperty('foo', 'bar')
expect(getViewUpdate(1).context).toEqual({ foo: 'bar' })
})

it('should get view context with getViewContext', () => {
viewTest = setupViewTest({ lifeCycle })
const { getViewContext, setViewContextProperty } = viewTest

setViewContextProperty('foo', 'bar')
expect(getViewContext()).toEqual({ foo: 'bar' })
})
})

describe('set view name', () => {
Expand Down
1 change: 1 addition & 0 deletions packages/rum-core/src/domain/view/trackViews.ts
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,7 @@ export function trackViews(
setViewName: (name: string) => {
currentView.setViewName(name)
},
getViewContext: () => currentView.contextManager.getContext(),

stop: () => {
if (locationChangeSubscription) {
Expand Down
12 changes: 11 additions & 1 deletion test/e2e/scenario/rum/init.scenario.ts
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,6 @@ describe('API calls and events around init', () => {

const initialView = intakeRegistry.rumViewEvents[0]
const nextView = intakeRegistry.rumViewEvents[1]

expect(initialView.context).toEqual(jasmine.objectContaining({ foo: 'bar', bar: 'foo' }))
expect(nextView.context!.foo).toBeUndefined()

Expand Down Expand Up @@ -176,6 +175,17 @@ describe('API calls and events around init', () => {
}
)
})

createTest('get the view context')
.withRum()
.withRumInit((configuration) => {
window.DD_RUM!.init(configuration)
window.DD_RUM!.setViewContext({ foo: 'bar' })
})
.run(async () => {
const viewContext = await browser.execute(() => window.DD_RUM?.getViewContext())
expect(viewContext).toEqual({ foo: 'bar' })
})
})

describe('beforeSend', () => {
Expand Down
Loading