forked from wavefrontHQ/nodejs-metrics-wavefront
-
Notifications
You must be signed in to change notification settings - Fork 0
/
csv-reporter.js
138 lines (120 loc) · 3.94 KB
/
csv-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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
'use strict';
var ScheduledReporter = require('./scheduled-reporter.js'),
Histogram = require('../metrics').Histogram,
util = require('util'),
fs = require('fs'),
noop = function () {};
/**
* A custom reporter that will create a csv file for each metric that is appended to on the defined reporting interval.
* @param {Report} registry report instance whose metrics to report on.
* @param {String} directory Directory to put csv files in.
* @constructor
*/
function CsvReporter(registry, directory) {
CsvReporter.super_.call(this, registry);
this.directory = directory;
}
util.inherits(CsvReporter, ScheduledReporter);
CsvReporter.prototype.report = function() {
var metrics = this.getMetrics();
var self = this;
var timestamp = (new Date).getTime() / 1000;
metrics.counters.forEach(function(counter) {
self.reportCounter.bind(self)(counter, timestamp);
});
metrics.meters.forEach(function(meter) {
self.reportMeter.bind(self)(meter, timestamp);
});
metrics.timers.forEach(function(timer) {
// Don't log timer if its recorded no metrics.
if(timer.min() != null) {
self.reportTimer.bind(self)(timer, timestamp);
}
});
metrics.histograms.forEach(function(histogram) {
// Don't log histogram if its recorded no metrics.
if(histogram.min != null) {
self.reportHistogram.bind(self)(histogram, timestamp);
}
})
};
CsvReporter.prototype.write = function(timestamp, name, header, line, values) {
var file = util.format('%s/%s.csv', this.directory, name);
// copy params and add line to the beginning and pass as arguments to util.format
// this operates like a quasi 'vsprintf'.
var params = values.slice();
params.unshift(line + "\n");
var data = util.format.apply(util, params);
data = util.format('%d,%s', timestamp, data);
fs.exists(file, function(exists) {
if(!exists) {
// include header if file didn't previously exist
data = util.format("t,%s\n%s", header, data);
}
fs.appendFile(file, data, {}, noop);
});
};
CsvReporter.prototype.reportCounter = function(counter, timestamp) {
var write = this.write.bind(this);
write(timestamp, counter.name, 'count', '%d', [counter.count]);
};
CsvReporter.prototype.reportMeter = function(meter, timestamp) {
var write = this.write.bind(this);
write(timestamp, meter.name,
'count,mean_rate,m1_rate,m5_rate,m15_rate,rate_unit',
'%d,%d,%d,%d,%d,events/%s', [
meter.count,
meter.meanRate(),
meter.oneMinuteRate(),
meter.fiveMinuteRate(),
meter.fifteenMinuteRate(),
'second'
]
);
};
CsvReporter.prototype.reportTimer = function(timer, timestamp) {
var write = this.write.bind(this);
var percentiles = timer.percentiles([.50,.75,.95,.98,.99,.999]);
write(timestamp, timer.name,
'count,max,mean,min,stddev,p50,p75,p95,p98,p99,p999,mean_rate,m1_rate,' +
'm5_rate,m15_rate,rate_unit,duration_unit',
'%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,calls/%s,%s', [
timer.count(),
timer.max(),
timer.mean(),
timer.min(),
timer.stdDev(),
percentiles[.50],
percentiles[.75],
percentiles[.95],
percentiles[.98],
percentiles[.99],
percentiles[.999],
timer.meanRate(),
timer.oneMinuteRate(),
timer.fiveMinuteRate(),
timer.fifteenMinuteRate(),
'second',
'millisecond'
]);
};
CsvReporter.prototype.reportHistogram = function(histogram, timestamp) {
var write = this.write.bind(this);
var percentiles = histogram.percentiles([.50,.75,.95,.98,.99,.999]);
write(timestamp, histogram.name,
'count,max,mean,min,stddev,p50,p75,p95,p98,p99,p999',
'%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d', [
histogram.count,
histogram.max,
histogram.mean(),
histogram.min,
histogram.stdDev(),
percentiles[.50],
percentiles[.75],
percentiles[.95],
percentiles[.98],
percentiles[.99],
percentiles[.999]
]);
};
module.exports = CsvReporter;