Skip to content

Commit ac363db

Browse files
authored
Log slow requests to sentry (compiler-explorer#1813)
1 parent 4d9e248 commit ac363db

File tree

4 files changed

+27
-2
lines changed

4 files changed

+27
-2
lines changed

app.js

+15-1
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ const nopt = require('nopt'),
4040
urljoin = require('url-join'),
4141
_ = require('underscore'),
4242
express = require('express'),
43+
responseTime = require('response-time'),
4344
Sentry = require('@sentry/node'),
4445
{logger, logToPapertrail, suppressConsoleLog} = require('./lib/logger'),
4546
utils = require('./lib/utils'),
@@ -456,16 +457,30 @@ aws.initConfig(awsProps)
456457
rescanCompilerSecs * 1000);
457458
}
458459

460+
const sentrySlowRequestMs = ceProps("sentrySlowRequestMs", 0);
461+
459462
webServer
460463
.set('trust proxy', true)
461464
.set('view engine', 'pug')
462465
.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+
}))
463477
// Handle healthchecks at the root, as they're not expected from the outside world
464478
.use('/healthcheck', new healthCheck.HealthCheckHandler(healthCheckFilePath).handle)
465479
.use(httpRoot, router)
466480
.use((req, res, next) => {
467481
next({status: 404, message: `page "${req.path}" could not be found`});
468482
})
483+
// sentry error handler must be the first error handling middleware
469484
.use(Sentry.Handlers.errorHandler)
470485
// eslint-disable-next-line no-unused-vars
471486
.use((err, req, res) => {
@@ -563,7 +578,6 @@ aws.initConfig(awsProps)
563578
});
564579

565580
router
566-
.use(Sentry.Handlers.requestHandler())
567581
.use(morgan(morganFormat, {
568582
stream: logger.stream,
569583
// Skip for non errors (2xx, 3xx)

etc/config/compiler-explorer.amazon.properties

+2-1
Original file line numberDiff line numberDiff line change
@@ -28,4 +28,5 @@ eventLoopLagThresholdWarn=100
2828
eventLoopLagThresholdErr=1000
2929

3030
staticUrl=https://static.ce-cdn.net/
31-
sentryEnvironment=prod
31+
sentryEnvironment=prod
32+
sentrySlowRequestMs=20000

package-lock.json

+9
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@
5353
"promise-queue": "2.2.3",
5454
"pug": "^2.0.4",
5555
"request": "^2.88.0",
56+
"response-time": "^2.3.2",
5657
"selectize": "0.12.4",
5758
"semver": "^5.7.1",
5859
"serve-favicon": "^2.4.5",

0 commit comments

Comments
 (0)