-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathstats.js
More file actions
59 lines (48 loc) · 1.6 KB
/
Copy pathstats.js
File metadata and controls
59 lines (48 loc) · 1.6 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
/**
* based on express-stats
* base code: https://raw.githubusercontent.com/chieffancypants/express-stats/master/index.js
*/
// Averages the sum of all elements in an array
var avg = function(arr) {
if (arr.length === 0) { return; }
var sum = arr.reduce(function(prev, curr) { return prev + curr; });
return sum / arr.length;
};
module.exports = function (options) {
var stats = {responses: []};
var defaultOptions = {
url : '/health',
cacheSize : 20,
appVersion : undefined,
statusCheck: function() {}
};
// extend the options with defaults:
options = options || {};
for (var i in defaultOptions) {
if (!options[i] && !options.hasOwnProperty(i)) {
options[i] = defaultOptions[i];
}
}
return function(req, res, next) {
if (req.url == options.url) {
res.send({
appVersion : options.appVersion,
status : options.statusCheck(req, res),
pid : process.pid,
uptimeSecs : process.uptime(),
avgResponseTimeMs : avg(stats.responses),
memory : process.memoryUsage()
});
}
else {
res.on('finish', function(data) {
var time = parseInt( res.get('x-response-time'), 10);
if (stats.responses.length == options.cacheSize) {
stats.responses.shift();
}
stats.responses.push(time);
});
return next();
}
};
};