From 1d32d563dc268b67c9940c767ab764dcc602c206 Mon Sep 17 00:00:00 2001 From: katelyn martin Date: Thu, 22 Jan 2026 00:00:00 +0000 Subject: [PATCH 1/2] 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 --- CHANGELOG.md | 3 +++ src/metrics/family.rs | 21 +++++++++++++++++++++ 2 files changed, 24 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index fa46522..cdd815e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 a `contains()` method when when the `test-util` feature + is enabled. See [PR 245]. [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 245]: https://github.com/prometheus/client_rust/pull/245 ## [0.24.0] diff --git a/src/metrics/family.rs b/src/metrics/family.rs index 16c923f..d54bec8 100644 --- a/src/metrics/family.rs +++ b/src/metrics/family.rs @@ -341,6 +341,27 @@ 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"); + /// ``` + #[cfg(any(test, feature = "test-util"))] + pub fn contains(&self, label_set: &S) -> bool { + self.metrics.read().get(label_set).is_some() + } + pub(crate) fn read(&self) -> RwLockReadGuard<'_, HashMap> { self.metrics.read() } From d31aa78a5a3dd1d2e8ada5111171a842070f2c74 Mon Sep 17 00:00:00 2001 From: Max Inden Date: Fri, 23 Jan 2026 21:23:05 +0100 Subject: [PATCH 2/2] Update CHANGELOG.md Signed-off-by: Max Inden --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index cdd815e..eda1011 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -15,7 +15,7 @@ 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 a `contains()` method when when the `test-util` feature +- `Family` now exposes a `contains()` method when the `test-util` feature is enabled. See [PR 245]. [PR 279]: https://github.com/prometheus/client_rust/pull/279