proxy: Implement a From converter for latency::Ms (#872)

This reduces callsite verbosity for latency measurements at the expense of a fn-level
generic.
This commit is contained in:
Oliver Gould 2018-04-27 17:55:44 -07:00 committed by GitHub
parent aeaf1bf072
commit 9dc06a56f2
3 changed files with 17 additions and 11 deletions

View File

@ -65,8 +65,9 @@ impl<V: Into<u64>> Histogram<V> {
} }
} }
pub fn add(&mut self, v: V) { pub fn add<U: Into<V>>(&mut self, u: U) {
let value = v.into(); let v: V = u.into();
let value: u64 = v.into();
let idx = self.bounds.0.iter() let idx = self.bounds.0.iter()
.position(|b| match *b { .position(|b| match *b {
@ -184,7 +185,7 @@ mod tests {
quickcheck! { quickcheck! {
fn bucket_incremented(obs: u64) -> bool { fn bucket_incremented(obs: u64) -> bool {
let mut hist = Histogram::new(&BOUNDS); let mut hist = Histogram::<u64>::new(&BOUNDS);
hist.add(obs); hist.add(obs);
let incremented_bucket = &BOUNDS.0.iter() let incremented_bucket = &BOUNDS.0.iter()
.position(|bucket| match *bucket { .position(|bucket| match *bucket {
@ -201,7 +202,7 @@ mod tests {
} }
fn sum_equals_total_of_observations(observations: Vec<u64>) -> bool { fn sum_equals_total_of_observations(observations: Vec<u64>) -> bool {
let mut hist = Histogram::new(&BOUNDS); let mut hist = Histogram::<u64>::new(&BOUNDS);
let mut expected_sum = Wrapping(0u64); let mut expected_sum = Wrapping(0u64);
for obs in observations { for obs in observations {
@ -213,7 +214,7 @@ mod tests {
} }
fn count_equals_number_of_observations(observations: Vec<u64>) -> bool { fn count_equals_number_of_observations(observations: Vec<u64>) -> bool {
let mut hist = Histogram::new(&BOUNDS); let mut hist = Histogram::<u64>::new(&BOUNDS);
for obs in &observations { for obs in &observations {
hist.add(*obs); hist.add(*obs);
@ -228,7 +229,7 @@ mod tests {
fn multiple_observations_increment_buckets(observations: Vec<u64>) -> bool { fn multiple_observations_increment_buckets(observations: Vec<u64>) -> bool {
let mut buckets_and_counts: HashMap<usize, u64> = HashMap::new(); let mut buckets_and_counts: HashMap<usize, u64> = HashMap::new();
let mut hist = Histogram::new(&BOUNDS); let mut hist = Histogram::<u64>::new(&BOUNDS);
for obs in observations { for obs in observations {
let incremented_bucket = &BOUNDS.0.iter() let incremented_bucket = &BOUNDS.0.iter()

View File

@ -43,7 +43,7 @@ pub const BOUNDS: &Bounds = &Bounds(&[
/// A duration in milliseconds. /// A duration in milliseconds.
#[derive(Debug, Default, Clone)] #[derive(Debug, Default, Clone)]
pub struct Ms(pub Duration); pub struct Ms(Duration);
// /// A duration in microseconds. // /// A duration in microseconds.
// #[derive(Debug, Default, Clone)] // #[derive(Debug, Default, Clone)]
@ -56,6 +56,12 @@ impl Into<u64> for Ms {
} }
} }
impl From<Duration> for Ms {
fn from(d: Duration) -> Self {
Ms(d)
}
}
impl Default for Histogram<Ms> { impl Default for Histogram<Ms> {
fn default() -> Self { fn default() -> Self {
Histogram::new(BOUNDS) Histogram::new(BOUNDS)

View File

@ -447,7 +447,7 @@ impl Aggregate {
)); ));
self.update(|metrics| { self.update(|metrics| {
metrics.response_total(&labels).incr(); metrics.response_total(&labels).incr();
metrics.response_latency(&labels).add(latency::Ms(end.since_request_open)); metrics.response_latency(&labels).add(end.since_request_open);
}); });
}, },
@ -456,7 +456,7 @@ impl Aggregate {
let labels = Arc::new(ResponseLabels::fail(res)); let labels = Arc::new(ResponseLabels::fail(res));
self.update(|metrics| { self.update(|metrics| {
metrics.response_total(&labels).incr(); metrics.response_total(&labels).incr();
metrics.response_latency(&labels).add(latency::Ms(fail.since_request_open)); metrics.response_latency(&labels).add(fail.since_request_open);
}); });
}, },
@ -475,8 +475,7 @@ impl Aggregate {
*metrics.tcp().write_bytes_total(&labels) += close.tx_bytes as u64; *metrics.tcp().write_bytes_total(&labels) += close.tx_bytes as u64;
*metrics.tcp().read_bytes_total(&labels) += close.rx_bytes as u64; *metrics.tcp().read_bytes_total(&labels) += close.rx_bytes as u64;
metrics.tcp().connection_duration(&close_labels) metrics.tcp().connection_duration(&close_labels).add(close.duration);
.add(latency::Ms(close.duration));
metrics.tcp().close_total(&close_labels).incr(); metrics.tcp().close_total(&close_labels).incr();
let metrics = metrics.tcp().open_connections.values.get_mut(&labels); let metrics = metrics.tcp().open_connections.values.get_mut(&labels);