diff options
author | Alex Auvolat <alex@adnab.me> | 2023-10-02 13:11:42 +0200 |
---|---|---|
committer | Alex Auvolat <alex@adnab.me> | 2023-10-02 13:11:42 +0200 |
commit | 4818e8cccc68c88ae64a7a6f65032906f6d818c3 (patch) | |
tree | a7491cf8b77d6556f5e128a2697ea4286256b1ed /src/metrics.rs | |
parent | 46d517b2f75bfff84b49fe3abc57152dcbc55133 (diff) | |
download | tricot-4818e8cccc68c88ae64a7a6f65032906f6d818c3.tar.gz tricot-4818e8cccc68c88ae64a7a6f65032906f6d818c3.zip |
cargo upgrades: update opentelemetry dependency to 0.20/prometheus to 0.13
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), + } + } +} |