Browse files

Initial version

Tests could be better
  • Loading branch information...
0 parents commit 5018367cebc1d12afff0333cf28b244c52babe3f @felixge committed Oct 31, 2011
2 .gitignore
@@ -0,0 +1,2 @@
+/node_modules
+/test/config.js
10 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 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 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 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 test/common.js
@@ -0,0 +1,4 @@
+var common = exports;
+
+common.graphite = require('..');
+common.config = require('./config');
29 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 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;
+});
0 test/unit/test-client.js
No changes.

0 comments on commit 5018367

Please sign in to comment.