Skip to content

Commit 27a2e49

Browse files
committed
feat(metrics/family): len() returns the number of metrics
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]>
1 parent 12923ca commit 27a2e49

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

src/metrics/family.rs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -288,6 +288,28 @@ impl<S: Clone + std::hash::Hash + Eq, M, C: MetricConstructor<M>> Family<S, M, C
288288
self.metrics.write().clear()
289289
}
290290

291+
/// Returns the number of metrics in this family.
292+
///
293+
/// ```
294+
/// # use prometheus_client::metrics::counter::{Atomic, Counter};
295+
/// # use prometheus_client::metrics::family::Family;
296+
/// #
297+
/// let family = Family::<Vec<(String, String)>, Counter>::default();
298+
/// assert_eq!(family.len(), 0);
299+
///
300+
/// // Will create the metric with label `method="GET"` on first call and
301+
/// // return a reference.
302+
/// family.get_or_create(&vec![("method".to_owned(), "GET".to_owned())]).inc();
303+
/// assert_eq!(family.len(), 1);
304+
///
305+
/// // Clear the family of all label sets.
306+
/// family.clear();
307+
/// assert_eq!(family.len(), 0);
308+
/// ```
309+
pub fn len(&self) -> usize {
310+
self.metrics.read().len()
311+
}
312+
291313
pub(crate) fn read(&self) -> RwLockReadGuard<HashMap<S, M>> {
292314
self.metrics.read()
293315
}

0 commit comments

Comments
 (0)