Skip to content

Commit 891a44e

Browse files
authored
fix(tracing): Better guarding for performance observer (#8872)
This removes a type cast for the performance observer and actually adds some guards to make sure we do not run into cases where a property we expect to exist does not exist. It seems we sometimes ran into cases where `nextHopProtocol` would be `undefined`, not a string, leading to https://github.com/getsentry/sentry-javascript/blob/develop/packages/tracing-internal/src/browser/request.ts#L202 failing. I now specifically check for the existence of this property, as well as also adding a default for all the time based stuff (0) to ensure these also work in the case one of the fields does not exist (instead of checking for existence of all of them). Closes #8870 Closes #8863
1 parent 81efb87 commit 891a44e

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)