This repository has been archived by the owner on Dec 8, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 34
/
librato.js
405 lines (337 loc) · 10.5 KB
/
librato.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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
/*
* Flushes stats to Librato Metrics (https://metrics.librato.com).
*
* To enable this backend, include 'statsd-librato-backend' in the
* backends configuration array:
*
* backends: ['statsd-librato-backend']
*
* The backend will read the configuration options from the following
* 'librato' hash defined in the main statsd config file:
*
* librato : {
* email : Email address of your Librato Metrics account (req'd)
* token : API Token of your Librato Metrics accounts (req'd)
* source: Name of a source to use for metrics (optional)
* snapTime : Lock timestamps to this interval in seconds (optional)
* countersAsGauges: Boolean on whether or not all counters should be
* reported as gauges, as was originally done with
* Librato's statsd. Defaults to false which means
* counters will be published as native Metrics
* counters.
* skipInternalMetrics: Boolean of whether to skip publishing of
* internal statsd metrics. This includes all
* metrics beginning with 'statsd.' and the
* metric numStats. Default false -- they
* are published to Librato.
* }
*/
var net = require('net'),
util = require('util'),
url_parse = require('url').parse,
https = require('https'),
http = require('http'),
fs = require('fs');
var debug;
var api, email, token, period, sourceName;
// How long to wait before retrying a failed post, in seconds
var retryDelay = 5;
var libratoStats = {};
var userAgent;
var basicAuthHeader;
var flushInterval;
// Maximum measurements we send in a single post
var maxBatchSize = 500;
// What epoch interval to align time stamps to (defaults to flush interval)
var snapTime = null;
// Previous versions treated counters as gauges, support
// a legacy mode to let users transition.
var countersAsGauges = false;
// Do we skip publishing internal statsd metrics.
//
var skipInternalMetrics = false;
// Statsd counters reset, we want monotonically increasing
// counters.
var libratoCounters = {};
var post_payload = function(options, proto, payload, retry)
{
var req = proto.request(options, function(res) {
// Retry 5xx codes
if (Math.floor(res.statusCode / 100) == 5){
res.on('data', function(d){
var errdata = "HTTP " + res.statusCode + ": " + d;
if (retry){
if (debug) {
util.log("Error sending metrics to Librato: " + errdata);
}
setTimeout(function() {
post_payload(options, proto, payload, false);
}, retryDelay * 1000);
} else {
util.log("Error connecting to Librato!\n" + errdata,"crit");
}
});
}
if (Math.floor(res.statusCode / 100) == 4){
res.on('data', function(d){
var errdata = "HTTP " + res.statusCode + ": " + d;
if (debug) {
util.log("Error sending metrics to Librato: " + errdata);
}
});
}
});
req.write(payload);
req.end();
libratoStats['last_flush'] = Math.round(new Date().getTime() / 1000);
req.on('error', function(errdata) {
if (retry){
setTimeout(function() {
post_payload(options, proto, payload, false);
}, retryDelay * 1000);
} else {
util.log("Error connecting to Librato!\n" + errdata);
}
});
};
var post_metrics = function(ts, gauges, counters)
{
var payload = {gauges: gauges,
counters: counters,
measure_time: ts};
var parsed_host = url_parse(api || 'https://metrics-api.librato.com');
if (sourceName) {
payload.source = sourceName;
}
payload = JSON.stringify(payload);
var options = {
host: parsed_host["hostname"],
port: parsed_host["port"] || 443,
path: '/v1/metrics',
method: 'POST',
headers: {
"Authorization": basicAuthHeader,
"Content-Length": payload.length,
"Content-Type": "application/json",
"User-Agent" : userAgent
}
};
var proto = http;
if ((parsed_host["protocol"] || 'http:').match(/https/)) {
proto = https;
}
post_payload(options, proto, payload, false);
};
var sanitize_name = function(name)
{
return name.replace(/[^-.:_\w]+/, '_').substr(0,255)
};
var timer_gauge_pct = function(timer_name, values, pct, suffix)
{
var thresholdIndex = Math.round(((100 - pct) / 100) * values.length);
var numInThreshold = values.length - thresholdIndex;
if (numInThreshold <= 0) {
return null;
}
var max = values[numInThreshold - 1];
var min = values[0];
var sum = 0;
var sumOfSquares = 0;
for (var i = 0; i < numInThreshold; i++) {
sum += values[i];
sumOfSquares += values[i] * values[i];
}
var name = timer_name;
if (suffix) {
name += suffix;
}
return {
name: sanitize_name(name),
count: numInThreshold,
sum: sum,
sum_squares: sumOfSquares,
min: min,
max: max
};
}
var flush_stats = function librato_flush(ts, metrics)
{
var numStats = 0, statCount;
var key;
var counters = [];
var gauges = [];
var measureTime = ts;
var internalStatsdRe = /^statsd\./;
if (snapTime) {
measureTime = Math.floor(ts / snapTime) * snapTime;
}
var addMeasure = function add_measure(mType, measure, countStat) {
countStat = typeof countStat !== 'undefined' ? countStat : true;
if (mType == 'counter') {
counters.push(measure);
} else {
gauges.push(measure);
}
if (countStat) {
numStats += 1;
}
// Post measurements and clear arrays if past batch size
if (counters.length + gauges.length >= maxBatchSize) {
post_metrics(measureTime, gauges, counters);
gauges = [];
counters = [];
}
};
for (key in metrics.counters) {
if (skipInternalMetrics && (key.match(internalStatsdRe) != null)) {
continue;
}
if (countersAsGauges) {
addMeasure('gauge', { name: sanitize_name(key),
value: metrics.counters[key]});
continue;
}
if (!libratoCounters[key]) {
libratoCounters[key] = {value: metrics.counters[key],
lastUpdate: ts};
} else {
libratoCounters[key].value += metrics.counters[key];
libratoCounters[key].lastUpdate = ts;
}
addMeasure('counter', { name: sanitize_name(key),
value: libratoCounters[key].value});
}
for (key in metrics.timers) {
if (metrics.timers[key].length == 0) {
continue;
}
if (skipInternalMetrics && (key.match(internalStatsdRe) != null)) {
continue;
}
var sortedVals = metrics.timers[key].sort(
function (a, b) { return a - b; }
);
// First build the 100% percentile
var gauge = timer_gauge_pct(key, sortedVals, 100, null);
if (gauge) {
addMeasure('gauge', gauge);
}
// Now for each percentile
var pKey;
for (pKey in metrics.pctThreshold) {
var pct = metrics.pctThreshold[pKey];
gauge = timer_gauge_pct(key, sortedVals, pct, "." + pct);
if (gauge) {
// Percentiles are not counted in numStats
addMeasure('gauge', gauge, false);
}
}
}
for (key in metrics.gauges) {
if (skipInternalMetrics && (key.match(internalStatsdRe) != null)) {
continue;
}
addMeasure('gauge', { name: sanitize_name(key),
value: metrics.gauges[key]});
}
statCount = numStats;
if (!skipInternalMetrics) {
if (countersAsGauges) {
addMeasure('gauge', { name: 'numStats',
value: statCount});
} else {
if (libratoCounters['numStats']) {
libratoCounters['numStats'].value += statCount;
libratoCounters['numStats'].lastUpdate = ts;
} else {
libratoCounters['numStats'] = {value: statCount,
lastUpdate: ts};
}
addMeasure('counter', { name: 'numStats',
value: libratoCounters['numStats'].value});
}
}
if (gauges.length > 0 || counters.length > 0) {
post_metrics(measureTime, gauges, counters);
}
// Delete any counters that were not published, as they
// were deleted.
var toDelete = [];
for (key in libratoCounters) {
if (libratoCounters[key].lastUpdate == ts) {
continue;
}
toDelete.push(key);
}
for (var i = 0; i < toDelete.length; i++) {
delete libratoCounters[toDelete[i]];
}
};
var backend_status = function librato_status(writeCb) {
for (stat in libratoStats) {
writeCb(null, 'librato', stat, libratoStats[stat]);
}
};
var build_basic_auth = function(email, token)
{
return 'Basic ' + new Buffer(email + ':' + token).toString('base64');
}
var build_user_agent = function()
{
var str;
var version = "unknown";
try {
str = fs.readFileSync(__dirname + '/../package.json', 'UTF-8');
json = JSON.parse(str);
version = json['version'];
} catch (e) {
if (debug) {
util.log(e);
}
}
return "statsd-librato-backend/" + version;
}
exports.init = function librato_init(startup_time, config, events)
{
debug = config.debug;
// Prefer config options nested under the top-level 'librato' hash
if (config.librato) {
api = config.librato.api;
email = config.librato.email;
token = config.librato.token;
sourceName = config.librato.source;
countersAsGauges = config.librato.countersAsGauges;
snapTime = config.librato.snapTime;
skipInternalMetrics = config.librato.skipInternalMetrics;
if (config.librato.batchSize) {
maxBatchSize = config.librato.batchSize;
}
} else {
// XXX: Previous versions of the librato/statsd client would read
// all configuration variables from the top-level of the configuration
// file. Detect this and map them to the appropriate configuration
// options.
//
api = config.libratoHost;
email = config.libratoUser;
token = config.libratoApiKey;
sourceName = config.libratoSource;
snapTime = config.libratoSnap;
// Detected old configuration, provide least surprise for users by
// sending counters in as gauges.
countersAsGauges = true;
}
if (!email || !token) {
util.log("Error: Invalid configuration for Librato Metrics backend");
return false;
}
flushInterval = config.flushInterval;
if (!snapTime) {
snapTime = Math.floor(flushInterval / 1000);
}
userAgent = build_user_agent();
basicAuthHeader = build_basic_auth(email, token);
events.on('flush', flush_stats);
events.on('status', backend_status);
return true;
};