diff --git a/metrics/src/lib.rs b/metrics/src/lib.rs index e94d91670f..4ef9c7efdd 100644 --- a/metrics/src/lib.rs +++ b/metrics/src/lib.rs @@ -59,6 +59,7 @@ impl MetricRegistry { /// For example: /// /// + /// ```text /// # HELP a_counter Counts things /// # TYPE a_counter counter /// a_counter{key="value"} 100 @@ -70,6 +71,7 @@ impl MetricRegistry { /// a_value_recorder_bucket{key="value",le="+Inf"} 1 /// a_value_recorder_sum{key="value"} 100 /// a_value_recorder_count{key="value"} 1 + /// ``` /// pub fn metrics_as_text(&self) -> Vec { let metric_families = self.exporter.registry().gather(); diff --git a/metrics/src/tests.rs b/metrics/src/tests.rs index 1d7f2d048d..b1b6ccf94b 100644 --- a/metrics/src/tests.rs +++ b/metrics/src/tests.rs @@ -1,6 +1,6 @@ use std::sync::Arc; -use snafu::{OptionExt, Snafu}; +use snafu::{ensure, OptionExt, Snafu}; use observability_deps::prometheus::proto::{ Counter as PromCounter, Histogram as PromHistogram, MetricFamily, @@ -68,7 +68,7 @@ impl TestMetricRegistry { .into_iter() .find(|fam| fam.get_name() == name) .context(MetricFamilyNotFoundError { - name: name.to_owned(), + name, metrics: self.registry.metrics_as_str(), })?; Ok(AssertionBuilder::new(family, &self.registry)) @@ -188,19 +188,21 @@ impl<'a> AssertionBuilder<'a> { }); // Can't find metric matching labels - if metric.is_none() { - return Histogram { - c: NoMatchingLabelsError { - name: self.family.get_name().to_owned(), - labels: self.labels.clone(), - metrics: self.registry.metrics_as_str(), - } - .fail(), - family_name: "".to_string(), - metric_dump: "".to_string(), - }; - } - let metric = metric.unwrap(); + let metric = match metric { + Some(metric) => metric, + None => { + return Histogram { + c: NoMatchingLabelsError { + name: self.family.get_name(), + labels: self.labels.clone(), // Maybe `labels: &self.labels` + metrics: self.registry.metrics_as_str(), + } + .fail(), + family_name: "".to_string(), + metric_dump: "".to_string(), + }; + } + }; if !metric.has_histogram() { return Histogram { @@ -237,14 +239,14 @@ impl<'a> Counter<'a> { pub fn eq(self, v: f64) -> Result<(), Error> { let c = self.c?; // return previous errors - if v != c.get_value() { - return FailedMetricAssertionError { + ensure!( + v == c.get_value(), + FailedMetricAssertionError { name: self.family_name, msg: format!("{:?} == {:?} failed", c.get_value(), v), metrics: self.metric_dump, } - .fail(); - } + ); Ok(()) }