Skip to content

Commit

Permalink
review: rename method to get_or_create_owned()
Browse files Browse the repository at this point in the history
prometheus#244 (comment)

Signed-off-by: katelyn martin <me+cratelyn@katelyn.world>
  • Loading branch information
cratelyn committed Feb 9, 2025
1 parent 5b097b0 commit 7d5e5cc
Showing 1 changed file with 7 additions and 7 deletions.
14 changes: 7 additions & 7 deletions src/metrics/family.rs
Original file line number Diff line number Diff line change
Expand Up @@ -222,15 +222,15 @@ where
/// let family = Family::<Vec<(String, String)>, Counter>::default();
///
/// // Will create and return the metric with label `method="GET"` when first called.
/// family.get_or_create_clone(&vec![("method".to_owned(), "GET".to_owned())]).inc();
/// family.get_or_create_owned(&vec![("method".to_owned(), "GET".to_owned())]).inc();
///
/// // Will return a clone of the existing metric on all subsequent calls.
/// family.get_or_create_clone(&vec![("method".to_owned(), "GET".to_owned())]).inc();
/// family.get_or_create_owned(&vec![("method".to_owned(), "GET".to_owned())]).inc();
/// ```
///
/// Callers wishing to avoid a clone of the metric `M` can call [`Family::get_or_create()`] to
/// return a reference to the metric instead.
pub fn get_or_create_clone(&self, label_set: &S) -> M {
pub fn get_or_create_owned(&self, label_set: &S) -> M {
use std::ops::Deref;

let guard = self.get_or_create(label_set);
Expand Down Expand Up @@ -261,7 +261,7 @@ impl<S: Clone + std::hash::Hash + Eq, M, C: MetricConstructor<M>> Family<S, M, C
/// ```
///
/// NB: This method can cause deadlocks if multiple metrics within this family are read at
/// once. Use [`Family::get_or_create_clone()`] if you would like to avoid this by cloning the
/// once. Use [`Family::get_or_create_owned()`] if you would like to avoid this by cloning the
/// metric `M`.
pub fn get_or_create(&self, label_set: &S) -> MappedRwLockReadGuard<M> {
if let Some(metric) = self.get(label_set) {
Expand Down Expand Up @@ -549,7 +549,7 @@ mod tests {
assert!(non_existent_string.is_none());
}

/// Tests that [`Family::get_or_create_clone()`] does not cause deadlocks.
/// Tests that [`Family::get_or_create_owned()`] does not cause deadlocks.
#[test]
fn counter_family_does_not_deadlock() {
/// A structure we'll place two counters into, within a single expression.
Expand All @@ -560,8 +560,8 @@ mod tests {

let family = Family::<(&str, &str), Counter>::default();
let s = S {
apples: family.get_or_create_clone(&("kind", "apple")),
oranges: family.get_or_create_clone(&("kind", "orange")),
apples: family.get_or_create_owned(&("kind", "apple")),
oranges: family.get_or_create_owned(&("kind", "orange")),
};

s.apples.inc();
Expand Down

0 comments on commit 7d5e5cc

Please sign in to comment.