@@ -12,6 +12,7 @@ import {
12
12
getLocationHref ,
13
13
GLOBAL_OBJ ,
14
14
logger ,
15
+ parseStringToURLObject ,
15
16
propagationContextFromHeaders ,
16
17
registerSpanErrorInstrumentation ,
17
18
SEMANTIC_ATTRIBUTE_SENTRY_IDLE_SPAN_FINISH_REASON ,
@@ -355,21 +356,11 @@ export const browserTracingIntegration = ((_options: Partial<BrowserTracingOptio
355
356
sampled : spanIsSampled ( idleSpan ) ,
356
357
dsc : getDynamicSamplingContextFromSpan ( span ) ,
357
358
} ) ;
358
-
359
- scope . setSDKProcessingMetadata ( {
360
- normalizedRequest : undefined ,
361
- } ) ;
362
359
} ,
363
360
} ) ;
364
361
365
362
setActiveIdleSpan ( client , idleSpan ) ;
366
363
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
-
373
364
function emitFinish ( ) : void {
374
365
if ( optionalWindowDocument && [ 'interactive' , 'complete' ] . includes ( optionalWindowDocument . readyState ) ) {
375
366
client . emit ( 'idleSpanEnableAutoFinish' , idleSpan ) ;
@@ -409,7 +400,14 @@ export const browserTracingIntegration = ((_options: Partial<BrowserTracingOptio
409
400
maybeEndActiveSpan ( ) ;
410
401
411
402
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
+ } ) ;
413
411
414
412
_createRouteSpan ( client , {
415
413
op : 'navigation' ,
@@ -427,7 +425,15 @@ export const browserTracingIntegration = ((_options: Partial<BrowserTracingOptio
427
425
const baggage = traceOptions . baggage || getMetaContent ( 'baggage' ) ;
428
426
429
427
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
+ } ) ;
431
437
432
438
_createRouteSpan ( client , {
433
439
op : 'pageload' ,
@@ -470,16 +476,23 @@ export const browserTracingIntegration = ((_options: Partial<BrowserTracingOptio
470
476
}
471
477
472
478
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
+ } ) ;
473
487
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
+ } ,
483
496
} ) ;
484
497
} ) ;
485
498
}
0 commit comments