1- import { getCurrentHub , WINDOW } from '@sentry/svelte' ;
1+ import { getActiveTransaction } from '@sentry/core' ;
2+ import { WINDOW } from '@sentry/svelte' ;
23import type { Span , Transaction , TransactionContext } from '@sentry/types' ;
34
45import { navigating , page } from '$app/stores' ;
56
7+ const DEFAULT_TAGS = {
8+ 'routing.instrumentation' : '@sentry/sveltekit' ,
9+ } ;
10+
611/**
712 *
813 * @param startTransactionFn
@@ -25,7 +30,16 @@ export function svelteKitRoutingInstrumentation<T extends Transaction>(
2530}
2631
2732function instrumentPageload ( startTransactionFn : ( context : TransactionContext ) => Transaction | undefined ) : void {
28- const pageloadTransaction = createPageloadTxn ( startTransactionFn ) ;
33+ const initialPath = WINDOW && WINDOW . location && WINDOW . location . pathname ;
34+
35+ const pageloadTransaction = startTransactionFn ( {
36+ name : initialPath ,
37+ op : 'pageload' ,
38+ description : initialPath ,
39+ tags : {
40+ ...DEFAULT_TAGS ,
41+ } ,
42+ } ) ;
2943
3044 page . subscribe ( page => {
3145 if ( ! page ) {
@@ -62,13 +76,20 @@ function instrumentNavigations(startTransactionFn: (context: TransactionContext)
6276 const routeDestination = navigation . to && navigation . to . route . id ;
6377 const routeOrigin = navigation . from && navigation . from . route . id ;
6478
79+ if ( routeOrigin === routeDestination ) {
80+ return ;
81+ }
82+
6583 activeTransaction = getActiveTransaction ( ) ;
6684
6785 if ( ! activeTransaction ) {
6886 activeTransaction = startTransactionFn ( {
69- name : routeDestination || 'unknown' ,
87+ name : routeDestination || ( WINDOW && WINDOW . location && WINDOW . location . pathname ) ,
7088 op : 'navigation' ,
7189 metadata : { source : 'route' } ,
90+ tags : {
91+ ...DEFAULT_TAGS ,
92+ } ,
7293 } ) ;
7394 }
7495
@@ -78,31 +99,10 @@ function instrumentNavigations(startTransactionFn: (context: TransactionContext)
7899 routingSpan . finish ( ) ;
79100 }
80101 routingSpan = activeTransaction . startChild ( {
81- description : 'SvelteKit Route Change' ,
82102 op : 'ui.sveltekit.routing' ,
83- tags : {
84- 'routing.instrumentation' : '@sentry/sveltekit' ,
85- from : routeOrigin ,
86- to : routeDestination ,
87- } ,
103+ description : 'SvelteKit Route Change' ,
88104 } ) ;
105+ activeTransaction . setTag ( 'from' , routeOrigin ) ;
89106 }
90107 } ) ;
91108}
92-
93- function createPageloadTxn (
94- startTransactionFn : ( context : TransactionContext ) => Transaction | undefined ,
95- ) : Transaction | undefined {
96- const ctx : TransactionContext = {
97- name : 'pageload' ,
98- op : 'pageload' ,
99- description : WINDOW . location . pathname ,
100- } ;
101-
102- return startTransactionFn ( ctx ) ;
103- }
104-
105- function getActiveTransaction ( ) : Transaction | undefined {
106- const scope = getCurrentHub ( ) . getScope ( ) ;
107- return scope && scope . getTransaction ( ) ;
108- }
0 commit comments