Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

initial import

  • Loading branch information...
commit 453ff2e82eb25946750d0a82b1f461901b6920e5 1 parent 05f69db
Steve Ivy authored
Showing with 80 additions and 0 deletions.
  1. +80 −0 statsd.js
View
80 statsd.js
@@ -0,0 +1,80 @@
+var sys = require('sys')
+ , dgram = require('dgram')
+ , mt = require('mersenne').MersenneTwister19937;
+
+Client = function (host, port) {
+ this.host = host;
+ this.port = port;
+}
+
+Client.prototype.timing = function (stat, time, sample_rate) {
+ var self = this;
+ var stats = {};
+ stats[stat] = time+"|ms";
+ self.send(stats, sample_rate);
+};
+
+Client.prototype.increment = function (stats, sample_rate) {
+ var self = this;
+ self.update_stats(stats, 1, sample_rate);
+}
+
+Client.prototype.decrement = function (stats, sample_rate) {
+ var self = this;
+ self.update_stats(stats, -1, sample_rate);
+}
+
+Client.prototype.update_stats = function (stats, delta, sampleRate) {
+ var self = this;
+ if (typeof(stats) === 'string') {
+ stats = [stats];
+ }
+ console.log(stats);
+ if (!delta) {
+ delta=1;
+ }
+ console.log(delta);
+ data = {};
+ for (var i=0; i<stats.length; i++){
+ data[stats[i]] = delta+"|c";
+ }
+ console.log(data);
+ self.send(data, sampleRate);
+}
+
+Client.prototype.send = function (data, sample_rate) {
+ var self = this;
+ if (!sample_rate) {
+ sample_rate = 1;
+ }
+
+ var client = dgram.createSocket('udp4');
+
+ sampled_data = {};
+ if(sample_rate < 1) {
+ if (mt.genrand_real2(0,1) <= sample_rate) {
+ for (stat in data) {
+ value = data[stat];
+ sampled_data[stat] = value + "|@" + sample_rate;
+ }
+ }
+ }
+ else {
+ sampled_data=data;
+ }
+ var client = dgram.createSocket('udp4');
+ for (stat in sampled_data) {
+ send_data = stat+":"+sampled_data[stat];
+ send_data = new Buffer(send_data);
+ client.send(send_data, 0, send_data.length, self.port, self.host,
+ function (err, bytes) {
+ if (err) {
+ console.log(err.msg);
+ }
+ client.close();
+ }
+ );
+ }
+};
+
+exports.StatsD = Client;
Please sign in to comment.
Something went wrong with that request. Please try again.