forked from siimon/prom-client
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathheapSpacesSizeAndUsed.js
75 lines (60 loc) · 1.58 KB
/
heapSpacesSizeAndUsed.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
'use strict';
const Gauge = require('../gauge');
let v8;
try {
v8 = require('v8');
} catch (e) {
// node version is too old
// probably we can use v8-heap-space-statistics for >=node-4.0.0 and <node-6.0.0
}
const METRICS = ['total', 'used', 'available'];
const NODEJS_HEAP_SIZE = {};
METRICS.forEach(metricType => {
NODEJS_HEAP_SIZE[metricType] = `nodejs_heap_space_size_${metricType}_bytes`;
});
module.exports = registry => {
if (
typeof v8 === 'undefined' ||
typeof v8.getHeapSpaceStatistics !== 'function'
) {
return () => {};
}
const registers = registry ? [registry] : undefined;
const gauges = {};
METRICS.forEach(metricType => {
gauges[metricType] = new Gauge({
name: NODEJS_HEAP_SIZE[metricType],
help: `Process heap space size ${metricType} from node.js in bytes.`,
labelNames: ['space'],
registers
});
});
return () => {
const data = {
total: {},
used: {},
available: {}
};
const now = Date.now();
v8.getHeapSpaceStatistics().forEach(space => {
const spaceName = space.space_name.substr(
0,
space.space_name.indexOf('_space')
);
data.total[spaceName] = space.space_size;
data.used[spaceName] = space.space_used_size;
data.available[spaceName] = space.space_available_size;
gauges.total.set({ space: spaceName }, space.space_size, now);
gauges.used.set({ space: spaceName }, space.space_used_size, now);
gauges.available.set(
{ space: spaceName },
space.space_available_size,
now
);
});
return data;
};
};
module.exports.metricNames = METRICS.map(
metricType => NODEJS_HEAP_SIZE[metricType]
);