-
Notifications
You must be signed in to change notification settings - Fork 430
Expand file tree
/
Copy patheventLoopUtilization.js
More file actions
85 lines (64 loc) · 2.39 KB
/
Copy patheventLoopUtilization.js
File metadata and controls
85 lines (64 loc) · 2.39 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
'use strict';
const Summary = require('../summary');
const Histogram = require('../histogram');
const { performance } = require('perf_hooks');
// Reported always.
const NODEJS_EVENTLOOP_UTILIZATION_SUMMARY =
'nodejs_eventloop_utilization_summary';
const NODEJS_EVENTLOOP_UTILIZATION_HISTOGRAM =
'nodejs_eventloop_utilization_histogram';
const DEFAULT_ELU_HISTOGRAM_BUCKETS = [
0.01, 0.05, 0.1, 0.25, 0.5, 0.6, 0.7, 0.75, 0.8, 0.9, 0.95, 0.99, 1,
];
const DEFAULT_ELU_SUMMARY_PERCENTILES = [
0.01, 0.05, 0.5, 0.9, 0.95, 0.99, 0.999,
];
module.exports = (registry, config = {}) => {
const eventLoopUtilization = performance.eventLoopUtilization;
const namePrefix = config.prefix ?? '';
const labels = config.labels ?? {};
const labelNames = Object.keys(labels);
const registers = registry === undefined ? undefined : [registry];
const ageBuckets = config.eventLoopUtilizationAgeBuckets ?? 5;
const maxAgeSeconds = config.eventLoopUtilizationMaxAgeSeconds ?? 60;
const percentiles =
config.eventLoopUtilizationPercentiles ?? DEFAULT_ELU_SUMMARY_PERCENTILES;
const summary = new Summary({
name: namePrefix + NODEJS_EVENTLOOP_UTILIZATION_SUMMARY,
help: 'Ratio of time the event loop is not idling in the event provider to the total time the event loop is running.',
maxAgeSeconds,
ageBuckets,
percentiles,
registers,
labelNames,
});
const buckets =
config.eventLoopUtilizationBuckets ?? DEFAULT_ELU_HISTOGRAM_BUCKETS;
const histogram = new Histogram({
name: namePrefix + NODEJS_EVENTLOOP_UTILIZATION_HISTOGRAM,
help: 'Ratio of time the event loop is not idling in the event provider to the total time the event loop is running.',
buckets,
registers,
labelNames,
});
const intervalTimeout = config.eventLoopUtilizationTimeout ?? 100;
let elu1 = eventLoopUtilization();
let start = process.hrtime();
setInterval(() => {
const elu2 = eventLoopUtilization();
const end = process.hrtime();
const timeMs = (end[0] - start[0]) * 1000 + (end[1] - start[1]) / 1e6;
const value = eventLoopUtilization(elu2, elu1).utilization;
const blockedIntervalsNumber = Math.round(timeMs / intervalTimeout);
for (let i = 0; i < blockedIntervalsNumber; i++) {
summary.observe(value);
histogram.observe(value);
}
elu1 = elu2;
start = end;
}, intervalTimeout).unref();
};
module.exports.metricNames = [
NODEJS_EVENTLOOP_UTILIZATION_SUMMARY,
NODEJS_EVENTLOOP_UTILIZATION_HISTOGRAM,
];