You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: dev-packages/browser-integration-tests/suites/tracing/browserTracingIntegration/http-timings-streamed/test.ts
+2-10Lines changed: 2 additions & 10 deletions
Original file line number
Diff line number
Diff line change
@@ -3,16 +3,8 @@ import { sentryTest } from '../../../../utils/fixtures';
Copy file name to clipboardExpand all lines: dev-packages/browser-integration-tests/suites/tracing/setSpanActive-streamed/nested-parentAlwaysRoot/test.ts
Copy file name to clipboardExpand all lines: packages/browser/src/tracing/request.ts
+43-5Lines changed: 43 additions & 5 deletions
Original file line number
Diff line number
Diff line change
@@ -5,6 +5,7 @@ import type {
5
5
ResponseHookInfo,
6
6
SentryWrappedXMLHttpRequest,
7
7
Span,
8
+
SpanTimeInput,
8
9
}from'@sentry/core';
9
10
import{
10
11
addFetchEndInstrumentationHandler,
@@ -238,8 +239,17 @@ export function instrumentOutgoingRequests(client: Client, _options?: Partial<Re
238
239
}
239
240
240
241
/**
241
-
* Creates a temporary observer to listen to the next fetch/xhr resourcing timings,
242
-
* so that when timings hit their per-browser limit they don't need to be removed.
242
+
* The maximum time (ms) to wait for PerformanceResourceTiming data before ending the span.
243
+
* Same approach is used by OTel's browser fetch instrumentation:
244
+
* See {@link https://github.com/open-telemetry/opentelemetry-js/blob/30f94fe99339287b1e4d3c8bb90172c2523f06f4/experimental/packages/opentelemetry-instrumentation-fetch/src/fetch.ts#L352-L372}
245
+
*/
246
+
constHTTP_TIMING_WAIT_MS=300;
247
+
248
+
/**
249
+
* Intercepts `span.end()` to delay the actual span ending until PerformanceResourceTiming
250
+
* data is available (or until the timeout elapses), so that timing attributes are always
251
+
* present on the span even in span-streaming mode where spans are serialised immediately
252
+
* on end.
243
253
*
244
254
* @param span A span that has yet to be finished, must contain `url` on data.
245
255
*/
@@ -250,16 +260,44 @@ function addHTTPTimings(span: Span): void {
250
260
return;
251
261
}
252
262
263
+
// Capture a reference to the real end implementation before shadowing it.
264
+
constoriginalEnd=span.end.bind(span);
265
+
letcapturedEndTimestamp: SpanTimeInput|undefined;
266
+
letisEnded=false;
267
+
268
+
// Shadow the prototype method with an instance property so that when
269
+
// instrumentFetchRequest / xhrCallback calls span.end(), we intercept it.
270
+
span.end=(endTimestamp?: SpanTimeInput)=>{
271
+
capturedEndTimestamp=endTimestamp;
272
+
// Do not call originalEnd here; finishSpan() handles that once timing data
273
+
// has arrived (or the timeout fires).
274
+
};
275
+
276
+
constendSpanAndCleanup=(): void=>{
277
+
if(isEnded){
278
+
return;
279
+
}
280
+
isEnded=true;
281
+
// In the next tick, clean up the performance observer
282
+
// We have to wait here because otherwise we clean it up before it is fully done
0 commit comments