Skip to content

Commit

Permalink
Remove feature(min_specialization) from Timer (#33189)
Browse files Browse the repository at this point in the history
Using specialization is overkill.

GitOrigin-RevId: 15ed415ec741808e1b3a2b243386c8c2ba60ca12
  • Loading branch information
goffrie authored and Convex, Inc. committed Jan 15, 2025
1 parent 043d867 commit b91e35f
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 36 deletions.
1 change: 0 additions & 1 deletion crates/metrics/src/lib.rs
Original file line number Diff line number Diff line change
@@ -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)]

Expand Down
59 changes: 24 additions & 35 deletions crates/metrics/src/timer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,25 +19,22 @@ use crate::{
log_distribution_with_labels,
};

pub struct Timer<T: 'static> {
pub trait TimerHistogram: Sized + 'static {
fn finish(timer: &mut Timer<Self>);
}

pub struct Timer<T: TimerHistogram> {
start: Instant,
histogram: &'static T,
labels: BTreeSet<StaticMetricLabel>,
}

trait DropInner {
fn drop_inner(&mut self);
}

impl<T: 'static> DropInner for Timer<T> {
default fn drop_inner(&mut self) {
panic!("Default Drop implementation for Timer should not be callable")
}
}

impl<T: 'static> Drop for Timer<T> {
impl<T: TimerHistogram> Drop for Timer<T> {
fn drop(&mut self) {
self.drop_inner();
if std::thread::panicking() {
return;
}
T::finish(self);
}
}

Expand All @@ -62,10 +59,6 @@ impl Timer<VMHistogramVec> {
self.labels.remove(&old_label);
self.labels.insert(new_label);
}

pub fn elapsed(&self) -> Duration {
self.start.elapsed()
}
}

impl Timer<VMHistogram> {
Expand All @@ -76,37 +69,33 @@ impl Timer<VMHistogram> {
labels: BTreeSet::new(),
}
}
}

impl<T: TimerHistogram> Timer<T> {
pub fn elapsed(&self) -> Duration {
self.start.elapsed()
}
}

impl DropInner for Timer<VMHistogram> {
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<Self>) {
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<VMHistogramVec> {
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<Self>) {
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());
}
}

Expand Down

0 comments on commit b91e35f

Please sign in to comment.