Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@

- "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 `traceStreams` to the browser tracing integration. Only when set to `true`, Sentry will
instrument streams via fetch.

## 8.34.0

### Important Changes
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ Sentry.init({
useNavigationType,
createRoutesFromChildren,
matchRoutes,
traceStreams: true,
}),
replay,
],
Expand Down
9 changes: 9 additions & 0 deletions packages/browser/src/tracing/browserTracingIntegration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,13 @@ export interface BrowserTracingOptions {
*/
traceXHR: boolean;

/**
* Flag to disable tracing of long-lived streams, like server-sent events (SSE) via fetch.
*
* Default: false
*/
traceStreams: boolean;

/**
* If true, Sentry will capture http timings and add them to the corresponding http spans.
*
Expand Down Expand Up @@ -200,6 +207,7 @@ export const browserTracingIntegration = ((_options: Partial<BrowserTracingOptio
markBackgroundSpan,
traceFetch,
traceXHR,
traceStreams,
shouldCreateSpanForRequest,
enableHTTPTimings,
instrumentPageLoad,
Expand Down Expand Up @@ -409,6 +417,7 @@ export const browserTracingIntegration = ((_options: Partial<BrowserTracingOptio
instrumentOutgoingRequests(client, {
traceFetch,
traceXHR,
traceStreams,
tracePropagationTargets: client.getOptions().tracePropagationTargets,
shouldCreateSpanForRequest,
enableHTTPTimings,
Expand Down
24 changes: 17 additions & 7 deletions packages/browser/src/tracing/request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,13 @@ export interface RequestInstrumentationOptions {
*/
traceXHR: boolean;

/**
* Flag to disable tracing of long-lived streams, like server-sent events (SSE) via fetch.
*
* Default: false
*/
traceStreams: boolean;

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

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

const shouldCreateSpan =
typeof shouldCreateSpanForRequest === 'function' ? shouldCreateSpanForRequest : (_: string) => true;
Expand Down Expand Up @@ -143,7 +153,7 @@ export function instrumentOutgoingRequests(client: Client, _options?: Partial<Re
spanIdToEndTimestamp.set(span, handlerData.endTimestamp);
}
}
});
}, traceStreams);

addFetchInstrumentationHandler(handlerData => {
const createdSpan = instrumentFetchRequest(handlerData, shouldCreateSpan, shouldAttachHeadersWithTargets, spans);
Expand All @@ -167,7 +177,7 @@ export function instrumentOutgoingRequests(client: Client, _options?: Partial<Re
if (enableHTTPTimings && createdSpan) {
addHTTPTimings(createdSpan);
}
});
}, traceStreams);
}

if (traceXHR) {
Expand Down
10 changes: 9 additions & 1 deletion packages/browser/test/tracing/request.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ describe('instrumentOutgoingRequests', () => {

instrumentOutgoingRequests(client);

expect(addFetchSpy).toHaveBeenCalledWith(expect.any(Function));
expect(addFetchSpy).toHaveBeenCalledWith(expect.any(Function), false);
expect(addXhrSpy).toHaveBeenCalledWith(expect.any(Function));
});

Expand All @@ -54,6 +54,14 @@ describe('instrumentOutgoingRequests', () => {

expect(addXhrSpy).not.toHaveBeenCalled();
});

it('does instrument streaming requests if traceStream is true', () => {
const addFetchSpy = vi.spyOn(utils, 'addFetchInstrumentationHandler');

instrumentOutgoingRequests(client, { traceStreams: true });

expect(addFetchSpy).toHaveBeenCalledWith(expect.any(Function), true);
});
});

interface ProtocolInfo {
Expand Down
7 changes: 5 additions & 2 deletions packages/utils/src/instrument/fetch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,13 @@ export function addFetchInstrumentationHandler(
* Only used internally
* @hidden
*/
export function addFetchEndInstrumentationHandler(handler: (data: HandlerDataFetch) => void): void {
export function addFetchEndInstrumentationHandler(
handler: (data: HandlerDataFetch) => void,
traceStreams?: boolean,
): void {
const type = 'fetch-body-resolved';
addHandler(type, handler);
maybeInstrument(type, () => instrumentFetch(streamHandler));
maybeInstrument(type, () => instrumentFetch(traceStreams ? streamHandler : undefined));
}

function instrumentFetch(onFetchResolved?: (response: Response) => void, skipNativeFetchCheck: boolean = false): void {
Expand Down
Loading