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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

馃攰 add view document_count in non-view events #1892

Merged
Show file tree
Hide file tree
Changes from all 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
32 changes: 30 additions & 2 deletions packages/rum-core/src/domain/assembly.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
import type { RelativeTime } from '@datadog/browser-core'
import { ErrorSource, ONE_MINUTE, display } from '@datadog/browser-core'
import {
updateExperimentalFeatures,
resetExperimentalFeatures,
ErrorSource,
ONE_MINUTE,
display,
} from '@datadog/browser-core'
import { createRumSessionManagerMock } from '../../test/mockRumSessionManager'
import { createRawRumEvent } from '../../test/fixtures'
import type { TestSetupBuilder } from '../../test/specHelper'
Expand Down Expand Up @@ -28,6 +34,7 @@ describe('rum assembly', () => {
findView = () => ({
id: '7890',
name: 'view name',
documentVersion: 42,
})
reportErrorSpy = jasmine.createSpy('reportError')
commonContext = {
Expand Down Expand Up @@ -441,6 +448,10 @@ describe('rum assembly', () => {
})

describe('view context', () => {
afterEach(() => {
resetExperimentalFeatures()
})

it('should be merged with event attributes', () => {
const { lifeCycle } = setupBuilder.build()
notifyRawRumEvent(lifeCycle, {
Expand All @@ -453,6 +464,23 @@ describe('rum assembly', () => {
})
)
})

it('should include the view document version in global context', () => {
updateExperimentalFeatures(['report_view_document_version'])
const { lifeCycle } = setupBuilder.build()
notifyRawRumEvent(lifeCycle, {
rawRumEvent: createRawRumEvent(RumEventType.RESOURCE),
})
expect(serverRumEvents[0].context).toEqual(
jasmine.objectContaining({
_dd: {
view: {
document_version: 42,
},
},
})
)
})
})

describe('service and version', () => {
Expand All @@ -472,7 +500,7 @@ describe('rum assembly', () => {

it('should be overridden by the view context', () => {
const { lifeCycle } = setupBuilder.build()
findView = () => ({ service: 'new service', version: 'new version', id: '1234' })
findView = () => ({ service: 'new service', version: 'new version', id: '1234', documentVersion: 0 })
notifyRawRumEvent(lifeCycle, {
rawRumEvent: createRawRumEvent(RumEventType.ACTION),
})
Expand Down
9 changes: 9 additions & 0 deletions packages/rum-core/src/domain/assembly.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import type { Context, RawError, EventRateLimiter, User } from '@datadog/browser-core'
import {
isExperimentalFeatureEnabled,
combine,
isEmptyObject,
limitModification,
Expand Down Expand Up @@ -140,6 +141,14 @@ export function startRumAssembly(
;(serverRumEvent.usr as Mutable<RumEvent['usr']>) = commonContext.user as User & Context
}

if (isExperimentalFeatureEnabled('report_view_document_version')) {
serverRumEvent.context._dd = {
view: {
document_version: viewContext.documentVersion,
},
}
}

if (shouldSend(serverRumEvent, configuration.beforeSend, domainContext, eventRateLimiters)) {
if (isEmptyObject(serverRumEvent.context)) {
delete serverRumEvent.context
Expand Down
20 changes: 19 additions & 1 deletion packages/rum-core/src/domain/contexts/viewContexts.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { relativeToClocks, CLEAR_OLD_CONTEXTS_INTERVAL } from '@datadog/browser-
import type { TestSetupBuilder } from '../../../test/specHelper'
import { setup } from '../../../test/specHelper'
import { LifeCycleEventType } from '../lifeCycle'
import type { ViewCreatedEvent } from '../rumEventsCollection/view/trackViews'
import type { ViewCreatedEvent, ViewEvent } from '../rumEventsCollection/view/trackViews'
import type { ViewContexts } from './viewContexts'
import { startViewContexts, VIEW_CONTEXT_TIME_OUT_DELAY } from './viewContexts'

Expand All @@ -15,6 +15,7 @@ describe('viewContexts', () => {
return {
startClocks,
id: FAKE_ID,
documentVersion: 0,
...partialViewCreatedEvent,
}
}
Expand Down Expand Up @@ -109,6 +110,23 @@ describe('viewContexts', () => {
lifeCycle.notify(LifeCycleEventType.VIEW_CREATED, buildViewCreatedEvent({ name: 'Fake name' }))
expect(viewContexts.findView()!.name).toBe('Fake name')
})

it('should update the document version on update', () => {
const { lifeCycle } = setupBuilder.build()

const startClocks = relativeToClocks(0 as RelativeTime)

lifeCycle.notify(LifeCycleEventType.VIEW_CREATED, buildViewCreatedEvent({ startClocks }))

expect(viewContexts.findView()!.documentVersion).toBe(0)

lifeCycle.notify(LifeCycleEventType.VIEW_UPDATED, {
startClocks,
documentVersion: 1,
} as ViewEvent)

expect(viewContexts.findView()!.documentVersion).toBe(1)
})
})

describe('history contexts', () => {
Expand Down
9 changes: 9 additions & 0 deletions packages/rum-core/src/domain/contexts/viewContexts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ export interface ViewContext {
service?: string
version?: string
id: string
documentVersion: number
name?: string
}

Expand All @@ -25,6 +26,13 @@ export function startViewContexts(lifeCycle: LifeCycle): ViewContexts {
viewContextHistory.add(buildViewContext(view), view.startClocks.relative)
})

lifeCycle.subscribe(LifeCycleEventType.VIEW_UPDATED, ({ startClocks, documentVersion }) => {
const viewContext = viewContextHistory.find(startClocks.relative)
if (viewContext) {
viewContext.documentVersion = documentVersion
}
})

lifeCycle.subscribe(LifeCycleEventType.VIEW_ENDED, ({ endClocks }) => {
viewContextHistory.closeActive(endClocks.relative)
})
Expand All @@ -39,6 +47,7 @@ export function startViewContexts(lifeCycle: LifeCycle): ViewContexts {
version: view.version,
id: view.id,
name: view.name,
documentVersion: view.documentVersion,
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ export interface ViewCreatedEvent {
service?: string
version?: string
startClocks: ClocksState
documentVersion: number
}

export interface ViewEndedEvent {
Expand Down Expand Up @@ -212,6 +213,7 @@ function newView(
startClocks,
service,
version,
documentVersion,
})

// Update the view every time the measures are changing
Expand Down
3 changes: 3 additions & 0 deletions packages/rum-core/src/rawRumEvent.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,9 @@ export interface RumContext {
session: {
plan: RumSessionPlan
}
view?: {
document_version: number
}
browser_sdk_version?: string
}
}
Expand Down
2 changes: 1 addition & 1 deletion packages/rum/src/boot/startRecording.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ describe('startRecording', () => {
setupBuilder = setup()
.withViewContexts({
findView() {
return { id: viewId }
return { id: viewId, documentVersion: 0 }
},
})
.withSessionManager(sessionManager)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,7 @@ describe('startSegmentCollection', () => {
})

describe('computeSegmentContext', () => {
const DEFAULT_VIEW_CONTEXT: ViewContext = { id: '123' }
const DEFAULT_VIEW_CONTEXT: ViewContext = { id: '123', documentVersion: 0 }
const DEFAULT_SESSION = createRumSessionManagerMock().setId('456')

it('returns a segment context', () => {
Expand Down