added new metric aralez_requests_by_upstream

This commit is contained in:
Ara Sadoyan
2026-01-25 18:08:15 +01:00
parent 703de9e909
commit 38055ae94e
4 changed files with 30 additions and 11 deletions

View File

@@ -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<str>,
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());
}

View File

@@ -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<Arc<str>> {
}
}
}
#[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))
}
}