55 BAGGAGE_HEADER_NAME ,
66 dynamicSamplingContextToSentryBaggageHeader ,
77 isInstanceOf ,
8+ isMatchingPattern ,
89} from '@sentry/utils' ;
910
1011import { getActiveTransaction , hasTracingEnabled } from '../utils' ;
@@ -102,26 +103,27 @@ export const defaultRequestInstrumentationOptions: RequestInstrumentationOptions
102103
103104/** Registers span creators for xhr and fetch requests */
104105export 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
132134export 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(
262267export 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