commonware_runtime/telemetry/metrics/
histogram.rs1use crate::Clock;
4use prometheus_client::metrics::histogram::Histogram;
5use std::{sync::Arc, time::SystemTime};
6
7pub struct Buckets;
11
12impl Buckets {
13 pub const NETWORK: [f64; 13] = [
18 0.010, 0.020, 0.050, 0.100, 0.200, 0.500, 1.0, 2.0, 5.0, 10.0, 30.0, 60.0, 300.0,
19 ];
20
21 pub const LOCAL: [f64; 12] = [
26 3e-6, 1e-5, 3e-5, 1e-4, 3e-4, 0.001, 0.003, 0.01, 0.03, 0.1, 0.3, 1.0,
27 ];
28}
29
30pub trait HistogramExt {
32 fn observe_between(&self, start: SystemTime, end: SystemTime);
36}
37
38impl HistogramExt for Histogram {
39 fn observe_between(&self, start: SystemTime, end: SystemTime) {
40 let duration = match end.duration_since(start) {
41 Ok(duration) => duration.as_secs_f64(),
42 Err(_) => 0.0, };
44 self.observe(duration);
45 }
46}
47
48pub struct Timed<C: Clock> {
50 histogram: Histogram,
52
53 clock: Arc<C>,
55}
56
57impl<C: Clock> Timed<C> {
58 pub fn new(histogram: Histogram, clock: Arc<C>) -> Self {
60 Self { histogram, clock }
61 }
62
63 pub fn timer(&self) -> Timer<C> {
65 let start = self.clock.current();
66 Timer {
67 histogram: self.histogram.clone(),
68 clock: self.clock.clone(), start,
70 canceled: false,
71 }
72 }
73}
74
75pub struct Timer<C: Clock> {
77 histogram: Histogram,
79
80 clock: Arc<C>,
82
83 start: SystemTime,
85
86 canceled: bool,
88}
89
90impl<C: Clock> Timer<C> {
91 pub fn observe(&mut self) {
93 self.canceled = true;
94 let end = self.clock.current();
95 self.histogram.observe_between(self.start, end);
96 }
97
98 pub fn cancel(mut self) {
100 self.canceled = true;
101 }
102}
103
104impl<C: Clock> Drop for Timer<C> {
105 fn drop(&mut self) {
106 if self.canceled {
107 return;
108 }
109 self.observe();
110 }
111}