node client for StatsD server with datadog extensions
Most of the code (including parts of this readme) is copied directly from the excellent https://github.com/sivy/node-statsd (v0.0.7), and the datadog parts wrote in by looking at https://github.com/joybro/node-dogstatsd, but with an added twist in the API.
IMPORTANT: This is not a drop-in replacement for node-statsd or node-dogstatsd, the API has changed.
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. (So this should work too.)
All initialization parameters are optional.
Parameters (specified as an options hash):
host: The host to send stats to
port: The port to send stats to
prefix: What to prefix each stat name with
suffix: What to suffix each stat name with
globalize: Expose this StatsD instance globally?
dnsCache: Cache the initial dns lookup to host
mock: Create a mock StatsD instance, sending no stats to the server?
All StatsD/DataDog methods have the same API:
name: Stat name
value: Stat value
required except in increment/decrement where it defaults to 1/-1 respectively
which returns a Stat Object which can sent by calling .send() on them. Additional options to the stat maybe chained by calling stat.sampleRate(value).tags(['x:1']) before sending.
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'dogstatsd-node'StatsDclient = ;// Timing: sends a timing command with the specified millisecondsclienttiming'response_time' 42send;// Increment: Increments a stat by a value (default is 1)clientincrement'my_counter'send;// Decrement: Decrements a stat by a value (default is -1)clientdecrement'my_counter'send;// Gauge: Gauge a stat by a specified amountclientgauge'my_gauge' 123.45send;// Histogram: Datadog's histogramclienthistogram'histogram' 10send;// Set: Counts unique occurrences of a stat (alias of unique)clientset'my_unique' 'foobar'send;clientunique'my_unique' 'foobarbaz'send;// Incrementing multiple itemsclientincrement'these' 'are' 'different' 'stats'send;// Sampling, this will sample 25% of the time the StatsD Daemon will compensate for samplingclientincrement'my_counter' 1sampleRate0.25send;// Datadog's Tagsclientincrement'my_counter' 1tags'host:xyz'send;// Using the callbackclientset'foo' 'bar' 42send//this only gets called once after all messages have been sentiferrorconsole.error'Oh noes! There was an error:' error;elseconsole.log'Successfully sent' bytes 'bytes';;
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.
clientsocketon'error'return console.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. So is this.