Permalink
Browse files

Add namespaced metrics to closer resemble coda's implementation

  • Loading branch information...
1 parent 1fd059f commit f9a8b50a31b15366703588365bff3080240d94f8 Mike Ihbe committed Jun 3, 2011
Showing with 42 additions and 25 deletions.
  1. +4 −4 README.md
  2. +1 −1 metrics/counter.js
  3. +16 −10 metrics/histogram.js
  4. +2 −1 metrics/meter.js
  5. +4 −3 metrics/timer.js
  6. +2 −2 package.json
  7. +13 −4 reporting/server.js
View
@@ -40,10 +40,10 @@ var counterForThingA = new metrics.Counter
**Add the metrics to the server**
```javascript
-metricsServer.addMetric('thingA', counterForThingA);
-metricsServer.addMetric('thingB', counterForThingB);
-metricsServer.addMetric('thingC', counterForThingC);
-metricsServer.addMetric('thingD', counterForThingD);
+metricsServer.addMetric('com.co.thingA', counterForThingA);
+metricsServer.addMetric('com.co.thingB', counterForThingB);
+metricsServer.addMetric('com.co.thingC', counterForThingC);
+metricsServer.addMetric('com.co.thingD', counterForThingD);
```
View
@@ -25,5 +25,5 @@ Counter.prototype.clear = function() {
}
Counter.prototype.printObj = function() {
- return {count: this.count};
+ return {type: 'counter', count: this.count};
}
View
@@ -1,10 +1,10 @@
var EDS = require('../stats/exponentially_decaying_sample')
, UniformSample = require('../stats/uniform_sample');
-var DEFAULT_PERCENTILES = [0.001, 0.01, 0.05, 0.1, 0.25, 0.5, 0.75, 0.9, 0.95, 0.99, 0.999];
+var DEFAULT_PERCENTILES = [0.001, 0.01, 0.05, 0.1, 0.25, 0.5, 0.75, 0.9, 0.95, 0.98, 0.99, 0.999];
/*
-*
+* A histogram tracks the distribution of items, given a sample type
*/
var Histogram = module.exports = function Histogram(sample) {
this.sample = sample || new EDS(1028, 0.015);
@@ -92,14 +92,20 @@ Histogram.prototype.values = function() {
Histogram.prototype.printObj = function() {
var percentiles = this.percentiles();
- return {min: this.min,
- max: this.max,
- sum: this.sum,
- variance: this.variance(),
- mean: this.mean(),
- std_dev: this.stdDev(),
- count: this.count,
- percentiles: percentiles};
+ return {
+ type: 'histogram'
+ , min: this.min
+ , max: this.max
+ , sum: this.sum
+ , variance: this.variance()
+ , mean: this.mean()
+ , std_dev: this.stdDev()
+ , count: this.count
+ , median: percentiles[0.5]
+ , p75: percentiles[0.75]
+ , p95: percentiles[0.95]
+ , p99: percentiles[0.99]
+ , p999: percentiles[0.999]};
}
module.exports.createExponentialDecayHistogram = function(size, alpha) { return new Histogram(new EDS((size || 1028), (alpha || 0.015))); };
View
@@ -45,7 +45,8 @@ Meter.prototype.meanRate = function() {
}
Meter.prototype.printObj = function() {
- return {count: this.count
+ return {type: 'meter'
+ , count: this.count
, m1: this.oneMinuteRate()
, m5: this.fiveMinuteRate()
, m15: this.fifteenMinuteRate()
View
@@ -2,7 +2,7 @@ var Meter = require('./meter');
Histogram = require('./histogram')
ExponentiallyDecayingSample = require('../stats/exponentially_decaying_sample');
/*
-*
+* Basically a timer tracks the rate of events and histograms the durations
*/
var Timer = module.exports = function Timer() {
this.meter = new Meter();
@@ -11,7 +11,7 @@ var Timer = module.exports = function Timer() {
this.type = 'timer';
}
-Timer.prototype.update = function(duration) {
+Timer.prototype.update = function(duration) {
this.histogram.update(duration);
this.meter.mark();
}
@@ -34,7 +34,8 @@ Timer.prototype.meanRate = function() { return this.meter.meanRate(); }
Timer.prototype.tick = function() { this.meter.tick(); } // primarily for testing
Timer.prototype.printObj = function() {
- return {duration: this.histogram.printObj()
+ return {type: 'timer'
+ , duration: this.histogram.printObj()
, rate: this.meter.printObj()};
}
View
@@ -1,7 +1,7 @@
{
"name": "metrics",
"description": "A node.js port of Coda Hale's metrics library. In use at Yammer.",
- "version": "0.0.0",
+ "version": "0.1.0",
"repository": {
"type": "git",
"url": "git://github.com/mikejihbe/metrics.git"
@@ -13,4 +13,4 @@
"engines": {
"node": "0.4.x"
}
-}
+}
View
@@ -11,8 +11,11 @@ var Server = module.exports = function Server(port, trackedMetrics) {
if (req.url.match(/^\/metrics/)) {
res.writeHead(200, {'Content-Type': 'application/json'});
var metricsObj = {};
- for (part in self.trackedMetrics) {
- metricsObj[part] = self.trackedMetrics[part].printObj();
+ for (namespace in self.trackedMetrics) {
+ metricsObj[namespace] = {};
+ for (event in self.trackedMetrics[namespace]) {
+ metricsObj[namespace][event] = self.trackedMetrics[namespace][event].printObj();
+ }
}
res.end(JSON.stringify(metricsObj));
} else {
@@ -23,8 +26,14 @@ var Server = module.exports = function Server(port, trackedMetrics) {
}
Server.prototype.addMetric = function(eventName, metric) {
- if (!this.trackedMetrics[eventName]) {
- this.trackedMetrics[eventName] = metric;
+ var namespaces = eventName.split('.')
+ , event = namespaces.pop()
+ , namespace = namespaces.join('.');
+ if (!this.trackedMetrics[namespace]) {
+ this.trackedMetrics[namespace] = {};
+ }
+ if(!this.trackedMetrics[namespace][event]) {
+ this.trackedMetrics[namespace][event] = metric;
}
}

0 comments on commit f9a8b50

Please sign in to comment.