From 8068b8a38a967d98b07c657852721161170341a2 Mon Sep 17 00:00:00 2001 From: katelyn martin Date: Tue, 15 Oct 2024 00:00:00 +0000 Subject: [PATCH] feat(metrics/family): `contains()` checks if metrics exist this commit introduces a small accessor to the `Family` metric family type. this new `contains()` method allows callers to check whether or not a metric with the provided set of labels exists. if no metric has been created via e.g. `get_or_create()`, this method will return `false`. Signed-off-by: katelyn martin --- src/metrics/family.rs | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/src/metrics/family.rs b/src/metrics/family.rs index 2f23b198..330e23e9 100644 --- a/src/metrics/family.rs +++ b/src/metrics/family.rs @@ -288,6 +288,26 @@ impl> Family, Counter>::default(); + /// let get = vec![("method".to_owned(), "GET".to_owned())]; + /// let post = vec![("method".to_owned(), "POST".to_owned())]; + /// + /// // Create the metric with labels `method="GET"`. + /// family.get_or_create(&get).inc(); + /// + /// assert!(family.contains(&get), "a `method=\"GET\"`-labeled metric exists"); + /// assert!(!family.contains(&post), "a `method=\"POST\"`-labeled metric does NOT exist"); + /// ``` + pub fn contains(&self, label_set: &S) -> bool { + self.metrics.read().get(label_set).is_some() + } + pub(crate) fn read(&self) -> RwLockReadGuard> { self.metrics.read() }