5
5
BAGGAGE_HEADER_NAME ,
6
6
dynamicSamplingContextToSentryBaggageHeader ,
7
7
isInstanceOf ,
8
+ isMatchingPattern ,
8
9
} from '@sentry/utils' ;
9
10
10
11
import { getActiveTransaction , hasTracingEnabled } from '../utils' ;
@@ -102,26 +103,27 @@ export const defaultRequestInstrumentationOptions: RequestInstrumentationOptions
102
103
103
104
/** Registers span creators for xhr and fetch requests */
104
105
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 } = {
107
107
...defaultRequestInstrumentationOptions ,
108
108
..._options ,
109
109
} ;
110
110
111
111
const shouldCreateSpan =
112
112
typeof shouldCreateSpanForRequest === 'function' ? shouldCreateSpanForRequest : ( _ : string ) => true ;
113
113
114
+ const shouldAttachHeaders = ( url : string ) : boolean => tracingOrigins . some ( origin => isMatchingPattern ( url , origin ) ) ;
115
+
114
116
const spans : Record < string , Span > = { } ;
115
117
116
118
if ( traceFetch ) {
117
119
addInstrumentationHandler ( 'fetch' , ( handlerData : FetchData ) => {
118
- fetchCallback ( handlerData , shouldCreateSpan , spans ) ;
120
+ fetchCallback ( handlerData , shouldCreateSpan , shouldAttachHeaders , spans ) ;
119
121
} ) ;
120
122
}
121
123
122
124
if ( traceXHR ) {
123
125
addInstrumentationHandler ( 'xhr' , ( handlerData : XHRData ) => {
124
- xhrCallback ( handlerData , shouldCreateSpan , spans ) ;
126
+ xhrCallback ( handlerData , shouldCreateSpan , shouldAttachHeaders , spans ) ;
125
127
} ) ;
126
128
}
127
129
}
@@ -132,6 +134,7 @@ export function instrumentOutgoingRequests(_options?: Partial<RequestInstrumenta
132
134
export function fetchCallback (
133
135
handlerData : FetchData ,
134
136
shouldCreateSpan : ( url : string ) => boolean ,
137
+ shouldAttachHeaders : ( url : string ) => boolean ,
135
138
spans : Record < string , Span > ,
136
139
) : void {
137
140
if ( ! hasTracingEnabled ( ) || ! ( handlerData . fetchData && shouldCreateSpan ( handlerData . fetchData . url ) ) ) {
@@ -181,14 +184,16 @@ export function fetchCallback(
181
184
// eslint-disable-next-line @typescript-eslint/no-explicit-any
182
185
const options : { [ key : string ] : any } = handlerData . args [ 1 ] ;
183
186
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
+ ) ;
190
194
191
- activeTransaction . metadata . propagations += 1 ;
195
+ activeTransaction . metadata . propagations += 1 ;
196
+ }
192
197
}
193
198
}
194
199
@@ -262,6 +267,7 @@ function addTracingHeadersToFetchRequest(
262
267
export function xhrCallback (
263
268
handlerData : XHRData ,
264
269
shouldCreateSpan : ( url : string ) => boolean ,
270
+ shouldAttachHeaders : ( url : string ) => boolean ,
265
271
spans : Record < string , Span > ,
266
272
) : void {
267
273
if (
@@ -307,7 +313,7 @@ export function xhrCallback(
307
313
handlerData . xhr . __sentry_xhr_span_id__ = span . spanId ;
308
314
spans [ handlerData . xhr . __sentry_xhr_span_id__ ] = span ;
309
315
310
- if ( handlerData . xhr . setRequestHeader ) {
316
+ if ( handlerData . xhr . setRequestHeader && shouldAttachHeaders ( handlerData . xhr . __sentry_xhr__ . url ) ) {
311
317
try {
312
318
handlerData . xhr . setRequestHeader ( 'sentry-trace' , span . toTraceparent ( ) ) ;
313
319
0 commit comments