|
| 1 | +use crate::metrics::macros::MetricFromOpts; |
| 2 | +use std::marker::PhantomData; |
| 3 | +use std::ops::{Deref, DerefMut}; |
| 4 | + |
| 5 | +/// Prometheus's histograms work by dividing datapoints in buckets, with each bucket containing the |
| 6 | +/// count of datapoints equal or greater to the bucket value. |
| 7 | +/// |
| 8 | +/// Histogram buckets are not an exact science, so feel free to tweak the buckets or create new |
| 9 | +/// ones if you see that the histograms are not really accurate. Just avoid adding too many buckets |
| 10 | +/// for a single type as that increases the number of exported metric series. |
| 11 | +pub trait HistogramBuckets { |
| 12 | + const BUCKETS: &'static [f64]; |
| 13 | +} |
| 14 | + |
| 15 | +/// Buckets geared towards measuring timings, such as the response time of our requests, going from |
| 16 | +/// 0.5ms to 100ms with a higher resolution and from 100ms to 5 seconds with a slightly lower |
| 17 | +/// resolution. This allows us to properly measure download requests (which take around 1ms) and |
| 18 | +/// other requests (our 95h is around 10-20ms). |
| 19 | +pub struct TimingBuckets; |
| 20 | + |
| 21 | +impl HistogramBuckets for TimingBuckets { |
| 22 | + const BUCKETS: &'static [f64] = &[ |
| 23 | + 0.0005, 0.001, 0.0025, 0.005, 0.01, 0.025, 0.05, 0.1, 0.5, 1.0, 5.0, |
| 24 | + ]; |
| 25 | +} |
| 26 | + |
| 27 | +/// Wrapper type over [`prometheus::Histogram`] to support defining buckets. |
| 28 | +pub struct Histogram<B: HistogramBuckets> { |
| 29 | + inner: prometheus::Histogram, |
| 30 | + _phantom: PhantomData<B>, |
| 31 | +} |
| 32 | + |
| 33 | +impl<B: HistogramBuckets> MetricFromOpts for Histogram<B> { |
| 34 | + fn from_opts(opts: prometheus::Opts) -> Result<Self, prometheus::Error> { |
| 35 | + Ok(Histogram { |
| 36 | + inner: prometheus::Histogram::with_opts(prometheus::HistogramOpts { |
| 37 | + common_opts: opts, |
| 38 | + buckets: B::BUCKETS.to_vec(), |
| 39 | + })?, |
| 40 | + _phantom: PhantomData, |
| 41 | + }) |
| 42 | + } |
| 43 | +} |
| 44 | + |
| 45 | +impl<B: HistogramBuckets> Deref for Histogram<B> { |
| 46 | + type Target = prometheus::Histogram; |
| 47 | + |
| 48 | + fn deref(&self) -> &Self::Target { |
| 49 | + &self.inner |
| 50 | + } |
| 51 | +} |
| 52 | + |
| 53 | +impl<B: HistogramBuckets> DerefMut for Histogram<B> { |
| 54 | + fn deref_mut(&mut self) -> &mut Self::Target { |
| 55 | + &mut self.inner |
| 56 | + } |
| 57 | +} |
| 58 | + |
| 59 | +/// Wrapper type over [`prometheus::HistogramVec`] to support defining buckets. |
| 60 | +pub struct HistogramVec<B: HistogramBuckets> { |
| 61 | + inner: prometheus::HistogramVec, |
| 62 | + _phantom: PhantomData<B>, |
| 63 | +} |
| 64 | + |
| 65 | +impl<B: HistogramBuckets> MetricFromOpts for HistogramVec<B> { |
| 66 | + fn from_opts(opts: prometheus::Opts) -> Result<Self, prometheus::Error> { |
| 67 | + Ok(HistogramVec { |
| 68 | + inner: prometheus::HistogramVec::new( |
| 69 | + prometheus::HistogramOpts { |
| 70 | + common_opts: opts.clone(), |
| 71 | + buckets: B::BUCKETS.to_vec(), |
| 72 | + }, |
| 73 | + opts.variable_labels |
| 74 | + .iter() |
| 75 | + .map(|s| s.as_str()) |
| 76 | + .collect::<Vec<_>>() |
| 77 | + .as_slice(), |
| 78 | + )?, |
| 79 | + _phantom: PhantomData, |
| 80 | + }) |
| 81 | + } |
| 82 | +} |
| 83 | + |
| 84 | +impl<B: HistogramBuckets> Deref for HistogramVec<B> { |
| 85 | + type Target = prometheus::HistogramVec; |
| 86 | + |
| 87 | + fn deref(&self) -> &Self::Target { |
| 88 | + &self.inner |
| 89 | + } |
| 90 | +} |
| 91 | + |
| 92 | +impl<B: HistogramBuckets> DerefMut for HistogramVec<B> { |
| 93 | + fn deref_mut(&mut self) -> &mut Self::Target { |
| 94 | + &mut self.inner |
| 95 | + } |
| 96 | +} |
0 commit comments