Skip to content

Commit

Permalink
Refactor metrics - pass owned Strings
Browse files Browse the repository at this point in the history
  • Loading branch information
adam-cattermole committed Jun 17, 2024
1 parent 69187b1 commit 1a579fb
Showing 1 changed file with 8 additions and 8 deletions.
16 changes: 8 additions & 8 deletions limitador-server/src/metrics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,15 +56,15 @@ struct SpanState {
}

impl SpanState {
fn new(group: &String) -> Self {
fn new(group: String) -> Self {
let mut group_times = HashMap::new();
group_times.insert(group.to_owned(), Timings::new());
group_times.insert(group, Timings::new());
Self { group_times }
}

fn increment(&mut self, group: &str, timings: Timings) {
fn increment(&mut self, group: String, timings: Timings) {
self.group_times
.entry(group.to_string())
.entry(group)
.and_modify(|x| *x += timings)
.or_insert(timings);
}
Expand Down Expand Up @@ -126,7 +126,7 @@ where
.or_insert_with(Timings::new);
} else {
// otherwise create a new SpanState with ourselves
extensions.insert(SpanState::new(&name))
extensions.insert(SpanState::new(name.to_owned()))
}
}

Expand Down Expand Up @@ -190,7 +190,7 @@ where
if self.groups.get(&group).unwrap().records.contains(&name) {
// if we are a record for this group then increment the relevant
// span-local timing and continue to the next group
span_state.increment(&group, timing);
span_state.increment(group, timing);
}
}
}
Expand Down Expand Up @@ -272,14 +272,14 @@ mod tests {
#[test]
fn span_state_increment() {
let group = String::from("group");
let mut span_state = SpanState::new(&group);
let mut span_state = SpanState::new(group.to_owned());
let t1 = Timings {
idle: 5,
busy: 5,
last: Instant::now(),
updated: true,
};
span_state.increment(&group, t1);
span_state.increment(group.to_owned(), t1);
assert_eq!(span_state.group_times.get(&group).unwrap().idle, t1.idle);
assert_eq!(span_state.group_times.get(&group).unwrap().busy, t1.busy);
}
Expand Down

0 comments on commit 1a579fb

Please sign in to comment.