-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
102 lines (87 loc) · 2.72 KB
/
index.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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
const pmx = require("pmx");
/**
* This module collect following metrics:
* - pmx:http:latency - request processing time, same as pmx http metric
* - HTTP - RPS counter, same as pmx http metric
* - Network Download/Upload - default pmx network metric
* - Active handles/requests - default pmx network metric
* - memory:rss/heapTotal/heapUsed - memory usage of the Node.js process from process.memoryUsage()
*/
module.exports = function PmxModule() {
pmx.init({
// Enable Network Download/Upload, Active handles/requests metrics
network: true
});
initMemoryUsageMetric();
this.addServerMiddleware(createHttpMetricsMiddleware());
};
module.exports.meta = require("./package.json");
/**
* Init collecting of memory usage metrics:
* - memory:rss - total memory allocated for the process execution
* - memory:heapTotal - total size of the allocated heap
* - memory:heapUsed - actual memory used during the execution of our process
* @param {number} interval - time interval between probes
*/
function initMemoryUsageMetric(interval = 5000) {
const megaMultiplier = 1024 * 1024;
const probe = pmx.probe();
const probeMemoryUsage = () => {
const { rss, heapTotal, heapUsed } = process.memoryUsage();
probe.metric({
name: `memory:rss`,
value: toFixed2(rss / megaMultiplier),
unit: "MB"
});
probe.metric({
name: `memory:heapTotal`,
value: toFixed2(heapTotal / megaMultiplier),
unit: "MB"
});
probe.metric({
name: `memory:heapUsed`,
value: toFixed2(heapUsed / megaMultiplier),
unit: "MB"
});
};
probeMemoryUsage();
setInterval(() => {
probeMemoryUsage();
}, interval);
}
/*
* Creates a middleware for collecting http latency and RPS metrics.
* It reproduce pmx http metric.
*
* `pmx.init({ http: true })` command has to be initialized before Nuxt http server,
* but there are no Nuxt hooks called before Nuxt server initialization, so we can't use built-in pmx http metric.
* We are using pmx metrics names:
* - HTTP - RPS counter
* - pmx:http:latency - request processing time.
*/
function createHttpMetricsMiddleware() {
const probe = pmx.probe();
const rpsMeter = probe.meter({
name: "HTTP",
samples: 1,
unit: "req/s"
});
const latencyHist = probe.histogram({
measurement: "mean",
name: "pmx:http:latency",
unit: "ms"
});
return (req, res, next) => {
rpsMeter.mark();
const start = process.hrtime();
res.once("finish", () => {
const [seconds, nanoseconds] = process.hrtime(start);
const msLatency = toFixed2(seconds * 1000 + nanoseconds / 1e6);
latencyHist.update(msLatency);
});
next();
};
}
function toFixed2(x) {
return Number.parseFloat(x).toFixed(2);
}