|
1 | | -import{ |
2 | | -captureException, |
3 | | -getActiveSpan, |
4 | | -getCurrentScope, |
5 | | -getRootSpan, |
6 | | -handleCallbackErrors, |
7 | | -SEMANTIC_ATTRIBUTE_SENTRY_OP, |
8 | | -SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN, |
9 | | -SEMANTIC_ATTRIBUTE_SENTRY_SOURCE, |
10 | | -setCapturedScopesOnSpan, |
11 | | -startSpan, |
12 | | -winterCGRequestToRequestData, |
13 | | -withIsolationScope, |
14 | | -}from'@sentry/core'; |
15 | | -import{addHeadersAsAttributes}from'../common/utils/addHeadersAsAttributes'; |
16 | | -import{flushSafelyWithTimeout,waitUntil}from'../common/utils/responseEnd'; |
| 1 | +import{captureException,getIsolationScope,winterCGRequestToRequestData}from'@sentry/core'; |
| 2 | +import{flushSafelyWithTimeout}from'../common/utils/responseEnd'; |
17 | 3 | importtype{EdgeRouteHandler}from'./types'; |
18 | 4 |
|
19 | 5 | /** |
20 | | - * Wraps a Next.js edge route handler with Sentry errorand performance instrumentation. |
| 6 | + * Wraps a Next.js edge route handler with Sentry errormonitoring. |
21 | 7 | */ |
22 | 8 | exportfunctionwrapApiHandlerWithSentry<HextendsEdgeRouteHandler>( |
23 | 9 | handler:H, |
24 | 10 | parameterizedRoute:string, |
25 | 11 | ):(...params:Parameters<H>)=>Promise<ReturnType<H>>{ |
26 | 12 | returnnewProxy(handler,{ |
27 | 13 | apply:async(wrappingTarget,thisArg,args:Parameters<H>)=>{ |
28 | | -// TODO: We still should add central isolation scope creation for when our build-time instrumentation does not work anymore with turbopack. |
29 | | - |
30 | | -returnwithIsolationScope(isolationScope=>{ |
| 14 | +try{ |
31 | 15 | constreq:unknown=args[0]; |
32 | | -constcurrentScope=getCurrentScope(); |
33 | 16 |
|
34 | | -letheaderAttributes:Record<string,string>={}; |
| 17 | +// Set transaction name on isolation scope to ensure parameterized routes are used |
| 18 | +// The HTTP server integration sets it on isolation scope, so we need to match that |
| 19 | +constisolationScope=getIsolationScope(); |
35 | 20 |
|
36 | 21 | if(reqinstanceofRequest){ |
| 22 | +constmethod=req.method||'GET'; |
| 23 | +isolationScope.setTransactionName(`${method}${parameterizedRoute}`); |
| 24 | +// Set SDK processing metadata |
37 | 25 | isolationScope.setSDKProcessingMetadata({ |
38 | 26 | normalizedRequest:winterCGRequestToRequestData(req), |
39 | 27 | }); |
40 | | -currentScope.setTransactionName(`${req.method}${parameterizedRoute}`); |
41 | | -headerAttributes=addHeadersAsAttributes(req.headers); |
42 | 28 | }else{ |
43 | | -currentScope.setTransactionName(`handler (${parameterizedRoute})`); |
| 29 | +isolationScope.setTransactionName(`handler (${parameterizedRoute})`); |
44 | 30 | } |
45 | 31 |
|
46 | | -letspanName:string; |
47 | | -letop:string|undefined='http.server'; |
| 32 | +returnawaitwrappingTarget.apply(thisArg,args); |
| 33 | +}catch(error){ |
| 34 | +captureException(error,{ |
| 35 | +mechanism:{ |
| 36 | +type:'auto.function.nextjs.wrap_api_handler', |
| 37 | +handled:false, |
| 38 | +}, |
| 39 | +}); |
48 | 40 |
|
49 | | -// If there is an active span, it likely means that the automatic Next.js OTEL instrumentation worked and we can |
50 | | -// rely on that for parameterization. |
51 | | -constactiveSpan=getActiveSpan(); |
52 | | -if(activeSpan){ |
53 | | -spanName=`handler (${parameterizedRoute})`; |
54 | | -op=undefined; |
| 41 | +// we need to await the flush here to ensure that the error is captured |
| 42 | +// as the runtime freezes as soon as the error is thrown below |
| 43 | +awaitflushSafelyWithTimeout(); |
55 | 44 |
|
56 | | -constrootSpan=getRootSpan(activeSpan); |
57 | | -if(rootSpan){ |
58 | | -rootSpan.updateName( |
59 | | -reqinstanceofRequest ?`${req.method}${parameterizedRoute}` :`handler${parameterizedRoute}`, |
60 | | -); |
61 | | -rootSpan.setAttributes({ |
62 | | -[SEMANTIC_ATTRIBUTE_SENTRY_OP]:'http.server', |
63 | | -[SEMANTIC_ATTRIBUTE_SENTRY_SOURCE]:'route', |
64 | | - ...headerAttributes, |
65 | | -}); |
66 | | -setCapturedScopesOnSpan(rootSpan,currentScope,isolationScope); |
67 | | -} |
68 | | -}elseif(reqinstanceofRequest){ |
69 | | -spanName=`${req.method}${parameterizedRoute}`; |
70 | | -}else{ |
71 | | -spanName=`handler${parameterizedRoute}`; |
72 | | -} |
73 | | - |
74 | | -returnstartSpan( |
75 | | -{ |
76 | | -name:spanName, |
77 | | -op:op, |
78 | | -attributes:{ |
79 | | -[SEMANTIC_ATTRIBUTE_SENTRY_SOURCE]:'route', |
80 | | -[SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN]:'auto.function.nextjs.wrap_api_handler', |
81 | | - ...headerAttributes, |
82 | | -}, |
83 | | -}, |
84 | | -()=>{ |
85 | | -returnhandleCallbackErrors( |
86 | | -()=>wrappingTarget.apply(thisArg,args), |
87 | | -error=>{ |
88 | | -captureException(error,{ |
89 | | -mechanism:{ |
90 | | -type:'auto.function.nextjs.wrap_api_handler', |
91 | | -handled:false, |
92 | | -}, |
93 | | -}); |
94 | | -}, |
95 | | -()=>{ |
96 | | -waitUntil(flushSafelyWithTimeout()); |
97 | | -}, |
98 | | -); |
99 | | -}, |
100 | | -); |
101 | | -}); |
| 45 | +// We rethrow here so that nextjs can do with the error whatever it would normally do. |
| 46 | +throwerror; |
| 47 | +} |
102 | 48 | }, |
103 | 49 | }); |
104 | 50 | } |