hostr/lib/koa-statsd.js

37 lines
639 B
JavaScript
Raw Permalink Normal View History

2018-06-02 15:50:39 +00:00
/**
* Module dependencies.
*/
2018-06-02 18:07:00 +00:00
const Stats = require('statsy');
2018-06-02 15:50:39 +00:00
/**
* Initialize stats middleware with `opts`
* which are passed to statsy.
*
* @param {Object} [opts]
* @return {Function}
* @api public
*/
2018-06-02 18:07:00 +00:00
export default function (opts) {
const s = new Stats(opts || {});
2018-06-02 15:50:39 +00:00
return async (ctx, next) => {
// counters
s.incr('request.count');
2018-06-02 18:07:00 +00:00
s.incr(`request.${ctx.method}.count`);
2018-06-02 15:50:39 +00:00
// size
s.histogram('request.size', ctx.request.length || 0);
// remote addr
// s.set('request.addresses', this.ip);
// duration
ctx.res.on('finish', s.timer('request.duration'));
await next();
2018-06-02 18:07:00 +00:00
};
}