@@ -12,6 +12,39 @@ import { flush } from './sdk';
12
12
13
13
const DEFAULT_SHUTDOWN_TIMEOUT = 2000 ;
14
14
15
+ /**
16
+ * Express compatible tracing handler.
17
+ * @see Exposed as `Handlers.tracingHandler`
18
+ */
19
+ export function tracingHandler ( ) : (
20
+ req : http . IncomingMessage ,
21
+ res : http . ServerResponse ,
22
+ next : ( error ?: any ) => void ,
23
+ ) => void {
24
+ return function sentryTracingMiddleware (
25
+ req : http . IncomingMessage ,
26
+ res : http . ServerResponse ,
27
+ next : ( error ?: any ) => void ,
28
+ ) : void {
29
+ // TODO: At this point req.route.path we use in `extractTransaction` is not available
30
+ // but `req.path` or `req.url` should do the job as well. We could unify this here.
31
+ const reqMethod = ( req . method || '' ) . toUpperCase ( ) ;
32
+ const reqUrl = req . url ;
33
+ const hub = getCurrentHub ( ) ;
34
+ const transaction = hub . startSpan ( {
35
+ transaction : `${ reqMethod } |${ reqUrl } ` ,
36
+ } ) ;
37
+ hub . configureScope ( scope => {
38
+ scope . setSpan ( transaction ) ;
39
+ } ) ;
40
+ res
41
+ . once ( 'response' , ( ) => transaction . setSuccess ( ) )
42
+ . once ( 'error' , ( ) => transaction . setFailure ( ) )
43
+ . once ( 'finish' , ( ) => transaction . finish ( ) ) ;
44
+ next ( ) ;
45
+ } ;
46
+ }
47
+
15
48
type TransactionTypes = 'path' | 'methodPath' | 'handler' ;
16
49
17
50
/** JSDoc */
0 commit comments