Skip to content

feat(tracing): add autoSentryTrace to enbale/disable sentry-trace #4378

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

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all 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
3 changes: 2 additions & 1 deletion packages/tracing/src/browser/browsertracing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,7 @@ export class BrowserTracing implements Integration {
traceFetch,
traceXHR,
tracingOrigins,
autoSentryTrace,
shouldCreateSpanForRequest,
} = this.options;

Expand All @@ -190,7 +191,7 @@ export class BrowserTracing implements Integration {
registerBackgroundTabDetection();
}

instrumentOutgoingRequests({ traceFetch, traceXHR, tracingOrigins, shouldCreateSpanForRequest });
instrumentOutgoingRequests({ traceFetch, traceXHR, tracingOrigins, autoSentryTrace, shouldCreateSpanForRequest });
}

/** Create routing idle transaction. */
Expand Down
21 changes: 16 additions & 5 deletions packages/tracing/src/browser/request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,14 @@ export interface RequestInstrumentationOptions {
*/
traceXHR: boolean;

/**
* Flag to disable send request with `sentry-trace`.
*
* Default: true
*
*/
autoSentryTrace: boolean;

/**
* This function will be called before creating a span for a request with the given url.
* Return false if you don't want a span for the given url.
Expand Down Expand Up @@ -79,13 +87,14 @@ export interface XHRData {
export const defaultRequestInstrumentationOptions: RequestInstrumentationOptions = {
traceFetch: true,
traceXHR: true,
autoSentryTrace: true,
tracingOrigins: DEFAULT_TRACING_ORIGINS,
};

/** Registers span creators for xhr and fetch requests */
export function instrumentOutgoingRequests(_options?: Partial<RequestInstrumentationOptions>): void {
// eslint-disable-next-line @typescript-eslint/unbound-method
const { traceFetch, traceXHR, tracingOrigins, shouldCreateSpanForRequest } = {
const { traceFetch, traceXHR, autoSentryTrace, tracingOrigins, shouldCreateSpanForRequest } = {
...defaultRequestInstrumentationOptions,
..._options,
};
Expand Down Expand Up @@ -118,13 +127,13 @@ export function instrumentOutgoingRequests(_options?: Partial<RequestInstrumenta

if (traceFetch) {
addInstrumentationHandler('fetch', (handlerData: FetchData) => {
fetchCallback(handlerData, shouldCreateSpan, spans);
fetchCallback(handlerData, shouldCreateSpan, spans, autoSentryTrace);
});
}

if (traceXHR) {
addInstrumentationHandler('xhr', (handlerData: XHRData) => {
xhrCallback(handlerData, shouldCreateSpan, spans);
xhrCallback(handlerData, shouldCreateSpan, spans, autoSentryTrace);
});
}
}
Expand All @@ -136,6 +145,7 @@ export function fetchCallback(
handlerData: FetchData,
shouldCreateSpan: (url: string) => boolean,
spans: Record<string, Span>,
autoSentryTrace: boolean,
): void {
if (!hasTracingEnabled() || !(handlerData.fetchData && shouldCreateSpan(handlerData.fetchData.url))) {
return;
Expand Down Expand Up @@ -180,7 +190,7 @@ export function fetchCallback(
if (isInstanceOf(request, Request)) {
headers = (request as Request).headers;
}
if (headers) {
if (autoSentryTrace && headers) {
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
if (typeof headers.append === 'function') {
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
Expand All @@ -204,6 +214,7 @@ export function xhrCallback(
handlerData: XHRData,
shouldCreateSpan: (url: string) => boolean,
spans: Record<string, Span>,
autoSentryTrace: boolean,
): void {
if (
!hasTracingEnabled() ||
Expand Down Expand Up @@ -245,7 +256,7 @@ export function xhrCallback(
handlerData.xhr.__sentry_xhr_span_id__ = span.spanId;
spans[handlerData.xhr.__sentry_xhr_span_id__] = span;

if (handlerData.xhr.setRequestHeader) {
if (autoSentryTrace && handlerData.xhr.setRequestHeader) {
try {
handlerData.xhr.setRequestHeader('sentry-trace', span.toTraceparent());
} catch (_) {
Expand Down