From 2a5609b292de019085f93a79b7b73f7a8341bf51 Mon Sep 17 00:00:00 2001 From: Alex Auvolat Date: Tue, 22 Feb 2022 14:52:41 +0100 Subject: Add metrics to API endpoint --- src/util/metrics.rs | 29 ++++++++++++++++++++++------- 1 file changed, 22 insertions(+), 7 deletions(-) (limited to 'src/util') diff --git a/src/util/metrics.rs b/src/util/metrics.rs index b3b1fc3c..cd5aa182 100644 --- a/src/util/metrics.rs +++ b/src/util/metrics.rs @@ -2,26 +2,40 @@ use std::time::SystemTime; use futures::{future::BoxFuture, Future, FutureExt}; -use opentelemetry::{KeyValue, metrics::*}; +use opentelemetry::{metrics::*, KeyValue}; pub trait RecordDuration<'a>: 'a { type Output; - fn record_duration(self, r: &'a ValueRecorder, attributes: &'a [KeyValue]) -> BoxFuture<'a, Self::Output>; + fn record_duration( + self, + r: &'a ValueRecorder, + attributes: &'a [KeyValue], + ) -> BoxFuture<'a, Self::Output>; fn bound_record_duration(self, r: &'a BoundValueRecorder) -> BoxFuture<'a, Self::Output>; } impl<'a, T, O> RecordDuration<'a> for T -where T: Future + Send + 'a { +where + T: Future + Send + 'a, +{ type Output = O; - fn record_duration(self, r: &'a ValueRecorder, attributes: &'a [KeyValue]) -> BoxFuture<'a, Self::Output> { + fn record_duration( + self, + r: &'a ValueRecorder, + attributes: &'a [KeyValue], + ) -> BoxFuture<'a, Self::Output> { async move { let request_start = SystemTime::now(); let res = self.await; - r.record(request_start.elapsed().map_or(0.0, |d| d.as_secs_f64()), attributes); + r.record( + request_start.elapsed().map_or(0.0, |d| d.as_secs_f64()), + attributes, + ); res - }.boxed() + } + .boxed() } fn bound_record_duration(self, r: &'a BoundValueRecorder) -> BoxFuture<'a, Self::Output> { @@ -30,6 +44,7 @@ where T: Future + Send + 'a { let res = self.await; r.record(request_start.elapsed().map_or(0.0, |d| d.as_secs_f64())); res - }.boxed() + } + .boxed() } } -- cgit v1.2.3