forked from siimon/prom-client
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathheapSizeAndUsed.js
62 lines (53 loc) · 1.49 KB
/
heapSizeAndUsed.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
'use strict';
const Gauge = require('../gauge');
const safeMemoryUsage = require('./helpers/safeMemoryUsage');
const NODEJS_HEAP_SIZE_TOTAL = 'nodejs_heap_size_total_bytes';
const NODEJS_HEAP_SIZE_USED = 'nodejs_heap_size_used_bytes';
const NODEJS_EXTERNAL_MEMORY = 'nodejs_external_memory_bytes';
module.exports = registry => {
if (typeof process.memoryUsage !== 'function') {
return () => {};
}
const registers = registry ? [registry] : undefined;
const heapSizeTotal = new Gauge({
name: NODEJS_HEAP_SIZE_TOTAL,
help: 'Process heap size from node.js in bytes.',
registers
});
const heapSizeUsed = new Gauge({
name: NODEJS_HEAP_SIZE_USED,
help: 'Process heap size used from node.js in bytes.',
registers
});
let externalMemUsed;
const usage = safeMemoryUsage();
if (usage && usage.external) {
externalMemUsed = new Gauge({
name: NODEJS_EXTERNAL_MEMORY,
help: 'Nodejs external memory size in bytes.',
registers
});
}
return () => {
// process.memoryUsage() can throw EMFILE errors, see #67
const memUsage = safeMemoryUsage();
if (memUsage) {
const now = Date.now();
heapSizeTotal.set(memUsage.heapTotal, now);
heapSizeUsed.set(memUsage.heapUsed, now);
if (memUsage.external && externalMemUsed) {
externalMemUsed.set(memUsage.external, now);
}
}
return {
total: heapSizeTotal,
used: heapSizeUsed,
external: externalMemUsed
};
};
};
module.exports.metricNames = [
NODEJS_HEAP_SIZE_TOTAL,
NODEJS_HEAP_SIZE_USED,
NODEJS_EXTERNAL_MEMORY
];