Line data Source code
1 : //! Helpers for observing duration on `HistogramVec` / `CounterVec` / `GaugeVec` / `MetricVec<T>`.
2 :
3 : use std::{future::Future, time::Instant};
4 :
5 : pub trait DurationResultObserver {
6 : fn observe_result<T, E>(&self, res: &Result<T, E>, duration: std::time::Duration);
7 : }
8 :
9 0 : pub async fn observe_async_block_duration_by_result<
10 0 : T,
11 0 : E,
12 0 : F: Future<Output = Result<T, E>>,
13 0 : O: DurationResultObserver,
14 0 : >(
15 0 : observer: &O,
16 0 : block: F,
17 0 : ) -> Result<T, E> {
18 0 : let start = Instant::now();
19 0 : let result = block.await;
20 0 : let duration = start.elapsed();
21 0 : observer.observe_result(&result, duration);
22 0 : result
23 0 : }
|