index.js
1.95 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
(function () {
'use strict';
const path = require('path');
const onHeaders = require('on-headers');
const pidusage = require('pidusage');
const responseTimes = [];
const osStats = [];
const defaultConfig = {
socketPort: 41338,
path: '/status',
interval: 1,
retention: 30,
};
const gatherOsMetrics = (io) => {
pidusage.stat(process.pid, (err, stat) => {
stat.timestamp = Date.now();
osStats.push(stat);
sendMetrics(io);
});
};
const sendMetrics = (io) => {
osStats.slice(Math.max(osStats.length - defaultConfig.retention, 1));
io.emit('stats', {
osStats: osStats.slice(Math.max(osStats.length - defaultConfig.retention, 1)),
responseTimes
});
};
const middlewareWrapper = (config) => {
if (config === null || config === {} || config === undefined) {
config = defaultConfig;
}
if (config.path === undefined || !config instanceof String) {
config.path = defaultConfig.path;
}
if (config.socketPort === undefined || !config instanceof Number) {
config.socketPort = defaultConfig.socketPort;
}
if (config.interval === undefined || !config instanceof Number) {
config.interval = defaultConfig.interval;
}
const io = require('socket.io')(config.socketPort);
setInterval(() => gatherOsMetrics(io), config.interval * 1000);
io.on('connection', (socket) => {
});
return (req, res, next) => {
const startTime = process.hrtime();
if (req.path === config.path) {
res.sendFile(path.join(__dirname + '/index.html'));
} else {
onHeaders(res, () => {
var diff = process.hrtime(startTime);
var responseTime = diff[0] * 1e3 + diff[1] * 1e-6;
responseTimes.push({
endpoint: req.path,
responseTime,
timestamp: Date.now()
});
});
next();
}
};
};
module.exports = middlewareWrapper;
}());