-
Notifications
You must be signed in to change notification settings - Fork 13
/
index.js
55 lines (40 loc) · 1.17 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
'use strict';
var builder = require('./lib/builder');
var header = require('./lib/header');
var chrono = require('./lib/chrono');
var optionsChecker = require('./lib/options.checker');
module.exports = function expressMetrics(options) {
var client;
options = optionsChecker.check(options);
builder.init(options);
client = builder.getClient();
header.init({ header: options.header });
chrono.init({ decimals: options.decimals });
return function (req, res, next) {
chrono.start();
// decorate response#end method from express
var end = res.end;
res.end = function () {
var responseTime = chrono.stop();
header.setResponseTime(res, responseTime);
// call to original express#res.end()
end.apply(res, arguments);
client.send({
route: req.route,
method: req.method,
status: res.statusCode,
time: responseTime
});
};
next();
};
};
module.exports.listen = function listen(port) {
if (builder.getServer()) {
return builder.getMetricsServer();
}
return builder.startServer(port);
};
module.exports.close = function close(callback) {
builder.getServer().stop(callback);
};