Skip to content

Commit 33f2d27

Browse files
committed
fix: some tests
1 parent 1e19f06 commit 33f2d27

File tree

2 files changed

+38
-6
lines changed

2 files changed

+38
-6
lines changed

src/api/metrics.rs

Lines changed: 37 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use lazy_static::lazy_static;
22
use prometheus::{
33
register_histogram_vec, register_int_counter_vec, Encoder, HistogramTimer, HistogramVec,
4-
IntCounterVec, TextEncoder,
4+
IntCounterVec, Registry, TextEncoder,
55
};
66
use std::convert::Infallible;
77
use std::fmt::Display;
@@ -142,17 +142,17 @@ fn remove_zero_metrics(mut data: &[u8]) -> Result<String, IoError> {
142142
Ok(len) => len,
143143
Err(e) => break Err(e),
144144
};
145-
if res.ends_with(" 0\n") {
145+
if res.ends_with(" 0\n") | res.ends_with(" 0") {
146146
res.truncate(res.len() - len);
147147
}
148148
}
149149
}
150150

151151
// maybe this implementation is wrong as it removes bucket items aswell
152152
// this method leaks internal errors so it should not be public
153-
fn metrics_handler() -> WarpResponse {
153+
fn metrics_handler(registry: &Registry) -> WarpResponse {
154154
let encoder = TextEncoder::new();
155-
let families = prometheus::gather();
155+
let families = registry.gather();
156156

157157
let mut res = vec![];
158158
if let Err(e) = encoder.encode(&families, &mut res) {
@@ -173,7 +173,7 @@ pub(crate) fn metrics(
173173
) -> impl Filter<Extract = impl Reply, Error = Rejection> + Clone + Send + 'static {
174174
warp::path(METRICS_PATH)
175175
.and(warp::get())
176-
.map(metrics_handler)
176+
.map(|| metrics_handler(prometheus::default_registry()))
177177
.and(MetricsConfig::path())
178178
.with(warp::wrap_fn(metrics_wrapper))
179179
.with(trace::request())
@@ -205,3 +205,35 @@ impl HistogramTimerWrapper {
205205
self.0.take()
206206
}
207207
}
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+
}

src/config/records.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ where
2525
where
2626
A: MapAccess<'de>,
2727
{
28-
let mut res = HashMap::new();
28+
let mut res = HashMap::with_capacity(map.size_hint().unwrap_or_default());
2929
while let Some(key) = map.next_key::<&str>()? {
3030
let mut name = Name::from_str(key).map_err(DeError::custom)?;
3131
name.set_fqdn(true);

0 commit comments

Comments
 (0)