This repository was archived by the owner on Feb 18, 2021. It is now read-only.
File tree Expand file tree Collapse file tree 3 files changed +36
-7
lines changed Expand file tree Collapse file tree 3 files changed +36
-7
lines changed Original file line number Diff line number Diff line change @@ -40,7 +40,16 @@ response_time:100|ms
40
40
41
41
### Per route example
42
42
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
44
53
will be used to namespace the stats. Be aware that stats will only be logged
45
54
once a response has been sent; this means that ` req.statsdKey ` can be
46
55
set even after the express-statsd middleware was added to the chain. Here's an
Original file line number Diff line number Diff line change @@ -9,18 +9,23 @@ module.exports = function expressStatsdInit (options) {
9
9
port : 8125
10
10
} , options ) ;
11
11
12
- assert ( options . requestKey , 'express-statsd expects a requestKey' ) ;
13
-
14
12
var client = options . client || new Lynx ( options . host , options . port , options ) ;
15
13
16
14
return function expressStatsd ( req , res , next ) {
17
15
var startTime = new Date ( ) . getTime ( ) ;
18
16
19
17
// Function called on response finish that sends stats to statsd
20
18
function sendStats ( ) {
19
+ var splitUrl = req . url . split ( '/' ) ;
21
20
var key = req [ options . requestKey ] ;
22
21
key = key ? key + '.' : '' ;
23
22
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
+
24
29
// Status Code
25
30
var statusCode = res . statusCode || 'unknown_status' ;
26
31
client . increment ( key + 'status_code.' + statusCode ) ;
Original file line number Diff line number Diff line change @@ -27,16 +27,31 @@ describe('An express server', function () {
27
27
} ) ;
28
28
29
29
it ( 'should send status_code stat' , function ( ) {
30
- expect ( this . messages [ 0 ] ) . to . match ( / s t a t u s _ c o d e \. 2 0 0 : \d \| c / ) ;
30
+ expect ( this . messages [ 0 ] ) . to . match ( / r o o t _ G E T . s t a t u s _ c o d e \. 2 0 0 : \d \| c / ) ;
31
31
} ) ;
32
32
33
33
it ( 'should send response_time stat' , function ( ) {
34
- expect ( this . messages [ 1 ] ) . to . match ( / r e s p o n s e _ t i m e : \d \| m s / ) ;
34
+ expect ( this . messages [ 1 ] ) . to . match ( / r o o t _ G E T . r e s p o n s e _ t i m e : \d \| m s / ) ;
35
35
} ) ;
36
36
37
37
it ( 'should send stats with no key' , function ( ) {
38
- expect ( this . messages [ 0 ] ) . to . match ( / ^ s t a t u s _ c o d e \. 2 0 0 : \d \| c $ / ) ;
39
- expect ( this . messages [ 1 ] ) . to . match ( / ^ r e s p o n s e _ t i m e : \d | m s $ / ) ;
38
+ expect ( this . messages [ 0 ] ) . to . match ( / ^ r o o t _ G E T .s t a t u s _ c o d e \. 2 0 0 : \d \| c $ / ) ;
39
+ expect ( this . messages [ 1 ] ) . to . match ( / ^ r o o t _ G E T .r e s p o n s e _ t i m e : \d | m s $ / ) ;
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 ( / n i n j a _ G E T .r e s p o n s e _ t i m e : \d \| m s / ) ;
40
55
} ) ;
41
56
} ) ;
42
57
You can’t perform that action at this time.
0 commit comments