This repository was archived by the owner on Feb 18, 2021. It is now read-only.
File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff 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
4453will be used to namespace the stats. Be aware that stats will only be logged
4554once a response has been sent; this means that ` req.statsdKey ` can be
4655set 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) {
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 ) ;
Original file line number Diff line number Diff 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 ( / 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 / ) ;
3131 } ) ;
3232
3333 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 / ) ;
3535 } ) ;
3636
3737 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 / ) ;
4055 } ) ;
4156 } ) ;
4257
You can’t perform that action at this time.
0 commit comments