* The module includes the following plugins to be used with restify's `after` * event, e.g., `server.on('after', restify.plugins.metrics());`: * * A plugin that listens to the server's after event and emits information * about that request. * * @public * @function metrics * @param {Object} o
(opts, callback)
| 46 | * })); |
| 47 | */ |
| 48 | function createMetrics(opts, callback) { |
| 49 | assert.object(opts, 'opts'); |
| 50 | assert.object(opts.server, 'opts.server'); |
| 51 | assert.func(callback, 'callback'); |
| 52 | |
| 53 | return function metrics(req, res, route, err) { |
| 54 | var data = { |
| 55 | // response status code. in most cases this should be a proper |
| 56 | // http status code, but in the case of an uncaughtException it can |
| 57 | // be undefined. otherwise, in most normal scenarios, even calling |
| 58 | // res.send() or res.end() should result in a 200 by default. |
| 59 | statusCode: res.statusCode, |
| 60 | // REST verb |
| 61 | method: req.method, |
| 62 | // overall request latency |
| 63 | totalLatency: hrTimeDurationInMs(req._timeStart, req._timeFinished), |
| 64 | latency: hrTimeDurationInMs(req._timeStart, req._timeFlushed), |
| 65 | preLatency: hrTimeDurationInMs(req._timePreStart, req._timePreEnd), |
| 66 | useLatency: hrTimeDurationInMs(req._timeUseStart, req._timeUseEnd), |
| 67 | routeLatency: hrTimeDurationInMs( |
| 68 | req._timeRouteStart, |
| 69 | req._timeRouteEnd |
| 70 | ), |
| 71 | // the cleaned up url path |
| 72 | // e.g., /foo?a=1 => /foo |
| 73 | path: req.path(), |
| 74 | // connection state can currently only have the following values: |
| 75 | // 'close' | undefined. |
| 76 | // |
| 77 | // if the connection state is 'close' |
| 78 | // the status code will be set to 444 |
| 79 | // it is possible to get a 200 statusCode with a connectionState |
| 80 | // value of 'close'. i.e., the client timed out, |
| 81 | // but restify thinks it "sent" a response. connectionState should |
| 82 | // always be the primary source of truth here, and check it first |
| 83 | // before consuming statusCode. otherwise, it may result in skewed |
| 84 | // metrics. |
| 85 | connectionState: req.connectionState && req.connectionState(), |
| 86 | unfinishedRequests: |
| 87 | opts.server.inflightRequests && opts.server.inflightRequests(), |
| 88 | inflightRequests: |
| 89 | opts.server.inflightRequests && opts.server.inflightRequests() |
| 90 | }; |
| 91 | |
| 92 | return callback(err, data, req, res, route); |
| 93 | }; |
| 94 | } |
| 95 | |
| 96 | /** |
| 97 | * Callback used by metrics plugin |
nothing calls this directly
no test coverage detected