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 628 : pub async fn observe_async_block_duration_by_result<
10 628 : T,
11 628 : E,
12 628 : F: Future<Output = Result<T, E>>,
13 628 : O: DurationResultObserver,
14 628 : >(
15 628 : observer: &O,
16 628 : block: F,
17 628 : ) -> Result<T, E> {
18 628 : let start = Instant::now();
19 46067 : let result = block.await;
20 628 : let duration = start.elapsed();
21 628 : observer.observe_result(&result, duration);
22 628 : result
23 628 : }
|