Created
March 27, 2015 21:12
-
-
Save bonpixel/88d285c5f6aace0cef89 to your computer and use it in GitHub Desktop.
Simple Metrics Server for Node-Measure
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 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