diff --git a/CHANGELOG.md b/CHANGELOG.md index eda1011..733e209 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -17,11 +17,14 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 feature is enabled. See [PR 242]. - `Family` now exposes a `contains()` method when the `test-util` feature is enabled. See [PR 245]. +- `Family` now exposes `len()` and `is_empty()` methods when the + `test-util` feature is enabled. See [PR 246]. [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 +[PR 246]: https://github.com/prometheus/client_rust/pull/246 ## [0.24.0] diff --git a/src/metrics/family.rs b/src/metrics/family.rs index d54bec8..b81d394 100644 --- a/src/metrics/family.rs +++ b/src/metrics/family.rs @@ -362,6 +362,35 @@ impl> Family, Counter>::default(); + /// assert_eq!(family.len(), 0); + /// + /// // Will create the metric with label `method="GET"` on first call and + /// // return a reference. + /// family.get_or_create(&vec![("method".to_owned(), "GET".to_owned())]).inc(); + /// assert_eq!(family.len(), 1); + /// + /// // Clear the family of all label sets. + /// family.clear(); + /// assert_eq!(family.len(), 0); + /// ``` + #[cfg(any(test, feature = "test-util"))] + pub fn len(&self) -> usize { + self.metrics.read().len() + } + + /// Returns `true` if the family contains no metrics. + #[cfg(any(test, feature = "test-util"))] + pub fn is_empty(&self) -> bool { + self.metrics.read().is_empty() + } + pub(crate) fn read(&self) -> RwLockReadGuard<'_, HashMap> { self.metrics.read() }