A node.js client for Etsy's StatsD server.
This client will let you fire stats at your StatsD server from a node.js application.
node-statsd Runs and is tested on Node 0.6+ on all *nix platforms and 0.8+ on all platforms including Windows.
$ npm install node-statsd
All initialization parameters are optional.
Parameters (specified as an options hash):
host: The host to send stats todefault: localhostport: The port to send stats todefault: 8125prefix: What to prefix each stat name withdefault: ''suffix: What to suffix each stat name withdefault: ''globalize: Expose this StatsD instance globally?default: falsecacheDns: Cache the initial dns lookup to hostdefault: falsemock: Create a mock StatsD instance, sending no stats to the server?default: falseglobal_tags: Optional tags that will be added to every metricdefault: []
All StatsD methods have the same API:
name: Stat namerequiredvalue: Stat valuerequired except in increment/decrement where it defaults to 1/-1 respectivelysampleRate: Sends only a sample of data to StatsDdefault: 1tags: The Array of tags to add to metricsdefault: []callback: The callback to execute once the metric has been sent
If an array is specified as the name parameter each item in that array will be sent along with the specified value.
varStatsD=require('node-statsd'),client=newStatsD();// Timing: sends a timing command with the specified millisecondsclient.timing('response_time',42);// Increment: Increments a stat by a value (default is 1)client.increment('my_counter');// Decrement: Decrements a stat by a value (default is -1)client.decrement('my_counter');// Histogram: send data for histogram statclient.histogram('my_histogram',42);// Gauge: Gauge a stat by a specified amountclient.gauge('my_gauge',123.45);// Set: Counts unique occurrences of a stat (alias of unique)client.set('my_unique','foobar');client.unique('my_unique','foobarbaz');// Incrementing multiple itemsclient.increment(['these','are','different','stats']);// Sampling, this will sample 25% of the time the StatsD Daemon will compensate for samplingclient.increment('my_counter',1,0.25);// Tags, this will add user-defined tags to the dataclient.histogram('my_histogram',42,['foo','bar']);// Using the callbackclient.set(['foo','bar'],42,function(error,bytes){//this only gets called once after all messages have been sentif(error){console.error('Oh noes! There was an error:',error);}else{console.log('Successfully sent',bytes,'bytes');}});// Sampling, tags and callback are optional and could be used in any combinationclient.histogram('my_histogram',42,0.25);// 25% Sample Rateclient.histogram('my_histogram',42,['tag']);// User-defined tagclient.histogram('my_histogram',42,next);// Callbackclient.histogram('my_histogram',42,0.25,['tag']);client.histogram('my_histogram',42,0.25,next);client.histogram('my_histogram',42,['tag'],next);client.histogram('my_histogram',42,0.25,['tag'],next);In the event that there is a socket error, node-statsd will allow this error to bubble up. If you would like to catch the errors, just attach a listener to the socket property on the instance.
client.socket.on('error',function(error){returnconsole.error("Error in socket: ",error);});If you want to catch errors in sending a message then use the callback provided.
node-statsd is licensed under the MIT license.
