diff --git a/CHANGELOG.md b/CHANGELOG.md index fa46522..eda1011 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -15,10 +15,13 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - Filter out empty metric families, to match the go client. See [PR 279]. - `Histogram` now exposes `count()` and `sum()` methods when the `test-util` feature is enabled. See [PR 242]. +- `Family` now exposes a `contains()` method when the `test-util` feature + is enabled. See [PR 245]. [PR 279]: https://github.com/prometheus/client_rust/pull/279 [PR 281]: https://github.com/prometheus/client_rust/pull/281 [PR 242]: https://github.com/prometheus/client_rust/pull/242 +[PR 245]: https://github.com/prometheus/client_rust/pull/245 ## [0.24.0] diff --git a/src/metrics/family.rs b/src/metrics/family.rs index 16c923f..d54bec8 100644 --- a/src/metrics/family.rs +++ b/src/metrics/family.rs @@ -341,6 +341,27 @@ impl> Family, Counter>::default(); + /// let get = vec![("method".to_owned(), "GET".to_owned())]; + /// let post = vec![("method".to_owned(), "POST".to_owned())]; + /// + /// // Create the metric with labels `method="GET"`. + /// family.get_or_create(&get).inc(); + /// + /// assert!(family.contains(&get), "a `method=\"GET\"`-labeled metric exists"); + /// assert!(!family.contains(&post), "a `method=\"POST\"`-labeled metric does NOT exist"); + /// ``` + #[cfg(any(test, feature = "test-util"))] + pub fn contains(&self, label_set: &S) -> bool { + self.metrics.read().get(label_set).is_some() + } + pub(crate) fn read(&self) -> RwLockReadGuard<'_, HashMap> { self.metrics.read() }