Skip to content

Commit bffe835

Browse files
cratelynmxinden
andauthored
feat(metrics/family): 🍬 contains() checks if metrics exist (#245)
* feat(metrics/family): `contains()` checks if metrics exist this commit introduces a small accessor to the `Family<S, M, C>` 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 <git@katelyn.world> * Update CHANGELOG.md Signed-off-by: Max Inden <mail@max-inden.de> --------- Signed-off-by: katelyn martin <git@katelyn.world> Signed-off-by: Max Inden <mail@max-inden.de> Co-authored-by: Max Inden <mail@max-inden.de>
1 parent 77a034b commit bffe835

File tree

2 files changed

+24
-0
lines changed

2 files changed

+24
-0
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,13 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1515
- Filter out empty metric families, to match the go client. See [PR 279].
1616
- `Histogram` now exposes `count()` and `sum()` methods when the `test-util`
1717
feature is enabled. See [PR 242].
18+
- `Family` now exposes a `contains()` method when the `test-util` feature
19+
is enabled. See [PR 245].
1820

1921
[PR 279]: https://github.com/prometheus/client_rust/pull/279
2022
[PR 281]: https://github.com/prometheus/client_rust/pull/281
2123
[PR 242]: https://github.com/prometheus/client_rust/pull/242
24+
[PR 245]: https://github.com/prometheus/client_rust/pull/245
2225

2326
## [0.24.0]
2427

src/metrics/family.rs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -341,6 +341,27 @@ impl<S: Clone + std::hash::Hash + Eq, M, C: MetricConstructor<M>> Family<S, M, C
341341
self.metrics.write().clear()
342342
}
343343

344+
/// Returns `true` if the given label set exists within the metric family.
345+
///
346+
/// ```
347+
/// # use prometheus_client::metrics::counter::{Atomic, Counter};
348+
/// # use prometheus_client::metrics::family::Family;
349+
/// #
350+
/// let family = Family::<Vec<(String, String)>, Counter>::default();
351+
/// let get = vec![("method".to_owned(), "GET".to_owned())];
352+
/// let post = vec![("method".to_owned(), "POST".to_owned())];
353+
///
354+
/// // Create the metric with labels `method="GET"`.
355+
/// family.get_or_create(&get).inc();
356+
///
357+
/// assert!(family.contains(&get), "a `method=\"GET\"`-labeled metric exists");
358+
/// assert!(!family.contains(&post), "a `method=\"POST\"`-labeled metric does NOT exist");
359+
/// ```
360+
#[cfg(any(test, feature = "test-util"))]
361+
pub fn contains(&self, label_set: &S) -> bool {
362+
self.metrics.read().get(label_set).is_some()
363+
}
364+
344365
pub(crate) fn read(&self) -> RwLockReadGuard<'_, HashMap<S, M>> {
345366
self.metrics.read()
346367
}

0 commit comments

Comments
 (0)