Skip to content

Commit

Permalink
feat(metrics/family): len() returns the number of metrics
Browse files Browse the repository at this point in the history
this commit introduces a `len()` method to `Family<S, M, C>`, which
returns the number of series within a metric family.

see also prometheus#245, which allows callers to check if a family `contains()` a
given label set.

Signed-off-by: katelyn martin <[email protected]>
  • Loading branch information
cratelyn committed Nov 18, 2024
1 parent 12923ca commit 27a2e49
Showing 1 changed file with 22 additions and 0 deletions.
22 changes: 22 additions & 0 deletions src/metrics/family.rs
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,28 @@ 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);
/// ```
pub fn len(&self) -> usize {

Check failure on line 309 in src/metrics/family.rs

View workflow job for this annotation

GitHub Actions / Clippy

struct `Family` has a public `len` method, but no `is_empty` method
self.metrics.read().len()
}

pub(crate) fn read(&self) -> RwLockReadGuard<HashMap<S, M>> {
self.metrics.read()
}
Expand Down

0 comments on commit 27a2e49

Please sign in to comment.