forked from github/docs
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Instrument all of our middleware (github#17527)
* Add instrument-middleware.js * Make it do some fancy require-ing * Use it * Prefix names * dot prefix * Improve async detection * Add some comments * Can't instrument error handler
- Loading branch information
Showing
2 changed files
with
50 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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' | ||
|
||
// Name it `middleware.<filename>` | ||
const name = `middleware.${path.basename(relativePath)}` | ||
|
||
return isAsyncFunction | ||
? statsd.asyncTimer(middleware, name) | ||
: statsd.timer(middleware, name) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters