-
Notifications
You must be signed in to change notification settings - Fork 3
/
connect-logger-statsd.js
56 lines (47 loc) · 1.35 KB
/
connect-logger-statsd.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
56
/*!
* connect logger-statsd middleware
*
* @author Pete Fritchman <petef@databits.net>
* MPL 1.1/GPL 2.0/LGPL 2.1
*/
/*
* Required modules.
*/
var StatsD = require("node-statsd").StatsD;
/*
* Generate statsd updates for each request.
*
* - statsd counters named "response.$code" (e.g. response.200)
* - a statsd timer named "response_time"
*
* Options:
* - `host` hostname or IP of statsd server (default "localhost")
* - `port` port of statsd server (default 8125)
* - `prefix` statsd metric name prefix. include the trailing "."
* (default "")
*
* @param {Object} options
* @api public
*/
module.exports = function(options) {
var statsd_client;
if (options == null) {
options = {};
}
options["host"] = options["host"] || "localhost";
options["port"] = options["port"] || 8125;
statsd_client = new StatsD(options["host"], options["port"]);
return function(req, res, next){
req._stats_startTime = new Date;
var end = res.end;
res.end = function(chunk, encoding) {
var reqTime = new Date - req._stats_startTime;
end.call(res, chunk, encoding);
/* request time */
statsd_client.timing(options["prefix"] + "response_time", reqTime);
/* responses by status code */
statsd_client.increment(options["prefix"] + "response." + res.statusCode);
};
next();
};
}