aboutsummaryrefslogtreecommitdiff
path: root/src/util/metrics.rs
diff options
context:
space:
mode:
authorAlex Auvolat <alex@adnab.me>2022-02-22 13:53:59 +0100
committerAlex Auvolat <alex@adnab.me>2022-03-14 10:53:35 +0100
commit818daa5c786813fdf50fecb6022e29b18e509b62 (patch)
tree5f4df9917e7939d7722daaafcbcebe4961ad1184 /src/util/metrics.rs
parentf0d0cd9a20979f59db246e6a545ddc1b7bbb20b3 (diff)
downloadgarage-818daa5c786813fdf50fecb6022e29b18e509b62.tar.gz
garage-818daa5c786813fdf50fecb6022e29b18e509b62.zip
Refactor how durations are measured
Diffstat (limited to 'src/util/metrics.rs')
-rw-r--r--src/util/metrics.rs35
1 files changed, 35 insertions, 0 deletions
diff --git a/src/util/metrics.rs b/src/util/metrics.rs
new file mode 100644
index 00000000..b3b1fc3c
--- /dev/null
+++ b/src/util/metrics.rs
@@ -0,0 +1,35 @@
+use std::time::SystemTime;
+
+use futures::{future::BoxFuture, Future, FutureExt};
+
+use opentelemetry::{KeyValue, metrics::*};
+
+pub trait RecordDuration<'a>: 'a {
+ type Output;
+
+ fn record_duration(self, r: &'a ValueRecorder<f64>, attributes: &'a [KeyValue]) -> BoxFuture<'a, Self::Output>;
+ fn bound_record_duration(self, r: &'a BoundValueRecorder<f64>) -> BoxFuture<'a, Self::Output>;
+}
+
+impl<'a, T, O> RecordDuration<'a> for T
+where T: Future<Output=O> + Send + 'a {
+ type Output = O;
+
+ fn record_duration(self, r: &'a ValueRecorder<f64>, 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);
+ res
+ }.boxed()
+ }
+
+ fn bound_record_duration(self, r: &'a BoundValueRecorder<f64>) -> 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()));
+ res
+ }.boxed()
+ }
+}