Skip to content

Commit fd9198e

Browse files
committed
fix(tracing): Better guarding for performance observer
1 parent 81efb87 commit fd9198e

File tree

1 file changed

+12
-3
lines changed

1 file changed

+12
-3
lines changed

packages/tracing-internal/src/browser/request.ts

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,15 @@ export function instrumentOutgoingRequests(_options?: Partial<RequestInstrumenta
166166
}
167167
}
168168

169+
function isPerformanceResourceTiming(entry: PerformanceEntry): entry is PerformanceResourceTiming {
170+
return (
171+
entry.entryType === 'resource' &&
172+
'initiatorType' in entry &&
173+
typeof (entry as PerformanceResourceTiming).nextHopProtocol === 'string' &&
174+
(entry.initiatorType === 'fetch' || entry.initiatorType === 'xmlhttprequest')
175+
);
176+
}
177+
169178
/**
170179
* Creates a temporary observer to listen to the next fetch/xhr resourcing timings,
171180
* so that when timings hit their per-browser limit they don't need to be removed.
@@ -175,9 +184,9 @@ export function instrumentOutgoingRequests(_options?: Partial<RequestInstrumenta
175184
function addHTTPTimings(span: Span): void {
176185
const url = span.data.url;
177186
const observer = new PerformanceObserver(list => {
178-
const entries = list.getEntries() as PerformanceResourceTiming[];
187+
const entries = list.getEntries();
179188
entries.forEach(entry => {
180-
if ((entry.initiatorType === 'fetch' || entry.initiatorType === 'xmlhttprequest') && entry.name.endsWith(url)) {
189+
if (isPerformanceResourceTiming(entry) && entry.name.endsWith(url)) {
181190
const spanData = resourceTimingEntryToSpanData(entry);
182191
spanData.forEach(data => span.setData(...data));
183192
observer.disconnect();
@@ -220,7 +229,7 @@ export function extractNetworkProtocol(nextHopProtocol: string): { name: string;
220229
return { name, version };
221230
}
222231

223-
function getAbsoluteTime(time: number): number {
232+
function getAbsoluteTime(time: number = 0): number {
224233
return ((browserPerformanceTimeOrigin || performance.timeOrigin) + time) / 1000;
225234
}
226235

0 commit comments

Comments
 (0)