Skip to content

Commit fecdc3e

Browse files
fix(tracing): Fix tracingOrigins not applying (#6079)
In the process of working on #5285, we missed the fact that the first two PRs (#6039 and #6041) were interdependent, in that the former accidentally introduced a bug (#6077) which the latter then inadvertently fixed. This would have been fine, except that we published a release after merging the bug-creating PR but before merging the bug-fixing PR. Whoops. This patch pulls just the bug-fixing part out of the second PR. It also adds tests to cover the buggy cases, using `it.each` to cover all of the different combinations of outcomes for `shouldCreateSpanForRequest` and `shouldAttachHeaders`. Finally, since I was already in the test file, I reorganized it a little: - `it('does not create span if shouldCreateSpan returns false')` -> absorbed into the `it.each()` - `it('does not create span if there is no fetch data in handler data')` -> added header check, became `it('adds neither fetch request spans nor fetch request headers if there is no fetch data in handler data')` - `it('does not add fetch request spans if tracing is disabled')` and `it('does not add fetch request headers if tracing is disabled` -> combined into `it('adds neither fetch request spans nor fetch request headers if tracing is disabled')` - `it('adds sentry-trace header to fetch requests')` -> absorbed into the `it.each()` - Similar changes made to XHR tests Co-authored-by: Tim Fish <[email protected]>
1 parent a800339 commit fecdc3e

File tree

2 files changed

+161
-104
lines changed

2 files changed

+161
-104
lines changed

packages/tracing/src/browser/request.ts

+18-12
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import {
55
BAGGAGE_HEADER_NAME,
66
dynamicSamplingContextToSentryBaggageHeader,
77
isInstanceOf,
8+
isMatchingPattern,
89
} from '@sentry/utils';
910

1011
import { getActiveTransaction, hasTracingEnabled } from '../utils';
@@ -102,26 +103,27 @@ export const defaultRequestInstrumentationOptions: RequestInstrumentationOptions
102103

103104
/** Registers span creators for xhr and fetch requests */
104105
export function instrumentOutgoingRequests(_options?: Partial<RequestInstrumentationOptions>): void {
105-
// eslint-disable-next-line @typescript-eslint/unbound-method
106-
const { traceFetch, traceXHR, shouldCreateSpanForRequest } = {
106+
const { traceFetch, traceXHR, tracingOrigins, shouldCreateSpanForRequest } = {
107107
...defaultRequestInstrumentationOptions,
108108
..._options,
109109
};
110110

111111
const shouldCreateSpan =
112112
typeof shouldCreateSpanForRequest === 'function' ? shouldCreateSpanForRequest : (_: string) => true;
113113

114+
const shouldAttachHeaders = (url: string): boolean => tracingOrigins.some(origin => isMatchingPattern(url, origin));
115+
114116
const spans: Record<string, Span> = {};
115117

116118
if (traceFetch) {
117119
addInstrumentationHandler('fetch', (handlerData: FetchData) => {
118-
fetchCallback(handlerData, shouldCreateSpan, spans);
120+
fetchCallback(handlerData, shouldCreateSpan, shouldAttachHeaders, spans);
119121
});
120122
}
121123

122124
if (traceXHR) {
123125
addInstrumentationHandler('xhr', (handlerData: XHRData) => {
124-
xhrCallback(handlerData, shouldCreateSpan, spans);
126+
xhrCallback(handlerData, shouldCreateSpan, shouldAttachHeaders, spans);
125127
});
126128
}
127129
}
@@ -132,6 +134,7 @@ export function instrumentOutgoingRequests(_options?: Partial<RequestInstrumenta
132134
export function fetchCallback(
133135
handlerData: FetchData,
134136
shouldCreateSpan: (url: string) => boolean,
137+
shouldAttachHeaders: (url: string) => boolean,
135138
spans: Record<string, Span>,
136139
): void {
137140
if (!hasTracingEnabled() || !(handlerData.fetchData && shouldCreateSpan(handlerData.fetchData.url))) {
@@ -181,14 +184,16 @@ export function fetchCallback(
181184
// eslint-disable-next-line @typescript-eslint/no-explicit-any
182185
const options: { [key: string]: any } = handlerData.args[1];
183186

184-
options.headers = addTracingHeadersToFetchRequest(
185-
request,
186-
activeTransaction.getDynamicSamplingContext(),
187-
span,
188-
options,
189-
);
187+
if (shouldAttachHeaders(handlerData.fetchData.url)) {
188+
options.headers = addTracingHeadersToFetchRequest(
189+
request,
190+
activeTransaction.getDynamicSamplingContext(),
191+
span,
192+
options,
193+
);
190194

191-
activeTransaction.metadata.propagations += 1;
195+
activeTransaction.metadata.propagations += 1;
196+
}
192197
}
193198
}
194199

@@ -262,6 +267,7 @@ function addTracingHeadersToFetchRequest(
262267
export function xhrCallback(
263268
handlerData: XHRData,
264269
shouldCreateSpan: (url: string) => boolean,
270+
shouldAttachHeaders: (url: string) => boolean,
265271
spans: Record<string, Span>,
266272
): void {
267273
if (
@@ -307,7 +313,7 @@ export function xhrCallback(
307313
handlerData.xhr.__sentry_xhr_span_id__ = span.spanId;
308314
spans[handlerData.xhr.__sentry_xhr_span_id__] = span;
309315

310-
if (handlerData.xhr.setRequestHeader) {
316+
if (handlerData.xhr.setRequestHeader && shouldAttachHeaders(handlerData.xhr.__sentry_xhr__.url)) {
311317
try {
312318
handlerData.xhr.setRequestHeader('sentry-trace', span.toTraceparent());
313319

0 commit comments

Comments
 (0)