Skip to content

Commit 79f3641

Browse files
author
Peter Bengtsson
authored
statsd increments on middleware.abort and middleware.timeout (#23003)
* statsd increments on middleware.abort and middleware.timeout * tags * for the abort increment too * for the abort increment too (2)
1 parent 7079099 commit 79f3641

2 files changed

Lines changed: 28 additions & 0 deletions

File tree

middleware/abort.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import statsd from '../lib/statsd.js'
2+
13
export default function abort(req, res, next) {
24
// If the client aborts the connection, send an error
35
req.once('aborted', () => {
@@ -7,6 +9,18 @@ export default function abort(req, res, next) {
79
}
810
// NOTE: Node.js will also automatically set `req.aborted = true`
911

12+
const incrementTags = []
13+
// Be careful with depending on attributes set on the `req` because
14+
// under certain conditions the contextualizers might not yet have
15+
// had a chance to run.
16+
if (req.pagePath) {
17+
incrementTags.push(`path:${req.pagePath}`)
18+
}
19+
if (req.context?.currentCategory) {
20+
incrementTags.push(`product:${req.context.currentCategory}`)
21+
}
22+
statsd.increment('middleware.abort', 1, incrementTags)
23+
1024
const abortError = new Error('Client closed request')
1125
abortError.statusCode = 499
1226
abortError.code = 'ECONNRESET'

middleware/timeout.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import timeout from 'express-timeout-handler'
22

3+
import statsd from '../lib/statsd.js'
4+
35
// Heroku router requests timeout after 30 seconds. We should stop them earlier!
46
const maxRequestTimeout = parseInt(process.env.REQUEST_TIMEOUT, 10) || 10000
57

@@ -14,6 +16,18 @@ export default timeout.handler({
1416
disable: [],
1517

1618
onTimeout: function (req, res, next) {
19+
const incrementTags = []
20+
// Be careful with depending on attributes set on the `req` because
21+
// under certain conditions the contextualizers might not yet have
22+
// had a chance to run.
23+
if (req.pagePath) {
24+
incrementTags.push(`path:${req.pagePath}`)
25+
}
26+
if (req.context?.currentCategory) {
27+
incrementTags.push(`product:${req.context.currentCategory}`)
28+
}
29+
statsd.increment('middleware.timeout', 1, incrementTags)
30+
1731
// Create a custom timeout error
1832
const timeoutError = new Error('Request timed out')
1933
timeoutError.statusCode = 503

0 commit comments

Comments
 (0)