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:
parent
aeaf1bf072
commit
9dc06a56f2
|
@ -65,8 +65,9 @@ impl<V: Into<u64>> Histogram<V> {
|
|||
}
|
||||
}
|
||||
|
||||
pub fn add(&mut self, v: V) {
|
||||
let value = v.into();
|
||||
pub fn add<U: Into<V>>(&mut self, u: U) {
|
||||
let v: V = u.into();
|
||||
let value: u64 = v.into();
|
||||
|
||||
let idx = self.bounds.0.iter()
|
||||
.position(|b| match *b {
|
||||
|
@ -184,7 +185,7 @@ mod tests {
|
|||
|
||||
quickcheck! {
|
||||
fn bucket_incremented(obs: u64) -> bool {
|
||||
let mut hist = Histogram::new(&BOUNDS);
|
||||
let mut hist = Histogram::<u64>::new(&BOUNDS);
|
||||
hist.add(obs);
|
||||
let incremented_bucket = &BOUNDS.0.iter()
|
||||
.position(|bucket| match *bucket {
|
||||
|
@ -201,7 +202,7 @@ mod tests {
|
|||
}
|
||||
|
||||
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);
|
||||
for obs in observations {
|
||||
|
@ -213,7 +214,7 @@ mod tests {
|
|||
}
|
||||
|
||||
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 {
|
||||
hist.add(*obs);
|
||||
|
@ -228,7 +229,7 @@ mod tests {
|
|||
|
||||
fn multiple_observations_increment_buckets(observations: Vec<u64>) -> bool {
|
||||
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 {
|
||||
let incremented_bucket = &BOUNDS.0.iter()
|
||||
|
|
|
@ -43,7 +43,7 @@ pub const BOUNDS: &Bounds = &Bounds(&[
|
|||
|
||||
/// A duration in milliseconds.
|
||||
#[derive(Debug, Default, Clone)]
|
||||
pub struct Ms(pub Duration);
|
||||
pub struct Ms(Duration);
|
||||
|
||||
// /// A duration in microseconds.
|
||||
// #[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> {
|
||||
fn default() -> Self {
|
||||
Histogram::new(BOUNDS)
|
||||
|
|
|
@ -447,7 +447,7 @@ impl Aggregate {
|
|||
));
|
||||
self.update(|metrics| {
|
||||
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));
|
||||
self.update(|metrics| {
|
||||
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().read_bytes_total(&labels) += close.rx_bytes as u64;
|
||||
|
||||
metrics.tcp().connection_duration(&close_labels)
|
||||
.add(latency::Ms(close.duration));
|
||||
metrics.tcp().connection_duration(&close_labels).add(close.duration);
|
||||
metrics.tcp().close_total(&close_labels).incr();
|
||||
|
||||
let metrics = metrics.tcp().open_connections.values.get_mut(&labels);
|
||||
|
|
Loading…
Reference in New Issue