Skip to content

Commit

Permalink
Merge pull request #1 from ashpool/v0.0.2-beta
Browse files Browse the repository at this point in the history
V0.0.2 beta
  • Loading branch information
ashpool committed Apr 2, 2015
2 parents 7e27c21 + cdef9d6 commit f668764
Show file tree
Hide file tree
Showing 10 changed files with 138 additions and 55 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,5 @@ node_modules
.idea/
.coverrun
reports/
.coverdata/
debug/
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,13 @@ Experimental/Unstable

```js
var graphite = require('graphite-promise'),
client = graphite.createClient(<Graphite URL>);
client = graphite.createClient(<graphite-url e.g 'plaintext://127.0.0.1:2003/'>);
client.write(metric, timestamp)
.then(function(){})
.catch(function(reason){})
.finally(function(){});
.finally(function(){
client.end();
});
```
### Example

Expand Down
52 changes: 24 additions & 28 deletions lib/carbon.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,57 +4,53 @@ var RSVP = require('rsvp'),

function CarbonClient (properties) {
properties = properties || {};
this._dsn = properties.dsn;
this._url = properties.url;
this._socket = properties.socket || null;
}

CarbonClient.prototype.write = function (metrics, timestamp) {
var self = this;
self._lazyConnect().then(function () {
});
return new RSVP.Promise(function (resolve, reject) {
var lines = '';
for (var path in metrics) {
if (metrics.hasOwnProperty(path)) {
var value = metrics[path];
lines += [path, value, timestamp].join(' ') + '\n';
self._connect().then(function (socket) {
var lines = '';
for (var path in metrics) {
if (metrics.hasOwnProperty(path)) {
var value = metrics[path];
lines += [path, value, timestamp].join(' ') + '\n';
}
}
}

self._socket.write(lines, 'utf-8', function (err) {
if (err) {
reject(err);
} else {
resolve(metrics);
}
});
socket.write(lines, 'utf-8', function (err) {
return err && reject(err) || resolve(lines);
});
}).catch(reject);
});
};

CarbonClient.prototype._lazyConnect = function () {
CarbonClient.prototype._connect = function () {
var self = this;
return new RSVP.Promise(function (resolve, reject) {
if (self._socket) {
return resolve();
return resolve(self._socket);
}
var dsn = url.parse(self._dsn),
var dsn = url.parse(self._url),
port = parseInt(dsn.port, 10) || 2003,
host = dsn.hostname,
socket = new net.Socket();
self._socket = socket.connect(port, host, 1000, function (err) {
if (err) {
reject(err);
} else {
resolve();
}
return err && reject(err) || resolve(self._socket);
});
});
};

CarbonClient.prototype.end = function () {
if (this._socket) {
this._socket.end();
}
return new RSVP.Promise(function (resolve, reject) {
try {
resolve(this._socket && this._socket.end());
}
catch (err) {
reject(err);
}
});
};

module.exports = CarbonClient;
37 changes: 15 additions & 22 deletions lib/graphite.js
Original file line number Diff line number Diff line change
@@ -1,41 +1,34 @@
var CarbonClient = require('./carbon');
var client = require('./carbon'),
metric = require('./metric');

function GraphiteClient (properties) {
this._carbon = properties.carbon;
}

GraphiteClient.createClient = function (carbonDsn) {
return new this({
carbon: new CarbonClient({dsn: carbonDsn})
carbon: new client({url: carbonDsn})
});
};

GraphiteClient.flatten = function (obj, flat, prefix) {
flat = flat || {};
prefix = prefix || '';

for (var key in obj) {
if (obj.hasOwnProperty(key)) {
var value = obj[key];
if (typeof value === 'object') {
this.flatten(value, flat, prefix + key + '.');
} else {
flat[prefix + key] = value;
}
}
}

return flat;
};

/**
* Writes metric with timestamp to Graphite
*
* @param metrics an object with values, e.g. {home:{indoor:{temp:21.2}}}
* @param timestamp defaults to Date.now()
* @returns {RSVP.Promise} a promise
*/
GraphiteClient.prototype.write = function (metrics, timestamp) {
timestamp = timestamp || Date.now();
timestamp = Math.floor(timestamp / 1000);
return this._carbon.write(GraphiteClient.flatten(metrics), timestamp);
return this._carbon.write(metric.flatten(metrics), timestamp);
};

/**
* Ends the connection
*/
GraphiteClient.prototype.end = function () {
this._carbon.end();
return this._carbon.end();
};

module.exports = GraphiteClient;
18 changes: 18 additions & 0 deletions lib/metric.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
module.exports = {
flatten: function (obj, flat, prefix) {
flat = flat || {};
prefix = prefix || '';

for (var key in obj) {
if (obj.hasOwnProperty(key)) {
var value = obj[key];
if (typeof value === 'object') {
this.flatten(value, flat, prefix + key + '.');
} else {
flat[prefix + key] = value;
}
}
}
return flat;
}
};
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "graphite-promise",
"version": "0.0.1",
"version": "0.0.2",
"description": "A node.js module to interface with Graphite promise style",
"main": "./lib/graphite",
"directories": {
Expand Down
37 changes: 37 additions & 0 deletions test/carbon.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*jshint undef:false */
var chaiAsPromised = require('chai-as-promised'),
chai = require('chai');

chai.should();
chai.use(chaiAsPromised);

describe('CarbonClient', function () {
var Carbon = require('./../lib/carbon'),
client = new Carbon({dsn: 'plaintext://127.0.0.1:2003/'}),
socketMock = {};

describe('#write', function () {
before(function () {
client._socket = socketMock;
});

it('writes flattened metrics encoded as utf-8', function (done) {
var metric = {'home.indoor.temp': 21.2},
timestamp = 1427727486200;
socketMock.write = function (lines, encoding, cb) {
lines.should.equal('home.indoor.temp 21.2 1427727486200\n');
encoding.should.equal('utf-8');
cb();
};
client.write(metric, timestamp).should.eventually.equal('home.indoor.temp 21.2 1427727486200\n').notify(done);
});
it('rejects when a socket error occur', function (done) {
var metric = {'home.indoor.temp': 21.2},
timestamp = 1427727486200;
socketMock.write = function (lines, encoding, cb) {
cb(new Error('fail'));
};
client.write(metric, timestamp).should.eventually.be.rejected.notify(done);
});
});
});
7 changes: 6 additions & 1 deletion test/graphite.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@ chai.should();
chai.use(chaiAsPromised);

describe('GraphiteClient', function () {
describe('#write', function () {
describe('#end', function () {
it('should be safe to call any time', function (done) {
var graphite = require('./../lib/graphite'),
client = graphite.createClient('plaintext://127.0.0.1:2003/');
client.end().should.be.fulfilled.notify(done);
});
});
});
3 changes: 2 additions & 1 deletion test/index.js
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
require('./carbon');
require('./graphite');
require('./graphite');
require('./metric');
29 changes: 29 additions & 0 deletions test/metric.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*jshint undef:false */
var chaiAsPromised = require('chai-as-promised'),
chai = require('chai'),
metric = require('./../lib/metric');

chai.should();
chai.use(chaiAsPromised);

describe('metric', function () {
describe('#flatten', function () {
it('flattens objects', function () {
metric.flatten({home: {indoor: {temp: 21.2}}}).should.deep.equal({'home.indoor.temp': 21.2});
});
it('flattens objects two properties', function () {
metric.flatten({home: {indoor: {temp: 21.2, humidity: 94.5}}})
.should.deep.equal({'home.indoor.humidity': 94.5, 'home.indoor.temp': 21.2});
});
it('flattens objects three properties', function () {
metric.flatten({foz: {baz: {foo: 42, bar: 1337, soz: 101}}})
.should.deep.equal({'foz.baz.bar': 1337, 'foz.baz.foo': 42, 'foz.baz.soz': 101});
});
it('handles empty objects', function () {
metric.flatten({}).should.deep.equal({});
});
it('handles shallow objects', function () {
metric.flatten({temp: 27.1}).should.deep.equal({temp: 27.1});
});
});
});

0 comments on commit f668764

Please sign in to comment.