index.js
3.45 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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
(function () {
'use strict';
const fs = require('fs');
const path = require('path');
const os = require('os');
const onHeaders = require('on-headers');
const pidusage = require('pidusage');
let io;
const defaultConfig = {
path: '/status',
spans: [{
interval: 1,
retention: 60
}, {
interval: 5,
retention: 60
}, {
interval: 15,
retention: 60
}]
};
const gatherOsMetrics = (io, span) => {
const defaultResponse = {
'2': 0,
'3': 0,
'4': 0,
'5': 0,
count: 0,
mean: 0,
timestamp: Date.now()
};
pidusage.stat(process.pid, (err, stat) => {
const last = span.responses[span.responses.length - 1];
// Convert from B to MB
stat.memory = stat.memory / 1024 / 1024;
stat.load = os.loadavg();
stat.timestamp = Date.now();
span.os.push(stat);
if (!span.responses[0] || last.timestamp + (span.interval * 1000) < Date.now()) span.responses.push(defaultResponse);
if (span.os.length >= span.retention) span.os.shift();
if (span.responses[0] && span.responses.length > span.retention) span.responses.shift();
sendMetrics(io, span);
});
};
const sendMetrics = (io, span) => {
io.emit('stats', {
os: span.os[span.os.length - 2],
responses: span.responses[span.responses.length - 2],
interval: span.interval,
retention: span.retention
});
};
const middlewareWrapper = (config) => {
if (config === null || config === undefined) {
config = defaultConfig;
}
if (config.path === undefined || !config instanceof String) {
config.path = defaultConfig.path;
}
if (config.spans === undefined || !config instanceof Array) {
config.spans = defaultConfig.spans;
}
return (req, res, next) => {
if (io === null || io === undefined) {
io = require('socket.io')(req.socket.server);
io.on('connection', (socket) => {
socket.emit('start', config.spans);
socket.on('change', function() { socket.emit('start', config.spans); });
});
config.spans.forEach((span) => {
span.os = [];
span.responses = [];
setInterval(() => gatherOsMetrics(io, span), span.interval * 1000);
});
}
const startTime = process.hrtime();
if (req.path === config.path) {
res.sendFile(path.join(__dirname + '/index.html'));
} else {
onHeaders(res, () => {
const diff = process.hrtime(startTime);
const responseTime = diff[0] * 1e3 + diff[1] * 1e-6;
const category = Math.floor(res.statusCode / 100);
config.spans.forEach((span) => {
const last = span.responses[span.responses.length - 1];
if (last !== undefined &&
last.timestamp / 1000 + span.interval > Date.now() / 1000) {
last[category]++;
last.count++;
last.mean = last.mean + ((responseTime - last.mean) / last.count);
} else {
span.responses.push({
'2': category === 2 ? 1 : 0,
'3': category === 3 ? 1 : 0,
'4': category === 4 ? 1 : 0,
'5': category === 5 ? 1 : 0,
count: 1,
mean: responseTime,
timestamp: Date.now()
});
}
});
});
next();
}
};
};
module.exports = middlewareWrapper;
}());