@@ -5,44 +5,46 @@ import type { H3Event } from 'h3';
5
5
* Update the root span (transaction) name for routes with parameters based on the matched route.
6
6
*/
7
7
export function updateRouteBeforeResponse ( event : H3Event ) : void {
8
- if ( event . context . matchedRoute ) {
9
- const matchedRoutePath = event . context . matchedRoute . path ;
10
-
11
- // If the matched route path is defined and differs from the event's path, it indicates a parametrized route
12
- // Example: Matched route is "/users/:id" and the event's path is "/users/123",
13
- if ( matchedRoutePath && matchedRoutePath !== event . _path ) {
14
- if ( matchedRoutePath === '/**' ) {
15
- // todo: support parametrized SSR pageload spans
16
- // If page is server-side rendered, the whole path gets transformed to `/**` (Example : `/users/123` becomes `/**` instead of `/users/:id`).
17
- return ; // Skip if the matched route is a catch-all route.
18
- }
8
+ if ( ! event . context . matchedRoute ) {
9
+ return ;
10
+ }
19
11
20
- const method = event . _method || 'GET' ;
12
+ const matchedRoutePath = event . context . matchedRoute . path ;
21
13
22
- const parametrizedTransactionName = `${ method . toUpperCase ( ) } ${ matchedRoutePath } ` ;
23
- getCurrentScope ( ) . setTransactionName ( parametrizedTransactionName ) ;
14
+ // If the matched route path is defined and differs from the event's path, it indicates a parametrized route
15
+ // Example: Matched route is "/users/:id" and the event's path is "/users/123",
16
+ if ( matchedRoutePath && matchedRoutePath !== event . _path ) {
17
+ if ( matchedRoutePath === '/**' ) {
18
+ // todo: support parametrized SSR pageload spans
19
+ // If page is server-side rendered, the whole path gets transformed to `/**` (Example : `/users/123` becomes `/**` instead of `/users/:id`).
20
+ return ; // Skip if the matched route is a catch-all route.
21
+ }
24
22
25
- const activeSpan = getActiveSpan ( ) ; // In development mode, getActiveSpan() is always undefined
26
- if ( activeSpan ) {
27
- const rootSpan = getRootSpan ( activeSpan ) ;
28
- if ( rootSpan ) {
29
- rootSpan . updateName ( parametrizedTransactionName ) ;
30
- rootSpan . setAttributes ( {
31
- [ SEMANTIC_ATTRIBUTE_SENTRY_SOURCE ] : 'route' ,
32
- 'http.route' : matchedRoutePath ,
33
- } ) ;
23
+ const method = event . _method || 'GET' ;
34
24
35
- const params = event . context ?. params || null ;
25
+ const parametrizedTransactionName = `${ method . toUpperCase ( ) } ${ matchedRoutePath } ` ;
26
+ getCurrentScope ( ) . setTransactionName ( parametrizedTransactionName ) ;
36
27
37
- if ( params && typeof params === 'object' ) {
38
- Object . entries ( params ) . forEach ( ( [ key , value ] ) => {
39
- // Based on this convention: https://getsentry.github.io/sentry-conventions/generated/attributes/url.html#urlpathparameterkey
40
- rootSpan . setAttribute ( `url.path.parameter.${ key } ` , String ( value ) ) ;
41
- } ) ;
42
- }
28
+ const activeSpan = getActiveSpan ( ) ; // In development mode, getActiveSpan() is always undefined
29
+ if ( activeSpan ) {
30
+ const rootSpan = getRootSpan ( activeSpan ) ;
31
+ if ( rootSpan ) {
32
+ rootSpan . updateName ( parametrizedTransactionName ) ;
33
+ rootSpan . setAttributes ( {
34
+ [ SEMANTIC_ATTRIBUTE_SENTRY_SOURCE ] : 'route' ,
35
+ 'http.route' : matchedRoutePath ,
36
+ } ) ;
43
37
44
- logger . log ( `Updated transaction name for parametrized route: ${ parametrizedTransactionName } ` ) ;
38
+ const params = event . context ?. params ;
39
+
40
+ if ( params && typeof params === 'object' ) {
41
+ Object . entries ( params ) . forEach ( ( [ key , value ] ) => {
42
+ // Based on this convention: https://getsentry.github.io/sentry-conventions/generated/attributes/url.html#urlpathparameterkey
43
+ rootSpan . setAttribute ( `url.path.parameter.${ key } ` , String ( value ) ) ;
44
+ } ) ;
45
45
}
46
+
47
+ logger . log ( `Updated transaction name for parametrized route: ${ parametrizedTransactionName } ` ) ;
46
48
}
47
49
}
48
50
}
0 commit comments