1
1
import {
2
- SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN ,
3
- SEMANTIC_ATTRIBUTE_SENTRY_SOURCE ,
4
2
SPAN_STATUS_ERROR ,
5
3
addTracingExtensions ,
6
4
captureException ,
7
- continueTrace ,
5
+ getActiveSpan ,
6
+ getIsolationScope ,
7
+ getRootSpan ,
8
8
handleCallbackErrors ,
9
9
setHttpStatus ,
10
- startSpan ,
11
10
} from '@sentry/core' ;
12
11
import { winterCGHeadersToDict } from '@sentry/utils' ;
13
-
14
12
import { isNotFoundNavigationError , isRedirectNavigationError } from './nextNavigationErrorUtils' ;
15
13
import type { RouteHandlerContext } from './types' ;
16
14
import { platformSupportsStreaming } from './utils/platformSupportsStreaming' ;
@@ -26,73 +24,55 @@ export function wrapRouteHandlerWithSentry<F extends (...args: any[]) => any>(
26
24
context : RouteHandlerContext ,
27
25
) : ( ...args : Parameters < F > ) => ReturnType < F > extends Promise < unknown > ? ReturnType < F > : Promise < ReturnType < F > > {
28
26
addTracingExtensions ( ) ;
29
- const { method, parameterizedRoute, headers } = context ;
27
+
28
+ const { headers } = context ;
29
+
30
30
return new Proxy ( routeHandler , {
31
- apply : ( originalFunction , thisArg , args ) => {
32
- return withIsolationScopeOrReuseFromRootSpan ( async isolationScope => {
33
- isolationScope . setSDKProcessingMetadata ( {
34
- request : {
35
- headers : headers ? winterCGHeadersToDict ( headers ) : undefined ,
36
- } ,
37
- } ) ;
38
- return continueTrace (
39
- {
40
- // TODO(v8): Make it so that continue trace will allow null as sentryTrace value and remove this fallback here
41
- sentryTrace : headers ?. get ( 'sentry-trace' ) ?? undefined ,
42
- baggage : headers ?. get ( 'baggage' ) ,
43
- } ,
44
- async ( ) => {
45
- try {
46
- return await startSpan (
47
- {
48
- op : 'http.server' ,
49
- name : `${ method } ${ parameterizedRoute } ` ,
50
- forceTransaction : true ,
51
- attributes : {
52
- [ SEMANTIC_ATTRIBUTE_SENTRY_SOURCE ] : 'route' ,
53
- [ SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN ] : 'auto.function.nextjs' ,
54
- } ,
55
- } ,
56
- async span => {
57
- const response : Response = await handleCallbackErrors (
58
- ( ) => originalFunction . apply ( thisArg , args ) ,
59
- error => {
60
- // Next.js throws errors when calling `redirect()`. We don't wanna report these.
61
- if ( isRedirectNavigationError ( error ) ) {
62
- // Don't do anything
63
- } else if ( isNotFoundNavigationError ( error ) ) {
64
- span . setStatus ( { code : SPAN_STATUS_ERROR , message : 'not_found' } ) ;
65
- } else {
66
- captureException ( error , {
67
- mechanism : {
68
- handled : false ,
69
- } ,
70
- } ) ;
71
- }
72
- } ,
73
- ) ;
31
+ apply : async ( originalFunction , thisArg , args ) => {
32
+ getIsolationScope ( ) . setSDKProcessingMetadata ( {
33
+ request : {
34
+ headers : headers ? winterCGHeadersToDict ( headers ) : undefined ,
35
+ } ,
36
+ } ) ;
74
37
75
- try {
76
- if ( span && response . status ) {
77
- setHttpStatus ( span , response . status ) ;
78
- }
79
- } catch {
80
- // best effort - response may be undefined?
81
- }
38
+ try {
39
+ const activeSpan = getActiveSpan ( ) ;
40
+ const rootSpan = activeSpan && getRootSpan ( activeSpan ) ;
82
41
83
- return response ;
42
+ const response : Response = await handleCallbackErrors (
43
+ ( ) => originalFunction . apply ( thisArg , args ) ,
44
+ error => {
45
+ // Next.js throws errors when calling `redirect()`. We don't wanna report these.
46
+ if ( isRedirectNavigationError ( error ) ) {
47
+ // Don't do anything
48
+ } else if ( isNotFoundNavigationError ( error ) && rootSpan ) {
49
+ rootSpan . setStatus ( { code : SPAN_STATUS_ERROR , message : 'not_found' } ) ;
50
+ } else {
51
+ captureException ( error , {
52
+ mechanism : {
53
+ handled : false ,
84
54
} ,
85
- ) ;
86
- } finally {
87
- if ( ! platformSupportsStreaming ( ) || process . env . NEXT_RUNTIME === 'edge' ) {
88
- // 1. Edge transport requires manual flushing
89
- // 2. Lambdas require manual flushing to prevent execution freeze before the event is sent
90
- await flushQueue ( ) ;
91
- }
55
+ } ) ;
92
56
}
93
57
} ,
94
58
) ;
95
- } ) ;
59
+
60
+ try {
61
+ if ( rootSpan && response . status ) {
62
+ setHttpStatus ( rootSpan , response . status ) ;
63
+ }
64
+ } catch {
65
+ // best effort - response may be undefined?
66
+ }
67
+
68
+ return response ;
69
+ } finally {
70
+ if ( ! platformSupportsStreaming ( ) || process . env . NEXT_RUNTIME === 'edge' ) {
71
+ // 1. Edge transport requires manual flushing
72
+ // 2. Lambdas require manual flushing to prevent execution freeze before the event is sent
73
+ await flushQueue ( ) ;
74
+ }
75
+ }
96
76
} ,
97
77
} ) ;
98
78
}
0 commit comments