Skip to content

Commit

Permalink
Merge branch 'benoit/ignore-init-if-synthetics-injected' (86fb536) in…
Browse files Browse the repository at this point in the history
…to staging-47

 pm_trace_id: 6078086
 feature_branch_pipeline_id: 6078086
 source: to-staging

* commit '86fb5361b42f6cd6c8ea565fae9b7e7be8e4d1ce':
  ✨ [RUMF-1084] implement synthetics injection check
  🚚✅ [RUMF-1084] move synthetics context tests in its own spec
  🚚 [RUMF-1084] move synthetics context getter in its own module
  v3.8.0 (#1169)
  • Loading branch information
BenoitZugmeyer committed Nov 16, 2021
2 parents 8d2343b + 86fb536 commit ac2c431
Show file tree
Hide file tree
Showing 18 changed files with 262 additions and 189 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,13 @@
---

## v3.8.0

-[RUMF-1082] start recording when the DOM is ready ([#1164](https://github.com/DataDog/browser-sdk/pull/1164))
- 🐛 [RUMF-1077] use cookies to get Synthetics context ([#1161](https://github.com/DataDog/browser-sdk/pull/1161))
-[RUMF-1074] bridge host checking ([#1157](https://github.com/DataDog/browser-sdk/pull/1157))
-[REPLAY-341] Add VisualViewport tracking (Pinch Zoom) ([#1118](https://github.com/DataDog/browser-sdk/pull/1118))

## v3.7.0

-[RUMF-1067] forward rum event to bridge ([#1148](https://github.com/DataDog/browser-sdk/pull/1148))
Expand Down
2 changes: 1 addition & 1 deletion developer-extension/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@datadog/browser-sdk-developer-extension",
"version": "3.7.0",
"version": "3.8.0",
"private": true,
"scripts": {
"build": "rm -rf dist && webpack --mode production",
Expand Down
2 changes: 1 addition & 1 deletion lerna.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"npmClient": "yarn",
"useWorkspaces": true,
"version": "3.7.0",
"version": "3.8.0",
"publishConfig": {
"access": "public"
}
Expand Down
2 changes: 1 addition & 1 deletion packages/core/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@datadog/browser-core",
"version": "3.7.0",
"version": "3.8.0",
"license": "Apache-2.0",
"main": "cjs/index.js",
"module": "esm/index.js",
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ export * from './tools/browserDetection'
export { instrumentMethod, instrumentMethodAndCallOriginal } from './tools/instrumentMethod'
export { ErrorSource, ErrorHandling, formatUnknownError, createHandlingStack, RawError } from './tools/error'
export { Context, ContextArray, ContextValue } from './tools/context'
export { areCookiesAuthorized, getCookie, setCookie, COOKIE_ACCESS_DELAY } from './browser/cookie'
export { areCookiesAuthorized, getCookie, setCookie, deleteCookie, COOKIE_ACCESS_DELAY } from './browser/cookie'
export { initXhrObservable, XhrCompleteContext, XhrStartContext } from './browser/xhrObservable'
export { initFetchObservable, FetchCompleteContext, FetchStartContext, FetchContext } from './browser/fetchObservable'
export { EndpointBuilder } from './domain/configuration/endpointBuilder'
Expand Down
4 changes: 2 additions & 2 deletions packages/logs/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@datadog/browser-logs",
"version": "3.7.0",
"version": "3.8.0",
"license": "Apache-2.0",
"main": "cjs/index.js",
"module": "esm/index.js",
Expand All @@ -13,7 +13,7 @@
"replace-build-env": "node ../../scripts/replace-build-env.js"
},
"dependencies": {
"@datadog/browser-core": "3.7.0",
"@datadog/browser-core": "3.8.0",
"tslib": "^1.10.0"
},
"devDependencies": {
Expand Down
4 changes: 2 additions & 2 deletions packages/rum-core/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@datadog/browser-rum-core",
"version": "3.7.0",
"version": "3.8.0",
"license": "Apache-2.0",
"main": "cjs/index.js",
"module": "esm/index.js",
Expand All @@ -12,7 +12,7 @@
"replace-build-env": "node ../../scripts/replace-build-env.js"
},
"dependencies": {
"@datadog/browser-core": "3.7.0",
"@datadog/browser-core": "3.8.0",
"tslib": "^1.10.0"
},
"devDependencies": {
Expand Down
28 changes: 27 additions & 1 deletion packages/rum-core/src/boot/rumPublicApi.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,13 @@ import {
resetExperimentalFeatures,
} from '@datadog/browser-core'
import { initEventBridgeStub, deleteEventBridgeStub } from '../../../core/test/specHelper'
import { noopRecorderApi, setup, TestSetupBuilder } from '../../test/specHelper'
import {
cleanupSyntheticsWorkerValues,
mockSyntheticsWorkerValues,
noopRecorderApi,
setup,
TestSetupBuilder,
} from '../../test/specHelper'
import { ActionType } from '../rawRumEvent.types'
import {
makeRumPublicApi,
Expand Down Expand Up @@ -143,6 +149,26 @@ describe('rum public api', () => {
})
})

describe('init', () => {
let rumPublicApi: RumPublicApi
let startRumSpy: jasmine.Spy<StartRum>

beforeEach(() => {
startRumSpy = jasmine.createSpy()
rumPublicApi = makeRumPublicApi(startRumSpy, noopRecorderApi)
})

afterEach(() => {
cleanupSyntheticsWorkerValues()
})

it('does not call startRum if Synthetics will inject its own instance of RUM', () => {
mockSyntheticsWorkerValues({ injectsRum: true })
rumPublicApi.init(DEFAULT_INIT_CONFIGURATION)
expect(startRumSpy).not.toHaveBeenCalled()
})
})

describe('getInternalContext', () => {
let getInternalContextSpy: jasmine.Spy<ReturnType<StartRum>['getInternalContext']>
let rumPublicApi: RumPublicApi
Expand Down
3 changes: 2 additions & 1 deletion packages/rum-core/src/boot/rumPublicApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import { RumSession } from '../domain/rumSession'
import { RumEventDomainContext } from '../domainContext.types'
import { CommonContext, User, ActionType, ReplayStats } from '../rawRumEvent.types'
import { RumEvent } from '../rumEvent.types'
import { willSyntheticsInjectRum } from '../tools/syntheticsContext'
import { buildEnv } from './buildEnv'
import { startRum } from './startRum'

Expand Down Expand Up @@ -103,7 +104,7 @@ export function makeRumPublicApi<C extends RumInitConfiguration>(startRumImpl: S
return
}

if (!isValidInitConfiguration(initConfiguration)) {
if (willSyntheticsInjectRum() || !isValidInitConfiguration(initConfiguration)) {
return
}

Expand Down
109 changes: 15 additions & 94 deletions packages/rum-core/src/domain/assembly.spec.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,19 @@
import { ErrorSource, ONE_MINUTE, RawError, RelativeTime, display, setCookie } from '@datadog/browser-core'
import { deleteCookie } from 'packages/core/src/browser/cookie'
import { createRumSessionMock } from 'packages/rum-core/test/mockRumSession'
import { ErrorSource, ONE_MINUTE, RawError, RelativeTime, display } from '@datadog/browser-core'
import { createRumSessionMock } from '../../test/mockRumSession'
import { createRawRumEvent } from '../../test/fixtures'
import { setup, TestSetupBuilder } from '../../test/specHelper'
import {
cleanupSyntheticsWorkerValues,
mockSyntheticsWorkerValues,
setup,
TestSetupBuilder,
} from '../../test/specHelper'
import { RumEventDomainContext } from '../domainContext.types'
import { CommonContext, RawRumActionEvent, RawRumErrorEvent, RawRumEvent, RumEventType } from '../rawRumEvent.types'
import { RumActionEvent, RumErrorEvent, RumEvent } from '../rumEvent.types'
import {
BrowserWindow,
startRumAssembly,
SYNTHETICS_RESULT_ID_COOKIE_NAME,
SYNTHETICS_TEST_ID_COOKIE_NAME,
} from './assembly'
import { startRumAssembly } from './assembly'
import { LifeCycle, LifeCycleEventType, RawRumEventCollectedData } from './lifeCycle'
import { RumSessionPlan } from './rumSession'

// Duration to create a cookie lasting at least until the end of the test
const COOKIE_DURATION = 1000

describe('rum assembly', () => {
let setupBuilder: TestSetupBuilder
let commonContext: CommonContext
Expand Down Expand Up @@ -65,9 +61,7 @@ describe('rum assembly', () => {

afterEach(() => {
setupBuilder.cleanup()
cleanupSyntheticsGlobals()
deleteCookie(SYNTHETICS_TEST_ID_COOKIE_NAME)
deleteCookie(SYNTHETICS_RESULT_ID_COOKIE_NAME)
cleanupSyntheticsWorkerValues()
})

describe('beforeSend', () => {
Expand Down Expand Up @@ -527,20 +521,8 @@ describe('rum assembly', () => {
})
})

it('should detect synthetics sessions from global', () => {
setSyntheticsGlobals('foo', 'bar')

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

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

it('should detect synthetics sessions from cookies', () => {
setCookie(SYNTHETICS_TEST_ID_COOKIE_NAME, 'foo', COOKIE_DURATION)
setCookie(SYNTHETICS_RESULT_ID_COOKIE_NAME, 'bar', COOKIE_DURATION)
it('should detect synthetics sessions based on synthetics worker values', () => {
mockSyntheticsWorkerValues()

const { lifeCycle } = setupBuilder.build()
notifyRawRumEvent(lifeCycle, {
Expand Down Expand Up @@ -572,66 +554,15 @@ describe('rum assembly', () => {
})

describe('synthetics context', () => {
it('sets the synthetics context defined by global variables', () => {
setSyntheticsGlobals('foo', 'bar')

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

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

it('sets the synthetics context defined by global cookie', () => {
setCookie(SYNTHETICS_TEST_ID_COOKIE_NAME, 'foo', COOKIE_DURATION)
setCookie(SYNTHETICS_RESULT_ID_COOKIE_NAME, 'bar', COOKIE_DURATION)

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

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

it('does not set synthetics context if one global variable is undefined', () => {
setSyntheticsGlobals('foo')

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

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

it('does not set synthetics context if global variables are not strings', () => {
setSyntheticsGlobals(1, 2)
it('includes the synthetics context', () => {
mockSyntheticsWorkerValues()

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

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

it('does not set synthetics context if one cookie is undefined', () => {
setCookie(SYNTHETICS_TEST_ID_COOKIE_NAME, 'foo', COOKIE_DURATION)

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

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

Expand Down Expand Up @@ -783,13 +714,3 @@ function notifyRawRumEvent<E extends RawRumEvent>(
}
lifeCycle.notify(LifeCycleEventType.RAW_RUM_EVENT_COLLECTED, fullData)
}

function setSyntheticsGlobals(publicId: any, resultId?: any) {
;(window as BrowserWindow)._DATADOG_SYNTHETICS_PUBLIC_ID = publicId
;(window as BrowserWindow)._DATADOG_SYNTHETICS_RESULT_ID = resultId
}

function cleanupSyntheticsGlobals() {
delete (window as BrowserWindow)._DATADOG_SYNTHETICS_PUBLIC_ID
delete (window as BrowserWindow)._DATADOG_SYNTHETICS_RESULT_ID
}
23 changes: 1 addition & 22 deletions packages/rum-core/src/domain/assembly.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import {
RawError,
createEventRateLimiter,
EventRateLimiter,
getCookie,
} from '@datadog/browser-core'
import { RumEventDomainContext } from '../domainContext.types'
import {
Expand All @@ -25,16 +24,12 @@ import {
User,
} from '../rawRumEvent.types'
import { RumEvent } from '../rumEvent.types'
import { getSyntheticsContext } from '../tools/syntheticsContext'
import { LifeCycle, LifeCycleEventType } from './lifeCycle'
import { ParentContexts } from './parentContexts'
import { RumSession, RumSessionPlan } from './rumSession'
import { UrlContexts } from './urlContexts'

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

enum SessionType {
SYNTHETICS = 'synthetics',
USER = 'user',
Expand Down Expand Up @@ -158,19 +153,3 @@ function needToAssembleWithAction(
): event is RawRumErrorEvent | RawRumResourceEvent | RawRumLongTaskEvent {
return [RumEventType.ERROR, RumEventType.RESOURCE, RumEventType.LONG_TASK].indexOf(event.type) !== -1
}

export const SYNTHETICS_TEST_ID_COOKIE_NAME = 'datadog-synthetics-public-id'
export const SYNTHETICS_RESULT_ID_COOKIE_NAME = 'datadog-synthetics-result-id'

function getSyntheticsContext() {
const testId = (window as BrowserWindow)._DATADOG_SYNTHETICS_PUBLIC_ID || getCookie(SYNTHETICS_TEST_ID_COOKIE_NAME)
const resultId =
(window as BrowserWindow)._DATADOG_SYNTHETICS_RESULT_ID || getCookie(SYNTHETICS_RESULT_ID_COOKIE_NAME)

if (typeof testId === 'string' && typeof resultId === 'string') {
return {
test_id: testId,
result_id: resultId,
}
}
}
Loading

0 comments on commit ac2c431

Please sign in to comment.