From 27a2e491fa95f7b4872fe926b9234dd9f7e78037 Mon Sep 17 00:00:00 2001 From: katelyn martin Date: Tue, 15 Oct 2024 00:00:00 +0000 Subject: [PATCH 1/2] feat(metrics/family): `len()` returns the number of metrics this commit introduces a `len()` method to `Family`, which returns the number of series within a metric family. see also #245, which allows callers to check if a family `contains()` a given label set. Signed-off-by: katelyn martin --- src/metrics/family.rs | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/src/metrics/family.rs b/src/metrics/family.rs index 2f23b198..092378af 100644 --- a/src/metrics/family.rs +++ b/src/metrics/family.rs @@ -288,6 +288,28 @@ 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); + /// ``` + pub fn len(&self) -> usize { + self.metrics.read().len() + } + pub(crate) fn read(&self) -> RwLockReadGuard> { self.metrics.read() } From c301225605b5d0464d644492cffc382c3c8d8e77 Mon Sep 17 00:00:00 2001 From: katelyn martin Date: Tue, 15 Oct 2024 00:00:00 +0000 Subject: [PATCH 2/2] lint(metrics/family): add `is_empty()` ```shell $ cargo clippy --all-features --all-targets --tests Compiling prometheus-client v0.23.0 (/path/to/prometheus-client) warning: struct `Family` has a public `len` method, but no `is_empty` method --> src/metrics/family.rs:309:5 | 309 | pub fn len(&self) -> usize { | ^^^^^^^^^^^^^^^^^^^^^^^^^^ | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#len_without_is_empty = note: `#[warn(clippy::len_without_is_empty)]` on by default warning: `prometheus-client` (lib) generated 1 warning warning: `prometheus-client` (lib test) generated 1 warning (1 duplicate) Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.63s ``` Signed-off-by: katelyn martin --- src/metrics/family.rs | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/metrics/family.rs b/src/metrics/family.rs index 092378af..8f4f4721 100644 --- a/src/metrics/family.rs +++ b/src/metrics/family.rs @@ -310,6 +310,11 @@ impl> Family bool { + self.metrics.read().is_empty() + } + pub(crate) fn read(&self) -> RwLockReadGuard> { self.metrics.read() }