Skip to content

Commit

Permalink
[dist] 0.0.10
Browse files Browse the repository at this point in the history
* Adds scopes
  • Loading branch information
dscape committed Nov 19, 2012
1 parent 43f50ad commit 1dbe91e
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 3 deletions.
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,14 @@ A minimalistic node.js client for [statsd] server. Fork of original work by [siv
$ npm install lynx
$ node
> var lynx = require('lynx');
//
// Options in this instantiation include:
// * `on_error` function to be executed when we have errors
// * `socket` if you wish to just use a existing udp socket
// * `scope` to define the a prefix for all stats, e.g. with `scope`
// 'product1' and stat 'somestat' the key would actually be
// 'product1.somestat'
//
> var metrics = new lynx('localhost', 8125);
{ host: 'localhost', port: 8125 }
> metrics.increment('node_test.int');
Expand Down
32 changes: 30 additions & 2 deletions lib/lynx.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,11 @@ var EPHEMERAL_LIFETIME_MS = 1000;
// #### @host {String} Server host name
// #### @port {Number} Server port
// #### @options {Object} Aditional options
// #### @options.socket {Object} Optional socket if we want to share
// #### @options.socket {Object} Optional socket if we want to share
// #### @options.on_error {Function} A function to execute on errors
// #### @options.scope {String} define the a prefix for all stats,
// e.g. with `scope` 'product1' and stat 'somestat' the key would
// actually be 'product1.somestat'.
//
// var client = new lynx('localhost', 8125);
//
Expand All @@ -46,6 +49,21 @@ function Lynx(host, port, options) {
//
this.socket = options && options.socket;

//
// Handle prefix
//
this.scope = options && options.scope || options && options.prefix || '';

//
// groups in graphite are delimited by `.` so we need to make sure our
// scope ends with `.`. If it doesn't we just add it (unless we have no
// scope defined).
//
if(typeof this.scope === 'string' && this.scope !== '' &&
!/\.$/.test(this.scope)) {
this.scope += '.';
}

//
// When a *shared* socked isn't provided, an ephemeral
// socket is demand allocated. This ephemeral socket is closed
Expand All @@ -71,9 +89,18 @@ function Lynx(host, port, options) {
this.parser = parser.createStream();

this.parser.on('error', this.on_error);

this.parser.on('stat', function (text, stat_obj) {
var stat = {};

//
// Construct a statsd value|type pair
//
stat[stat_obj.stat] = stat_obj.value + '|' + stat_obj.type;

//
// Add sample rate if one exists
//
if(stat_obj.sample_rate) {
stat[stat_obj.stat] += '@' + stat_obj.sample_rate;
self.send(stat, parseFloat(stat_obj.sample_rate));
Expand Down Expand Up @@ -361,7 +388,7 @@ 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];
return self.scope + stat + ':' + sampled_stats[stat];
}).join('\n');

//
Expand All @@ -370,6 +397,7 @@ Lynx.prototype.send = function send(stats, sample_rate) {
var buffer = new Buffer(send_data, 'utf8')
, socket
;

//
// Do we already have a socket object we can use?
//
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.9"
, "version" : "0.0.10"
, "author" : "Lloyd Hilaiel"
, "contributors": [ "Nuno Job <nunojobpinto@gmail.com> (http://nunojob.com)" ]
, "scripts" : { "test": "tap tests/*-test.js" }
Expand Down
5 changes: 5 additions & 0 deletions tests/fixtures/scopes.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
[ "scope.bar:1|c"
, "scope.baz:-1|c"
, "scope.uno:-1|c\nscope.two:-1|c\nscope.trezentos:-1|c"
, "scope.boaz:101|c"
]
5 changes: 5 additions & 0 deletions tests/macros.js
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,11 @@ macros.matchFixturesTest = function genericTest(resource, f) {
//
// Run our client code
//
if(resource === 'scopes') {
macros.connection.close();
macros.connection = new lynx('localhost', macros.udpServerPort, {
scope: 'scope' });
}
f(macros.connection);
});
};
Expand Down
12 changes: 12 additions & 0 deletions tests/scopes-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
var macros = require('./macros');

//
// Our `counting` tests
// Should match `tests/fixtures/counting.json`
//
macros.matchFixturesTest('scopes', function runTest(connection) {
connection.increment('bar');
connection.decrement('baz');
connection.decrement(['uno', 'two', 'trezentos']);
connection.count('boaz', 101);
});

0 comments on commit 1dbe91e

Please sign in to comment.