-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
47 lines (41 loc) · 1.12 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
var Pulse = module.exports = function pulse(metric) {
if (!(this instanceof Pulse)) {
return new Pulse(metric);
}
var p = this;
p._metric = metric;
p._measureLoop();
}
Pulse.prototype._measureLoop = function _measureLoop() {
var p = this;
p._metric.count('health.loop.blocked', 0);
blocked(function blockHandler(ms) {
p._metric.count('health.loop.blocked', ms);
});
memory(function memHandler(heapUsed, rss) {
p._metric.gauge('health.memory.heapUsed', heapUsed);
p._metric.gauge('health.memory.rss', rss);
p._metric.gauge('health.process.uptime', process.uptime());
})
}
var blocked = function(fn) {
var start = process.hrtime()
var interval = 100;
setInterval(function(){
var delta = process.hrtime(start);
var nanosec = delta[0] * 1e9 + delta[1];
var ms = nanosec / 1e6;
var n = ms - interval;
if (n > 10) {
fn(Math.round(n))
}
start = process.hrtime();
}, interval).unref();
};
var memory = function(fn) {
var interval = 1000;
setInterval(function() {
var mem = process.memoryUsage();
fn(mem.heapUsed, mem.rss);
}, interval).unref();
}