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

✨ [RUMF-996] set synthetics ids on RUM events #1007

Merged
merged 3 commits into from
Aug 20, 2021
Merged
Show file tree
Hide file tree
Changes from 2 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
39 changes: 37 additions & 2 deletions packages/rum-core/src/domain/assembly.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -516,7 +516,8 @@ describe('rum assembly', () => {
})

it('should detect synthetics sessions from global', () => {
;(window as BrowserWindow)._DATADOG_SYNTHETICS_BROWSER = true
;(window as BrowserWindow)._DATADOG_SYNTHETICS_PUBLIC_ID = 'foo'
;(window as BrowserWindow)._DATADOG_SYNTHETICS_RESULT_ID = 'bar'
BenoitZugmeyer marked this conversation as resolved.
Show resolved Hide resolved

const { lifeCycle } = setupBuilder.build()
notifyRawRumEvent(lifeCycle, {
Expand All @@ -525,7 +526,8 @@ describe('rum assembly', () => {

expect(serverRumEvents[0].session.type).toEqual('synthetics')

delete (window as BrowserWindow)._DATADOG_SYNTHETICS_BROWSER
delete (window as BrowserWindow)._DATADOG_SYNTHETICS_PUBLIC_ID
delete (window as BrowserWindow)._DATADOG_SYNTHETICS_RESULT_ID
})

it('should set the session.has_replay attribute if it is defined in the common context', () => {
Expand All @@ -549,6 +551,39 @@ describe('rum assembly', () => {
})
})

describe('synthetics context', () => {
it('sets the synthetics context defined by global variables', () => {
;(window as BrowserWindow)._DATADOG_SYNTHETICS_PUBLIC_ID = 'foo'
;(window as BrowserWindow)._DATADOG_SYNTHETICS_RESULT_ID = 'bar'

const { lifeCycle } = setupBuilder.build()
notifyRawRumEvent(lifeCycle, {
rawRumEvent: createRawRumEvent(RumEventType.VIEW),
})

expect(serverRumEvents[0].synthetics).toEqual({
test_id: 'foo',
result_id: 'bar',
})

delete (window as BrowserWindow)._DATADOG_SYNTHETICS_PUBLIC_ID
delete (window as BrowserWindow)._DATADOG_SYNTHETICS_RESULT_ID
})

it('does not set synthetics context if one global variable is undefined', () => {
;(window as BrowserWindow)._DATADOG_SYNTHETICS_PUBLIC_ID = 'foo'

const { lifeCycle } = setupBuilder.build()
notifyRawRumEvent(lifeCycle, {
rawRumEvent: createRawRumEvent(RumEventType.VIEW),
})

expect(serverRumEvents[0].synthetics).toBeUndefined()

delete (window as BrowserWindow)._DATADOG_SYNTHETICS_PUBLIC_ID
})
})

describe('error events limitation', () => {
const notifiedRawErrors: RawError[] = []

Expand Down
19 changes: 16 additions & 3 deletions packages/rum-core/src/domain/assembly.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ import { ParentContexts } from './parentContexts'
import { RumSession, RumSessionPlan } from './rumSession'

export interface BrowserWindow extends Window {
_DATADOG_SYNTHETICS_BROWSER?: unknown
_DATADOG_SYNTHETICS_PUBLIC_ID?: string
_DATADOG_SYNTHETICS_RESULT_ID?: string
}

enum SessionType {
Expand Down Expand Up @@ -93,6 +94,7 @@ export function startRumAssembly(
// must be computed on each event because synthetics instrumentation can be done after sdk execution
type: getSessionType(),
},
synthetics: getSyntheticsContext(),
}
const serverRumEvent = (needToAssembleWithAction(rawRumEvent)
? combine(rumContext, viewContext, actionContext, rawRumEvent)
Expand Down Expand Up @@ -162,8 +164,19 @@ function needToAssembleWithAction(
}

function getSessionType() {
return navigator.userAgent.indexOf('DatadogSynthetics') === -1 &&
(window as BrowserWindow)._DATADOG_SYNTHETICS_BROWSER === undefined
return navigator.userAgent.indexOf('DatadogSynthetics') === -1 && !getSyntheticsContext()
? SessionType.USER
: SessionType.SYNTHETICS
}

function getSyntheticsContext() {
const testId = (window as BrowserWindow)._DATADOG_SYNTHETICS_PUBLIC_ID
const resultId = (window as BrowserWindow)._DATADOG_SYNTHETICS_RESULT_ID
BenoitZugmeyer marked this conversation as resolved.
Show resolved Hide resolved

if (testId && resultId) {
return {
test_id: testId,
result_id: resultId,
}
}
}
4 changes: 4 additions & 0 deletions packages/rum-core/src/rawRumEvent.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,10 @@ export interface RumContext {
type: string
has_replay?: boolean
}
synthetics?: {
test_id: string
result_id: string
}
_dd: {
format_version: 2
drift: number
Expand Down
54 changes: 48 additions & 6 deletions packages/rum-core/src/rumEvent.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export type RumActionEvent = CommonProperties & {
/**
* RUM event type
*/
readonly type: string
readonly type: 'action'
BenoitZugmeyer marked this conversation as resolved.
Show resolved Hide resolved
/**
* Action properties
*/
Expand Down Expand Up @@ -102,7 +102,7 @@ export type RumErrorEvent = CommonProperties & {
/**
* RUM event type
*/
readonly type: string
readonly type: 'error'
/**
* Error properties
*/
Expand Down Expand Up @@ -220,7 +220,7 @@ export type RumLongTaskEvent = CommonProperties & {
/**
* RUM event type
*/
readonly type: string
readonly type: 'long_task'
/**
* Long Task properties
*/
Expand All @@ -233,6 +233,10 @@ export type RumLongTaskEvent = CommonProperties & {
* Duration in ns of the long task
*/
readonly duration: number
/**
* Whether this long task is considered a frozen frame
*/
readonly is_frozen_frame?: boolean
[k: string]: unknown
}
/**
Expand All @@ -254,7 +258,7 @@ export type RumResourceEvent = CommonProperties & {
/**
* RUM event type
*/
readonly type: string
readonly type: 'resource'
/**
* Resource properties
*/
Expand Down Expand Up @@ -438,7 +442,7 @@ export type RumViewEvent = CommonProperties & {
/**
* RUM event type
*/
readonly type: string
readonly type: 'view'
/**
* View properties
*/
Expand Down Expand Up @@ -509,6 +513,10 @@ export type RumViewEvent = CommonProperties & {
* Whether the View corresponding to this event is considered active
*/
readonly is_active?: boolean
/**
* Whether the View had a low average refresh rate
*/
readonly is_slow_rendered?: boolean
/**
* Properties of the actions of the view
*/
Expand Down Expand Up @@ -549,6 +557,16 @@ export type RumViewEvent = CommonProperties & {
readonly count: number
[k: string]: unknown
}
/**
* Properties of the frozen frames of the view
*/
readonly frozen_frame?: {
/**
* Number of frozen frames that occurred on the view
*/
readonly count: number
[k: string]: unknown
}
/**
* Properties of the resources of the view
*/
Expand Down Expand Up @@ -730,14 +748,38 @@ export interface CommonProperties {
}
[k: string]: unknown
}
/**
* Synthetics properties
*/
readonly synthetics?: {
/**
* The identifier of the current Synthetics test
*/
readonly test_id: string
/**
* The identifier of the current Synthetics test results
*/
readonly result_id: string
[k: string]: unknown
}
/**
* Internal properties
*/
readonly _dd: {
/**
* Version of the RUM event format
*/
readonly format_version: number
readonly format_version: 2
/**
* Session-related internal properties
*/
session?: {
/**
* Session plan: 1 is the 'lite' plan, 2 is the 'replay' plan
*/
plan: 1 | 2
[k: string]: unknown
}
[k: string]: unknown
}
/**
Expand Down