Permalink
Browse files

Merge branch 'master' of github.com:sivy/node-statsd

  • Loading branch information...
2 parents c424315 + 429e0d5 commit e7168189cc52912308f3abe0513149c1788be349 Steve Ivy committed Aug 9, 2012
View
@@ -6,7 +6,7 @@ This client will let you fire stats at your StatsD server from a node.js applica
% npm install node-statsd
% node
- > require('statsd').StatsD
+ > var StatsD = require('node-statsd').StatsD
> c = new StatsD('example.org',8125)
{ host: 'example.org', port: 8125 }
> c.increment('node_test.int')
@@ -17,3 +17,15 @@ This client will let you fire stats at your StatsD server from a node.js applica
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)
+
+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);
+ });
View
@@ -1,11 +1,10 @@
-var sys = require('sys')
- , socket = require('dgram').createSocket('udp4')
- , mersenne = require('mersenne')
- , mt = new mersenne.MersenneTwister19937();
+mersenne = require('mersenne');
+mt = new mersenne.MersenneTwister19937();
Client = function (host, port) {
this.host = host;
this.port = port;
+ this.socket = require('dgram').createSocket('udp4');
}
Client.prototype.timing = function (stat, time, sample_rate) {
@@ -25,6 +24,13 @@ Client.prototype.decrement = function (stats, sample_rate) {
self.update_stats(stats, -1, sample_rate);
}
+Client.prototype.gauge = function (stat, value, sample_rate) {
+ var self = this;
+ var stats = {};
+ stats[stat] = value+"|g";
+ self.send(stats, sample_rate);
+}
+
Client.prototype.update_stats = function (stats, delta, sampleRate) {
var self = this;
if (typeof(stats) === 'string') {
@@ -33,7 +39,7 @@ Client.prototype.update_stats = function (stats, delta, sampleRate) {
if (!delta) {
delta=1;
}
- data = {};
+ var data = {};
for (var i=0; i<stats.length; i++){
data[stats[i]] = delta+"|c";
}
@@ -46,7 +52,7 @@ Client.prototype.send = function (data, sample_rate) {
sample_rate = 1;
}
- sampled_data = {};
+ var sampled_data = {};
if(sample_rate < 1) {
if (mt.genrand_real2(0,1) <= sample_rate) {
for (stat in data) {
@@ -58,16 +64,10 @@ Client.prototype.send = function (data, sample_rate) {
else {
sampled_data=data;
}
- for (stat in sampled_data) {
- send_data = stat+":"+sampled_data[stat];
+ for (var stat in sampled_data) {
+ var send_data = stat+":"+sampled_data[stat];
send_data = new Buffer(send_data);
- socket.send(send_data, 0, send_data.length, self.port, self.host,
- function (err, bytes) {
- if (err) {
- console.log(err.msg);
- }
- }
- );
+ this.socket.send(send_data, 0, send_data.length, self.port, self.host);
}
};

Some generated files are not rendered by default. Learn more.

Oops, something went wrong.

Some generated files are not rendered by default. Learn more.

Oops, something went wrong.
Oops, something went wrong.

0 comments on commit e716818

Please sign in to comment.