Skip to content

Commit

Permalink
Add .get() to Family hashmap wrapper
Browse files Browse the repository at this point in the history
Signed-off-by: jaydenelliott <[email protected]>
  • Loading branch information
JaydenElliott committed Mar 14, 2023
1 parent e35e99d commit a7affe5
Showing 1 changed file with 17 additions and 0 deletions.
17 changes: 17 additions & 0 deletions src/metrics/family.rs
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,23 @@ impl<S: Clone + std::hash::Hash + Eq, M, C: MetricConstructor<M>> Family<S, M, C
})
}

/// Access a metric with the given label set, returning None if one
/// does not yet exist.
///
/// ```
/// # use prometheus_client::metrics::counter::{Atomic, Counter};
/// # use prometheus_client::metrics::family::Family;
/// #
/// let family = Family::<Vec<(String, String)>, Counter>::default();
///
/// if let Some(metric) = family.get(&vec![("method".to_owned(), "GET".to_owned())]) {
/// metric.inc();
/// };
/// ```
pub fn get(&self, label_set: &S) -> Option<MappedRwLockReadGuard<M>> {
RwLockReadGuard::try_map(self.metrics.read(), |metrics| metrics.get(label_set)).ok()
}

/// Remove a label set from the metric family.
///
/// Returns a bool indicating if a label set was removed or not.
Expand Down

0 comments on commit a7affe5

Please sign in to comment.