-
Notifications
You must be signed in to change notification settings - Fork 378
/
osMemoryHeapLinux.js
82 lines (70 loc) · 2.28 KB
/
osMemoryHeapLinux.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
'use strict';
const Gauge = require('../gauge');
const fs = require('fs');
const values = ['VmSize', 'VmRSS', 'VmData'];
const PROCESS_RESIDENT_MEMORY = 'process_resident_memory_bytes';
const PROCESS_VIRTUAL_MEMORY = 'process_virtual_memory_bytes';
const PROCESS_HEAP = 'process_heap_bytes';
function structureOutput(input) {
return input.split('\n').reduce((acc, string) => {
if (!values.some(value => string.startsWith(value))) {
return acc;
}
const split = string.split(':');
// Get the value
let value = split[1].trim();
// Remove trailing ` kb`
value = value.substr(0, value.length - 3);
// Make it into a number in bytes bytes
value = Number(value) * 1024;
acc[split[0]] = value;
return acc;
}, {});
}
module.exports = (registry, config = {}) => {
const registers = registry ? [registry] : undefined;
const namePrefix = config.prefix ? config.prefix : '';
const labels = config.labels ? config.labels : {};
const labelNames = Object.keys(labels);
const residentMemGauge = new Gauge({
name: namePrefix + PROCESS_RESIDENT_MEMORY,
help: 'Resident memory size in bytes.',
registers,
labelNames,
// Use this one metric's `collect` to set all metrics' values.
collect() {
try {
// Sync I/O is often problematic, but /proc isn't really I/O, it
// a virtual filesystem that maps directly to in-kernel data
// structures and never blocks.
//
// Node.js/libuv do this already for process.memoryUsage(), see:
// - https://github.com/libuv/libuv/blob/a629688008694ed8022269e66826d4d6ec688b83/src/unix/linux-core.c#L506-L523
const stat = fs.readFileSync('/proc/self/status', 'utf8');
const structuredOutput = structureOutput(stat);
residentMemGauge.set(labels, structuredOutput.VmRSS);
virtualMemGauge.set(labels, structuredOutput.VmSize);
heapSizeMemGauge.set(labels, structuredOutput.VmData);
} catch {
// noop
}
},
});
const virtualMemGauge = new Gauge({
name: namePrefix + PROCESS_VIRTUAL_MEMORY,
help: 'Virtual memory size in bytes.',
registers,
labelNames,
});
const heapSizeMemGauge = new Gauge({
name: namePrefix + PROCESS_HEAP,
help: 'Process heap size in bytes.',
registers,
labelNames,
});
};
module.exports.metricNames = [
PROCESS_RESIDENT_MEMORY,
PROCESS_VIRTUAL_MEMORY,
PROCESS_HEAP,
];