Skip to content

Commit

Permalink
Initial version
Browse files Browse the repository at this point in the history
Tests could be better
  • Loading branch information
felixge committed Oct 31, 2011
0 parents commit 5018367
Show file tree
Hide file tree
Showing 9 changed files with 199 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
@@ -0,0 +1,2 @@
/node_modules
/test/config.js
10 changes: 10 additions & 0 deletions index.js
@@ -0,0 +1,10 @@
var CarbonClient = require('./lib/carbon_client');
var GraphiteClient = require('./lib/graphite_client');

exports.createCarbonClient = function() {
return CarbonClient.createClient.apply(CarbonClient, arguments);
};

exports.createGraphiteClient = function() {
return GraphiteClient.createClient.apply(GraphiteClient, arguments);
};
58 changes: 58 additions & 0 deletions lib/carbon_client.js
@@ -0,0 +1,58 @@
var net = require('net');
var url = require('url');

module.exports = CarbonClient;
function CarbonClient(socket) {
this.socket = socket;
this.host = null;
this.port = null;
}
CarbonClient.DEFAULT_HOST = 'localhost';
CarbonClient.DEFAULT_PORT = 2003;

CarbonClient.parse = function(dsn) {
var parsed = url.parse(dsn);
if (parsed.protocol !== 'plaintext:') {
throw new Error('CarbonClient.UnknownProtocol: ' + parsed.protocol);
}

return {
host : parsed.hostname || CarbonClient.DEFAULT_HOST,
port : parsed.port || CarbonClient.DEFAULT_PORT,
namespace : (parsed.pathname)
? parsed.pathname.substr(1)
: '',
};

};

CarbonClient.createClient = function(dsn) {
var parsed = this.parse(dsn);
var socket = net.createConnection(parsed.port, parsed.host);

var client = new this(socket);
client.host = parsed.host;
client.port = parsed.port;

return client;
};

CarbonClient.prototype.write = function(path, value, timestamp, cb) {
if (arguments.length === 3) {
cb = timestamp;
timestamp = Date.now();
}

timestamp = timestamp / 1000;

var line = [path, value, timestamp].join(' ');
this.socket.write(line + '\n', 'utf8', cb);
};

CarbonClient.prototype.getJSON = function() {
var url = 'http://' + this.host + '/';
};

CarbonClient.prototype.end = function() {
this.socket.end();
};
63 changes: 63 additions & 0 deletions lib/graphite_client.js
@@ -0,0 +1,63 @@
var querystring = require('querystring');
var request = require('request');

module.exports = GraphiteClient;
function GraphiteClient(endpoint) {
this.endpoint = endpoint;
}

GraphiteClient.createClient = function(endpoint) {
var client = new this(endpoint);
return client;
};

GraphiteClient.prototype.getJSON = function(query, cb) {
query = JSON.parse(JSON.stringify(query));
query.format = 'json';

var options = {
path : '/render',
query : query,
};

this._request(options, function(err, response, body) {
if (err) return cb(err);

try{
var json = JSON.parse(body);
} catch (err) {
cb(new Error('GraphiteClient.InvalidJson: ' + err + ': ' + body));
return;
}

cb(null, json);
});
};

GraphiteClient.prototype.event = function(event, cb) {
var options = {
method : 'POST',
path : '/events/',
json : event,
};

this._request(options, function(err, response) {
console.error(err);
console.error(response.body);
console.error(response.statusCode);
});
};

GraphiteClient.prototype._request = function(options, cb) {
var path = options.path || '/';
if (options.query) path += '?' + querystring.stringify(options.query);

var requestOptions = {
method : options.method || 'GET',
uri : this.endpoint + path,
timeout : 10 * 1000,
json : options.json
};

request(requestOptions, cb);
};
19 changes: 19 additions & 0 deletions package.json
@@ -0,0 +1,19 @@
{
"author": "Felix Geisendörfer <felix@debuggable.com> (http://debuggable.com/)",
"name": "graphite",
"description": "A node.js client for graphite.",
"version": "0.0.0",
"homepage": "https://github.com/felixge/node-graphite",
"repository": {
"type": "git",
"url": "git://github.com/felixge/node-graphite.git"
},
"main": "./index",
"engines": {
"node": "*"
},
"dependencies": {
"request": "2.1.1"
},
"devDependencies": {}
}
4 changes: 4 additions & 0 deletions test/common.js
@@ -0,0 +1,4 @@
var common = exports;

common.graphite = require('..');
common.config = require('./config');
29 changes: 29 additions & 0 deletions test/integration/test-carbon-client.js
@@ -0,0 +1,29 @@
var common = require('../common');
var graphite = common.graphite;
var assert = require('assert');
var carbonClient = graphite.createCarbonClient(common.config.carbon);
var key = 'test.write';
var value = 42;

carbonClient.write(key, value, function(err) {
if (err) throw err;

var graphiteClient = graphite.createGraphiteClient(common.config.graphite);
var options = {
target : key,
from : '-1minute',

// Undocumented magic we hoped would help us with a better assert, but
// has not so far.
noCache : 'True',
cacheTimeout : 0,
};

graphiteClient.getJSON(options, function(err, json) {
if (err) throw err;

// Terrible check, but we had trouble getting the latest info
assert.ok(json[0].datapoints.length > 0);
carbonClient.end();
});
});
14 changes: 14 additions & 0 deletions test/integration/test-graphite-event.js
@@ -0,0 +1,14 @@
var common = require('../common');
var graphite = common.graphite;
var assert = require('assert');
var event = {
what : 'test.event',
tags : 'foo, bar',
data : 'some data',
when : Date.now() / 1000,
}

var graphiteClient = graphite.createGraphiteClient(common.config.graphite);
graphiteClient.event(event, function(err) {
if (err) throw err;
});
Empty file added test/unit/test-client.js
Empty file.

0 comments on commit 5018367

Please sign in to comment.