Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 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]

Expand Down
21 changes: 21 additions & 0 deletions src/metrics/family.rs
Original file line number Diff line number Diff line change
Expand Up @@ -341,6 +341,27 @@ impl<S: Clone + std::hash::Hash + Eq, M, C: MetricConstructor<M>> Family<S, M, C
self.metrics.write().clear()
}

/// Returns `true` if the given label set exists within the metric family.
///
/// ```
/// # use prometheus_client::metrics::counter::{Atomic, Counter};
/// # use prometheus_client::metrics::family::Family;
/// #
/// let family = Family::<Vec<(String, String)>, 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<S, M>> {
self.metrics.read()
}
Expand Down
Loading