-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathapi.js
More file actions
132 lines (123 loc) · 4.6 KB
/
api.js
File metadata and controls
132 lines (123 loc) · 4.6 KB
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
let Prometheus;
try { Prometheus = require('prom-client'); } catch (e) {}
//const Config = require('./config');
const create = () => {
if (!Prometheus) { return {}; }
const Api = {};
const rssMetric = new Prometheus.Gauge({
name: `memory_rss`,
help: 'The amount of space occupied in the main memory device for the process.',
labelNames: ['pid', 'type']
});
const heapTotalMetric = new Prometheus.Gauge({
name: `memory_heap_total`,
help: "Total heap memory.",
labelNames: ['pid', 'type']
});
const heapUsedMetric = new Prometheus.Gauge({
name: `memory_heap_used`,
help: 'Used heap memory.',
labelNames: ['pid', 'type']
});
const externalMetric = new Prometheus.Gauge({
name: `memory_external`,
help: 'Memory usage of C++ objects bound to JavaScript objects managed by V8.',
labelNames: ['pid', 'type']
});
const arrayBufferMetric = new Prometheus.Gauge({
name: `memory_array_buffers`,
help: 'Memory allocated for ArrayBuffers and SharedArrayBuffers.',
labelNames: ['pid', 'type']
});
const cpuUserMetric = new Prometheus.Gauge({
name: `process_cpu_user_seconds_total`,
help: 'Total user CPU time spent in seconds since last measure.',
labelNames: ['pid', 'type']
});
const cpuSystemMetric = new Prometheus.Gauge({
name: `process_cpu_system_seconds_total`,
help: 'Total system CPU time spent in seconds since last measure.',
labelNames: ['pid', 'type']
});
const cpuTotalMetric = new Prometheus.Gauge({
name: `process_cpu_seconds_total`,
help: 'Total user and system CPU time spent in seconds since last measure',
labelNames: ['pid', 'type']
});
const cpuPercentMetric = new Prometheus.Gauge({
name: `process_cpu_percent`,
help: 'Avarage CPU usage (user+system) since last measure',
labelNames: ['pid', 'type']
});
const wsMetric = new Prometheus.Gauge({
name: `active_websockets`,
help: 'Number of active websocket connections',
});
const regMetric = new Prometheus.Gauge({
name: `active_registered_users`,
help: 'Number of registered users online',
});
const chanMetric = new Prometheus.Gauge({
name: `active_channels`,
help: 'Number of active pads',
});
const pidMetrics = Object.values(Prometheus.register._metrics);
const callsMetrics = {};
const clearMetric = (pids, m) => {
const h = m.hashMap;
Object.keys(h).forEach(key => {
let v = h[key];
let pid = v?.labels?.pid;
if (!pid) { return; }
if (!pids.includes(+pid)) {
delete h[key];
}
});
};
const updateProm = (map) => {
// Clear old workers data
const pids = Object.keys(map).map(Number).filter(Boolean);
pidMetrics.forEach(m => {
try { clearMetric(pids, m); } catch (e) {}
});
// Update metrics
Object.keys(map).forEach(pid => {
if (pid === 'calls') { return; }
let val = map[pid];
let type = val.type;
rssMetric.set({pid, type}, val.mem?.rss || 0);
heapTotalMetric.set({pid, type}, val.mem?.heapTotal || 0);
heapUsedMetric.set({pid, type}, val.mem?.heapUsed || 0);
externalMetric.set({pid, type}, val.mem?.external || 0);
arrayBufferMetric.set({pid, type}, val.mem?.arrayBuffers || 0);
cpuUserMetric.set({pid, type}, val.cpu?.user || 0);
cpuSystemMetric.set({pid, type}, val.cpu?.system || 0);
cpuTotalMetric.set({pid, type}, val.cpu?.total || 0);
cpuPercentMetric.set({pid, type}, val.cpu?.percent || 0);
if (type === 'main') {
wsMetric.set(val.other?.ws || 0);
regMetric.set(val.other?.reg || 0);
chanMetric.set(val.other?.channels || 0);
}
});
Object.keys(map.calls || {}).forEach(key => {
let m = callsMetrics[key];
if (!m) {
m = callsMetrics[key] = new Prometheus.Gauge({
name: key,
help: key
});
}
m.set(map.calls[key]);
});
};
Api.onMetricsEndpoint = (res, value) => {
updateProm(value);
Prometheus.register.metrics().then((data) => {
res.set('Content-Type', Prometheus.register.contentType);
res.send(data);
});
};
return Api;
};
module.exports = { create };