diff --git a/packages/rum-core/src/browser/performanceCollection.ts b/packages/rum-core/src/browser/performanceCollection.ts index 5b121c89d4..5f417bf6b9 100644 --- a/packages/rum-core/src/browser/performanceCollection.ts +++ b/packages/rum-core/src/browser/performanceCollection.ts @@ -98,6 +98,11 @@ export function supportPerformanceTimingEvent(entryType: string) { ) } +export function supportPerformanceEntry() { + // Safari 10 doesn't support PerformanceEntry + return typeof PerformanceEntry === 'function' +} + export function startPerformanceCollection(lifeCycle: LifeCycle, configuration: Configuration) { retrieveInitialDocumentResourceTiming((timing) => { handleRumPerformanceEntry(lifeCycle, configuration, timing) diff --git a/packages/rum-core/src/domain/rumEventsCollection/resource/resourceCollection.ts b/packages/rum-core/src/domain/rumEventsCollection/resource/resourceCollection.ts index 9aab09d5f8..624b62f9ca 100644 --- a/packages/rum-core/src/domain/rumEventsCollection/resource/resourceCollection.ts +++ b/packages/rum-core/src/domain/rumEventsCollection/resource/resourceCollection.ts @@ -6,8 +6,18 @@ import { toServerDuration, relativeToClocks, } from '@datadog/browser-core' -import { RumPerformanceResourceTiming } from '../../../browser/performanceCollection' -import { RawRumResourceEvent, RumEventType } from '../../../rawRumEvent.types' +import { + RumPerformanceEntry, + RumPerformanceResourceTiming, + supportPerformanceEntry, +} from '../../../browser/performanceCollection' +import { + PerformanceEntryRepresentation, + RawRumResourceEvent, + RumEventType, + RumFetchResourceEventDomainContext, + RumXhrResourceEventDomainContext, +} from '../../../rawRumEvent.types' import { LifeCycle, LifeCycleEventType, RawRumEventCollectedData } from '../../lifeCycle' import { RequestCompleteEvent } from '../../requestCollection' import { RumSession } from '../../rumSession' @@ -63,13 +73,13 @@ function processRequest(request: RequestCompleteEvent): RawRumEventCollectedData startTime: startClocks.relative, rawRumEvent: resourceEvent, domainContext: { - performanceEntry: matchingTiming instanceof PerformanceEntry ? matchingTiming.toJSON() : matchingTiming, + performanceEntry: matchingTiming && toPerformanceEntryRepresentation(matchingTiming), xhr: request.xhr, response: request.response, requestInput: request.input, requestInit: request.init, error: request.error, - }, + } as RumFetchResourceEventDomainContext | RumXhrResourceEventDomainContext, } } @@ -96,7 +106,7 @@ function processResourceEntry(entry: RumPerformanceResourceTiming): RawRumEventC startTime: startClocks.relative, rawRumEvent: resourceEvent, domainContext: { - performanceEntry: entry instanceof PerformanceEntry ? entry.toJSON() : entry, + performanceEntry: toPerformanceEntryRepresentation(entry), }, } } @@ -127,3 +137,10 @@ function computeRequestTracingInfo(request: RequestCompleteEvent) { function computeEntryTracingInfo(entry: RumPerformanceResourceTiming) { return entry.traceId ? { _dd: { trace_id: entry.traceId } } : undefined } + +function toPerformanceEntryRepresentation(entry: RumPerformanceEntry): PerformanceEntryRepresentation { + if (supportPerformanceEntry() && entry instanceof PerformanceEntry) { + entry.toJSON() + } + return entry as PerformanceEntryRepresentation +}