@@ -11,6 +11,7 @@ use dropshot::{
11
11
} ;
12
12
use futures:: Future ;
13
13
use http:: StatusCode ;
14
+ use http:: Uri ;
14
15
use oximeter:: histogram:: Histogram ;
15
16
use oximeter:: { Metric , MetricsError , Producer , Sample , Target } ;
16
17
use std:: collections:: BTreeMap ;
@@ -36,6 +37,12 @@ pub struct RequestLatencyHistogram {
36
37
pub latency : Histogram < f64 > ,
37
38
}
38
39
40
+ // Return the route portion of the request, normalized to include a single
41
+ // leading slash and no trailing slashes.
42
+ fn normalized_uri_path ( uri : & Uri ) -> String {
43
+ format ! ( "/{}" , uri. path( ) . trim_end_matches( '/' ) . trim_start_matches( '/' ) )
44
+ }
45
+
39
46
impl RequestLatencyHistogram {
40
47
/// Build a new `RequestLatencyHistogram` with a specified histogram.
41
48
///
@@ -46,7 +53,7 @@ impl RequestLatencyHistogram {
46
53
histogram : Histogram < f64 > ,
47
54
) -> Self {
48
55
Self {
49
- route : request. uri ( ) . path ( ) . to_string ( ) ,
56
+ route : normalized_uri_path ( request. uri ( ) ) ,
50
57
method : request. method ( ) . to_string ( ) ,
51
58
status_code : status_code. as_u16 ( ) . into ( ) ,
52
59
latency : histogram,
@@ -77,7 +84,7 @@ impl RequestLatencyHistogram {
77
84
fn key_for ( request : & RequestInfo , status_code : StatusCode ) -> String {
78
85
format ! (
79
86
"{}:{}:{}" ,
80
- request. uri( ) . path ( ) ,
87
+ normalized_uri_path ( request. uri( ) ) ,
81
88
request. method( ) ,
82
89
status_code. as_u16( )
83
90
)
@@ -240,4 +247,21 @@ mod tests {
240
247
let bins = actual_hist. iter ( ) . collect :: < Vec < _ > > ( ) ;
241
248
assert_eq ! ( bins[ 1 ] . count, 1 ) ;
242
249
}
250
+
251
+ #[ test]
252
+ fn test_normalize_uri_path ( ) {
253
+ const EXPECTED : & str = "/foo/bar" ;
254
+ const TESTS : & [ & str ] = & [
255
+ "/foo/bar" ,
256
+ "/foo/bar/" ,
257
+ "//foo/bar" ,
258
+ "//foo/bar/" ,
259
+ "/foo/bar//" ,
260
+ "////foo/bar/////" ,
261
+ ] ;
262
+ for test in TESTS . iter ( ) {
263
+ println ! ( "{test}" ) ;
264
+ assert_eq ! ( normalized_uri_path( & test. parse( ) . unwrap( ) ) , EXPECTED ) ;
265
+ }
266
+ }
243
267
}
0 commit comments