@@ -89,75 +89,73 @@ export function withSentry(apiHandler: NextApiHandler, parameterizedRoute?: stri
8989 const currentScope = hub . getScope ( ) ;
9090 const options = hub . getClient ( ) ?. getOptions ( ) ;
9191
92- if ( currentScope ) {
93- currentScope . setSDKProcessingMetadata ( { request : req } ) ;
94-
95- if ( hasTracingEnabled ( options ) && options ?. instrumenter === 'sentry' ) {
96- const sentryTrace =
97- req . headers && isString ( req . headers [ 'sentry-trace' ] ) ? req . headers [ 'sentry-trace' ] : undefined ;
98- const baggage = req . headers ?. baggage ;
99- const { traceparentData, dynamicSamplingContext, propagationContext } = tracingContextFromHeaders (
100- sentryTrace ,
101- baggage ,
102- ) ;
103- hub . getScope ( ) . setPropagationContext ( propagationContext ) ;
104-
105- if ( __DEBUG_BUILD__ && traceparentData ) {
106- logger . log ( `[Tracing] Continuing trace ${ traceparentData . traceId } .` ) ;
107- }
92+ currentScope . setSDKProcessingMetadata ( { request : req } ) ;
93+
94+ if ( hasTracingEnabled ( options ) && options ?. instrumenter === 'sentry' ) {
95+ const sentryTrace =
96+ req . headers && isString ( req . headers [ 'sentry-trace' ] ) ? req . headers [ 'sentry-trace' ] : undefined ;
97+ const baggage = req . headers ?. baggage ;
98+ const { traceparentData, dynamicSamplingContext, propagationContext } = tracingContextFromHeaders (
99+ sentryTrace ,
100+ baggage ,
101+ ) ;
102+ currentScope . setPropagationContext ( propagationContext ) ;
103+
104+ if ( __DEBUG_BUILD__ && traceparentData ) {
105+ logger . log ( `[Tracing] Continuing trace ${ traceparentData . traceId } .` ) ;
106+ }
108107
109- // prefer the parameterized route, if we have it (which we will if we've auto-wrapped the route handler)
110- let reqPath = parameterizedRoute ;
111-
112- // If not, fake it by just replacing parameter values with their names, hoping that none of them match either
113- // each other or any hard-coded parts of the path
114- if ( ! reqPath ) {
115- const url = `${ req . url } ` ;
116- // pull off query string, if any
117- reqPath = stripUrlQueryAndFragment ( url ) ;
118- // Replace with placeholder
119- if ( req . query ) {
120- for ( const [ key , value ] of Object . entries ( req . query ) ) {
121- reqPath = reqPath . replace ( `${ value } ` , `[${ key } ]` ) ;
122- }
108+ // prefer the parameterized route, if we have it (which we will if we've auto-wrapped the route handler)
109+ let reqPath = parameterizedRoute ;
110+
111+ // If not, fake it by just replacing parameter values with their names, hoping that none of them match either
112+ // each other or any hard-coded parts of the path
113+ if ( ! reqPath ) {
114+ const url = `${ req . url } ` ;
115+ // pull off query string, if any
116+ reqPath = stripUrlQueryAndFragment ( url ) ;
117+ // Replace with placeholder
118+ if ( req . query ) {
119+ for ( const [ key , value ] of Object . entries ( req . query ) ) {
120+ reqPath = reqPath . replace ( `${ value } ` , `[${ key } ]` ) ;
123121 }
124122 }
123+ }
125124
126- const reqMethod = `${ ( req . method || 'GET' ) . toUpperCase ( ) } ` ;
127-
128- transaction = startTransaction (
129- {
130- name : `${ reqMethod } ${ reqPath } ` ,
131- op : 'http.server' ,
132- origin : 'auto.http.nextjs' ,
133- ...traceparentData ,
134- metadata : {
135- dynamicSamplingContext : traceparentData && ! dynamicSamplingContext ? { } : dynamicSamplingContext ,
136- source : 'route' ,
137- request : req ,
138- } ,
125+ const reqMethod = `${ ( req . method || 'GET' ) . toUpperCase ( ) } ` ;
126+
127+ transaction = startTransaction (
128+ {
129+ name : `${ reqMethod } ${ reqPath } ` ,
130+ op : 'http.server' ,
131+ origin : 'auto.http.nextjs' ,
132+ ...traceparentData ,
133+ metadata : {
134+ dynamicSamplingContext : traceparentData && ! dynamicSamplingContext ? { } : dynamicSamplingContext ,
135+ source : 'route' ,
136+ request : req ,
139137 } ,
140- // extra context passed to the `tracesSampler`
141- { request : req } ,
142- ) ;
143- currentScope . setSpan ( transaction ) ;
144- if ( platformSupportsStreaming ( ) && ! wrappingTarget . __sentry_test_doesnt_support_streaming__ ) {
145- autoEndTransactionOnResponseEnd ( transaction , res ) ;
146- } else {
147- // If we're not on a platform that supports streaming, we're blocking res.end() until the queue is flushed.
148- // res.json() and res.send() will implicitly call res.end(), so it is enough to wrap res.end().
149-
150- // eslint-disable-next-line @typescript-eslint/unbound-method
151- const origResEnd = res . end ;
152- res . end = async function ( this : unknown , ... args : unknown [ ] ) {
153- if ( transaction ) {
154- await finishTransaction ( transaction , res ) ;
155- await flushQueue ( ) ;
156- }
157-
158- origResEnd . apply ( this , args ) ;
159- } ;
160- }
138+ } ,
139+ // extra context passed to the `tracesSampler`
140+ { request : req } ,
141+ ) ;
142+ currentScope . setSpan ( transaction ) ;
143+ if ( platformSupportsStreaming ( ) && ! wrappingTarget . __sentry_test_doesnt_support_streaming__ ) {
144+ autoEndTransactionOnResponseEnd ( transaction , res ) ;
145+ } else {
146+ // If we're not on a platform that supports streaming, we're blocking res.end() until the queue is flushed .
147+ // res.json() and res.send() will implicitly call res.end(), so it is enough to wrap res.end().
148+
149+ // eslint-disable-next-line @typescript-eslint/unbound-method
150+ const origResEnd = res . end ;
151+ res . end = async function ( this : unknown , ... args : unknown [ ] ) {
152+ if ( transaction ) {
153+ await finishTransaction ( transaction , res ) ;
154+ await flushQueue ( ) ;
155+ }
156+
157+ origResEnd . apply ( this , args ) ;
158+ } ;
161159 }
162160 }
163161
@@ -187,21 +185,19 @@ export function withSentry(apiHandler: NextApiHandler, parameterizedRoute?: stri
187185 // way to prevent it from actually being reported twice.)
188186 const objectifiedErr = objectify ( e ) ;
189187
190- if ( currentScope ) {
191- currentScope . addEventProcessor ( event => {
192- addExceptionMechanism ( event , {
193- type : 'instrument' ,
194- handled : false ,
195- data : {
196- wrapped_handler : wrappingTarget . name ,
197- function : 'withSentry' ,
198- } ,
199- } ) ;
200- return event ;
188+ currentScope . addEventProcessor ( event => {
189+ addExceptionMechanism ( event , {
190+ type : 'instrument' ,
191+ handled : false ,
192+ data : {
193+ wrapped_handler : wrappingTarget . name ,
194+ function : 'withSentry' ,
195+ } ,
201196 } ) ;
197+ return event ;
198+ } ) ;
202199
203- captureException ( objectifiedErr ) ;
204- }
200+ captureException ( objectifiedErr ) ;
205201
206202 // Because we're going to finish and send the transaction before passing the error onto nextjs, it won't yet
207203 // have had a chance to set the status to 500, so unless we do it ourselves now, we'll incorrectly report that
0 commit comments