11/* eslint-disable max-lines */
2- import { captureException , getCurrentHub } from '@sentry/node' ;
3- import { getActiveTransaction , hasTracingEnabled , Span } from '@sentry/tracing' ;
4- import { WrappedFunction } from '@sentry/types' ;
2+ import { captureException , getCurrentHub , Hub } from '@sentry/node' ;
3+ import { getActiveTransaction , hasTracingEnabled } from '@sentry/tracing' ;
4+ import { Transaction , WrappedFunction } from '@sentry/types' ;
55import { addExceptionMechanism , fill , isNodeEnv , loadModule , logger , serializeBaggage } from '@sentry/utils' ;
66import * as domain from 'domain' ;
77
@@ -292,9 +292,9 @@ export function startRequestHandlerTransaction(
292292 url : URL ,
293293 method : string ,
294294 routes : ServerRoute [ ] ,
295+ hub : Hub ,
295296 pkg ?: ReactRouterDomPkg ,
296- ) : Span | undefined {
297- const hub = getCurrentHub ( ) ;
297+ ) : Transaction {
298298 const currentScope = hub . getScope ( ) ;
299299 const matches = matchServerRoutes ( routes , url . pathname , pkg ) ;
300300
@@ -312,9 +312,7 @@ export function startRequestHandlerTransaction(
312312 } ,
313313 } ) ;
314314
315- if ( transaction ) {
316- currentScope ?. setSpan ( transaction ) ;
317- }
315+ currentScope ?. setSpan ( transaction ) ;
318316 return transaction ;
319317}
320318
@@ -324,13 +322,20 @@ function wrapRequestHandler(origRequestHandler: RequestHandler, build: ServerBui
324322 return async function ( this : unknown , request : Request , loadContext ?: unknown ) : Promise < Response > {
325323 const local = domain . create ( ) ;
326324 return local . bind ( async ( ) => {
325+ const hub = getCurrentHub ( ) ;
326+ const options = hub . getClient ( ) ?. getOptions ( ) ;
327+
328+ if ( ! options || ! hasTracingEnabled ( options ) ) {
329+ return origRequestHandler . call ( this , request , loadContext ) ;
330+ }
331+
327332 const url = new URL ( request . url ) ;
328- const transaction = startRequestHandlerTransaction ( url , request . method , routes , pkg ) ;
333+ const transaction = startRequestHandlerTransaction ( url , request . method , routes , hub , pkg ) ;
329334
330335 const res = ( await origRequestHandler . call ( this , request , loadContext ) ) as Response ;
331336
332- transaction ? .setHttpStatus ( res . status ) ;
333- transaction ? .finish ( ) ;
337+ transaction . setHttpStatus ( res . status ) ;
338+ transaction . finish ( ) ;
334339
335340 return res ;
336341 } ) ( ) ;
0 commit comments