-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Expand file tree
/
Copy pathresource-timing.ts
More file actions
41 lines (39 loc) · 2.05 KB
/
resource-timing.ts
File metadata and controls
41 lines (39 loc) · 2.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
import type { Span } from '@sentry/core';
import { browserPerformanceTimeOrigin } from '@sentry/core';
import { extractNetworkProtocol } from '@sentry-internal/browser-utils';
function getAbsoluteTime(time: number = 0): number {
return ((browserPerformanceTimeOrigin() || performance.timeOrigin) + time) / 1000;
}
/**
* Converts a PerformanceResourceTiming entry to span data for the resource span.
*
* @param resourceTiming
* @returns An array where the first element is the attribute name and the second element is the attribute value.
*/
export function resourceTimingToSpanAttributes(
resourceTiming: PerformanceResourceTiming,
): Array<Parameters<Span['setAttribute']>> {
const timingSpanData: Array<Parameters<Span['setAttribute']>> = [];
// Checking for only `undefined` and `null` is intentional because it's
// valid for `nextHopProtocol` to be an empty string.
if (resourceTiming.nextHopProtocol != undefined) {
const { name, version } = extractNetworkProtocol(resourceTiming.nextHopProtocol);
timingSpanData.push(['network.protocol.version', version], ['network.protocol.name', name]);
}
if (!browserPerformanceTimeOrigin()) {
return timingSpanData;
}
return [
...timingSpanData,
['http.request.redirect_start', getAbsoluteTime(resourceTiming.redirectStart)],
['http.request.fetch_start', getAbsoluteTime(resourceTiming.fetchStart)],
['http.request.domain_lookup_start', getAbsoluteTime(resourceTiming.domainLookupStart)],
['http.request.domain_lookup_end', getAbsoluteTime(resourceTiming.domainLookupEnd)],
['http.request.connect_start', getAbsoluteTime(resourceTiming.connectStart)],
['http.request.secure_connection_start', getAbsoluteTime(resourceTiming.secureConnectionStart)],
['http.request.connection_end', getAbsoluteTime(resourceTiming.connectEnd)],
['http.request.request_start', getAbsoluteTime(resourceTiming.requestStart)],
['http.request.response_start', getAbsoluteTime(resourceTiming.responseStart)],
['http.request.response_end', getAbsoluteTime(resourceTiming.responseEnd)],
];
}