-
Notifications
You must be signed in to change notification settings - Fork 4
/
backend.js
101 lines (90 loc) · 2.67 KB
/
backend.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
/*jslint node: true, indent: 2, plusplus: true */
var monitis = require("./api/api.js");
var util = require('./api/util.js');
function print_body(body) {
'use strict';
util.debug(body);
}
function missing_monitor_cb(name, type, body) {
'use strict';
// When recording a metric encounters a monitor that doesn't exist, add it.
// Note that we lose one datapoint, since we don't retry the post.
return function (body) {
if (body.status === 'no such monitor') {
console.log("Adding missing custom monitor: ", name, ":", type);
monitis.add_statsd_monitor(name, type, print_body);
}
};
}
// flush_stats
function monitis_flush(ts, metrics) {
'use strict';
var key, // iteration over members of metrics
name, // iteration over metrics.raws
value,
sum, // iteration over metrics.timers
avg,
times,
i; // iteration in for loop
console.log("Called flush_stats");
util.debug(metrics);
for (key in metrics.counters) {
if (metrics.counters.hasOwnProperty(key)) {
monitis.add_result_by_name(key,
{count: metrics.counters[key]}, missing_monitor_cb(key, 'counter'));
}
}
for (key in metrics.gauges) {
if (metrics.gauges.hasOwnProperty(key)) {
monitis.add_result_by_name(key,
{gauge: metrics.counters[key]}, missing_monitor_cb(key, 'gauge'));
}
}
for (key in metrics.raws) {
if (metrics.raws.hasOwnProperty(key)) {
name = metrics.raws[key][0];
value = metrics.raws[key][1];
monitis.add_result_by_name(name,
{raw: value}, missing_monitor_cb(name, 'raw'));
}
}
for (key in metrics.timers) {
if (metrics.timers.hasOwnProperty(key)) {
sum = 0;
avg = 0;
times = metrics.timers[key];
for (i = 0; i < times.length; i++) {
sum += times[i];
}
if (times.length > 0) {
avg = sum / times.length;
}
monitis.add_result_by_name(key,
{sum: sum, avg: avg}, missing_monitor_cb(key, 'timer'));
util.debug(metrics.timers);
}
}
}
// backend_status
function monitis_status(writeCb) {
'use strict';
console.log("Called backend_status");
}
exports.init = function monitis_init(startup_time, config, events) {
'use strict';
var debug, flushInterval;
debug = config.debug;
flushInterval = config.flushInterval;
// read monitis config
global.monitis = config.monitis;
if (!config.monitis.host) {
global.monitis.host = 'monitis.com';
}
if (!config.monitis.path) {
global.monitis.path = '/customMonitorApi';
}
util.debug("global.monitis.debug is ", global.monitis.debug);
events.on('flush', monitis_flush);
events.on('status', monitis_status);
return true;
};