-
Notifications
You must be signed in to change notification settings - Fork 430
Expand file tree
/
Copy pathosMemoryHeapLinux.js
More file actions
99 lines (84 loc) · 3 KB
/
Copy pathosMemoryHeapLinux.js
File metadata and controls
99 lines (84 loc) · 3 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
// Copyright The Prometheus Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
'use strict';
const { debuglog } = require('node:util');
const Gauge = require('../gauge');
const fs = require('fs');
const debug = debuglog('prom:metrics:os-memory-heap-linux');
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 === undefined ? undefined : [registry];
const namePrefix = config.prefix ?? '';
const 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 (error) {
debug('failed to collect Linux memory metrics: %o', error);
}
},
});
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,
];