Skip to content

Commit

Permalink
[RUMF-636] set initial trace id to the initial document resource
Browse files Browse the repository at this point in the history
  • Loading branch information
BenoitZugmeyer committed Aug 17, 2020
1 parent 9dae5c4 commit 949f4f7
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 6 deletions.
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) {
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)
})
})
})

0 comments on commit 949f4f7

Please sign in to comment.