node-statsd
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.
Installation
$ npm install node-statsd
Usage
All initialization parameters are optional.
Parameters (specified as an options hash):
host
: The host to send stats to default: localhost
port
: The port to send stats to default: 8125
prefix
: What to prefix each stat name with default: ''
suffix
: What to suffix each stat name with default: ''
globalize
: Expose this StatsD instance globally? default: false
dnsCache
: Cache the initial dns lookup to host default: false
All StatsD methods have the same API:
name
: Stat name required
value
: Stat value required except in increment/decrement where it defaults to 1/-1 respectively
sampleRate
: Sends only a sample of data to StatsD default: 1
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.
var StatsD = require('node-statsd').StatsD,
client = new StatsD();
client.timing('response_time', 42);
client.increment('my_counter');
client.decrement('my_counter');
client.gauge('my_gauge', 123.45);
client.set('my_unique', 'foobar');
client.unique('my_unique', 'foobarbaz');
client.increment(['these', 'are', 'different', 'stats']);
client.increment('my_counter', 1, 0.25);
client.set(['foo', 'bar'], 42, null, function(error, bytes){
if(error){
console.error('Oh noes! There was an error:', error);
} else {
console.log('Successfully sent', bytes, 'bytes');
}
});
Errors
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) {
return console.error("Error in socket: ", error);
});
If you want to catch errors in sending a message then use the callback provided.
License
node-statsd is licensed under the MIT license.