Skip to content
This repository was archived by the owner on Feb 18, 2021. It is now read-only.

Commit 4030092

Browse files
committed
Add default behavior so that the response_time timing reports
the top level URI in addition to the method
1 parent 4e3ee7d commit 4030092

File tree

3 files changed

+36
-7
lines changed

3 files changed

+36
-7
lines changed

README.md

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,16 @@ response_time:100|ms
4040

4141
### Per route example
4242

43-
However, it's **highly recommended** that you set `req.statsdKey` which
43+
The default behavior reports statsd metrics based on the top-level URI.
44+
``` js
45+
https://www.domain.com/ --> root_GET.status_code.200:1|c
46+
https://www.domain.com/ --> root_GET.response_time.100|ms
47+
48+
https://www.domain.com/something --> something_GET.status_code.200:1|c
49+
https://www.domain.com/something --> something_GET.response_time.100|ms
50+
```
51+
52+
However, if you want to override this behavior you can set `req.statsdKey` which
4453
will be used to namespace the stats. Be aware that stats will only be logged
4554
once a response has been sent; this means that `req.statsdKey` can be
4655
set even after the express-statsd middleware was added to the chain. Here's an

lib/express-statsd.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,18 +9,23 @@ module.exports = function expressStatsdInit (options) {
99
port: 8125
1010
}, options);
1111

12-
assert(options.requestKey, 'express-statsd expects a requestKey');
13-
1412
var client = options.client || new Lynx(options.host, options.port, options);
1513

1614
return function expressStatsd (req, res, next) {
1715
var startTime = new Date().getTime();
1816

1917
// Function called on response finish that sends stats to statsd
2018
function sendStats() {
19+
var splitUrl = req.url.split('/');
2120
var key = req[options.requestKey];
2221
key = key ? key + '.' : '';
2322

23+
// Report timing based on top-level URI as default behavior
24+
if (!key && splitUrl.length > 0) {
25+
var topLevelURI = req.url.split('/')[1] || 'root';
26+
key = topLevelURI + '_' + req.method + '.';
27+
}
28+
2429
// Status Code
2530
var statusCode = res.statusCode || 'unknown_status';
2631
client.increment(key + 'status_code.' + statusCode);

test/express-statsd.js

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,16 +27,31 @@ describe('An express server', function () {
2727
});
2828

2929
it('should send status_code stat', function () {
30-
expect(this.messages[0]).to.match(/status_code\.200:\d\|c/);
30+
expect(this.messages[0]).to.match(/root_GET.status_code\.200:\d\|c/);
3131
});
3232

3333
it('should send response_time stat', function () {
34-
expect(this.messages[1]).to.match(/response_time:\d\|ms/);
34+
expect(this.messages[1]).to.match(/root_GET.response_time:\d\|ms/);
3535
});
3636

3737
it('should send stats with no key', function () {
38-
expect(this.messages[0]).to.match(/^status_code\.200:\d\|c$/);
39-
expect(this.messages[1]).to.match(/^response_time:\d|ms$/);
38+
expect(this.messages[0]).to.match(/^root_GET.status_code\.200:\d\|c$/);
39+
expect(this.messages[1]).to.match(/^root_GET.response_time:\d|ms$/);
40+
});
41+
});
42+
43+
describe('receiving a request to /ninja', function () {
44+
utils.runServer(1337, [
45+
expressStatsd(),
46+
function (req, res) {
47+
res.send(200);
48+
}
49+
]);
50+
utils.saveRequest('http://localhost:1337/ninja');
51+
utils.getStatsdMessages();
52+
53+
it('should send ninja_get.response_time stat', function () {
54+
expect(this.messages[1]).to.match(/ninja_GET.response_time:\d\|ms/);
4055
});
4156
});
4257

0 commit comments

Comments
 (0)