racker/dreadnot — pull request #58 by jirwin (2014-01-24 21:00:09+00:00)
Title: feature(graphite): Add a graphite utility

This adds a utility that allows you to output metrics and events to graphite.

Add the following options to your configuration

```json
{
graphite_host: 'graphiteHost', // Hostname for graphite server
graphite_metrics_port: 2003, // Port for metrics submission
graphite_web_port: 80, // Port for graphite web interface(events are submitted this way)
graphite_secure: false // Whether or not to use SSL while submitting events
}
```

It adds two functions:
* `writeMetrics(metrics, callback)`
* metrics is an object containing metric label -> metric data
* `writeEvent(event, tags, callback)`
* `event` is the name of the event
* `tags` is an Array of strings

An example stack would look like this:
```javascript
var exec = require('child_process').exec;

var async = require('async');

var BuildBot = require('util/buildbot').BuildBot;
var knife = require('util/knife');
var misc = require('util/misc');
var sprintf = require('util/sprintf');
var git = require('util/git');
var Graphite = require('util/graphite').Graphite;

exports.get_deployedRevision = function(args, callback) {
git.revParse(this.config.treslek_dir, 'HEAD', function(err, stdout) {
// trim leading and trailing whitespace
callback(null, stdout.replace(/^\s+|\s+$/g, ''));
});
};

exports.task_deploy = function(stack, baton, args, callback) {
var opts = {cwd: stack.config.treslek_dir, env: process.env},
graphite = new Graphite(stack.config.graphite_host,
stack.config.graphite_metrics_port,
stack.config.graphite_web_port,
stack.config.graphite_secure),
deployStartTime = Date.now();

async.series([
function emitStartDeploy(callback) {
baton.log.info('submitting start deploy event to graphite');
graphite.writeEvent("start deploy", ['deploy', 'startDeploy', 'treslek'], callback);
},
function fetch(callback) {
misc.taskSpawn(baton, args, ['git', 'fetch'], opts, callback);
},

function checkout(callback) {
misc.taskSpawn(baton, args, ['git', 'checkout', args.revision], opts, callback);
},

function wait(callback) {
setTimeout(callback, 45000);
},

function emitDeployTime(callback) {
baton.log.info('submitting end deploy event to graphite');
graphite.writeEvent("end deploy", ['deploy', 'endDeploy', 'treslek'], callback);
}
], callback);
};

exports.targets = {
'deploy': [
'task_deploy',
]
};
```

This will result in two events being sent to graphite marking the start and end of the deploy. They can be graphed with the events function like: `drawAsInfinite(events("deploy"))`. You pass the tags of the events you'd like to graph, or use `*` to graph all events. The result is something like:
![screen shot 2014-01-24 at 12 59 47 pm](https://f.cloud.github.com/assets/61822/1998813/7f4d3f0e-853a-11e3-9338-34fe615cdef1.png)