1
1
use lazy_static:: lazy_static;
2
2
use prometheus:: {
3
3
register_histogram_vec, register_int_counter_vec, Encoder , HistogramTimer , HistogramVec ,
4
- IntCounterVec , TextEncoder ,
4
+ IntCounterVec , Registry , TextEncoder ,
5
5
} ;
6
6
use std:: convert:: Infallible ;
7
7
use std:: fmt:: Display ;
@@ -142,17 +142,17 @@ fn remove_zero_metrics(mut data: &[u8]) -> Result<String, IoError> {
142
142
Ok ( len) => len,
143
143
Err ( e) => break Err ( e) ,
144
144
} ;
145
- if res. ends_with ( " 0\n " ) {
145
+ if res. ends_with ( " 0\n " ) | res . ends_with ( " 0" ) {
146
146
res. truncate ( res. len ( ) - len) ;
147
147
}
148
148
}
149
149
}
150
150
151
151
// maybe this implementation is wrong as it removes bucket items aswell
152
152
// this method leaks internal errors so it should not be public
153
- fn metrics_handler ( ) -> WarpResponse {
153
+ fn metrics_handler ( registry : & Registry ) -> WarpResponse {
154
154
let encoder = TextEncoder :: new ( ) ;
155
- let families = prometheus :: gather ( ) ;
155
+ let families = registry . gather ( ) ;
156
156
157
157
let mut res = vec ! [ ] ;
158
158
if let Err ( e) = encoder. encode ( & families, & mut res) {
@@ -173,7 +173,7 @@ pub(crate) fn metrics(
173
173
) -> impl Filter < Extract = impl Reply , Error = Rejection > + Clone + Send + ' static {
174
174
warp:: path ( METRICS_PATH )
175
175
. and ( warp:: get ( ) )
176
- . map ( metrics_handler)
176
+ . map ( || metrics_handler ( prometheus :: default_registry ( ) ) )
177
177
. and ( MetricsConfig :: path ( ) )
178
178
. with ( warp:: wrap_fn ( metrics_wrapper) )
179
179
. with ( trace:: request ( ) )
@@ -205,3 +205,35 @@ impl HistogramTimerWrapper {
205
205
self . 0 . take ( )
206
206
}
207
207
}
208
+
209
+ #[ cfg( test) ]
210
+ mod tests {
211
+ use hyper:: body;
212
+ use tracing_test:: traced_test;
213
+
214
+ use super :: { internal_server_error_and_trace, remove_zero_metrics} ;
215
+
216
+ #[ test]
217
+ fn test_remove_zero_metrics ( ) {
218
+ let actual = r#"tcp_closed_connection_counter{endpoint="PROM"} 0
219
+ http_status_counter{method="GET",path="/metrics",status="200"} 4
220
+ tcp_open_connection_counter{endpoint="PROM"} 0"# ;
221
+
222
+ let expected = r#"http_status_counter{method="GET",path="/metrics",status="200"} 4
223
+ "# ;
224
+ let actual = remove_zero_metrics ( actual. as_bytes ( ) ) . unwrap ( ) ;
225
+ assert_eq ! ( expected, actual) ;
226
+ }
227
+
228
+ #[ tokio:: test]
229
+ #[ traced_test]
230
+ async fn test_internal_server_error_and_trace ( ) {
231
+ let error = "This is a error" . to_owned ( ) ;
232
+
233
+ let actual = internal_server_error_and_trace ( & error) ;
234
+ assert ! ( logs_contain( "This is a error" ) ) ;
235
+
236
+ let body = body:: to_bytes ( actual) . await . unwrap ( ) ;
237
+ assert_eq ! ( "This is a error" , body) ;
238
+ }
239
+ }
0 commit comments