Skip to content

Commit

Permalink
Merge pull request #1 from ifwe/generic-data-processor
Browse files Browse the repository at this point in the history
Make sitemon-js work with sitemon GenericDataProcessor format
  • Loading branch information
djvirgen committed Nov 5, 2015
2 parents 955a183 + 15bf3c0 commit 2cb027d
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 35 deletions.
12 changes: 6 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ A node client for sending statistics to sitemon.

```javascript
var Sitemon = require('sitemon-js');
var apiLogger = new Sitemon('api', '1.2.3.4:5678');
apiLogger.send({ method: 'tagged.user.getInfo' }); // sends message to sitemon over UDP
var apiLogger = new Sitemon('apitable', '1.2.3.4:5678');
apiLogger.send({ method: 'tagged.user.getInfo' }, { calls: 1, process_time_ms: 20 }); // sends message to sitemon over UDP
```

## Round-robin target hosts
Expand All @@ -19,10 +19,10 @@ var apiLogger = new Sitemon('api', [
'2.3.4.5:6789',
'3.4.5.6:7890'
]);
apiLogger.send({ method: 'tagged.user.getInfo' }); // sends to 1.2.3.4:5678
apiLogger.send({ method: 'tagged.profile.update' }); // sends to 2.3.4.5:6789
apiLogger.send({ method: 'tagged.stuff.do' }); // sends to 3.4.5.6:7890
apiLogger.send({ method: 'tagged.photo.info' }); // sends to 1.2.3.4:5678
apiLogger.send({ method: 'tagged.user.getInfo' }, { calls: 1, process_time_ms: 20 }); // sends to 1.2.3.4:5678
apiLogger.send({ method: 'tagged.profile.update' }, { calls: 1, process_time_ms: 20 }); // sends to 2.3.4.5:6789
apiLogger.send({ method: 'tagged.stuff.do' }, { calls: 1, process_time_ms: 20 }); // sends to 3.4.5.6:7890
apiLogger.send({ method: 'tagged.photo.info' }, { calls: 1, process_time_ms: 20 }); // sends to 1.2.3.4:5678
```

## Files and Directory Structure
Expand Down
21 changes: 12 additions & 9 deletions lib/index.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
var Promise = require('bluebird');
var dgram = require('dgram');

function Sitemon(type, hosts) {
this._type = type;
function Sitemon(table, hosts) {
this._table = table;
this._hosts = Array.isArray(hosts) ? hosts : [hosts];
this._totalHosts = this._hosts.length;
this._currentHostIndex = 0;
this._client = dgram.createSocket("udp4");
}

Sitemon.prototype.send = function(data) {
Sitemon.prototype.send = function(keys, values) {
return new Promise(function(resolve, reject) {
var message = formatData(this._type, data);
var message = formatData(this._table, keys, values);
var buf = new Buffer(message);

var selectedHost = this._hosts[this._currentHostIndex].split(':', 2);
Expand All @@ -31,14 +31,17 @@ Sitemon.prototype.send = function(data) {
}.bind(this));
};

var formatData = function(type, data) {
var message = ['type=' + type];
var formatData = function(table, keys, values) {
var message = ['table=' + table];

for (var key in data) {
message.push(key + '=' + data[key]);
for (var keyColumn in keys) {
message.push('k.' + keyColumn + '=' + keys[keyColumn]);
}
for (var valueColumn in values) {
message.push('v.' + valueColumn + '=' + values[valueColumn]);
}

return message.join('\n');
};

module.exports = Sitemon;
module.exports = Sitemon;
40 changes: 20 additions & 20 deletions test/lib/index_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,18 +33,18 @@ describe('Sitemon', function() {
return this.sitemon.send({
foo: 'test_foo',
bar: 'test_bar'
}).then(function() {
}, {calls: 1}).then(function() {
this.client.send.called.should.be.true;
this.client.send.lastCall.args[0].should.be.an.instanceOf(Buffer);
this.client.send.lastCall.args[0].toString().should.equal('type=test\nfoo=test_foo\nbar=test_bar');
this.client.send.lastCall.args[0].toString().should.equal('table=test\nk.foo=test_foo\nk.bar=test_bar\nv.calls=1');
}.bind(this));
});

it('sends with offset 0', function() {
return this.sitemon.send({
foo: 'test_foo',
bar: 'test_bar'
}).then(function() {
}, {calls: 1}).then(function() {
this.client.send.called.should.be.true;
this.client.send.lastCall.args[1].should.equal(0);
}.bind(this));
Expand All @@ -54,17 +54,17 @@ describe('Sitemon', function() {
return this.sitemon.send({
foo: 'test_foo',
bar: 'test_bar'
}).then(function() {
}, {calls: 1}).then(function() {
this.client.send.called.should.be.true;
this.client.send.lastCall.args[2].should.equal(35); // based on formatted message
this.client.send.lastCall.args[2].should.equal(50); // based on formatted message
}.bind(this));
});

it('sends udp packet to target port', function() {
return this.sitemon.send({
foo: 'test_foo',
bar: 'test_bar'
}).then(function() {
}, {calls: 1}).then(function() {
this.client.send.called.should.be.true;
this.client.send.lastCall.args[3].should.equal(5678);
}.bind(this));
Expand All @@ -74,7 +74,7 @@ describe('Sitemon', function() {
return this.sitemon.send({
foo: 'test_foo',
bar: 'test_bar'
}).then(function() {
}, {calls: 1}).then(function() {
this.client.send.called.should.be.true;
this.client.send.lastCall.args[4].should.equal('1.2.3.4');
}.bind(this));
Expand All @@ -85,7 +85,7 @@ describe('Sitemon', function() {
return this.sitemon.send({
foo: 'test_foo',
bar: 'test_bar'
}).then(function() {
}, {calls: 1}).then(function() {
return Promise.reject("Unexpected resolve");
}).catch(function(error) {
error.should.equal('some error');
Expand All @@ -103,18 +103,18 @@ describe('Sitemon', function() {
return this.sitemon.send({
foo: 'test_foo',
bar: 'test_bar'
}).then(function() {
}, {calls: 1}).then(function() {
this.client.send.called.should.be.true;
this.client.send.lastCall.args[0].should.be.an.instanceOf(Buffer);
this.client.send.lastCall.args[0].toString().should.equal('type=test\nfoo=test_foo\nbar=test_bar');
this.client.send.lastCall.args[0].toString().should.equal('table=test\nk.foo=test_foo\nk.bar=test_bar\nv.calls=1');
}.bind(this));
});

it('sends with offset 0', function() {
return this.sitemon.send({
foo: 'test_foo',
bar: 'test_bar'
}).then(function() {
}, {calls: 1}).then(function() {
this.client.send.called.should.be.true;
this.client.send.lastCall.args[1].should.equal(0);
}.bind(this));
Expand All @@ -124,17 +124,17 @@ describe('Sitemon', function() {
return this.sitemon.send({
foo: 'test_foo',
bar: 'test_bar'
}).then(function() {
}, {calls: 1}).then(function() {
this.client.send.called.should.be.true;
this.client.send.lastCall.args[2].should.equal(35); // based on formatted message
this.client.send.lastCall.args[2].should.equal(50); // based on formatted message
}.bind(this));
});

it('sends first udp packet to first host/port pair', function() {
return this.sitemon.send({
foo: 'test_foo',
bar: 'test_bar'
}).then(function() {
}, {calls: 1}).then(function() {
this.client.send.called.should.be.true;
this.client.send.lastCall.args[3].should.equal(5678);
this.client.send.lastCall.args[4].should.equal('1.2.3.4');
Expand All @@ -143,11 +143,11 @@ describe('Sitemon', function() {

it('sends each udp packet to a host/port pair in a round-robin fashion', function() {
return Promise.all([
this.sitemon.send({foo: 'test_foo'}),
this.sitemon.send({foo: 'test_foo'}),
this.sitemon.send({foo: 'test_foo'}),
this.sitemon.send({foo: 'test_foo'}),
this.sitemon.send({foo: 'test_foo'})
this.sitemon.send({foo: 'test_foo'}, {calls: 1}),
this.sitemon.send({foo: 'test_foo'}, {calls: 1}),
this.sitemon.send({foo: 'test_foo'}, {calls: 1}),
this.sitemon.send({foo: 'test_foo'}, {calls: 1}),
this.sitemon.send({foo: 'test_foo'}, {calls: 1})
]).then(function() {
this.client.send.called.should.be.true;
this.client.send.getCall(0).args[3].should.equal(5678);
Expand All @@ -172,7 +172,7 @@ describe('Sitemon', function() {
return this.sitemon.send({
foo: 'test_foo',
bar: 'test_bar'
}).then(function() {
}, {calls: 1}).then(function() {
return Promise.reject("Unexpected resolve");
}).catch(function(error) {
error.should.equal('some error');
Expand Down

0 comments on commit 2cb027d

Please sign in to comment.