1
0
mirror of synced 2025-12-25 02:17:36 -05:00

Merge branch 'main' into openapi-update-e3d37e33f5d6f3db5d80d5d602d7772843d043867c57c3e0ed3dd0af80aeec77

This commit is contained in:
Shati Patel
2021-01-28 09:35:26 +00:00
committed by GitHub
11 changed files with 152 additions and 39 deletions

View File

@@ -0,0 +1,19 @@
const path = require('path')
const statsd = require('./statsd')
module.exports = function instrumentMiddleware (relativePath) {
// Requires the file as if it were being required from '../middleware/index.js'.
// This is a little wonky, but let's us write `app.use(instrument(path))` and
// maintain the name of the file, instead of hard-coding it for each middleware.
const middleware = require(path.resolve(__dirname, '../middleware', relativePath))
// Check if the middleware is an async function, to use the appropriate timer
const isAsyncFunction = middleware.constructor.name === 'AsyncFunction'
// Add a tag so we can see all middleware together
const tags = { middleware: path.basename(relativePath) }
return isAsyncFunction
? statsd.asyncTimer(middleware, 'middleware', tags)
: statsd.timer(middleware, 'middleware', tags)
}