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-636] initial document trace id #492

Merged
merged 10 commits into from
Aug 24, 2020
Merged
23 changes: 17 additions & 6 deletions packages/rum/src/performanceCollection.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { DOM_EVENT, getRelativeTime, isNumber, monitor } from '@datadog/browser-core'
import { DOM_EVENT, getRelativeTime, isNumber, monitor, ONE_MINUTE } from '@datadog/browser-core'

import { getAPMDocumentData } from './getAPMDocumentData'
import { LifeCycle, LifeCycleEventType } from './lifeCycle'
import { FAKE_INITIAL_DOCUMENT } from './resourceUtils'

Expand Down Expand Up @@ -67,9 +68,8 @@ function supportPerformanceNavigationTimingEvent() {
}

export function startPerformanceCollection(lifeCycle: LifeCycle) {
retrieveInitialDocumentResourceTiming((timing) => {
handleRumPerformanceEntry(lifeCycle, timing)
})
handleRumPerformanceEntry(lifeCycle, retrieveInitialDocumentResourceTiming())

if (supportPerformanceObject()) {
handlePerformanceEntries(lifeCycle, performance.getEntries())
}
Expand Down Expand Up @@ -99,9 +99,20 @@ export function startPerformanceCollection(lifeCycle: LifeCycle) {
}
}

function retrieveInitialDocumentResourceTiming(callback: (timing: RumPerformanceResourceTiming) => void) {
export const INITIAL_DOCUMENT_OUTDATED_TRACE_ID_THRESHOLD = 2 * ONE_MINUTE

export function retrieveInitialDocumentResourceTiming() {
let timing: RumPerformanceResourceTiming

let traceId
const apmDocumentData = getAPMDocumentData(document)
const now = Date.now()
if (apmDocumentData && apmDocumentData.traceTime > now - INITIAL_DOCUMENT_OUTDATED_TRACE_ID_THRESHOLD) {
BenoitZugmeyer marked this conversation as resolved.
Show resolved Hide resolved
traceId = apmDocumentData.traceId
}

const forcedAttributes = {
traceId,
entryType: 'resource' as const,
initiatorType: FAKE_INITIAL_DOCUMENT,
}
Expand All @@ -119,7 +130,7 @@ function retrieveInitialDocumentResourceTiming(callback: (timing: RumPerformance
...forcedAttributes,
}
}
callback(timing)
return timing
}

function retrieveNavigationTimingWhenLoaded(callback: (timing: RumPerformanceNavigationTiming) => void) {
Expand Down
67 changes: 67 additions & 0 deletions packages/rum/test/performanceCollection.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
import { isIE } from '@datadog/browser-core'

import { restorePageVisibility, setPageVisibility } from '../../core/src/specHelper'
import {
INITIAL_DOCUMENT_OUTDATED_TRACE_ID_THRESHOLD,
retrieveInitialDocumentResourceTiming,
} from '../../rum/src/performanceCollection'
import { setup, TestSetupBuilder } from './specHelper'

describe('rum first_contentful_paint', () => {
Expand Down Expand Up @@ -34,3 +38,66 @@ describe('rum first_contentful_paint', () => {
expect(stubBuilder.getEntryTypes()).toContain('paint')
})
})

describe('rum initial document resource', () => {
let setupBuilder: TestSetupBuilder
beforeEach(() => {
setupBuilder = setup().withPerformanceCollection()
})

afterEach(() => {
setupBuilder.cleanup()
})

it('creates a resource timing for the initial document', () => {
const timing = retrieveInitialDocumentResourceTiming()
expect(timing.entryType).toBe('resource')
expect(timing.duration).toBeGreaterThan(0)
})

describe('initial trace id association', () => {
const nodesToRemove: Node[] = []

afterEach(() => {
nodesToRemove.forEach((node) => {
node.parentNode!.removeChild(node)
})
nodesToRemove.length = 0
})

it('should set a trace id found from an APM document comment', () => {
const comment = document.createComment(`DATADOG;trace-id=123;trace-time=${Date.now()}`)
nodesToRemove.push(comment)
document.appendChild(comment)

const timing = retrieveInitialDocumentResourceTiming()
expect(timing.traceId).toBe('123')
})

it('should set a trace id found from meta tags', () => {
const metaTraceId = document.createElement('meta')
metaTraceId.name = 'dd-trace-id'
metaTraceId.content = '456'
document.head.appendChild(metaTraceId)
const metaTraceTime = document.createElement('meta')
metaTraceTime.name = 'dd-trace-time'
metaTraceTime.content = String(Date.now())
document.head.appendChild(metaTraceTime)
nodesToRemove.push(metaTraceId, metaTraceTime)

const timing = retrieveInitialDocumentResourceTiming()
expect(timing.traceId).toBe('456')
})

it('should ignore the trace id if it has been created too long ago', () => {
const comment = document.createComment(
`DATADOG;trace-id=123;trace-time=${Date.now() - INITIAL_DOCUMENT_OUTDATED_TRACE_ID_THRESHOLD}`
)
nodesToRemove.push(comment)
document.appendChild(comment)

const timing = retrieveInitialDocumentResourceTiming()
expect(timing.traceId).toBe(undefined)
})
})
})