mirror of
https://github.com/sadoyan/aralez.git
synced 2026-04-30 23:08:40 +08:00
added new metric aralez_requests_by_upstream
This commit is contained in:
1
Cargo.lock
generated
1
Cargo.lock
generated
@@ -124,6 +124,7 @@ dependencies = [
|
|||||||
"dashmap",
|
"dashmap",
|
||||||
"env_logger",
|
"env_logger",
|
||||||
"futures",
|
"futures",
|
||||||
|
"http",
|
||||||
"jsonwebtoken",
|
"jsonwebtoken",
|
||||||
"lazy_static",
|
"lazy_static",
|
||||||
"log",
|
"log",
|
||||||
|
|||||||
@@ -50,6 +50,7 @@ privdrop = "0.5.6"
|
|||||||
ctrlc = "3.5.1"
|
ctrlc = "3.5.1"
|
||||||
port_check = "0.3.0"
|
port_check = "0.3.0"
|
||||||
serde_json = "1.0.140"
|
serde_json = "1.0.140"
|
||||||
|
http = "1.2.0"
|
||||||
#moka = { version = "0.12.10", features = ["sync"] }
|
#moka = { version = "0.12.10", features = ["sync"] }
|
||||||
#rustls = { version = "0.23.27", features = ["ring"] }
|
#rustls = { version = "0.23.27", features = ["ring"] }
|
||||||
#hickory-client = { version = "0.25.2" }
|
#hickory-client = { version = "0.25.2" }
|
||||||
|
|||||||
@@ -1,9 +1,12 @@
|
|||||||
|
use http::method::Method;
|
||||||
use pingora_http::Version;
|
use pingora_http::Version;
|
||||||
use prometheus::{register_histogram, register_int_counter, register_int_counter_vec, Histogram, IntCounter, IntCounterVec};
|
use prometheus::{register_histogram, register_int_counter, register_int_counter_vec, Histogram, IntCounter, IntCounterVec};
|
||||||
|
use std::sync::Arc;
|
||||||
use std::time::Duration;
|
use std::time::Duration;
|
||||||
|
|
||||||
pub struct MetricTypes {
|
pub struct MetricTypes {
|
||||||
pub method: String,
|
pub method: Method,
|
||||||
|
pub upstream: Arc<str>,
|
||||||
pub code: String,
|
pub code: String,
|
||||||
pub latency: Duration,
|
pub latency: Duration,
|
||||||
pub version: Version,
|
pub version: Version,
|
||||||
@@ -33,6 +36,11 @@ lazy_static::lazy_static! {
|
|||||||
"Number of requests by HTTP method",
|
"Number of requests by HTTP method",
|
||||||
&["method"]
|
&["method"]
|
||||||
).unwrap();
|
).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!(
|
pub static ref REQUESTS_BY_VERSION: IntCounterVec = register_int_counter_vec!(
|
||||||
"aralez_requests_by_version_total",
|
"aralez_requests_by_version_total",
|
||||||
"Number of requests by HTTP versions",
|
"Number of requests by HTTP versions",
|
||||||
@@ -57,7 +65,8 @@ pub fn calc_metrics(metric_types: &MetricTypes) {
|
|||||||
_ => "Unknown",
|
_ => "Unknown",
|
||||||
};
|
};
|
||||||
REQUESTS_BY_VERSION.with_label_values(&[&version_str]).inc();
|
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_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());
|
RESPONSE_LATENCY.observe(metric_types.latency.as_secs_f64());
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -143,14 +143,6 @@ impl ProxyHttp for LB {
|
|||||||
ctx.to_https = true;
|
ctx.to_https = true;
|
||||||
ctx.redirect_to = Arc::from(format!("https://{}:{}{}", host, port, uri));
|
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());
|
let response_code = session.response_written().map_or(0, |resp| resp.status.as_u16());
|
||||||
debug!("{}, response code: {response_code}", self.request_summary(session, ctx));
|
debug!("{}, response code: {response_code}", self.request_summary(session, ctx));
|
||||||
let m = &MetricTypes {
|
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()),
|
code: session.response_written().map(|resp| resp.status.as_str().to_owned()).unwrap_or("0".to_string()),
|
||||||
latency: ctx.start_time.elapsed(),
|
latency: ctx.start_time.elapsed(),
|
||||||
version: session.req_header().version,
|
version: session.req_header().version,
|
||||||
|
upstream: ctx.hostname.clone().unwrap_or(Arc::from("localhost")),
|
||||||
};
|
};
|
||||||
calc_metrics(m);
|
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))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user