Skip to content

Instantly share code, notes, and snippets.

@bonpixel
Created March 27, 2015 21:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bonpixel/88d285c5f6aace0cef89 to your computer and use it in GitHub Desktop.
Save bonpixel/88d285c5f6aace0cef89 to your computer and use it in GitHub Desktop.
Simple Metrics Server for Node-Measure
var config = require('../config/config');
var http = require('http');
var Measured = require('measured');
module.exports = (function(){
var metricsServer = {
// Create default collection of collections
collections : {
gauges: new Measured.createCollection('gauges'),
counters: new Measured.createCollection('counters'),
histograms: new Measured.createCollection('histograms'),
meters: new Measured.createCollection('meters'),
timers: new Measured.createCollection('timers'),
// Helper method to get back json from this collection
toJSON: function(){
var res = {};
for (var prop in this){
if(this.hasOwnProperty(prop) && typeof(this[prop]) !== 'function'){
res[prop] = this[prop].toJSON()[prop];
}
}
return res;
}
},
// Create a server for metrics
server : http.createServer(function(req, res) {
res.writeHead(200, {"Content-Type": "application/json"});
res.end(JSON.stringify(metricsServer.collections.toJSON()));
}).listen(config.metrics.port || 9091),
// Helper method to make adding metrics to parent collections easy
addToCollection: function(collectionName, metricName, metric){
if(!this.collections[collectionName]){
throw new Error('No Collection Regestered to that name');
}
return this.collections[collectionName].register(metricName, metric);
}
};
// return metricsServer object;
return metricsServer;
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment