diff --git a/CHANGELOG.md b/CHANGELOG.md index 2abc4bbb61e5..0a7cb97c8c4f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,6 +10,12 @@ - "You miss 100 percent of the chances you don't take. — Wayne Gretzky" — Michael Scott +- **feat(core): Make stream instrumentation opt-in + ([#13951](https://github.com/getsentry/sentry-javascript/pull/13951))** + +This change adds a new option `trackFetchStreamPerformance` to the browser tracing integration. Only when set to `true`, +Sentry will instrument streams via fetch. + Work in this release was contributed by @ZakrepaShe. Thank you for your contribution! ## 8.34.0 diff --git a/dev-packages/e2e-tests/test-applications/react-router-6/src/index.tsx b/dev-packages/e2e-tests/test-applications/react-router-6/src/index.tsx index 434c1677bf88..8c219563e5a4 100644 --- a/dev-packages/e2e-tests/test-applications/react-router-6/src/index.tsx +++ b/dev-packages/e2e-tests/test-applications/react-router-6/src/index.tsx @@ -26,6 +26,7 @@ Sentry.init({ useNavigationType, createRoutesFromChildren, matchRoutes, + trackFetchStreamPerformance: true, }), replay, ], diff --git a/packages/browser/src/tracing/browserTracingIntegration.ts b/packages/browser/src/tracing/browserTracingIntegration.ts index 523edd7e4262..b3f8d9abdba8 100644 --- a/packages/browser/src/tracing/browserTracingIntegration.ts +++ b/packages/browser/src/tracing/browserTracingIntegration.ts @@ -132,6 +132,14 @@ export interface BrowserTracingOptions { */ traceXHR: boolean; + /** + * Flag to disable tracking of long-lived streams, like server-sent events (SSE) via fetch. + * Do not enable this in case you have live streams or very long running streams. + * + * Default: false + */ + trackFetchStreamPerformance: boolean; + /** * If true, Sentry will capture http timings and add them to the corresponding http spans. * @@ -200,6 +208,7 @@ export const browserTracingIntegration = ((_options: Partial): void { - const { traceFetch, traceXHR, shouldCreateSpanForRequest, enableHTTPTimings, tracePropagationTargets } = { + const { + traceFetch, + traceXHR, + trackFetchStreamPerformance, + shouldCreateSpanForRequest, + enableHTTPTimings, + tracePropagationTargets, + } = { traceFetch: defaultRequestInstrumentationOptions.traceFetch, traceXHR: defaultRequestInstrumentationOptions.traceXHR, + trackFetchStreamPerformance: defaultRequestInstrumentationOptions.trackFetchStreamPerformance, ..._options, }; @@ -136,14 +155,16 @@ export function instrumentOutgoingRequests(client: Client, _options?: Partial { - if (handlerData.response) { - const span = responseToSpanId.get(handlerData.response); - if (span && handlerData.endTimestamp) { - spanIdToEndTimestamp.set(span, handlerData.endTimestamp); + if (trackFetchStreamPerformance) { + addFetchEndInstrumentationHandler(handlerData => { + if (handlerData.response) { + const span = responseToSpanId.get(handlerData.response); + if (span && handlerData.endTimestamp) { + spanIdToEndTimestamp.set(span, handlerData.endTimestamp); + } } - } - }); + }); + } addFetchInstrumentationHandler(handlerData => { const createdSpan = instrumentFetchRequest(handlerData, shouldCreateSpan, shouldAttachHeadersWithTargets, spans); diff --git a/packages/browser/test/tracing/request.test.ts b/packages/browser/test/tracing/request.test.ts index f1067c9e4b52..4f1c0bfdaf0a 100644 --- a/packages/browser/test/tracing/request.test.ts +++ b/packages/browser/test/tracing/request.test.ts @@ -54,6 +54,14 @@ describe('instrumentOutgoingRequests', () => { expect(addXhrSpy).not.toHaveBeenCalled(); }); + + it('does instrument streaming requests if trackFetchStreamPerformance is true', () => { + const addFetchEndSpy = vi.spyOn(utils, 'addFetchEndInstrumentationHandler'); + + instrumentOutgoingRequests(client, { trackFetchStreamPerformance: true }); + + expect(addFetchEndSpy).toHaveBeenCalledWith(expect.any(Function)); + }); }); interface ProtocolInfo {