simplify metrics into async fn

This commit is contained in:
Mingwei Samuel 2025-01-13 11:31:36 -08:00
parent 9718cebcfc
commit 318ac138c5

View file

@ -3,19 +3,13 @@ use std::future::Future;
use crate::time::Instant;
/// Returns a wrapped future that records the time it takes to complete the future as a histogram metric.
pub fn timed<Fut>(
future: Fut,
operation: &'static str,
route: &'static str,
) -> impl Future<Output = Fut::Output>
pub async fn timed<Fut>(future: Fut, operation: &'static str, route: &'static str) -> Fut::Output
where
Fut: Future,
{
async move {
let start = Instant::now();
let out = future.await;
metrics::histogram!("riot_api", "operation" => operation, "route" => route)
.record(start.elapsed());
out
}
let start = Instant::now();
let out = future.await;
metrics::histogram!("riot_api", "operation" => operation, "route" => route)
.record(start.elapsed());
out
}