Skip to content

Commit

Permalink
[dist] Changes api for onError to on_error
Browse files Browse the repository at this point in the history
* Minor style fixes
  • Loading branch information
dscape committed Nov 12, 2012
1 parent 1c750db commit 66ca3d6
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 57 deletions.
11 changes: 5 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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'))
;
```

Expand Down Expand Up @@ -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`
```

Expand All @@ -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.
Expand Down
64 changes: 32 additions & 32 deletions lib/lynx.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
//
Expand Down Expand Up @@ -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
;

Expand All @@ -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);
Expand All @@ -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
Expand Down Expand Up @@ -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;
}
Expand All @@ -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;
}
Expand All @@ -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';
}

//
Expand All @@ -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);
};

Expand All @@ -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);
};

Expand All @@ -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);
};

Expand Down Expand Up @@ -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;
}
Expand All @@ -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
Expand All @@ -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;
});
}
Expand Down Expand Up @@ -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;
});
}

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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 <nunojobpinto@gmail.com> (http://nunojob.com)" ]
, "scripts" : { "test": "tap tests/*-test.js" }
Expand Down
10 changes: 5 additions & 5 deletions tests/errors-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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);
Expand Down
14 changes: 7 additions & 7 deletions tests/sampling-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ for(i=0; i<TOTAL; i++) {
// this can never be an exact test and might break while the code is
// perfectly fine
//
test("sampling", function (t) {
test('sampling', function (t) {
var server = udpServer(function (message, remote) {
count++;

Expand All @@ -49,8 +49,8 @@ test("sampling", function (t) {
//
if(count > 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();
}
});
Expand All @@ -68,24 +68,24 @@ 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));
})();
};

runAll(coll, function() {
if (finished) {
t.ok(true, "Reached " + DESIRED + " on " + TOTAL + " packets.");
t.ok(true, 'Reached ' + DESIRED + ' on ' + TOTAL + ' packets.');
t.end();
return;
}
//
// 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();
});
Expand Down
12 changes: 6 additions & 6 deletions tests/stream-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 () {});
Expand Down Expand Up @@ -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();
}
Expand Down Expand Up @@ -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
Expand All @@ -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)
Expand Down

0 comments on commit 66ca3d6

Please sign in to comment.