mof-statsdclient
Version:
middleware of floodesh, which provides a statsd client that allows you to send data to statsd server and automatically has some statistics sent that generally apply to all requests.
29 lines (25 loc) • 1.7 kB
JavaScript
const StatsdClient = require('./lib/StatsdClient');
const os = require('os');
module.exports.middleware = function(statsdClient, sampleRate) {
return function(ctx, next) {
if(statsdClient && (statsdClient instanceof StatsdClient)) {
let appname = ctx.app.name;
let hostname = os.hostname();
// let pid = process.pid;
let memUsage = process.memoryUsage();
statsdClient.gauge([appname,hostname,'mem_rss'].join('.'), memUsage.rss || 0);
statsdClient.gauge([appname,hostname,'mem_heapTotal'].join('.'), memUsage.heapTotal || 0);
statsdClient.gauge([appname,hostname,'mem_heapUsed'].join('.'), memUsage.heapUsed || 0);
statsdClient.count([appname,hostname,'request_count'].join('.'), '1', sampleRate);
statsdClient.timer([appname,hostname,'queue_time'].join('.'), ctx.performance.bottleneckTimestamp-ctx.performance.enqueueTimestamp, sampleRate);
statsdClient.timer([appname,hostname,'requestmw_time'].join('.'), ctx.performance.requestTimestamp-ctx.performance.bottleneckTimestamp, sampleRate);
statsdClient.timer([appname,hostname,'network_time'].join('.'), ctx.performance.responseTimestamp-ctx.performance.requestTimestamp, sampleRate);
statsdClient.timer([appname,hostname,'reponsemw_time'].join('.'), ctx.performance.responsemwTimestamp-ctx.performance.responseTimestamp, sampleRate);
statsdClient.timer([appname,hostname,'parse_time'].join('.'), ctx.performance.parsedTimestamp-ctx.performance.responsemwTimestamp, sampleRate);
statsdClient.timer([appname,hostname,'total_time'].join('.'), ctx.performance.parsedTimestamp-ctx.performance.bottleneckTimestamp, sampleRate);
}
return next();
}
}
module.exports.client = StatsdClient;