Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(core): Make stream instrumentation opt-in #13951

Merged
merged 8 commits into from
Oct 11, 2024
Merged
Changes from 5 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
@@ -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 `trackFetchStreamPerformance` to the browser tracing integration. Only when set to `true`,
Sentry will instrument streams via fetch.

## 8.34.0

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

/**
* Flag to disable tracking of long-lived streams, like server-sent events (SSE) via fetch.
*
* Default: false
*/
trackFetchStreamPerformance: boolean;
chargome marked this conversation as resolved.
Show resolved Hide resolved

/**
* If true, Sentry will capture http timings and add them to the corresponding http spans.
*
@@ -200,6 +207,7 @@ export const browserTracingIntegration = ((_options: Partial<BrowserTracingOptio
markBackgroundSpan,
traceFetch,
traceXHR,
trackFetchStreamPerformance,
shouldCreateSpanForRequest,
enableHTTPTimings,
instrumentPageLoad,
@@ -409,6 +417,7 @@ export const browserTracingIntegration = ((_options: Partial<BrowserTracingOptio
instrumentOutgoingRequests(client, {
traceFetch,
traceXHR,
trackFetchStreamPerformance,
tracePropagationTargets: client.getOptions().tracePropagationTargets,
shouldCreateSpanForRequest,
enableHTTPTimings,
35 changes: 27 additions & 8 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,
@@ -78,6 +79,13 @@ export interface RequestInstrumentationOptions {
*/
traceXHR: boolean;

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

/**
* If true, Sentry will capture http timings and add them to the corresponding http spans.
*
@@ -101,13 +109,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,
};

@@ -136,14 +153,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);
}
chargome marked this conversation as resolved.
Show resolved Hide resolved
}
}
});
});
}

addFetchInstrumentationHandler(handlerData => {
const createdSpan = instrumentFetchRequest(handlerData, shouldCreateSpan, shouldAttachHeadersWithTargets, spans);
8 changes: 8 additions & 0 deletions packages/browser/test/tracing/request.test.ts
Original file line number Diff line number Diff line change
@@ -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 {