Created
April 30, 2012 14:18
-
-
Save mbrevoort/2558714 to your computer and use it in GitHub Desktop.
Simple Counter/Timer Statsd Node Client
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var dgram = require('dgram'); | |
module.exports = function (port, host) { | |
return new function() { | |
this._host = host || '127.0.0.1'; | |
this._port = port || 8125; | |
this._client = dgram.createSocket("udp4"); | |
this._send = function (message) { | |
console.log('STATSD', message); | |
var buffer = new Buffer(message); | |
this._client.send (buffer, 0, buffer.length, this._port, this._host); | |
} | |
this.counter = function (name, value) { | |
value = value || 1; | |
var message = this._counterMessage(name, value); | |
this._send (message); | |
}; | |
this.timer = function (name, duration) { | |
var self = this; | |
// if duration is not passed in, return an object that | |
// has an end function that can be called. | |
if (!duration) { | |
var start = Date.now(); | |
return new function () { | |
this.end = function () { | |
var duration = Date.now() - start; | |
var message = self._timerMessage(name, duration); | |
self._send(message); | |
} | |
} | |
} | |
else { | |
var message = self._timerMessage(name, duration); | |
self._send(message); | |
} | |
}; | |
this._timerMessage = function (name, duration) { | |
return name + ':' + duration + '|ms'; | |
}; | |
this._counterMessage = function (name, value) { | |
return name + ':' + value + '|c'; | |
}; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment