Created
April 23, 2014 11:14
-
-
Save hanshuebner/11211196 to your computer and use it in GitHub Desktop.
Create high-level varnish events in librato
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var readline = require('readline'); | |
var spawn = require('child_process').spawn; | |
var request = require('request'); | |
var config = { | |
interval: 10000, | |
email: '[email protected]', | |
apitoken: 'the-librato-readonly-api-token' | |
}; | |
var varnishncsa = spawn('varnishncsa', ['-F', '%{Varnish:handling}x %m %{Host}i %U %s']); | |
var varnish_output = readline.createInterface({ | |
input: varnishncsa.stdout, | |
output: process.stdout, | |
terminal: false | |
}); | |
var stats = {}; | |
function flush_stats() { | |
var gauges = []; | |
for (var key in stats) { | |
gauges.push({ name: key, | |
value: stats[key]}); | |
stats[key] = 0; | |
} | |
request.post({ url: 'https://metrics-api.librato.com/v1/metrics', | |
json: { measure_time: (new Date().getTime()) / 1000, | |
gauges: gauges, | |
source: "varnish-highlevel-stats" | |
}, | |
auth: { user: config.email, | |
pass: config.apitoken } | |
}, | |
function (error, response, body) { | |
if (error) { | |
console.log('error:', error, 'body:', body); | |
} | |
}); | |
} | |
setInterval(flush_stats, config.interval); | |
function inc(key) { | |
stats[key] = (stats[key] || 0) + 1; | |
} | |
function process_varnish_request(varnish_handling, method, host, url, status) { | |
if (url.match(/^\/(static|beta|assets|frontend)\//)) { | |
// this is just noise | |
return; | |
} | |
if (varnish_handling != '-') { | |
inc("varnish." + varnish_handling); | |
} | |
inc("host." + host.replace(/:.*/, "")); | |
inc("status." + status); | |
if (url == "/widget/api/v1") { | |
inc('widget.request'); | |
} else if (url == '/api/v2/search') { | |
inc('api.search'); | |
} else if (url == '/search/') { | |
inc('web.search.start'); | |
} else { | |
url.replace(/^\/search\/\d+\/(.*)/, function (match, what) { | |
inc('web.search.' + what.replace(/\//g, '.')); | |
}); | |
} | |
} | |
varnish_output.on('line', function (line) { | |
process_varnish_request.apply(null, line.split(" ")); | |
}); | |
varnishncsa.on('exit', function (e) { | |
console.log('varnishncsa unexpectedly exited with exit code', e); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment