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
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i've also added changelog notes summarizing this change.

Original file line number Diff line number Diff line change
Expand Up @@ -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 `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 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 @@ -341,6 +341,35 @@ impl<S: Clone + std::hash::Hash + Eq, M, C: MetricConstructor<M>> Family<S, M, C
self.metrics.write().clear()
}

/// 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"))]
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i've rebased this branch, and added the conditional test-util feature flag introduced in #242!

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