Skip to content

Commit a3f3a5f

Browse files
committed
Add .get() to Family hashmap wrapper
1 parent e35e99d commit a3f3a5f

File tree

1 file changed

+17
-0
lines changed

1 file changed

+17
-0
lines changed

src/metrics/family.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,23 @@ impl<S: Clone + std::hash::Hash + Eq, M, C: MetricConstructor<M>> Family<S, M, C
248248
})
249249
}
250250

251+
/// Access a metric with the given label set, returning None if one
252+
/// does not yet exist.
253+
///
254+
/// ```
255+
/// # use prometheus_client::metrics::counter::{Atomic, Counter};
256+
/// # use prometheus_client::metrics::family::Family;
257+
/// #
258+
/// let family = Family::<Vec<(String, String)>, Counter>::default();
259+
///
260+
/// if let Some(metric) = family.get(&vec![("method".to_owned(), "GET".to_owned())]) {
261+
/// metric.inc();
262+
/// };
263+
/// ```
264+
pub fn get(&self, label_set: &S) -> Option<MappedRwLockReadGuard<M>> {
265+
RwLockReadGuard::try_map(self.metrics.read(), |metrics| metrics.get(label_set)).ok()
266+
}
267+
251268
/// Remove a label set from the metric family.
252269
///
253270
/// Returns a bool indicating if a label set was removed or not.

0 commit comments

Comments
 (0)