1
- import { getCurrentHub , WINDOW } from '@sentry/svelte' ;
1
+ import { getActiveTransaction } from '@sentry/core' ;
2
+ import { WINDOW } from '@sentry/svelte' ;
2
3
import type { Span , Transaction , TransactionContext } from '@sentry/types' ;
3
4
4
5
import { navigating , page } from '$app/stores' ;
5
6
7
+ const DEFAULT_TAGS = {
8
+ 'routing.instrumentation' : '@sentry/sveltekit' ,
9
+ } ;
10
+
6
11
/**
7
12
*
8
13
* @param startTransactionFn
@@ -25,7 +30,16 @@ export function svelteKitRoutingInstrumentation<T extends Transaction>(
25
30
}
26
31
27
32
function 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
+ } ) ;
29
43
30
44
page . subscribe ( page => {
31
45
if ( ! page ) {
@@ -62,13 +76,20 @@ function instrumentNavigations(startTransactionFn: (context: TransactionContext)
62
76
const routeDestination = navigation . to && navigation . to . route . id ;
63
77
const routeOrigin = navigation . from && navigation . from . route . id ;
64
78
79
+ if ( routeOrigin === routeDestination ) {
80
+ return ;
81
+ }
82
+
65
83
activeTransaction = getActiveTransaction ( ) ;
66
84
67
85
if ( ! activeTransaction ) {
68
86
activeTransaction = startTransactionFn ( {
69
- name : routeDestination || 'unknown' ,
87
+ name : routeDestination || ( WINDOW && WINDOW . location && WINDOW . location . pathname ) ,
70
88
op : 'navigation' ,
71
89
metadata : { source : 'route' } ,
90
+ tags : {
91
+ ...DEFAULT_TAGS ,
92
+ } ,
72
93
} ) ;
73
94
}
74
95
@@ -78,31 +99,10 @@ function instrumentNavigations(startTransactionFn: (context: TransactionContext)
78
99
routingSpan . finish ( ) ;
79
100
}
80
101
routingSpan = activeTransaction . startChild ( {
81
- description : 'SvelteKit Route Change' ,
82
102
op : 'ui.sveltekit.routing' ,
83
- tags : {
84
- 'routing.instrumentation' : '@sentry/sveltekit' ,
85
- from : routeOrigin ,
86
- to : routeDestination ,
87
- } ,
103
+ description : 'SvelteKit Route Change' ,
88
104
} ) ;
105
+ activeTransaction . setTag ( 'from' , routeOrigin ) ;
89
106
}
90
107
} ) ;
91
108
}
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