diff --git a/crates/metrics/src/lib.rs b/crates/metrics/src/lib.rs index de3d073d..3ceecb58 100644 --- a/crates/metrics/src/lib.rs +++ b/crates/metrics/src/lib.rs @@ -1,7 +1,6 @@ //! Code for interacting with our metrics logging #![feature(iter_intersperse)] #![feature(try_blocks)] -#![feature(min_specialization)] #![feature(type_alias_impl_trait)] #![feature(impl_trait_in_assoc_type)] diff --git a/crates/metrics/src/timer.rs b/crates/metrics/src/timer.rs index 69099720..e547a151 100644 --- a/crates/metrics/src/timer.rs +++ b/crates/metrics/src/timer.rs @@ -19,25 +19,22 @@ use crate::{ log_distribution_with_labels, }; -pub struct Timer { +pub trait TimerHistogram: Sized + 'static { + fn finish(timer: &mut Timer); +} + +pub struct Timer { start: Instant, histogram: &'static T, labels: BTreeSet, } -trait DropInner { - fn drop_inner(&mut self); -} - -impl DropInner for Timer { - default fn drop_inner(&mut self) { - panic!("Default Drop implementation for Timer should not be callable") - } -} - -impl Drop for Timer { +impl Drop for Timer { fn drop(&mut self) { - self.drop_inner(); + if std::thread::panicking() { + return; + } + T::finish(self); } } @@ -62,10 +59,6 @@ impl Timer { self.labels.remove(&old_label); self.labels.insert(new_label); } - - pub fn elapsed(&self) -> Duration { - self.start.elapsed() - } } impl Timer { @@ -76,37 +69,33 @@ impl Timer { labels: BTreeSet::new(), } } +} +impl Timer { pub fn elapsed(&self) -> Duration { self.start.elapsed() } } -impl DropInner for Timer { - fn drop_inner(&mut self) { - if std::thread::panicking() { - return; - } - let elapsed_duration = self.start.elapsed(); +impl TimerHistogram for VMHistogram { + fn finish(timer: &mut Timer) { + let elapsed_duration = timer.start.elapsed(); let elapsed = elapsed_duration.as_secs_f64(); - let desc = get_desc(self.histogram); + let desc = get_desc(timer.histogram); tracing::debug!("{elapsed_duration:?} for timer {desc:?}"); - log_distribution(self.histogram, elapsed); + log_distribution(timer.histogram, elapsed); } } -impl DropInner for Timer { - fn drop_inner(&mut self) { - if std::thread::panicking() { - return; - } - let elapsed_duration = self.start.elapsed(); +impl TimerHistogram for VMHistogramVec { + fn finish(timer: &mut Timer) { + let elapsed_duration = timer.start.elapsed(); let elapsed = elapsed_duration.as_secs_f64(); - let desc = get_desc(self.histogram); - tracing::debug!("{elapsed_duration:?} for timer {desc:?} {:?}", self.labels); - let labels = mem::take(&mut self.labels); - log_distribution_with_labels(self.histogram, elapsed, labels.into_iter().collect()); + let desc = get_desc(timer.histogram); + tracing::debug!("{elapsed_duration:?} for timer {desc:?} {:?}", timer.labels); + let labels = mem::take(&mut timer.labels); + log_distribution_with_labels(timer.histogram, elapsed, labels.into_iter().collect()); } }