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
+
1
18
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 > ;
3
20
}
4
21
5
22
#[ macro_export]
@@ -53,20 +70,19 @@ macro_rules! metrics {
53
70
} ;
54
71
}
55
72
56
- #[ macro_export]
57
73
macro_rules! load_metric_type {
58
74
( $name: ident as single) => {
59
75
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 > {
62
78
$name:: with_opts( opts. into( ) )
63
79
}
64
80
}
65
81
} ;
66
82
( $name: ident as vec) => {
67
83
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 > {
70
86
$name:: new(
71
87
opts. clone( ) . into( ) ,
72
88
opts. variable_labels
@@ -79,3 +95,39 @@ macro_rules! load_metric_type {
79
95
}
80
96
} ;
81
97
}
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
+ }
0 commit comments