diff --git a/README.md b/README.md index 8246dd9..25bdb99 100644 --- a/README.md +++ b/README.md @@ -44,9 +44,9 @@ The protocol is super simple, so feel free to check out the source code to under You can stream to `lynx`: ``` js -createDelayedStream() +fs.createReadStream('file.statsd') .pipe(new lynx('localhost', port)) - .pipe(createTestStream(t)) + .pipe(fs.createReadStream('file-fixed.statsd')) ; ``` @@ -94,7 +94,6 @@ metrics.send( , "bar" : "15|g" // gauge , "baz" : "500|ms" // timing , "boaz": "40|s" // set - , "" }, 0.1); // sample rate at `0.1` ``` @@ -104,14 +103,14 @@ You can close your open socket when you no longer need it by using `metrics.clos ### Errors -By default `errors` get logged. If you wish to change this behavior simply specify a `onError` function when instantiating the `lynx` client. +By default `errors` get logged. If you wish to change this behavior simply specify a `on_error` function when instantiating the `lynx` client. ``` js -function onError(err) { +function on_error(err) { console.log(err.message); } -var connection = new lynx('localhost', 1234, {onError: onError}); +var connection = new lynx('localhost', 1234, {on_error: on_error}); ``` Source code is super minimal, if you want try to get familiar with when errors occur check it out. If you would like to change behavior on how this is handled send a pull request justifying why and including the alterations you would like to propose. diff --git a/lib/lynx.js b/lib/lynx.js index 4ee80ca..56f2363 100644 --- a/lib/lynx.js +++ b/lib/lynx.js @@ -26,7 +26,7 @@ var EPHEMERAL_LIFETIME_MS = 1000; // #### @port {Number} Server port // #### @options {Object} Aditional options // #### @options.socket {Object} Optional socket if we want to share -// #### @options.onError {Function} A function to execute on errors +// #### @options.on_error {Function} A function to execute on errors // // var client = new lynx('localhost', 8125); // @@ -57,8 +57,8 @@ function Lynx(host, port, options) { // // Set out error handling code // - this.onError = options && typeof options.onError === "function" - ? options.onError + this.on_error = options && typeof options.on_error === 'function' + ? options.on_error : this._default_error_handler ; @@ -70,13 +70,13 @@ function Lynx(host, port, options) { this.parser = parser.createStream(); - this.parser.on('error', this.onError); - this.parser.on('stat', function (text, statObj) { + this.parser.on('error', this.on_error); + this.parser.on('stat', function (text, stat_obj) { 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)); + stat[stat_obj.stat] = stat_obj.value + '|' + stat_obj.type; + if(stat_obj.sample_rate) { + stat[stat_obj.stat] += '@' + stat_obj.sample_rate; + self.send(stat, parseFloat(stat_obj.sample_rate)); } else { self.send(stat); @@ -93,7 +93,7 @@ util.inherits(Lynx, Stream); // packets should be sent. If set to 0.1 it sends 1 in each 10. // // var client = new lynx('localhost', 8125); -// var timer = client.Timer("foo"); +// var timer = client.Timer('foo'); // // // // // Sends something like: `foo:100|ms` via udp to the server @@ -197,10 +197,10 @@ Lynx.prototype.count = function count(stats, delta, sample_rate) { // // Error: Can't set if its not even an array by now // - this.onError( - { "message" : "Can't set if its not even an array by now" - , "f" : "count" - , "args" : arguments + this.on_error( + { message : "Can't set if its not even an array by now" + , f : 'count' + , args : arguments }); return; } @@ -212,10 +212,10 @@ Lynx.prototype.count = function count(stats, delta, sample_rate) { // // Error: Must be either a number or a string, we cant send other stuff // - this.onError( - { "message" : "Must be either a number or a string" - , "f" : "count" - , "args" : arguments + this.on_error( + { message : 'Must be either a number or a string' + , f : 'count' + , args : arguments }); return; } @@ -225,7 +225,7 @@ Lynx.prototype.count = function count(stats, delta, sample_rate) { // batch = {}; for(var i in stats) { - batch[stats[i]] = delta + "|c"; + batch[stats[i]] = delta + '|c'; } // @@ -248,7 +248,7 @@ Lynx.prototype.count = function count(stats, delta, sample_rate) { // Lynx.prototype.timing = function timing(stat, duration, sample_rate) { var stats = {}; - stats[stat] = duration + "|ms"; + stats[stat] = duration + '|ms'; this.send(stats, sample_rate); }; @@ -266,7 +266,7 @@ Lynx.prototype.timing = function timing(stat, duration, sample_rate) { // Lynx.prototype.set = function set(stat, value, sample_rate) { var stats = {}; - stats[stat] = value + "|s"; + stats[stat] = value + '|s'; this.send(stats, sample_rate); }; @@ -284,7 +284,7 @@ Lynx.prototype.set = function set(stat, value, sample_rate) { // Lynx.prototype.gauge = function gauge(stat, value, sample_rate) { var stats = {}; - stats[stat] = value + "|g"; + stats[stat] = value + '|g'; this.send(stats, sample_rate); }; @@ -323,10 +323,10 @@ Lynx.prototype.send = function send(stats, sample_rate) { // // Error: Nothing to send // - this.onError( - { "message" : "Nothing to send" - , "f" : "send" - , "args" : arguments + this.on_error( + { message : 'Nothing to send' + , f : 'send' + , args : arguments }); return; } @@ -337,8 +337,8 @@ Lynx.prototype.send = function send(stats, sample_rate) { // This is achieved by having newline separated stats. // send_data = all_stats.map(function construct_stat(stat) { - return stat + ":" + sampled_stats[stat]; - }).join("\n"); + return stat + ':' + sampled_stats[stat]; + }).join('\n'); // // Encode our data to a buffer @@ -364,10 +364,10 @@ Lynx.prototype.send = function send(stats, sample_rate) { // this.ephemeral_socket.on('error', function (err) { err.reason = err.message; - err.f = "send"; - err.message = "Failed sending the buffer"; + err.f = 'send'; + err.message = 'Failed sending the buffer'; err.args = arguments; - self.onError(err); + self.on_error(err); return; }); } @@ -518,7 +518,7 @@ Lynx.sample = function sample(stats, sample_rate) { // Object.keys(stats).forEach(function construct_sampled(stat) { value = stats[stat]; - sampled_stats[stat] = value + "|@" + sample_rate; + sampled_stats[stat] = value + '|@' + sample_rate; }); } diff --git a/package.json b/package.json index 3e53848..1519766 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name" : "lynx" , "description" : "Minimalistic StatsD client for Node.js programs" -, "version" : "0.0.5" +, "version" : "0.0.6" , "author" : "Lloyd Hilaiel" , "contributors": [ "Nuno Job (http://nunojob.com)" ] , "scripts" : { "test": "tap tests/*-test.js" } diff --git a/tests/errors-test.js b/tests/errors-test.js index 3a54c17..7491748 100644 --- a/tests/errors-test.js +++ b/tests/errors-test.js @@ -2,18 +2,18 @@ var macros = require('./macros') , lynx = macros.lynx , port = macros.udpServerPort , test = macros.test - , fixture = require("./fixtures/errors.json") + , fixture = require('./fixtures/errors.json') ; test('errors', function (t) { - function onError(actual) { + function on_error(actual) { var expected = fixture.shift(); // // Should return the function that invoked this and the arguments // for inspection // - t.ok(actual.f, "should have a reference to the function"); - t.ok(actual.args, "args should be supplied"); + t.ok(actual.f, 'should have a reference to the function'); + t.ok(actual.args, 'args should be supplied'); // // Message should match fixture @@ -34,7 +34,7 @@ test('errors', function (t) { // // Wrong host // - var connection = new lynx('locahost', port, {onError: onError}); + var connection = new lynx('locahost', port, {on_error: on_error}); connection.count(1); connection.count('foo', NaN); diff --git a/tests/sampling-test.js b/tests/sampling-test.js index bb8fffe..f9ea526 100644 --- a/tests/sampling-test.js +++ b/tests/sampling-test.js @@ -40,7 +40,7 @@ for(i=0; i DESIRED) { finished = true; - t.ok(true, "Reached " + DESIRED + " on " + (TOTAL - coll.length) + - " packets."); + t.ok(true, 'Reached ' + DESIRED + ' on ' + (TOTAL - coll.length) + + ' packets.'); server.close(); } }); @@ -68,7 +68,7 @@ test("sampling", function (t) { // // Send a sample // - connection.gauge("spl.foo", 500, SAMPLE); + connection.gauge('spl.foo', 500, SAMPLE); process.nextTick(iterate); }, Math.ceil(Math.random() * 10)); })(); @@ -76,7 +76,7 @@ test("sampling", function (t) { runAll(coll, function() { if (finished) { - t.ok(true, "Reached " + DESIRED + " on " + TOTAL + " packets."); + t.ok(true, 'Reached ' + DESIRED + ' on ' + TOTAL + ' packets.'); t.end(); return; } @@ -84,8 +84,8 @@ test("sampling", function (t) { // If we reached the end and this has not closed by having // the desired amount of requests // - t.ok(false, "Didnt reach the desired amount of packets " + DESIRED + - "/" + TOTAL + " was -> " + count); + t.ok(false, 'Didnt reach the desired amount of packets ' + DESIRED + + '/' + TOTAL + ' was -> ' + count); server.close(); t.end(); }); diff --git a/tests/stream-test.js b/tests/stream-test.js index c483e1f..a062b7d 100644 --- a/tests/stream-test.js +++ b/tests/stream-test.js @@ -4,8 +4,8 @@ var Stream = require('stream') , port = macros.udpServerPort , udpServer = macros.udpServer , test = macros.test - , fixture_send = require("./fixtures/stream-send.json") - , fixture_recv = require("./fixtures/stream-recv.json") + , fixture_send = require('./fixtures/stream-send.json') + , fixture_recv = require('./fixtures/stream-recv.json') ; var server = udpServer(function () {}); @@ -64,7 +64,7 @@ function createTestStream(t) { // test_stream.write = function (buf) { var expected = fixture_recv.shift(); - t.equal(expected, buf.toString(), "===" + expected); + t.equal(expected, buf.toString(), ' should be equal to ' + expected); if(fixture_recv.length === 0) { test_stream.end(); } @@ -105,12 +105,12 @@ function createTestStream(t) { test('streams', function (t) { // - // ### function onError(err) + // ### function on_error(err) // #### @err {Error} Error object // // Assertion to run if we get any errors // - function onError(err) { + function on_error(err) { t.equal({}, err, "didn't expect any errors"); // // End early @@ -121,7 +121,7 @@ test('streams', function (t) { // // Our connection // - var conn = new lynx('localhost', port, {onError: onError}); + var conn = new lynx('localhost', port, {on_error: on_error}); createDelayedStream() .pipe(conn)