@@ -11,7 +11,7 @@ import * as os from 'os';
1111import * as url from 'url' ;
1212
1313import { NodeClient } from './client' ;
14- import { flush } from './sdk' ;
14+ import { flush , withAutosessionTracking } from './sdk' ;
1515
1616const DEFAULT_SHUTDOWN_TIMEOUT = 2000 ;
1717
@@ -375,35 +375,38 @@ export type RequestHandlerOptions = ParseRequestOptions & {
375375export function requestHandler (
376376 options ?: RequestHandlerOptions ,
377377) : ( req : http . IncomingMessage , res : http . ServerResponse , next : ( error ?: any ) => void ) => void {
378- return function sentryRequestMiddleware (
379- req : http . IncomingMessage ,
380- res : http . ServerResponse ,
381- next : ( error ?: any ) => void ,
382- ) : void {
383- if ( options && options . flushTimeout && options . flushTimeout > 0 ) {
384- // eslint-disable-next-line @typescript-eslint/unbound-method
385- const _end = res . end ;
386- res . end = function ( chunk ?: any | ( ( ) => void ) , encoding ?: string | ( ( ) => void ) , cb ?: ( ) => void ) : void {
387- flush ( options . flushTimeout )
388- . then ( ( ) => {
389- _end . call ( this , chunk , encoding , cb ) ;
390- } )
391- . then ( null , e => {
392- logger . error ( e ) ;
393- } ) ;
394- } ;
395- }
396- const local = domain . create ( ) ;
397- local . add ( req ) ;
398- local . add ( res ) ;
399- local . on ( 'error' , next ) ;
400- local . run ( ( ) => {
401- getCurrentHub ( ) . configureScope ( scope =>
402- scope . addEventProcessor ( ( event : Event ) => parseRequest ( event , req , options ) ) ,
403- ) ;
404- next ( ) ;
405- } ) ;
406- } ;
378+ return withAutosessionTracking (
379+ function sentryRequestMiddleware (
380+ req : http . IncomingMessage ,
381+ res : http . ServerResponse ,
382+ next : ( error ?: any ) => void ,
383+ ) : void {
384+ if ( options && options . flushTimeout && options . flushTimeout > 0 ) {
385+ // eslint-disable-next-line @typescript-eslint/unbound-method
386+ const _end = res . end ;
387+ res . end = function ( chunk ?: any | ( ( ) => void ) , encoding ?: string | ( ( ) => void ) , cb ?: ( ) => void ) : void {
388+ flush ( options . flushTimeout )
389+ . then ( ( ) => {
390+ _end . call ( this , chunk , encoding , cb ) ;
391+ } )
392+ . then ( null , e => {
393+ logger . error ( e ) ;
394+ } ) ;
395+ } ;
396+ }
397+ const local = domain . create ( ) ;
398+ local . add ( req ) ;
399+ local . add ( res ) ;
400+ local . on ( 'error' , next ) ;
401+ local . run ( ( ) => {
402+ getCurrentHub ( ) . configureScope ( scope =>
403+ scope . addEventProcessor ( ( event : Event ) => parseRequest ( event , req , options ) ) ,
404+ ) ;
405+ next ( ) ;
406+ } ) ;
407+ } ,
408+ { sessionMode : 'request' } ,
409+ ) ;
407410}
408411
409412/** JSDoc */
0 commit comments