Skip to content

Commit

Permalink
πŸ”Š Collect computed and perf entry durations (#1861)
Browse files Browse the repository at this point in the history
* πŸ”Š Collect computed and perf entry durations
  • Loading branch information
amortemousque authored and William Lacroix committed Dec 13, 2022
1 parent 4e6013c commit 752cf6c
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
import type { Duration, RelativeTime, ServerDuration, TimeStamp } from '@datadog/browser-core'
import { isIE, RequestType, ResourceType } from '@datadog/browser-core'
import {
relativeToClocks,
resetExperimentalFeatures,
updateExperimentalFeatures,
isIE,
RequestType,
ResourceType,
} from '@datadog/browser-core'
import { createResourceEntry } from '../../../../test/fixtures'
import type { TestSetupBuilder } from '../../../../test/specHelper'
import { setup } from '../../../../test/specHelper'
Expand All @@ -26,6 +33,7 @@ describe('resourceCollection', () => {
})

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

Expand Down Expand Up @@ -100,6 +108,35 @@ describe('resourceCollection', () => {
})
})

it('should collect computed duration and performance entry duration when resource_durations ff is enabled', () => {
updateExperimentalFeatures(['resource_durations'])

const match = createResourceEntry({ startTime: 200 as RelativeTime, duration: 300 as Duration })
spyOn(performance, 'getEntriesByName').and.returnValues([match] as unknown as PerformanceResourceTiming[])

const { lifeCycle, rawRumEvents } = setupBuilder.build()
lifeCycle.notify(
LifeCycleEventType.REQUEST_COMPLETED,
createCompletedRequest({
duration: 500 as Duration,
resolveDuration: 50 as Duration,
method: 'GET',
startClocks: relativeToClocks(100 as RelativeTime),
status: 200,
type: RequestType.FETCH,
url: 'https://resource.com/valid',
input: 'https://resource.com/valid',
})
)
const rawRumResourceEvent = rawRumEvents[0].rawRumEvent as RawRumResourceEvent
expect(rawRumResourceEvent._dd).toEqual(
jasmine.objectContaining({
computed_duration: 500000000,
performance_entry_duration: 300000000,
})
)
})

it('should create resource from completed fetch request', () => {
if (isIE()) {
pending('No IE support')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@ import {
relativeToClocks,
assign,
isNumber,
isExperimentalFeatureEnabled,
} from '@datadog/browser-core'
import type { ClocksState } from '@datadog/browser-core'
import type { ClocksState, ServerDuration } from '@datadog/browser-core'
import type { RumConfiguration } from '../../configuration'
import type { RumPerformanceEntry, RumPerformanceResourceTiming } from '../../../browser/performanceCollection'
import type {
Expand Down Expand Up @@ -66,13 +67,16 @@ function processRequest(
const tracingInfo = computeRequestTracingInfo(request, configuration)
const indexingInfo = computeIndexingInfo(sessionManager, startClocks)

const duration = toServerDuration(request.duration)
const durationOverrideInfo = computeDurationOverrideInfo(duration, correspondingTimingOverrides?.resource.duration)

const resourceEvent = combine(
{
date: startClocks.timeStamp,
resource: {
id: generateUUID(),
type,
duration: toServerDuration(request.duration),
duration,
method: request.method,
status_code: request.status,
url: request.url,
Expand All @@ -81,7 +85,8 @@ function processRequest(
},
tracingInfo,
correspondingTimingOverrides,
indexingInfo
indexingInfo,
durationOverrideInfo
)
return {
startTime: startClocks.relative,
Expand Down Expand Up @@ -171,6 +176,21 @@ function computeEntryTracingInfo(entry: RumPerformanceResourceTiming, configurat
}
}

function computeDurationOverrideInfo(
computedDuration: ServerDuration,
performanceEntryDuration: ServerDuration | undefined
) {
if (!isExperimentalFeatureEnabled('resource_durations')) return

return {
_dd: {
computed_duration: computedDuration,
performance_entry_duration: performanceEntryDuration,
override_duration_diff: performanceEntryDuration ? computedDuration - performanceEntryDuration : undefined,
},
}
}

// TODO next major: use directly PerformanceEntry type in domain context
function toPerformanceEntryRepresentation(entry: RumPerformanceEntry): PerformanceEntryRepresentation {
return entry as PerformanceEntryRepresentation
Expand Down

0 comments on commit 752cf6c

Please sign in to comment.