Permalink
Browse files

add initial version

  • Loading branch information...
1 parent 94dc68c commit c4d1e9d87a997a4f50600512d74876fc66abc903 @mrtazz committed May 15, 2012
Showing with 132 additions and 9 deletions.
  1. +10 −9 README.md
  2. +20 −0 package.json
  3. +102 −0 statsd-amqp-backend/index.js
View
@@ -11,12 +11,14 @@ publishes stats to an AMQP queue instead of graphite
## Configuration
You have to give basic information about your AMQP server to use
```
-{ host: 'localhost'
-, port: 5672
-, login: 'guest'
-, password: 'guest'
-, vhost: '/'
-} }
+{ amqpHost: 'localhost'
+, amqpPort: 5672
+, amqpLogin: 'guest'
+, amqpPassword: 'guest'
+, amqpVhost: '/'
+, amqpQueue: 'statsd'
+, amqpDefaultExchange: ''
+}
```
## Dependencies
@@ -30,6 +32,5 @@ If you want to contribute:
1. Clone your fork
2. Hack away
3. If you are adding new functionality, document it in the README
-4. Add tests so I don't accidentally break functionality
-5. Push the branch up to GitHub
-6. Send a pull request
+4. Push the branch up to GitHub
+5. Send a pull request
View
@@ -0,0 +1,20 @@
+{
+ "name": "statsd-amqp-backend",
+ "description": "A backend for StatsD to emit stats to an AMQP queue",
+ "author": "Daniel Schauenberg",
+ "repository": {
+ "type": "git",
+ "url": "https://github.com/mrtazz/statsd-amqp-backend.git"
+ },
+ "version": "0.1.0",
+ "dependencies": {
+ "amqp": "0.1.3"
+ },
+ "devDependencies": {
+ "nodeunit": "0.7.x"
+ },
+ "engine": {
+ "node": ">=0.4"
+ },
+ "main": "statsd-amqp-backend/index.js"
+}
@@ -0,0 +1,102 @@
+/*
+ * Flush stats to AMQP
+ *
+ * To enable this backend, include 'statsd-amqp-backend' in the backends
+ * configuration array:
+ *
+ * backends: ['statsd-amqp-backend']
+ *
+ * This backend supports the following config options:
+ *
+ * amqpHost: Hostname of AMQP server.
+ * amqpPort: Port to contact AMQP server at.
+ * amqpLogin: Login for the AMQP server.
+ * amqpPassword: Password for the AMQP server.
+ * amqpVhost: vhost for the AMQP server.
+ * amqpQueue: queue for the AMQP server.
+ * amqpDefaultExchange: default exchange to use.
+ */
+var util = require('util');
+
+var amqp;
+var conn;
+
+var debug;
+var flushInterval;
+
+var amqpQueue;
+
+var amqpStats = {};
+
+var deepCopy = function(obj) {
+ if (Object.prototype.toString.call(obj) === '[object Array]') {
+ var out = [], i = 0, len = obj.length;
+ for ( ; i < len; i++ ) {
+ out[i] = arguments.callee(obj[i]);
+ }
+ return out;
+ }
+ if (typeof obj === 'object') {
+ var out = {}, i;
+ for ( i in obj ) {
+ out[i] = arguments.callee(obj[i]);
+ }
+ return out;
+ }
+ return obj;
+};
+
+var flush_stats = function(ts, metrics)
+{
+ var data = deepCopy(metrics);
+
+ if (debug) {
+ util.log("Publishing metrics: "+JSON.stringify(data));
+ }
+ conn.publish(amqpQueue, JSON.stringify(data));
+
+
+};
+
+
+var backend_status = function(writeCb)
+{
+ for (stat in amqpStats) {
+ writeCb(null, 'amqp', stat, amqpStats[stat]);
+ }
+};
+
+exports.init = function(startup_time, config, events, amq)
+{
+ // take the amqp module as a param for testing
+ amqp = typeof amq !== 'undefined' ? amq : require('amqp');
+ debug = config.debug;
+ // amqp settings
+ amqpQueue = config.amqpQueue || 'statsd';
+ var options = {};
+
+ options.host = config.amqpHost;
+ options.port = config.amqpPort || 5672;
+ if (typeof config.amqpLogin !== 'undefined')
+ {
+ options.login = config.amqpLogin;
+ }
+ if (typeof config.amqpPassword !== 'undefined')
+ {
+ options.password = config.amqpPassword;
+ }
+ options.vhost = config.amqpVhost || '/';
+ options.defaultExchange = config.amqpDefaultExchange || '';
+
+ conn = amqp.createConnection(options);
+
+ amqpStats.last_flush = startup_time;
+ amqpStats.last_exception = startup_time;
+
+ flushInterval = config.flushInterval;
+
+ events.on('flush', flush_stats);
+ events.on('status', backend_status);
+
+ return true;
+};

0 comments on commit c4d1e9d

Please sign in to comment.