Skip to content

Commit

Permalink
feat: Added process metrics for linux
Browse files Browse the repository at this point in the history
  • Loading branch information
chriswk committed Jan 16, 2023
1 parent 487d2ad commit c0edf75
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 18 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"] }
Expand Down
21 changes: 3 additions & 18 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -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};
Expand Down Expand Up @@ -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);

Expand Down
27 changes: 27 additions & 0 deletions src/metrics.rs
Original file line number Diff line number Diff line change
@@ -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
}

0 comments on commit c0edf75

Please sign in to comment.