Skip to content

Commit 3993dd3

Browse files
committed
fix(tracing): ignore the fetch response if its request is not being tracked
1 parent d788c70 commit 3993dd3

File tree

2 files changed

+23
-3
lines changed

2 files changed

+23
-3
lines changed

packages/tracing/src/browser/request.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -141,8 +141,11 @@ export function fetchCallback(
141141
return;
142142
}
143143

144-
if (handlerData.endTimestamp && handlerData.fetchData.__span) {
145-
const span = spans[handlerData.fetchData.__span];
144+
if (handlerData.endTimestamp) {
145+
const spanId = handlerData.fetchData.__span;
146+
if (!spanId) return;
147+
148+
const span = spans[spanId];
146149
if (span) {
147150
if (handlerData.response) {
148151
// TODO (kmclb) remove this once types PR goes through
@@ -154,7 +157,7 @@ export function fetchCallback(
154157
span.finish();
155158

156159
// eslint-disable-next-line @typescript-eslint/no-dynamic-delete
157-
delete spans[handlerData.fetchData.__span];
160+
delete spans[spanId];
158161
}
159162
return;
160163
}

packages/tracing/test/browser/request.test.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,23 @@ describe('callbacks', () => {
174174
expect(newSpan!.status).toBe(spanStatusfromHttpCode(404));
175175
});
176176

177+
it('ignores response with no associated span', () => {
178+
// the request might be missed somehow. E.g. if it was sent before tracing gets enabled.
179+
180+
const postRequestFetchHandlerData = {
181+
...fetchHandlerData,
182+
endTimestamp,
183+
response: { status: 404 } as Response,
184+
};
185+
186+
// in that case, the response coming back will be ignored
187+
fetchCallback(postRequestFetchHandlerData, alwaysCreateSpan, {});
188+
189+
const newSpan = transaction.spanRecorder?.spans[1];
190+
191+
expect(newSpan).toBeUndefined();
192+
});
193+
177194
it('adds sentry-trace header to fetch requests', () => {
178195
// TODO
179196
});

0 commit comments

Comments
 (0)