Skip to content

Commit

Permalink
feat(core): Make stream instrumentation opt-in (#13951)
Browse files Browse the repository at this point in the history
  • Loading branch information
chargome authored Oct 11, 2024
1 parent 9af0d84 commit 81df165
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 9 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ Sentry.init({
useNavigationType,
createRoutesFromChildren,
matchRoutes,
trackFetchStreamPerformance: true,
}),
replay,
],
Expand Down
10 changes: 10 additions & 0 deletions packages/browser/src/tracing/browserTracingIntegration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down Expand Up @@ -200,6 +208,7 @@ export const browserTracingIntegration = ((_options: Partial<BrowserTracingOptio
markBackgroundSpan,
traceFetch,
traceXHR,
trackFetchStreamPerformance,
shouldCreateSpanForRequest,
enableHTTPTimings,
instrumentPageLoad,
Expand Down Expand Up @@ -409,6 +418,7 @@ export const browserTracingIntegration = ((_options: Partial<BrowserTracingOptio
instrumentOutgoingRequests(client, {
traceFetch,
traceXHR,
trackFetchStreamPerformance,
tracePropagationTargets: client.getOptions().tracePropagationTargets,
shouldCreateSpanForRequest,
enableHTTPTimings,
Expand Down
39 changes: 30 additions & 9 deletions packages/browser/src/tracing/request.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* eslint-disable max-lines */
import {
SENTRY_XHR_DATA_KEY,
addPerformanceInstrumentationHandler,
Expand Down Expand Up @@ -76,7 +77,16 @@ export interface RequestInstrumentationOptions {
*
* Default: true
*/
traceXHR: boolean;
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.
*
* Disabled by default since it can lead to issues with streams using the `cancel()` api
* (https://github.com/getsentry/sentry-javascript/issues/13950)
*
* Default: false
*/;
trackFetchStreamPerformance: boolean;

/**
* If true, Sentry will capture http timings and add them to the corresponding http spans.
Expand All @@ -101,13 +111,22 @@ export const defaultRequestInstrumentationOptions: RequestInstrumentationOptions
traceFetch: true,
traceXHR: true,
enableHTTPTimings: true,
trackFetchStreamPerformance: false,
};

/** Registers span creators for xhr and fetch requests */
export function instrumentOutgoingRequests(client: Client, _options?: Partial<RequestInstrumentationOptions>): void {
const { traceFetch, traceXHR, shouldCreateSpanForRequest, enableHTTPTimings, tracePropagationTargets } = {
const {
traceFetch,
traceXHR,
trackFetchStreamPerformance,
shouldCreateSpanForRequest,
enableHTTPTimings,
tracePropagationTargets,
} = {
traceFetch: defaultRequestInstrumentationOptions.traceFetch,
traceXHR: defaultRequestInstrumentationOptions.traceXHR,
trackFetchStreamPerformance: defaultRequestInstrumentationOptions.trackFetchStreamPerformance,
..._options,
};

Expand Down Expand Up @@ -136,14 +155,16 @@ export function instrumentOutgoingRequests(client: Client, _options?: Partial<Re
return event;
});

addFetchEndInstrumentationHandler(handlerData => {
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);
Expand Down
8 changes: 8 additions & 0 deletions packages/browser/test/tracing/request.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit 81df165

Please sign in to comment.