@@ -12,6 +12,7 @@ import {
1212 getLocationHref ,
1313 GLOBAL_OBJ ,
1414 logger ,
15+ parseStringToURLObject ,
1516 propagationContextFromHeaders ,
1617 registerSpanErrorInstrumentation ,
1718 SEMANTIC_ATTRIBUTE_SENTRY_IDLE_SPAN_FINISH_REASON ,
@@ -355,21 +356,11 @@ export const browserTracingIntegration = ((_options: Partial<BrowserTracingOptio
355356 sampled : spanIsSampled ( idleSpan ) ,
356357 dsc : getDynamicSamplingContextFromSpan ( span ) ,
357358 } ) ;
358-
359- scope . setSDKProcessingMetadata ( {
360- normalizedRequest : undefined ,
361- } ) ;
362359 } ,
363360 } ) ;
364361
365362 setActiveIdleSpan ( client , idleSpan ) ;
366363
367- // We store the normalized request data on the scope, so we get the request data at time of span creation
368- // otherwise, the URL etc. may already be of the following navigation, and we'd report the wrong URL
369- getCurrentScope ( ) . setSDKProcessingMetadata ( {
370- normalizedRequest : getHttpRequestData ( ) ,
371- } ) ;
372-
373364 function emitFinish ( ) : void {
374365 if ( optionalWindowDocument && [ 'interactive' , 'complete' ] . includes ( optionalWindowDocument . readyState ) ) {
375366 client . emit ( 'idleSpanEnableAutoFinish' , idleSpan ) ;
@@ -409,7 +400,14 @@ export const browserTracingIntegration = ((_options: Partial<BrowserTracingOptio
409400 maybeEndActiveSpan ( ) ;
410401
411402 getIsolationScope ( ) . setPropagationContext ( { traceId : generateTraceId ( ) , sampleRand : Math . random ( ) } ) ;
412- getCurrentScope ( ) . setPropagationContext ( { traceId : generateTraceId ( ) , sampleRand : Math . random ( ) } ) ;
403+
404+ const scope = getCurrentScope ( ) ;
405+ scope . setPropagationContext ( { traceId : generateTraceId ( ) , sampleRand : Math . random ( ) } ) ;
406+ // We reset this to ensure we do not have lingering incorrect data here
407+ // places that call this hook may set this where appropriate - else, the URL at span sending time is used
408+ scope . setSDKProcessingMetadata ( {
409+ normalizedRequest : undefined ,
410+ } ) ;
413411
414412 _createRouteSpan ( client , {
415413 op : 'navigation' ,
@@ -427,7 +425,15 @@ export const browserTracingIntegration = ((_options: Partial<BrowserTracingOptio
427425 const baggage = traceOptions . baggage || getMetaContent ( 'baggage' ) ;
428426
429427 const propagationContext = propagationContextFromHeaders ( sentryTrace , baggage ) ;
430- getCurrentScope ( ) . setPropagationContext ( propagationContext ) ;
428+
429+ const scope = getCurrentScope ( ) ;
430+ scope . setPropagationContext ( propagationContext ) ;
431+
432+ // We store the normalized request data on the scope, so we get the request data at time of span creation
433+ // otherwise, the URL etc. may already be of the following navigation, and we'd report the wrong URL
434+ scope . setSDKProcessingMetadata ( {
435+ normalizedRequest : getHttpRequestData ( ) ,
436+ } ) ;
431437
432438 _createRouteSpan ( client , {
433439 op : 'pageload' ,
@@ -470,16 +476,23 @@ export const browserTracingIntegration = ((_options: Partial<BrowserTracingOptio
470476 }
471477
472478 startingUrl = undefined ;
479+ const parsed = parseStringToURLObject ( to ) ;
480+ startBrowserTracingNavigationSpan ( client , {
481+ name : parsed ?. pathname || WINDOW . location . pathname ,
482+ attributes : {
483+ [ SEMANTIC_ATTRIBUTE_SENTRY_SOURCE ] : 'url' ,
484+ [ SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN ] : 'auto.navigation.browser' ,
485+ } ,
486+ } ) ;
473487
474- // We wait a tick here to ensure that WINDOW.location.pathname is updated
475- setTimeout ( ( ) => {
476- startBrowserTracingNavigationSpan ( client , {
477- name : WINDOW . location . pathname ,
478- attributes : {
479- [ SEMANTIC_ATTRIBUTE_SENTRY_SOURCE ] : 'url' ,
480- [ SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN ] : 'auto.navigation.browser' ,
481- } ,
482- } ) ;
488+ // We store the normalized request data on the scope, so we get the request data at time of span creation
489+ // otherwise, the URL etc. may already be of the following navigation, and we'd report the wrong URL
490+ getCurrentScope ( ) . setSDKProcessingMetadata ( {
491+ normalizedRequest : {
492+ ...getHttpRequestData ( ) ,
493+ // Ensure to set this, so this matches the target route even if the URL has not yet been updated
494+ url : to ,
495+ } ,
483496 } ) ;
484497 } ) ;
485498 }
0 commit comments