diff --git a/Cargo.lock b/Cargo.lock index 12076eb..47c7ae8 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -124,6 +124,7 @@ dependencies = [ "dashmap", "env_logger", "futures", + "http", "jsonwebtoken", "lazy_static", "log", diff --git a/Cargo.toml b/Cargo.toml index 92e89f0..bf1b0cd 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -50,6 +50,7 @@ privdrop = "0.5.6" ctrlc = "3.5.1" port_check = "0.3.0" serde_json = "1.0.140" +http = "1.2.0" #moka = { version = "0.12.10", features = ["sync"] } #rustls = { version = "0.23.27", features = ["ring"] } #hickory-client = { version = "0.25.2" } diff --git a/src/utils/metrics.rs b/src/utils/metrics.rs index 3bc8e0e..a7d9ca0 100644 --- a/src/utils/metrics.rs +++ b/src/utils/metrics.rs @@ -1,9 +1,12 @@ +use http::method::Method; use pingora_http::Version; use prometheus::{register_histogram, register_int_counter, register_int_counter_vec, Histogram, IntCounter, IntCounterVec}; +use std::sync::Arc; use std::time::Duration; pub struct MetricTypes { - pub method: String, + pub method: Method, + pub upstream: Arc, pub code: String, pub latency: Duration, pub version: Version, @@ -33,6 +36,11 @@ lazy_static::lazy_static! { "Number of requests by HTTP method", &["method"] ).unwrap(); + pub static ref REQUESTS_BY_UPSTREAM: IntCounterVec = register_int_counter_vec!( + "aralez_requests_by_upstream", + "Number of requests by UPSTREAM server", + &["method"] + ).unwrap(); pub static ref REQUESTS_BY_VERSION: IntCounterVec = register_int_counter_vec!( "aralez_requests_by_version_total", "Number of requests by HTTP versions", @@ -57,7 +65,8 @@ pub fn calc_metrics(metric_types: &MetricTypes) { _ => "Unknown", }; REQUESTS_BY_VERSION.with_label_values(&[&version_str]).inc(); - RESPONSE_CODES.with_label_values(&[&metric_types.code.to_string()]).inc(); + RESPONSE_CODES.with_label_values(&[&metric_types.code]).inc(); REQUESTS_BY_METHOD.with_label_values(&[&metric_types.method]).inc(); + REQUESTS_BY_UPSTREAM.with_label_values(&[metric_types.upstream.as_ref()]).inc(); RESPONSE_LATENCY.observe(metric_types.latency.as_secs_f64()); } diff --git a/src/web/proxyhttp.rs b/src/web/proxyhttp.rs index 3ff24e3..47258d9 100644 --- a/src/web/proxyhttp.rs +++ b/src/web/proxyhttp.rs @@ -143,14 +143,6 @@ impl ProxyHttp for LB { ctx.to_https = true; ctx.redirect_to = Arc::from(format!("https://{}:{}{}", host, port, uri)); } - // if let Some(addr) = session.server_addr() { - // if let Some((host, _)) = addr.to_string().split_once(':') { - // let uri = session.req_header().uri.path_and_query().map_or("/", |pq| pq.as_str()); - // let port = self.config.proxy_port_tls.unwrap_or(403); - // ctx.to_https = true; - // ctx.redirect_to = Arc::from(format!("https://{}:{}{}", host, port, uri)); - // } - // } } } } @@ -232,10 +224,12 @@ impl ProxyHttp for LB { let response_code = session.response_written().map_or(0, |resp| resp.status.as_u16()); debug!("{}, response code: {response_code}", self.request_summary(session, ctx)); let m = &MetricTypes { - method: session.req_header().method.to_string(), + method: session.req_header().method.clone(), + // method: Arc::from(session.req_header().method.as_str()), code: session.response_written().map(|resp| resp.status.as_str().to_owned()).unwrap_or("0".to_string()), latency: ctx.start_time.elapsed(), version: session.req_header().version, + upstream: ctx.hostname.clone().unwrap_or(Arc::from("localhost")), }; calc_metrics(m); } @@ -257,3 +251,17 @@ fn return_header_host(session: &Session) -> Option> { } } } + +#[allow(dead_code)] +fn return_str_host<'a>(session: &'a Session) -> Option<&'a str> { + if session.is_http2() { + session.req_header().uri.host() + } else { + session + .req_header() + .headers + .get("host") + .and_then(|h| h.to_str().ok()) + .map(|h| h.split_once(':').map_or(h, |(host, _)| host)) + } +}