-
Notifications
You must be signed in to change notification settings - Fork 13
/
index.js
89 lines (75 loc) · 2.35 KB
/
index.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
'use strict';
const client = require('prom-client');
// The current tests has circuit names like:
// 'circuit one' (with blank space) and others like
// 3beb8f49-62c0-46e0-b458-dcd4a62d0f48.
// So to avoid "Error: Invalid metric name" we are changing the
// circuit name to pass the tests.
// More details:
// https://prometheus.io/docs/concepts/data_model/#metric-names-and-labels
class PrometheusMetrics {
constructor (options = {}) {
this._registry = options.registry || client.register;
this._metricPrefix = options.metricPrefix || ``;
this._client = client;
this._options = options;
this._counter = new this._client.Counter({
name: `${this._metricPrefix}circuit`,
help: `A count of all circuit' events`,
registers: [this._registry],
labelNames: ['name', 'event']
});
if (this.exposePerformanceMetrics()) {
this._summary = new this._client.Summary({
name: `${this._metricPrefix}circuit_perf`,
help: `A summary of all circuit's events`,
registers: [this._registry],
labelNames: ['name', 'event']
});
}
if (!options.registry) {
this._client.collectDefaultMetrics({
prefix: `${this._metricPrefix}opossum_`,
register: this._registry
});
}
if (options.circuits) {
this.add(options.circuits);
}
}
exposePerformanceMetrics () {
return this._options === undefined ||
this._options.exposePerformanceMetrics === undefined ||
this._options.exposePerformanceMetrics;
}
add (circuits) {
if (!circuits) {
return;
}
circuits = Array.isArray(circuits) ? circuits : [circuits];
circuits.forEach(circuit => {
for (const eventName of circuit.eventNames()) {
circuit.on(eventName, _ => {
this._counter.labels(circuit.name, eventName).inc();
});
if (this.exposePerformanceMetrics() &&
(eventName === 'success' || eventName === 'failure')) {
// not the timeout event because runtime == timeout
circuit.on(eventName, (result, runTime) => {
this._summary.labels(circuit.name, eventName).observe(runTime);
});
}
}
});
}
clear () {
this._registry.clear();
}
metrics () {
return this._registry.metrics();
}
get client () {
return this._client;
}
}
module.exports = PrometheusMetrics;