diff options
Diffstat (limited to 'src/metrics.rs')
-rw-r--r-- | src/metrics.rs | 42 |
1 files changed, 31 insertions, 11 deletions
diff --git a/src/metrics.rs b/src/metrics.rs index ee5c60a..d2e29c7 100644 --- a/src/metrics.rs +++ b/src/metrics.rs @@ -11,27 +11,29 @@ use hyper::{ service::{make_service_fn, service_fn}, Body, Method, Request, Response, Server, }; -use opentelemetry_prometheus::PrometheusExporter; +use opentelemetry::sdk::metrics; use prometheus::{Encoder, TextEncoder}; pub struct MetricsServer { bind_addr: Option<SocketAddr>, - exporter: PrometheusExporter, + registry: prometheus::Registry, } impl MetricsServer { pub fn init(bind_addr: Option<SocketAddr>) -> MetricsServer { + let registry = prometheus::Registry::new(); let exporter = opentelemetry_prometheus::exporter() - .with_default_summary_quantiles(vec![0.25, 0.5, 0.75, 0.9, 0.95, 0.99]) - .with_default_histogram_boundaries(vec![ - 0.001, 0.0015, 0.002, 0.003, 0.005, 0.007, 0.01, 0.015, 0.02, 0.03, 0.05, 0.07, - 0.1, 0.15, 0.2, 0.3, 0.5, 0.7, 1., 1.5, 2., 3., 5., 7., 10., 15., 20., 30., 40., - 50., 60., 70., 100., - ]) - .init(); + .with_registry(registry.clone()) + .with_aggregation_selector(AggregationSelector) + .build() + .expect("build prometheus registry"); + let mp = metrics::MeterProvider::builder() + .with_reader(exporter) + .build(); + opentelemetry::global::set_meter_provider(mp); Self { bind_addr, - exporter, + registry, } } @@ -70,7 +72,7 @@ impl MetricsServer { (&Method::GET, "/metrics") => { let mut buffer = vec![]; let encoder = TextEncoder::new(); - let metric_families = self.exporter.registry().gather(); + let metric_families = self.registry.gather(); encoder.encode(&metric_families, &mut buffer).unwrap(); Response::builder() @@ -88,3 +90,21 @@ impl MetricsServer { Ok(response) } } + +struct AggregationSelector; + +impl metrics::reader::AggregationSelector for AggregationSelector { + fn aggregation(&self, kind: metrics::InstrumentKind) -> metrics::Aggregation { + match kind { + metrics::InstrumentKind::Histogram => metrics::Aggregation::ExplicitBucketHistogram { + boundaries: vec![ + 0.001, 0.0015, 0.002, 0.003, 0.005, 0.007, 0.01, 0.015, 0.02, 0.03, 0.05, 0.07, + 0.1, 0.15, 0.2, 0.3, 0.5, 0.7, 1., 1.5, 2., 3., 5., 7., 10., 15., 20., 30., + 40., 50., 60., 70., 100., + ], + record_min_max: true, + }, + _ => metrics::reader::DefaultAggregationSelector::new().aggregation(kind), + } + } +} |