66 BAGGAGE_HEADER_NAME ,
77 dynamicSamplingContextToSentryBaggageHeader ,
88 isInstanceOf ,
9+ SENTRY_XHR_DATA_KEY ,
910 stringMatchesSomePattern ,
1011} from '@sentry/utils' ;
1112
@@ -74,7 +75,7 @@ export interface FetchData {
7475/** Data returned from XHR request */
7576export interface XHRData {
7677 xhr ?: {
77- __sentry_xhr__ ?: {
78+ [ SENTRY_XHR_DATA_KEY ] ?: {
7879 method : string ;
7980 url : string ;
8081 status_code : number ;
@@ -297,24 +298,25 @@ export function xhrCallback(
297298 shouldAttachHeaders : ( url : string ) => boolean ,
298299 spans : Record < string , Span > ,
299300) : void {
301+ const xhr = handlerData . xhr ;
302+ const sentryXhrData = xhr && xhr [ SENTRY_XHR_DATA_KEY ] ;
303+
300304 if (
301305 ! hasTracingEnabled ( ) ||
302- ( handlerData . xhr && handlerData . xhr . __sentry_own_request__ ) ||
303- ! ( handlerData . xhr && handlerData . xhr . __sentry_xhr__ && shouldCreateSpan ( handlerData . xhr . __sentry_xhr__ . url ) )
306+ ( xhr && xhr . __sentry_own_request__ ) ||
307+ ! ( xhr && sentryXhrData && shouldCreateSpan ( sentryXhrData . url ) )
304308 ) {
305309 return ;
306310 }
307311
308- const xhr = handlerData . xhr . __sentry_xhr__ ;
309-
310312 // check first if the request has finished and is tracked by an existing span which should now end
311313 if ( handlerData . endTimestamp ) {
312- const spanId = handlerData . xhr . __sentry_xhr_span_id__ ;
314+ const spanId = xhr . __sentry_xhr_span_id__ ;
313315 if ( ! spanId ) return ;
314316
315317 const span = spans [ spanId ] ;
316318 if ( span ) {
317- span . setHttpStatus ( xhr . status_code ) ;
319+ span . setHttpStatus ( sentryXhrData . status_code ) ;
318320 span . finish ( ) ;
319321
320322 // eslint-disable-next-line @typescript-eslint/no-dynamic-delete
@@ -330,21 +332,21 @@ export function xhrCallback(
330332 if ( currentSpan && activeTransaction ) {
331333 const span = currentSpan . startChild ( {
332334 data : {
333- ...xhr . data ,
335+ ...sentryXhrData . data ,
334336 type : 'xhr' ,
335- method : xhr . method ,
336- url : xhr . url ,
337+ method : sentryXhrData . method ,
338+ url : sentryXhrData . url ,
337339 } ,
338- description : `${ xhr . method } ${ xhr . url } ` ,
340+ description : `${ sentryXhrData . method } ${ sentryXhrData . url } ` ,
339341 op : 'http.client' ,
340342 } ) ;
341343
342- handlerData . xhr . __sentry_xhr_span_id__ = span . spanId ;
343- spans [ handlerData . xhr . __sentry_xhr_span_id__ ] = span ;
344+ xhr . __sentry_xhr_span_id__ = span . spanId ;
345+ spans [ xhr . __sentry_xhr_span_id__ ] = span ;
344346
345- if ( handlerData . xhr . setRequestHeader && shouldAttachHeaders ( handlerData . xhr . __sentry_xhr__ . url ) ) {
347+ if ( xhr . setRequestHeader && shouldAttachHeaders ( sentryXhrData . url ) ) {
346348 try {
347- handlerData . xhr . setRequestHeader ( 'sentry-trace' , span . toTraceparent ( ) ) ;
349+ xhr . setRequestHeader ( 'sentry-trace' , span . toTraceparent ( ) ) ;
348350
349351 const dynamicSamplingContext = activeTransaction . getDynamicSamplingContext ( ) ;
350352 const sentryBaggageHeader = dynamicSamplingContextToSentryBaggageHeader ( dynamicSamplingContext ) ;
@@ -353,7 +355,7 @@ export function xhrCallback(
353355 // From MDN: "If this method is called several times with the same header, the values are merged into one single request header."
354356 // We can therefore simply set a baggage header without checking what was there before
355357 // https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/setRequestHeader
356- handlerData . xhr . setRequestHeader ( BAGGAGE_HEADER_NAME , sentryBaggageHeader ) ;
358+ xhr . setRequestHeader ( BAGGAGE_HEADER_NAME , sentryBaggageHeader ) ;
357359 }
358360 } catch ( _ ) {
359361 // Error: InvalidStateError: Failed to execute 'setRequestHeader' on 'XMLHttpRequest': The object's state must be OPENED.
0 commit comments