Comparing version 0.0.3 to 0.0.5
156
lib/lynx.js
var dgram = require('dgram') | ||
, Stream = require('stream').Stream | ||
, util = require('util') | ||
, parser = require('statsd-parser') | ||
// | ||
@@ -8,3 +11,3 @@ // `Math.random` doesn't cut it, based on tests from sampling.js | ||
, mt = new mersenne.MersenneTwister19937() | ||
, noop = function () {} | ||
, noop = function noop() {} | ||
; | ||
@@ -34,2 +37,4 @@ | ||
function Lynx(host, port, options) { | ||
var self = this; | ||
// | ||
@@ -55,16 +60,2 @@ // Server hostname and port | ||
// | ||
// ### function default_error_handler() | ||
// #### @err {Object} The error object. Includes: | ||
// err.message, err.* | ||
// | ||
// Function that defines what to do on error. | ||
// Errors are soft errors, and while interesting they are mostly informative | ||
// A simple console log would do but that doesn't allow people to do | ||
// custom stuff with errors | ||
// | ||
function default_error_handler (err) { | ||
console.log(err.message); | ||
} | ||
// | ||
// Set out error handling code | ||
@@ -74,6 +65,29 @@ // | ||
? options.onError | ||
: default_error_handler | ||
: this._default_error_handler | ||
; | ||
// | ||
// Stream properties | ||
// | ||
this.readable = true; | ||
this.writable = true; | ||
this.parser = parser.createStream(); | ||
this.parser.on('error', this.onError); | ||
this.parser.on('stat', function (text, statObj) { | ||
var stat = {}; | ||
stat[statObj.stat] = statObj.value + '|' + statObj.type; | ||
if(statObj.sample_rate) { | ||
stat[statObj.stat] += '@' + statObj.sample_rate; | ||
self.send(stat, parseFloat(statObj.sample_rate)); | ||
} | ||
else { | ||
self.send(stat); | ||
} | ||
}); | ||
} | ||
util.inherits(Lynx, Stream); | ||
// | ||
@@ -97,3 +111,3 @@ // ### constructor Timer(stat, sample_rate) | ||
// | ||
Lynx.prototype.Timer = function (stat, sample_rate) { | ||
Lynx.prototype.createTimer = function createTimer(stat, sample_rate) { | ||
var self = this | ||
@@ -142,3 +156,3 @@ , start_time = new Date ().getTime() | ||
// | ||
Lynx.prototype.increment = function (stats, sample_rate) { | ||
Lynx.prototype.increment = function increment(stats, sample_rate) { | ||
this.count(stats, 1, sample_rate); | ||
@@ -159,3 +173,3 @@ }; | ||
// | ||
Lynx.prototype.decrement = function (stats, sample_rate) { | ||
Lynx.prototype.decrement = function decrement(stats, sample_rate) { | ||
this.count(stats, -1, sample_rate); | ||
@@ -178,3 +192,3 @@ }; | ||
// | ||
Lynx.prototype.count = function (stats, delta, sample_rate) { | ||
Lynx.prototype.count = function count(stats, delta, sample_rate) { | ||
// | ||
@@ -243,3 +257,3 @@ // If we are given a string stat (key) then transform it into array | ||
// | ||
Lynx.prototype.timing = function (stat, duration, sample_rate) { | ||
Lynx.prototype.timing = function timing(stat, duration, sample_rate) { | ||
var stats = {}; | ||
@@ -262,3 +276,3 @@ stats[stat] = duration + "|ms"; | ||
// | ||
Lynx.prototype.set = function (stat, value, sample_rate) { | ||
Lynx.prototype.set = function set(stat, value, sample_rate) { | ||
var stats = {}; | ||
@@ -281,3 +295,3 @@ stats[stat] = value + "|s"; | ||
// | ||
Lynx.prototype.gauge = function (stat, value, sample_rate) { | ||
Lynx.prototype.gauge = function gauge(stat, value, sample_rate) { | ||
var stats = {}; | ||
@@ -306,3 +320,3 @@ stats[stat] = value + "|g"; | ||
// | ||
Lynx.prototype.send = function (stats, sample_rate) { | ||
Lynx.prototype.send = function send(stats, sample_rate) { | ||
var self = this | ||
@@ -337,3 +351,3 @@ , sampled_stats = Lynx.sample(stats, sample_rate) | ||
// | ||
send_data = all_stats.map(function (stat) { | ||
send_data = all_stats.map(function construct_stat(stat) { | ||
return stat + ":" + sampled_stats[stat]; | ||
@@ -348,3 +362,2 @@ }).join("\n"); | ||
; | ||
// | ||
@@ -362,2 +375,14 @@ // Do we already have a socket object we can use? | ||
this.ephemeral_socket = dgram.createSocket('udp4'); | ||
// | ||
// Register on error: Failed sending the buffer | ||
// | ||
this.ephemeral_socket.on('error', function (err) { | ||
err.reason = err.message; | ||
err.f = "send"; | ||
err.message = "Failed sending the buffer"; | ||
err.args = arguments; | ||
self.onError(err); | ||
return; | ||
}); | ||
} | ||
@@ -382,15 +407,4 @@ | ||
// | ||
this.emit('data', buffer); | ||
socket.send(buffer, 0, buffer.length, this.port, this.host, noop); | ||
// | ||
// Error: Failed sending the buffer | ||
// | ||
socket.on('error', function (err) { | ||
err.reason = err.message; | ||
err.f = "send"; | ||
err.message = "Failed sending the buffer"; | ||
err.args = arguments; | ||
self.onError(err); | ||
return; | ||
}); | ||
}; | ||
@@ -407,3 +421,3 @@ | ||
// | ||
Lynx.prototype.close = function () { | ||
Lynx.prototype.close = function close() { | ||
// | ||
@@ -434,3 +448,47 @@ // User defined socket | ||
// | ||
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ streams ~~ | ||
// | ||
// | ||
// ### function write() | ||
// | ||
// Implements `Stream.prototype.write()`. | ||
// | ||
Lynx.prototype.write = function write(buffer) { | ||
this.parser.write(buffer); | ||
}; | ||
// | ||
// ### function end() | ||
// | ||
// Implements `Stream.prototype.end()`. | ||
// | ||
Lynx.prototype.end = function end(buffer) { | ||
// | ||
// If there's stuff to flush please do | ||
// | ||
if (arguments.length) { | ||
this.write(buffer); | ||
} | ||
// | ||
// Make this not writable | ||
// | ||
this.writable = false; | ||
}; | ||
// | ||
// ### function destroy() | ||
// | ||
// Implements `Stream.prototype.destroy()`. Nothing to do here, we don't | ||
// open any stuff | ||
// | ||
Lynx.prototype.destroy = function destroy() { | ||
this.writable = false; | ||
}; | ||
// | ||
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ aux ~~ | ||
@@ -456,3 +514,3 @@ // | ||
// | ||
Lynx.sample = function (stats, sample_rate) { | ||
Lynx.sample = function sample(stats, sample_rate) { | ||
// | ||
@@ -480,3 +538,3 @@ // If we don't have a sample rate between 0 and 1 | ||
// | ||
Object.keys(stats).forEach(function (stat) { | ||
Object.keys(stats).forEach(function construct_sampled(stat) { | ||
value = stats[stat]; | ||
@@ -498,3 +556,3 @@ sampled_stats[stat] = value + "|@" + sample_rate; | ||
// | ||
Lynx.prototype._update_last_used = function () { | ||
Lynx.prototype._update_last_used = function _update_last_used() { | ||
var self = this; | ||
@@ -533,2 +591,16 @@ | ||
// | ||
// ### function default_error_handler() | ||
// #### @err {Object} The error object. Includes: | ||
// err.message, err.* | ||
// | ||
// Function that defines what to do on error. | ||
// Errors are soft errors, and while interesting they are mostly informative | ||
// A simple console log would do but that doesn't allow people to do | ||
// custom stuff with errors | ||
// | ||
Lynx.prototype._default_error_handler = function _default_error_handler(e) { | ||
console.log(e.message); | ||
}; | ||
// | ||
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ exports ~~ | ||
@@ -535,0 +607,0 @@ // |
{ "name" : "lynx" | ||
, "description" : "Minimalistic StatsD client for Node.js programs" | ||
, "version" : "0.0.3" | ||
, "version" : "0.0.5" | ||
, "author" : "Lloyd Hilaiel" | ||
@@ -25,3 +25,3 @@ , "contributors": [ "Nuno Job <nunojobpinto@gmail.com> (http://nunojob.com)" ] | ||
, "devDependencies": { "tap" : "~0.3.2" } | ||
, "dependencies" : { "mersenne" : "~0.0.3" } | ||
, "dependencies" : { "mersenne" : "~0.0.3", "statsd-parser": "~0.0.0" } | ||
, "licenses" : | ||
@@ -28,0 +28,0 @@ [ { "type" : "MIT" |
@@ -41,2 +41,15 @@ # lynx | ||
### Streams | ||
You can stream to `lynx`: | ||
``` js | ||
createDelayedStream() | ||
.pipe(new lynx('localhost', port)) | ||
.pipe(createTestStream(t)) | ||
; | ||
``` | ||
Feel free to check the `stream-test` for more info. | ||
### Timers | ||
@@ -48,3 +61,3 @@ | ||
var metrics = new lynx('localhost', 8125) | ||
, timer = metrics.Timer('some.interval') | ||
, timer = metrics.createTimer('some.interval') | ||
; | ||
@@ -51,0 +64,0 @@ |
[ "foo.baz.time:10|ms" | ||
, "foo.bar.time:500|ms" | ||
, "foo.interval:~100|ms" | ||
, "foo.interval:~200|ms" | ||
, "bar.comes.first:~100|ms" | ||
] |
@@ -33,3 +33,3 @@ var path = require('path') | ||
// | ||
macros.updServer = function udpServer(onMessage) { | ||
macros.udpServer = function udpServer(onMessage) { | ||
var socket = dgram.createSocket('udp4', onMessage); | ||
@@ -77,3 +77,3 @@ | ||
// | ||
var socket = macros.updServer(function (message, remote) { | ||
var socket = macros.udpServer(function (message, remote) { | ||
// | ||
@@ -80,0 +80,0 @@ // We got another one |
var macros = require('./macros') | ||
, lynx = macros.lynx | ||
, test = macros.test | ||
, updServer = macros.updServer | ||
, udpServer = macros.udpServer | ||
, connection = macros.connection | ||
@@ -44,3 +44,3 @@ , count = 0 | ||
test("sampling", function (t) { | ||
var server = updServer(function (message, remote) { | ||
var server = udpServer(function (message, remote) { | ||
count++; | ||
@@ -47,0 +47,0 @@ |
@@ -17,4 +17,4 @@ var macros = require('./macros'); | ||
// | ||
var timer = connection.Timer('foo.interval'); | ||
var timer = connection.createTimer('foo.interval'); | ||
// | ||
@@ -28,3 +28,15 @@ // Wait 100ms | ||
timer.stop(); | ||
}, 200); | ||
// | ||
// A second timer | ||
// | ||
var second_timer = connection.createTimer('bar.comes.first'); | ||
setTimeout(function () { | ||
// | ||
// Stop the timer | ||
// | ||
second_timer.stop(); | ||
}, 100); | ||
}); |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
37633
22
1123
146
2
+ Addedstatsd-parser@~0.0.0
+ Addedstatsd-parser@0.0.4(transitive)