@@ -4,7 +4,7 @@ use std::sync::atomic::{AtomicBool, Ordering};
4
4
use std:: sync:: { Arc , Mutex } ;
5
5
use std:: thread;
6
6
7
- use http:: Uri ;
7
+ use http:: uri :: PathAndQuery ;
8
8
use log:: { debug, error} ;
9
9
use time:: { format_description, OffsetDateTime } ;
10
10
use tiny_http:: { ConfigListenAddr , Method , Response , Server } ;
@@ -127,16 +127,17 @@ impl MetricsServer {
127
127
///
128
128
/// The server will only respond synchronously as it blocks until receiving new requests.
129
129
/// Suqsequent calls to this method will return a no-op and not affect the underlying server.
130
- pub fn serve_uri ( & mut self , uri : String ) {
130
+ pub fn serve_uri ( & mut self , path : String ) {
131
131
// Check if we already have a thread running.
132
132
if let Some ( thread) = & self . thread {
133
133
if !thread. is_finished ( ) {
134
+ debug ! ( "metrics server already running, continuing" ) ;
134
135
return ;
135
136
}
136
137
}
137
138
138
- // Ensure URI is valid.
139
- let u = parse_uri ( uri ) ;
139
+ // Ensure path is valid.
140
+ let path = parse_path ( & path ) ;
140
141
141
142
// Invoking clone on Arc produces a new Arc instance, which points to the
142
143
// same allocation on the heap as the source Arc, while increasing a reference count.
@@ -149,11 +150,12 @@ impl MetricsServer {
149
150
for req in s. server . incoming_requests ( ) {
150
151
// Check to see if we should stop handling requests.
151
152
if s. stop . load ( Ordering :: Relaxed ) {
152
- break ;
153
+ debug ! ( "metrics server stopping" ) ;
154
+ return ;
153
155
}
154
156
155
- // Only serve the specified uri path.
156
- if req. url ( ) . to_lowercase ( ) != u {
157
+ // Only serve the specified URI path.
158
+ if req. url ( ) != path {
157
159
let res = Response :: empty ( 404 ) ;
158
160
respond ( req, res) ;
159
161
continue ;
@@ -198,16 +200,18 @@ impl MetricsServer {
198
200
}
199
201
}
200
202
201
- // Validate the provided URI or return the default /metrics on error.
202
- fn parse_uri ( mut uri : String ) -> String {
203
- if !uri. starts_with ( '/' ) {
204
- uri. insert ( 0 , '/' ) ;
205
- }
206
-
207
- match Uri :: from_str ( & uri) {
208
- Ok ( u) => u. path ( ) . to_lowercase ( ) ,
203
+ // Validate the provided URL path, or return the default path on error.
204
+ fn parse_path ( uri : & str ) -> String {
205
+ match PathAndQuery :: from_str ( uri) {
206
+ Ok ( pq) => {
207
+ let mut path = pq. path ( ) . to_lowercase ( ) ;
208
+ if !path. starts_with ( '/' ) {
209
+ path. insert ( 0 , '/' ) ;
210
+ }
211
+ path
212
+ }
209
213
Err ( _) => {
210
- error ! ( "invalid uri, defaulting to {}" , DEFAULT_METRICS_PATH ) ;
214
+ error ! ( "invalid uri, defaulting to {DEFAULT_METRICS_PATH}" ) ;
211
215
DEFAULT_METRICS_PATH . to_string ( )
212
216
}
213
217
}
@@ -242,21 +246,19 @@ mod tests {
242
246
use super :: * ;
243
247
244
248
#[ test]
245
- fn test_parse_uri ( ) {
249
+ fn test_parse_path ( ) {
246
250
let expected_default = DEFAULT_METRICS_PATH . to_string ( ) ;
247
- let expected_valid = "/v1 /metrics" . to_string ( ) ;
251
+ let expected_valid = "/debug /metrics" . to_string ( ) ;
248
252
249
253
// Invalid.
250
- assert_eq ! ( parse_uri( "Hello, World!" . to_string( ) ) , expected_default) ;
251
- // No slash prefix.
252
- assert_eq ! ( parse_uri( "metrics" . to_string( ) ) , expected_default) ;
253
- // Leading slash prefix.
254
- assert_eq ! ( parse_uri( "/metrics" . to_string( ) ) , expected_default) ;
254
+ assert_eq ! ( parse_path( "Hello, World!" ) , expected_default) ;
255
255
// Whitespace.
256
- assert_eq ! ( parse_uri ( " metr ics " . to_string ( ) ) , expected_default) ;
257
- // Uppercase .
258
- assert_eq ! ( parse_uri ( "METRICS" . to_string ( ) ) , expected_default) ;
256
+ assert_eq ! ( parse_path ( " metr ics " ) , expected_default) ;
257
+ // Non-ASCII .
258
+ assert_eq ! ( parse_path ( "mëtrîcs" ) , expected_default) ;
259
259
// Valid.
260
- assert_eq ! ( parse_uri( "/v1/metrics" . to_string( ) ) , expected_valid) ;
260
+ assert_eq ! ( parse_path( "/debug/metrics" ) , expected_valid) ;
261
+ assert_eq ! ( parse_path( "debug/metrics" ) , expected_valid) ;
262
+ assert_eq ! ( parse_path( "DEBUG/METRICS" ) , expected_valid) ;
261
263
}
262
264
}
0 commit comments