Skip to content

Commit f508a63

Browse files
committed
Auto merge of #3770 - pietroalbini:improve-metrics, r=Turbo87
Improve metrics accuracy This should hopefully improve the accuracy of our download metrics and should stop reporting most of the downloads as taking more than 2ms (most download requests finish processing in around 1ms).
2 parents 9166623 + c680bd0 commit f508a63

File tree

2 files changed

+58
-12
lines changed

2 files changed

+58
-12
lines changed

src/metrics/macros.rs

Lines changed: 58 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,22 @@
1+
use prometheus::{Histogram, HistogramOpts, HistogramVec, Opts};
2+
3+
/// Prometheus's histograms work by dividing datapoints in buckets, with each bucket containing
4+
/// the count of datapoints equal or greater to the bucket value.
5+
///
6+
/// The buckets used by crates.io are geared towards measuring the response time of our requests,
7+
/// going from 0.5ms to 100ms with a higher resolution and from 100ms to 5 seconds with a slightly
8+
/// lower resolution. This allows us to properly measure download requests (which take around 1ms)
9+
/// and other requests (our 95h is around 10-20ms).
10+
///
11+
/// Histogram buckets are not an exact science, so feel free to tweak the buckets if you see that
12+
/// the histograms are not really accurate. Just avoid adding too many buckets as that increases
13+
/// the number of exported metric series.
14+
const HISTOGRAM_BUCKETS: &[f64] = &[
15+
0.0005, 0.001, 0.0025, 0.005, 0.01, 0.025, 0.05, 0.1, 0.5, 1.0, 5.0,
16+
];
17+
118
pub(super) trait MetricFromOpts: Sized {
2-
fn from_opts(opts: prometheus::Opts) -> Result<Self, prometheus::Error>;
19+
fn from_opts(opts: Opts) -> Result<Self, prometheus::Error>;
320
}
421

522
#[macro_export]
@@ -53,20 +70,19 @@ macro_rules! metrics {
5370
};
5471
}
5572

56-
#[macro_export]
5773
macro_rules! load_metric_type {
5874
($name:ident as single) => {
5975
use prometheus::$name;
60-
impl crate::metrics::macros::MetricFromOpts for $name {
61-
fn from_opts(opts: prometheus::Opts) -> Result<Self, prometheus::Error> {
76+
impl MetricFromOpts for $name {
77+
fn from_opts(opts: Opts) -> Result<Self, prometheus::Error> {
6278
$name::with_opts(opts.into())
6379
}
6480
}
6581
};
6682
($name:ident as vec) => {
6783
use prometheus::$name;
68-
impl crate::metrics::macros::MetricFromOpts for $name {
69-
fn from_opts(opts: prometheus::Opts) -> Result<Self, prometheus::Error> {
84+
impl MetricFromOpts for $name {
85+
fn from_opts(opts: Opts) -> Result<Self, prometheus::Error> {
7086
$name::new(
7187
opts.clone().into(),
7288
opts.variable_labels
@@ -79,3 +95,39 @@ macro_rules! load_metric_type {
7995
}
8096
};
8197
}
98+
99+
load_metric_type!(Counter as single);
100+
load_metric_type!(CounterVec as vec);
101+
load_metric_type!(IntCounter as single);
102+
load_metric_type!(IntCounterVec as vec);
103+
load_metric_type!(Gauge as single);
104+
load_metric_type!(GaugeVec as vec);
105+
load_metric_type!(IntGauge as single);
106+
load_metric_type!(IntGaugeVec as vec);
107+
108+
// Use a custom implementation for histograms to customize the buckets.
109+
110+
impl MetricFromOpts for Histogram {
111+
fn from_opts(opts: Opts) -> Result<Self, prometheus::Error> {
112+
Histogram::with_opts(HistogramOpts {
113+
common_opts: opts,
114+
buckets: HISTOGRAM_BUCKETS.to_vec(),
115+
})
116+
}
117+
}
118+
119+
impl MetricFromOpts for HistogramVec {
120+
fn from_opts(opts: Opts) -> Result<Self, prometheus::Error> {
121+
HistogramVec::new(
122+
HistogramOpts {
123+
common_opts: opts.clone(),
124+
buckets: HISTOGRAM_BUCKETS.to_vec(),
125+
},
126+
opts.variable_labels
127+
.iter()
128+
.map(|s| s.as_str())
129+
.collect::<Vec<_>>()
130+
.as_slice(),
131+
)
132+
}
133+
}

src/metrics/mod.rs

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,3 @@ mod macros;
88
mod instance;
99
mod log_encoder;
1010
mod service;
11-
12-
load_metric_type!(IntGauge as single);
13-
load_metric_type!(IntCounter as single);
14-
load_metric_type!(IntCounterVec as vec);
15-
load_metric_type!(IntGaugeVec as vec);
16-
load_metric_type!(HistogramVec as vec);

0 commit comments

Comments
 (0)