forked from wavefrontHQ/nodejs-metrics-wavefront
-
Notifications
You must be signed in to change notification settings - Fork 0
/
scheduled-reporter.js
85 lines (76 loc) · 2.75 KB
/
scheduled-reporter.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
'use strict';
var Counter = require('../metrics').Counter,
Histogram = require('../metrics').Histogram,
Meter = require('../metrics').Meter,
Timer = require('../metrics').Timer,
util = require('util'),
EventEmitter = require('events').EventEmitter;
/**
* Creates a scheduled reporter instance with a given report instance. This is meant to be extended and not used on
* its own, see {@link GraphiteReporter} for an example implementation.
* @param {Report} registry report instance whose metrics to report on.
* @constructor
*/
function ScheduledReporter(registry) {
ScheduledReporter.super_.call(this);
this.registry = registry;
}
util.inherits(ScheduledReporter, EventEmitter);
/**
* Starts the calling {@link ScheduledReporter.report} on a scheduled interval.
* @param {Number} intervalInMs Number How often to report in milliseconds.
*/
ScheduledReporter.prototype.start = function(intervalInMs) {
this.interval = setInterval(this.report.bind(this), intervalInMs);
};
/**
* Stops the reporter if it was previously started.
*/
ScheduledReporter.prototype.stop = function() {
if('interval' in this) {
clearInterval(this.interval);
}
};
/**
* Method that is called on every intervalInMs that was passed into {@link ScheduledReporter.start}. This method
* does nothing and should be overridden by implementers.
*/
ScheduledReporter.prototype.report = function() {
// implemented by children.
};
/**
* Retrieve the metrics associated with the report given to this reporter in a format that's easy to consume
* by reporters. That is an object with separate references for meters, timers counters, and histograms.
* @returns {{meters: Array, timers: Array, counters: Array}}
*/
ScheduledReporter.prototype.getMetrics = function() {
var meters = [];
var timers = [];
var counters = [];
var histograms = [];
var trackedMetrics = this.registry.trackedMetrics;
// Flatten metric name to be namespace.name is has a namespace and separate out metrics
// by type.
for(var namespace in trackedMetrics) {
for(var name in trackedMetrics[namespace]) {
var metric = trackedMetrics[namespace][name];
if(namespace.length > 0) {
metric.name = namespace + '.' + name;
} else {
metric.name = name;
}
var metricType = Object.getPrototypeOf(metric);
if(metricType === Meter.prototype) {
meters.push(metric);
} else if(metricType == Timer.prototype) {
timers.push(metric);
} else if(metricType == Counter.prototype) {
counters.push(metric);
} else if(metricType == Histogram.prototype) {
histograms.push(metric);
}
}
}
return { meters: meters, timers: timers, counters: counters, histograms: histograms };
};
module.exports = ScheduledReporter;