Skip to content

Commit

Permalink
Only consume timing metrics if set
Browse files Browse the repository at this point in the history
  • Loading branch information
adam-cattermole committed May 9, 2024
1 parent 49d4467 commit bc73b67
Showing 1 changed file with 22 additions and 3 deletions.
25 changes: 22 additions & 3 deletions limitador-server/src/metrics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ pub struct Timings {
idle: u64,
busy: u64,
last: Instant,
updated: bool,
}

impl Timings {
Expand All @@ -19,6 +20,7 @@ impl Timings {
idle: 0,
busy: 0,
last: Instant::now(),
updated: false,
}
}
}
Expand All @@ -31,6 +33,7 @@ impl ops::Add for Timings {
busy: self.busy + rhs.busy,
idle: self.idle + rhs.idle,
last: self.last.max(rhs.last),
updated: self.updated || rhs.updated,
}
}
}
Expand Down Expand Up @@ -160,6 +163,7 @@ where
let now = Instant::now();
timings.idle += (now - timings.last).as_nanos() as u64;
timings.last = now;
timings.updated = true;
}
}

Expand All @@ -171,6 +175,7 @@ where
let now = Instant::now();
timings.busy += (now - timings.last).as_nanos() as u64;
timings.last = now;
timings.updated = true;
}
}

Expand Down Expand Up @@ -210,7 +215,14 @@ where
}
// IF we are aggregator call consume function
if let Some(metrics_group) = self.groups.get(name) {
(metrics_group.consumer)(*span_state.group_times.get(name).unwrap())
span_state

Check failure on line 218 in limitador-server/src/metrics.rs

View workflow job for this annotation

GitHub Actions / Clippy

using `Option.and_then(|x| Some(y))`, which is more succinctly expressed as `map(|x| y)`
.group_times
.get(name)
.filter(|&x| x.updated)
.and_then(|x| {
(metrics_group.consumer)(*x);
Some(x)
});
}
}
}
Expand All @@ -228,19 +240,22 @@ mod tests {
idle: 5,
busy: 5,
last: now,
updated: false,
};
let t2 = Timings {
idle: 3,
busy: 5,
last: now,
updated: false,
};
let t3 = t1 + t2;
assert_eq!(
t3,
Timings {
idle: 8,
busy: 10,
last: now
last: now,
updated: false,
}
)
}
Expand All @@ -252,19 +267,22 @@ mod tests {
idle: 5,
busy: 5,
last: now,
updated: false,
};
let t2 = Timings {
idle: 3,
busy: 5,
last: now,
updated: false,
};
t1 += t2;
assert_eq!(
t1,
Timings {
idle: 8,
busy: 10,
last: now
last: now,
updated: false,
}
)
}
Expand All @@ -277,6 +295,7 @@ mod tests {
idle: 5,
busy: 5,
last: Instant::now(),
updated: true,
};
span_state.increment(&group, t1);
assert_eq!(span_state.group_times.get(&group).unwrap().idle, t1.idle);
Expand Down

0 comments on commit bc73b67

Please sign in to comment.