Skip to content

Commit

Permalink
test: ☑️ improve metrics testing
Browse files Browse the repository at this point in the history
  • Loading branch information
rodneylab committed Nov 12, 2024
1 parent c894dbc commit 5eb0ef6
Showing 1 changed file with 7 additions and 2 deletions.
9 changes: 7 additions & 2 deletions src/observability/metrics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,10 @@ mod tests {
assert_eq!(&body[..], b"");
}

/// Internally, `prometheus_parse` uses an [`std::collections::HashMap`] to store labels, which means the order of the
/// values will not be deterministic and so not suitable for snapshot testing. This helper
/// function creates a [`BTreeMap`] from the labels, and will return labels in a deterministic
/// order.
fn sorted_prometheus_metric_labels(labels: &prometheus_parse::Labels) -> BTreeMap<&str, &str> {
labels
.iter()
Expand All @@ -125,7 +129,7 @@ mod tests {
#[should_panic(
expected = "Could not install the Prometheus recorder, there might already be an instance running. It should only be started once.: FailedToSetGlobalRecorder(SetRecorderError { .. })"
)]
async fn metrics_endpoint_warns_if_create_prometheus_caled_more_than_once() {
async fn metrics_endpoint_warns_if_create_prometheus_called_more_than_once() {
// arrange
let _ = Lazy::force(&METRICS);

Expand Down Expand Up @@ -163,7 +167,8 @@ mod tests {
let lines: Vec<_> = body.lines().map(|val| Ok(val.to_owned())).collect();
let Scrape { docs, samples } = prometheus_parse::Scrape::parse(lines.into_iter()).unwrap();
assert!(docs.is_empty());
assert_eq!(samples.len(), 4);
assert!(samples.len() > 3);
assert_eq!(samples.len() % 4, 0);

let metric = "http_requests_duration_seconds";
let Some(sample) = samples.iter().find(|val| val.metric == metric) else {
Expand Down

0 comments on commit 5eb0ef6

Please sign in to comment.