diff --git a/Cargo.toml b/Cargo.toml index 2ca42ae..6710ebe 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -15,6 +15,7 @@ clap = { version = "4.0.32", features = ["derive", "env"] } dotenv = { version = "0.15.0", features = ["clap"] } opentelemetry = { version = "0.18.0", features = ["trace", "rt-tokio", "metrics"] } opentelemetry-prometheus = "0.11.0" +prometheus = { version = "0.13.3", features = ["process"] } rustls = { version = "0.20.7" } rustls-pemfile = "1.0.1" serde = { version = "1.0.152", features = ["derive"] } diff --git a/src/main.rs b/src/main.rs index 04089c2..888c4c3 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,16 +1,13 @@ mod cli; +mod metrics; use std::{fs::File, io::BufReader, path::PathBuf}; use actix_tls::accept::rustls::reexports::ServerConfig; use actix_web::{middleware, web, App, HttpResponse, HttpServer}; -use actix_web_opentelemetry::{PrometheusMetricsHandler, RequestMetricsBuilder, RequestTracing}; +use actix_web_opentelemetry::{RequestMetricsBuilder, RequestTracing}; use clap::Parser; use opentelemetry::{ global, - sdk::{ - export::metrics::aggregation, - metrics::{controllers, processors, selectors}, - }, }; use rustls::{Certificate, PrivateKey}; use rustls_pemfile::{certs, pkcs8_private_keys}; @@ -53,19 +50,7 @@ async fn main() -> Result<(), anyhow::Error> { let collector = Registry::default().with(logger).with(env_filter); // Initialize tracing tracing::subscriber::set_global_default(collector).unwrap(); - let metrics_handler = { - let controller = controllers::basic( - processors::factory( - selectors::simple::histogram([1.0, 2.0, 5.0, 10.0, 20.0, 50.0]), // Will give histogram for with resolution in n ms - aggregation::cumulative_temporality_selector(), - ) - .with_memory(true), - ) - .build(); - - let exporter = opentelemetry_prometheus::exporter(controller).init(); - PrometheusMetricsHandler::new(exporter) - }; + let metrics_handler = metrics::http_metrics_handler(metrics::registry()); let meter = global::meter("actix_web"); let request_metrics = RequestMetricsBuilder::new().build(meter); diff --git a/src/metrics.rs b/src/metrics.rs new file mode 100644 index 0000000..a8300ad --- /dev/null +++ b/src/metrics.rs @@ -0,0 +1,27 @@ +use actix_web_opentelemetry::PrometheusMetricsHandler; +use opentelemetry::sdk::{metrics::{controllers, processors, selectors}, export::metrics::aggregation}; +use prometheus::{Registry, process_collector::ProcessCollector}; + +pub fn http_metrics_handler(registry: Registry) -> PrometheusMetricsHandler { + let controller = controllers::basic( + processors::factory( + selectors::simple::histogram([1.0, 2.0, 5.0, 10.0, 20.0, 50.0]), // Will give histogram for with resolution in n ms + aggregation::cumulative_temporality_selector(), + ) + .with_memory(true), + ) + .build(); + + let exporter = opentelemetry_prometheus::exporter(controller).with_registry(registry).init(); + PrometheusMetricsHandler::new(exporter) +} + +pub fn registry() -> Registry { + let registry = Registry::new(); + + #[cfg(target_os = "linux")] + let process_collector = ProcessCollector::for_self(); + let _register_result = registry.register(Box::new(process_collector)); + + registry +} \ No newline at end of file