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 1 commit
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 undefined', () => {
const strategy = createPreStartStrategy(
{},
getCommonContextSpy,
createTrackingConsentState(),
createCustomVitalsState(),
doStartRumSpy
)
expect(strategy.getViewContext()).toBe(undefined)
})
})

describe('stopSession', () => {
it('does not buffer the call before starting RUM', () => {
const strategy = createPreStartStrategy(
Expand Down
2 changes: 2 additions & 0 deletions packages/rum-core/src/boot/preStartRum.ts
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,8 @@ export function createPreStartStrategy(
bufferApiCalls.add((startRumResult) => startRumResult.setViewContextProperty(key, value))
},

getViewContext: noop as () => undefined,

addAction(action, commonContext = getCommonContext()) {
bufferApiCalls.add((startRumResult) => startRumResult.addAction(action, commonContext))
},
Expand Down
39 changes: 39 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: () => undefined,
getInternalContext: () => undefined,
lifeCycle: {} as any,
viewHistory: {} as any,
Expand Down Expand Up @@ -891,4 +892,42 @@ 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 undefined before init', () => {
expect(rumPublicApi.getViewContext()).toBeUndefined()
expect(getViewContextSpy).not.toHaveBeenCalled()
})

it('should return undefined if getViewContext returns undefined', () => {
getViewContextSpy.and.callFake(() => undefined)
rumPublicApi.init(DEFAULT_INIT_CONFIGURATION)

expect(rumPublicApi.getViewContext()).toBeUndefined()
expect(getViewContextSpy).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 | undefined

/**
* 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
4 changes: 4 additions & 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,10 @@ export function trackViews(
setViewName: (name: string) => {
currentView.setViewName(name)
},
getViewContext: () => {
RomanGaignault marked this conversation as resolved.
Show resolved Hide resolved
const context = currentView.contextManager.getContext()
return context ? context : undefined
},

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