forked from SimenB/node-prometheus-gc-stats
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
86 lines (70 loc) · 2.16 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
'use strict';
// Credits go to @tcolgate
const Counter = require('prom-client').Counter;
const optional = require('optional');
const gc = optional('@sematext/gc-stats');
const v8Version = process.versions.v8;
const v8MajorVersion = Number(v8Version.split('.')[0]);
const gcTypes =
v8MajorVersion < 10
? {
// https://github.com/nodejs/node/blob/554fa24916c5c6d052b51c5cee9556b76489b3f7/deps/v8/include/v8.h#L6137-L6144
0: 'Unknown',
1: 'Scavenge',
2: 'MarkSweepCompact',
3: 'ScavengeAndMarkSweepCompact',
4: 'IncrementalMarking',
8: 'WeakPhantom',
15: 'All',
}
: {
// https://github.com/nodejs/node/blob/ccd3a42dd9ea2132111610e667ee338618e3b101/deps/v8/include/v8-callbacks.h#L150-L159
0: 'Unknown',
1: 'Scavenge',
2: 'MinorMarkCompact',
4: 'MarkSweepCompact',
8: 'IncrementalMarking',
16: 'ProcessWeakCallbacks',
31: 'All',
};
const noop = () => {};
module.exports = (registry, config = {}) => {
if (typeof gc !== 'function') {
return noop;
}
const registers = registry ? [registry] : undefined;
const labelNames = ['gctype'];
const namePrefix = config.prefix ? config.prefix : '';
const gcCount = new Counter({
name: `${namePrefix}nodejs_gc_runs_total`,
help: 'Count of total garbage collections.',
labelNames,
registers,
});
const gcTimeCount = new Counter({
name: `${namePrefix}nodejs_gc_pause_seconds_total`,
help: 'Time spent in GC Pause in seconds.',
labelNames,
registers,
});
const gcReclaimedCount = new Counter({
name: `${namePrefix}nodejs_gc_reclaimed_bytes_total`,
help: 'Total number of bytes reclaimed by GC.',
labelNames,
registers,
});
let started = false;
return () => {
if (started !== true) {
started = true;
gc().on('stats', stats => {
const gcType = gcTypes[stats.gctype];
gcCount.labels(gcType).inc();
gcTimeCount.labels(gcType).inc(stats.pause / 1e9);
if (stats.diff.usedHeapSize < 0) {
gcReclaimedCount.labels(gcType).inc(stats.diff.usedHeapSize * -1);
}
});
}
};
};