Skip to content

Commit

Permalink
⚗️[RUMF-766] add match request timing debug infos (experimental) (#609)
Browse files Browse the repository at this point in the history
behind 'match-debug' FF
  • Loading branch information
bcaudan authored Nov 6, 2020
1 parent e0eb8c0 commit a393136
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ describe('matchRequestTiming', () => {

const timing = matchRequestTiming(FAKE_REQUEST as RequestCompleteEvent)

expect(timing).toEqual(match)
expect(timing.candidate).toEqual(match)
})

it('should not match single timing outside the request ', () => {
Expand All @@ -32,7 +32,7 @@ describe('matchRequestTiming', () => {

const timing = matchRequestTiming(FAKE_REQUEST as RequestCompleteEvent)

expect(timing).toEqual(undefined)
expect(timing.candidate).toEqual(undefined)
})

it('should match two following timings nested in the request ', () => {
Expand All @@ -42,7 +42,7 @@ describe('matchRequestTiming', () => {

const timing = matchRequestTiming(FAKE_REQUEST as RequestCompleteEvent)

expect(timing).toEqual(actualTiming)
expect(timing.candidate).toEqual(actualTiming)
})

it('should not match two not following timings nested in the request ', () => {
Expand All @@ -52,7 +52,7 @@ describe('matchRequestTiming', () => {

const timing = matchRequestTiming(FAKE_REQUEST as RequestCompleteEvent)

expect(timing).toEqual(undefined)
expect(timing.candidate).toEqual(undefined)
})

it('should not match multiple timings nested in the request', () => {
Expand All @@ -63,7 +63,7 @@ describe('matchRequestTiming', () => {

const timing = matchRequestTiming(FAKE_REQUEST as RequestCompleteEvent)

expect(timing).toEqual(undefined)
expect(timing.candidate).toEqual(undefined)
})

it('should match invalid timing nested in the request ', () => {
Expand All @@ -76,6 +76,6 @@ describe('matchRequestTiming', () => {

const timing = matchRequestTiming(FAKE_REQUEST as RequestCompleteEvent)

expect(timing).toEqual(undefined)
expect(timing.candidate).toEqual(undefined)
})
})
Original file line number Diff line number Diff line change
Expand Up @@ -22,23 +22,34 @@ interface Timing {
*/
export function matchRequestTiming(request: RequestCompleteEvent) {
if (!performance || !('getEntriesByName' in performance)) {
return
return {}
}
const candidates = performance
.getEntriesByName(request.url, 'resource')
.map((entry) => entry.toJSON() as RumPerformanceResourceTiming)
.filter(toValidEntry)
.filter((entry) => isBetween(entry, request.startTime, endTime(request)))

let result

if (candidates.length === 1) {
return candidates[0]
result = candidates[0]
}

if (candidates.length === 2 && firstCanBeOptionRequest(candidates)) {
return candidates[1]
result = candidates[1]
}

return
return {
candidate: result,
debug: {
candidates: candidates.map((c) => ({ startTime: c.startTime, duration: c.duration })),
matchesNb: candidates.length,
request: { startTime: request.startTime, duration: request.duration },
result: result && { startTime: result.startTime, duration: result.duration },
resultDiff: result && request.duration - result.duration,
},
}
}

function firstCanBeOptionRequest(correspondingEntries: RumPerformanceResourceTiming[]) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,7 @@ describe('resourceCollection V2', () => {
expect(rawRumEventsV2[0].startTime).toBe(1234)
expect(rawRumEventsV2[0].rawRumEvent).toEqual({
date: (jasmine.any(Number) as unknown) as number,
debug: jasmine.anything(),
resource: {
duration: 100 * 1e6,
method: 'GET',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ export function startResourceCollection(lifeCycle: LifeCycle, configuration: Con
lifeCycle.subscribe(LifeCycleEventType.REQUEST_COMPLETED, (request: RequestCompleteEvent) => {
if (session.isTrackedWithResource()) {
configuration.isEnabled('v2_format')
? lifeCycle.notify(LifeCycleEventType.RAW_RUM_EVENT_V2_COLLECTED, processRequestV2(request))
: lifeCycle.notify(LifeCycleEventType.RAW_RUM_EVENT_COLLECTED, processRequest(request))
? lifeCycle.notify(LifeCycleEventType.RAW_RUM_EVENT_V2_COLLECTED, processRequestV2(request, configuration))
: lifeCycle.notify(LifeCycleEventType.RAW_RUM_EVENT_COLLECTED, processRequest(request, configuration))
}
})

Expand All @@ -40,12 +40,14 @@ export function startResourceCollection(lifeCycle: LifeCycle, configuration: Con
})
}

function processRequest(request: RequestCompleteEvent) {
function processRequest(request: RequestCompleteEvent, configuration: Configuration) {
const kind = request.type === RequestType.XHR ? ResourceType.XHR : ResourceType.FETCH

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

const tracingInfo = computeRequestTracingInfo(request)

Expand All @@ -66,17 +68,24 @@ function processRequest(request: RequestCompleteEvent) {
},
},
tracingInfo,
correspondingTimingOverrides
correspondingTimingOverrides,
configuration.isEnabled('match-debug')
? {
debug: matchingTiming.debug,
}
: undefined
)
return { startTime, rawRumEvent: resourceEvent }
}

function processRequestV2(request: RequestCompleteEvent) {
function processRequestV2(request: RequestCompleteEvent, configuration: Configuration) {
const type = request.type === RequestType.XHR ? ResourceType.XHR : ResourceType.FETCH

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

const tracingInfo = computeRequestTracingInfo(request)

Expand All @@ -93,7 +102,12 @@ function processRequestV2(request: RequestCompleteEvent) {
type: RumEventType.RESOURCE,
},
tracingInfo,
correspondingTimingOverrides
correspondingTimingOverrides,
configuration.isEnabled('match-debug')
? {
debug: matchingTiming.debug,
}
: undefined
)
return { startTime, rawRumEvent: resourceEvent as RumResourceEventV2 }
}
Expand Down
1 change: 1 addition & 0 deletions packages/rum/src/typesV2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ export enum RumEventType {

export interface RumResourceEventV2 {
date: number
debug?: any
type: RumEventType.RESOURCE
resource: {
type: ResourceType
Expand Down

0 comments on commit a393136

Please sign in to comment.