statsd/backends/console.js

53 строки
1.4 KiB
JavaScript

/*jshint node:true, laxcomma:true */
var util = require('util');
function ConsoleBackend(startupTime, config, emitter){
var self = this;
this.lastFlush = startupTime;
this.lastException = startupTime;
this.config = config.console || {};
// attach
emitter.on('flush', function(timestamp, metrics) { self.flush(timestamp, metrics); });
emitter.on('status', function(callback) { self.status(callback); });
}
ConsoleBackend.prototype.flush = function(timestamp, metrics) {
console.log('Flushing stats at ', new Date(timestamp * 1000).toString());
var out = {
counters: metrics.counters,
timers: metrics.timers,
gauges: metrics.gauges,
timer_data: metrics.timer_data,
counter_rates: metrics.counter_rates,
sets: function (vals) {
var ret = {};
for (var val in vals) {
ret[val] = vals[val].values();
}
return ret;
}(metrics.sets),
pctThreshold: metrics.pctThreshold
};
if(this.config.prettyprint) {
console.log(util.inspect(out, false, 5, true));
} else {
console.log(out);
}
};
ConsoleBackend.prototype.status = function(write) {
['lastFlush', 'lastException'].forEach(function(key) {
write(null, 'console', key, this[key]);
}, this);
};
exports.init = function(startupTime, config, events) {
var instance = new ConsoleBackend(startupTime, config, events);
return true;
};