A generic stat tracker for NodeJS that can support many instances and different reporting backends.
Tested for NodeJS: 4+
vardummy_backend={counts: {},gauges: {},timings: {},count: function(key,num){this.counts[key]+=num;},gauge: function(key,amount){this.gauges[key]=amount;},timings: function(key,time){if(!this.timings[key]){this.timings[key]=[]}this.timings[key].push(time);}}vartracker=newStatTracker.Tracker({});functiontrack_this(){varprofiler=tracker.profiler('profile_scope');
... Somecomputation ...
tracker.count('step1_num_runs',1,["sometag"]);profiler.tick('first_step');
... Somecomputation ...
tracker.count('step2_num_runs',1);profiler.end();}A backend is a JavaScript prototype that implements the following:
functionBackend(config){}Backend.prototype.count=function(metric,value,tags){};Backend.prototype.time=function(metric,value,tags){};Backend.prototype.gauge=function(metric,value,tags){};module.exports=Backend;The initialization config is provided by StatTracker backend_config property.
Node Stat Tracker comes with two backends:
statsd_backend: Usesnode-statsdfor sending StatsD packets.logger_backend: Logs the values usingbunyanor config provided logger.