Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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]

Expand Down
29 changes: 29 additions & 0 deletions src/metrics/family.rs
Original file line number Diff line number Diff line change
Expand Up @@ -362,6 +362,35 @@ impl<S: Clone + std::hash::Hash + Eq, M, C: MetricConstructor<M>> Family<S, M, C
self.metrics.read().get(label_set).is_some()
}

/// Returns the number of metrics in this family.
///
/// ```
/// # use prometheus_client::metrics::counter::{Atomic, Counter};
/// # use prometheus_client::metrics::family::Family;
/// #
/// let family = Family::<Vec<(String, String)>, 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<S, M>> {
self.metrics.read()
}
Expand Down
Loading