Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 25 additions & 24 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,40 +2,41 @@

A node.js client for extended StatsD server of [Datadog](http://www.datadoghq.com).

Datadog added new some features(histogram and tags) to their own StatsD implementation.
This client is an extension of general StatsD client to work with that server.
Datadog added new some features(histogram and tags) to their own StatsD implementation. This client is an extension of general StatsD client to work with that server.

Most parts of codes came from [Steve Ivy](https://github.com/sivy)'s [node-statsd](https://github.com/sivy/node-statsd).
I just added few lines to support datadog's histogram and tags features.
Most parts of codes came from [Steve Ivy](https://github.com/sivy)'s [node-statsd](https://github.com/sivy/node-statsd). I just added few lines to support datadog's histogram and tags features.

The name of the package is changed because this isn't really statsd client and should be able to be used with original statsd client.

% npm install node-dogstatsd
% node
> var StatsD = require('node-dogstatsd').StatsD
> c = new StatsD('example.org',8125)
{ host: 'example.org', port: 8125 }
> c.increment('node_test.int')
> c.incrementBy('node_test.int', 7)
> c.decrement('node_test.int')
> c.decrementBy('node_test.int', 12)
> c.timing('node_test.some_service.task.time', 500) // time in millis
> c.histogram('node_test.some_service.data', 100) // works only with datadog' StatsD
> c.increment('node_test.int', 1, ['tag:one']) // works only with datadog' StatsD
```
% npm install node-dogstatsd
% node
> var StatsD = require('node-dogstatsd').StatsD
> c = new StatsD({host:'example.org',port:8125})
{ host: 'example.org', port: 8125 }
> c.increment('node_test.int')
> c.incrementBy('node_test.int', 7)
> c.decrement('node_test.int')
> c.decrementBy('node_test.int', 12)
> c.timing('node_test.some_service.task.time', 500) // time in millis
> c.histogram('node_test.some_service.data', 100) // works only with datadog' StatsD
> c.increment('node_test.int', 1, ['tag:one']) // works only with datadog' StatsD
```

## License

node-statsd is licensed under the MIT license.

## Error handling policy

* exceptions "bubble up" into the app that uses this library
* we don't log or print to console any errors ourself, it's the toplevel app that decides how to log/write to console.
* we document which exceptions can be raised, and where. (TODO, https://github.com/sivy/node-statsd/issues/17)
- exceptions "bubble up" into the app that uses this library
- we don't log or print to console any errors ourself, it's the toplevel app that decides how to log/write to console.
- we document which exceptions can be raised, and where. (TODO, <https://github.com/sivy/node-statsd/issues/17>)

in your main app, you can leverage the fact that you have access to c.socket and do something like:
(this is the best way I've found so far)
in your main app, you can leverage the fact that you have access to c.socket and do something like: (this is the best way I've found so far)

c.socket.on('error', function (exception) {
return console.log ("error event in socket.send(): " + exception);
});
```
c.socket.on('error', function (exception) {
return console.log ("error event in socket.send(): " + exception);
});
```
44 changes: 22 additions & 22 deletions lib/statsd.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,22 @@ var mt = new mersenne.MersenneTwister19937();

var EPHEMERAL_LIFETIME_MS = 1000;

var Client = function(host, port, socket, options) {
this.host = host || "localhost";
this.port = port || 8125;
var Client = function(params) {
params = !!params ? params : {};
this.host = params.host || "localhost";
this.port = params.port || 8125;
this.errorHandler = params.errorHandler || function() {};

// optional shared socket
this.socket = socket;
this.socket = params.socket;

// when a *shared* socked isn't provided, an ephemeral
// socket is demand allocated. This ephemeral socket is closed
// after being idle for EPHEMERAL_LIFETIME_MS.
this.ephemeral_socket = this.last_used_timer = null;

options = options || {};
this.global_tags = options.global_tags;
params.options = params.options || {};
this.global_tags = params.options.global_tags;
};

Client.prototype.timing = function(stat, time, sample_rate, tags) {
Expand All @@ -42,7 +44,7 @@ Client.prototype.incrementBy = function(stats, value, tags) {

Client.prototype.decrement = function(stats, sample_rate, tags) {
var self = this;
self.update_stats(stats, - 1, sample_rate, tags);
self.update_stats(stats, -1, sample_rate, tags);
};

Client.prototype.decrementBy = function(stats, value, tags) {
Expand Down Expand Up @@ -89,7 +91,7 @@ Client.prototype.update_stats = function(stats, delta, sampleRate, tags) {
// used. This function is called when the socket is used
// and causes demand allocated ephemeral sockets to be closed
// after a period of inactivity.
Client.prototype._update_last_used = function () {
Client.prototype._update_last_used = function() {
if (!this.ephemeral_socket)
return;

Expand All @@ -104,17 +106,16 @@ Client.prototype._update_last_used = function () {
}, EPHEMERAL_LIFETIME_MS);
};

Client.prototype.send_data = function (buf) {
Client.prototype.send_data = function(buf) {
var socket;

if (!this.socket) {
if (!this.ephemeral_socket) {
this.ephemeral_socket = dgram.createSocket("udp4");
this.ephemeral_socket.on("error", function() {});
this.ephemeral_socket.on("error", this.errorHandler);
}
socket = this.ephemeral_socket;
}
else {
} else {
socket = this.socket;
}

Expand All @@ -141,24 +142,23 @@ Client.prototype.send = function(data, sample_rate, tags) {
sampled_data[stat] = value + "|@" + sample_rate;
}
}
}
else
} else
sampled_data = data;

if (this.global_tags || tags) {
var merged_tags = [];

if (Array.isArray(this.global_tags))
merged_tags = merged_tags.concat(this.global_tags);

merged_tags = merged_tags.concat(this.global_tags);

if (Array.isArray(tags))
merged_tags = merged_tags.concat(tags);
merged_tags = merged_tags.concat(tags);

if (merged_tags.length > 0) {
var merged_tags_str = merged_tags.join(',');
for (stat in sampled_data)
sampled_data[stat] = sampled_data[stat] + "|#" + merged_tags_str;
var merged_tags_str = merged_tags.join(',');
for (stat in sampled_data)
sampled_data[stat] = sampled_data[stat] + "|#" +
merged_tags_str;
}
}

Expand All @@ -177,8 +177,8 @@ Client.prototype.close = function() {
clearTimeout(this.last_used_timer);

this.ephemeral_socket =
this.last_used_timer =
this.socket = null;
this.last_used_timer =
this.socket = null;
};

exports.StatsD = Client;
38 changes: 25 additions & 13 deletions test/statsd_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,12 @@ describe('StatsD', function() {
beforeEach(function(done) {
fakeStatsDServerSocket = dgram.createSocket('udp4');
fakeStatsDServerSocket.bind(function() {
client = new StatsD('localhost', fakeStatsDServerSocket.address().port);
client = new StatsD({
host: 'localhost',
port: fakeStatsDServerSocket
.address()
.port
});
done();
});
});
Expand All @@ -24,26 +29,33 @@ describe('StatsD', function() {
fakeStatsDServerSocket.close();
});

// Wraps up listening for the next message on our fake server and
// verifying that it's contents match those expected. Calls back
// once this check has been performed.
// Wraps up listening for the next message on our fake server and verifying
// that it's contents match those expected. Calls back once this check has been
// performed.
var serverShouldReceive = function(expected, cb) {
fakeStatsDServerSocket.once('message', function(msg) {
try {
msg.toString('utf8').should.eql(expected);
} catch (e) {
return cb(e);
}
cb();
});
fakeStatsDServerSocket
.once('message', function(msg) {
try {
msg
.toString('utf8')
.should
.eql(expected);
} catch (e) {
return cb(e);
}
cb();
});
};

var serverShouldReceiveMultiple = function(expected, n, cb) {
var remaining = n;
fakeStatsDServerSocket.on('message', function(msg) {
remaining--;
try {
msg.toString('utf8').should.eql(expected);
msg
.toString('utf8')
.should
.eql(expected);
} catch (e) {
return cb(e);
}
Expand Down