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-737] migrate resource to v2 format (experimental) #584

Merged
merged 7 commits into from
Oct 23, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ describe('resourceCollection', () => {

expect(rawRumEvents[0].startTime).toBe(1234)
expect(rawRumEvents[0].rawRumEvent).toEqual({
_dd: undefined,
date: (jasmine.any(Number) as unknown) as number,
duration: 100 * 1e6,
evt: {
Expand Down Expand Up @@ -77,23 +76,17 @@ describe('resourceCollection', () => {

expect(rawRumEvents[0].startTime).toBe(1234)
expect(rawRumEvents[0].rawRumEvent).toEqual({
_dd: undefined,
date: (jasmine.any(Number) as unknown) as number,
duration: 100 * 1e6,
evt: {
category: RumEventCategory.RESOURCE,
},
http: {
method: 'GET',
performance: undefined,
statusCode: 200,
url: 'https://resource.com/valid',
},
network: {
bytesWritten: undefined,
},
resource: {
id: undefined,
kind: ResourceType.XHR,
},
})
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import {
combine,
Configuration,
generateUUID,
getTimestamp,
includes,
msToNs,
RequestType,
ResourceType,
} from '@datadog/browser-core'
import { RumPerformanceResourceTiming } from '../../../browser/performanceCollection'
import { RawRumEvent, RumEventCategory, RumResourceEvent } from '../../../types'
import { RumEventCategory, RumResourceEvent } from '../../../types'
import { LifeCycle, LifeCycleEventType } from '../../lifeCycle'
import { RequestCompleteEvent } from '../../requestCollection'
import { RumSession } from '../../rumSession'
Expand All @@ -17,102 +17,107 @@ import {
computePerformanceResourceDuration,
computeResourceKind,
computeSize,
isRequestKind,
} from '../resourceUtils'
import { matchRequestTiming } from './matchRequestTiming'

export function startResourceCollection(lifeCycle: LifeCycle, configuration: Configuration, session: RumSession) {
const handler = (startTime: number, rawRumEvent: RawRumEvent) =>
lifeCycle.notify(LifeCycleEventType.RAW_RUM_EVENT_COLLECTED, {
rawRumEvent,
startTime,
})
lifeCycle.subscribe(LifeCycleEventType.REQUEST_COMPLETED, (request: RequestCompleteEvent) => {
handleRequest(lifeCycle, session, handler, request)
if (session.isTrackedWithResource()) {
lifeCycle.notify(LifeCycleEventType.RAW_RUM_EVENT_COLLECTED, processRequest(request))
lifeCycle.notify(LifeCycleEventType.RESOURCE_ADDED_TO_BATCH)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

NIT for another PR: RESOURCE_ADDED_TO_BATCH could probably be replaced with a RAW_RUM_EVENT_COLLECTED usage + test on evt.category

Copy link
Contributor Author

@bcaudan bcaudan Oct 23, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yep, I've listed some potential cleanup here

}
})

lifeCycle.subscribe(LifeCycleEventType.PERFORMANCE_ENTRY_COLLECTED, (entry) => {
if (entry.entryType === 'resource') {
handleResourceEntry(lifeCycle, session, handler, entry)
if (session.isTrackedWithResource() && entry.entryType === 'resource' && !isRequestKind(entry)) {
lifeCycle.notify(LifeCycleEventType.RAW_RUM_EVENT_COLLECTED, processResourceEntry(entry))
lifeCycle.notify(LifeCycleEventType.RESOURCE_ADDED_TO_BATCH)
}
})
}

function handleRequest(
lifeCycle: LifeCycle,
session: RumSession,
handler: (startTime: number, event: RumResourceEvent) => void,
request: RequestCompleteEvent
) {
if (!session.isTrackedWithResource()) {
return
}
const timing = matchRequestTiming(request)
function processRequest(request: RequestCompleteEvent) {
const kind = request.type === RequestType.XHR ? ResourceType.XHR : ResourceType.FETCH
const startTime = timing ? timing.startTime : request.startTime
const hasBeenTraced = request.traceId && request.spanId
handler(startTime, {
_dd: hasBeenTraced
? {
spanId: request.spanId!.toDecimalString(),
traceId: request.traceId!.toDecimalString(),
}
: undefined,
date: getTimestamp(startTime),
duration: timing ? computePerformanceResourceDuration(timing) : msToNs(request.duration),
evt: {
category: RumEventCategory.RESOURCE,
},
http: {
method: request.method,
performance: timing ? computePerformanceResourceDetails(timing) : undefined,
statusCode: request.status,
url: request.url,
},
network: {
bytesWritten: timing ? computeSize(timing) : undefined,
},
resource: {
kind,
id: hasBeenTraced ? generateUUID() : undefined,

const matchingTiming = matchRequestTiming(request)
const startTime = matchingTiming ? matchingTiming.startTime : request.startTime
const correspondingTimingOverrides = matchingTiming ? computePerformanceEntryMetrics(matchingTiming) : undefined

const tracingInfo = computeRequestTracingInfo(request)

const resourceEvent: RumResourceEvent = combine(
{
date: getTimestamp(startTime),
duration: msToNs(request.duration),
evt: {
category: RumEventCategory.RESOURCE as const,
},
http: {
method: request.method,
statusCode: request.status,
url: request.url,
},
resource: {
kind,
},
},
})
lifeCycle.notify(LifeCycleEventType.RESOURCE_ADDED_TO_BATCH)
tracingInfo,
correspondingTimingOverrides
)
return { startTime, rawRumEvent: resourceEvent }
}

function handleResourceEntry(
lifeCycle: LifeCycle,
session: RumSession,
handler: (startTime: number, event: RumResourceEvent) => void,
entry: RumPerformanceResourceTiming
) {
if (!session.isTrackedWithResource()) {
return
}
function processResourceEntry(entry: RumPerformanceResourceTiming) {
const resourceKind = computeResourceKind(entry)
if (includes([ResourceType.XHR, ResourceType.FETCH], resourceKind)) {
return
}
handler(entry.startTime, {
_dd: entry.traceId
? {
traceId: entry.traceId,
}
: undefined,
date: getTimestamp(entry.startTime),
duration: computePerformanceResourceDuration(entry),
evt: {
category: RumEventCategory.RESOURCE,
const entryMetrics = computePerformanceEntryMetrics(entry)
const tracingInfo = computeEntryTracingInfo(entry)

const resourceEvent: RumResourceEvent = combine(
{
date: getTimestamp(entry.startTime),
evt: {
category: RumEventCategory.RESOURCE as const,
},
http: {
url: entry.name,
},
resource: {
kind: resourceKind,
},
},
tracingInfo,
entryMetrics
)
return { startTime: entry.startTime, rawRumEvent: resourceEvent }
}

function computePerformanceEntryMetrics(timing: RumPerformanceResourceTiming) {
return {
duration: computePerformanceResourceDuration(timing),
http: {
performance: computePerformanceResourceDetails(entry),
url: entry.name,
performance: computePerformanceResourceDetails(timing),
},
network: {
bytesWritten: computeSize(entry),
bytesWritten: computeSize(timing),
},
resource: {
kind: resourceKind,
}
}

function computeRequestTracingInfo(request: RequestCompleteEvent) {
const hasBeenTraced = request.traceId && request.spanId
if (!hasBeenTraced) {
return undefined
}
return {
_dd: {
spanId: request.spanId!.toDecimalString(),
traceId: request.traceId!.toDecimalString(),
},
})
lifeCycle.notify(LifeCycleEventType.RESOURCE_ADDED_TO_BATCH)
resource: { id: generateUUID() },
}
}

function computeEntryTracingInfo(entry: RumPerformanceResourceTiming) {
return entry.traceId ? { _dd: { traceId: entry.traceId } } : undefined
}
4 changes: 4 additions & 0 deletions packages/rum/src/domain/rumEventsCollection/resourceUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,10 @@ function areInOrder(...numbers: number[]) {
return true
}

export function isRequestKind(timing: RumPerformanceResourceTiming) {
return timing.initiatorType === 'xmlhttprequest' || timing.initiatorType === 'fetch'
}

export function computePerformanceResourceDuration(entry: RumPerformanceResourceTiming): number {
const { duration, startTime, responseEnd } = entry

Expand Down
2 changes: 1 addition & 1 deletion packages/rum/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export interface RumResourceEvent {
statusCode?: number
url: string
}
network: {
network?: {
bytesWritten?: number
}
resource: {
Expand Down