@@ -40,6 +40,7 @@ const nopt = require('nopt'),
40
40
urljoin = require ( 'url-join' ) ,
41
41
_ = require ( 'underscore' ) ,
42
42
express = require ( 'express' ) ,
43
+ responseTime = require ( 'response-time' ) ,
43
44
Sentry = require ( '@sentry/node' ) ,
44
45
{ logger, logToPapertrail, suppressConsoleLog} = require ( './lib/logger' ) ,
45
46
utils = require ( './lib/utils' ) ,
@@ -456,16 +457,30 @@ aws.initConfig(awsProps)
456
457
rescanCompilerSecs * 1000 ) ;
457
458
}
458
459
460
+ const sentrySlowRequestMs = ceProps ( "sentrySlowRequestMs" , 0 ) ;
461
+
459
462
webServer
460
463
. set ( 'trust proxy' , true )
461
464
. set ( 'view engine' , 'pug' )
462
465
. on ( 'error' , err => logger . error ( 'Caught error in web handler; continuing:' , err ) )
466
+ // sentry request handler must be the first middleware on the app
467
+ . use ( Sentry . Handlers . requestHandler ( ) )
468
+ // eslint-disable-next-line no-unused-vars
469
+ . use ( responseTime ( ( req , res , time ) => {
470
+ if ( sentrySlowRequestMs > 0 && time >= sentrySlowRequestMs ) {
471
+ Sentry . withScope ( scope => {
472
+ scope . setExtra ( 'duration_ms' , time ) ;
473
+ Sentry . captureMessage ( 'SlowRequest' , 'warning' ) ;
474
+ } ) ;
475
+ }
476
+ } ) )
463
477
// Handle healthchecks at the root, as they're not expected from the outside world
464
478
. use ( '/healthcheck' , new healthCheck . HealthCheckHandler ( healthCheckFilePath ) . handle )
465
479
. use ( httpRoot , router )
466
480
. use ( ( req , res , next ) => {
467
481
next ( { status : 404 , message : `page "${ req . path } " could not be found` } ) ;
468
482
} )
483
+ // sentry error handler must be the first error handling middleware
469
484
. use ( Sentry . Handlers . errorHandler )
470
485
// eslint-disable-next-line no-unused-vars
471
486
. use ( ( err , req , res ) => {
@@ -563,7 +578,6 @@ aws.initConfig(awsProps)
563
578
} ) ;
564
579
565
580
router
566
- . use ( Sentry . Handlers . requestHandler ( ) )
567
581
. use ( morgan ( morganFormat , {
568
582
stream : logger . stream ,
569
583
// Skip for non errors (2xx, 3xx)
0 commit comments