diff --git a/rust_http_proxy/src/proxy.rs b/rust_http_proxy/src/proxy.rs index 0b132db..887ef92 100644 --- a/rust_http_proxy/src/proxy.rs +++ b/rust_http_proxy/src/proxy.rs @@ -49,9 +49,9 @@ use tokio::{net::TcpStream, pin}; pub struct ProxyHandler { pub(crate) config: Config, - prom_registry: Registry, - metrics: Metrics, - net_monitor: NetMonitor, + pub(crate) prom_registry: Registry, + pub(crate) metrics: Metrics, + pub(crate) net_monitor: NetMonitor, http1_client: HttpClient, reverse_client: legacy::Client, Incoming>, redirect_bachpaths: Vec, @@ -60,6 +60,7 @@ pub struct ProxyHandler { pub(crate) struct Metrics { pub(crate) http_req_counter: Family, Counter>, pub(crate) proxy_traffic: Family, Counter>, + #[cfg(feature = "bpf")] pub(crate) net_bytes: Family, Counter>, #[cfg(feature = "bpf")] pub(crate) cgroup_bytes: Family, Counter>, @@ -350,17 +351,9 @@ impl ProxyHandler { "reject http GET/POST when ask_for_auth and basic_auth not empty", )); } - web_func::serve_http_request( - req, - client_socket_addr, - &self.config, - path, - &self.net_monitor, - &self.metrics, - &self.prom_registry, - ) - .await - .map_err(|e| io::Error::new(ErrorKind::InvalidData, e)) + web_func::serve_http_request(self, req, client_socket_addr, path) + .await + .map_err(|e| io::Error::new(ErrorKind::InvalidData, e)) } async fn reverse_proxy( @@ -411,6 +404,53 @@ impl ProxyHandler { } } } + + #[cfg(feature = "bpf")] + pub(crate) fn snapshot_metrics(&self) { + { + self.metrics + .net_bytes + .get_or_create(&LabelImpl::new(NetDirectionLabel { + direction: "egress", + })) + .inner() + .store( + crate::net_monitor::get_egress(), + std::sync::atomic::Ordering::Relaxed, + ); + self.metrics + .net_bytes + .get_or_create(&LabelImpl::new(NetDirectionLabel { + direction: "ingress", + })) + .inner() + .store( + crate::net_monitor::get_ingress(), + std::sync::atomic::Ordering::Relaxed, + ); + + self.metrics + .cgroup_bytes + .get_or_create(&LabelImpl::new(NetDirectionLabel { + direction: "egress", + })) + .inner() + .store( + self.net_monitor.get_cgroup_egress(), + std::sync::atomic::Ordering::Relaxed, + ); + self.metrics + .cgroup_bytes + .get_or_create(&LabelImpl::new(NetDirectionLabel { + direction: "ingress", + })) + .inner() + .store( + self.net_monitor.get_cgroup_ingress(), + std::sync::atomic::Ordering::Relaxed, + ); + } + } } fn mod_http1_proxy_req(req: &mut Request) -> io::Result<()> { @@ -674,9 +714,10 @@ fn register_metrics(registry: &mut Registry) -> Metrics { ); let proxy_traffic = Family::, Counter>::default(); registry.register("proxy_traffic", "num proxy_traffic", proxy_traffic.clone()); + #[cfg(feature = "bpf")] let net_bytes = Family::, Counter>::default(); + #[cfg(feature = "bpf")] registry.register("net_bytes", "num net_bytes", net_bytes.clone()); - #[cfg(feature = "bpf")] let cgroup_bytes = Family::, Counter>::default(); #[cfg(feature = "bpf")] @@ -688,6 +729,7 @@ fn register_metrics(registry: &mut Registry) -> Metrics { Metrics { http_req_counter, proxy_traffic, + #[cfg(feature = "bpf")] net_bytes, #[cfg(feature = "bpf")] cgroup_bytes, diff --git a/rust_http_proxy/src/web_func.rs b/rust_http_proxy/src/web_func.rs index 51baf75..8c3a12f 100644 --- a/rust_http_proxy/src/web_func.rs +++ b/rust_http_proxy/src/web_func.rs @@ -4,10 +4,8 @@ use crate::proxy::build_authenticate_resp; use crate::proxy::check_auth; use crate::proxy::empty_body; use crate::proxy::full_body; -use crate::proxy::Metrics; -use crate::proxy::NetDirectionLabel; +use crate::proxy::ProxyHandler; use crate::proxy::ReqLabels; -use crate::Config; use http::response::Builder; use prom_label::LabelImpl; @@ -42,18 +40,14 @@ const SERVER_NAME: &str = "arloor's creation"; static GZIP: &str = "gzip"; -#[allow(clippy::too_many_arguments)] pub async fn serve_http_request( + proxy_handler: &ProxyHandler, req: &Request, client_socket_addr: SocketAddr, - proxy_config: &Config, path: &str, - _net_monitor: &NetMonitor, - metrics: &Metrics, - prom_registry: &Registry, ) -> Result>, Error> { - let web_content_path = &proxy_config.web_content_path; - let refer = &proxy_config.referer; + let web_content_path = &proxy_handler.config.web_content_path; + let refer = &proxy_handler.config.referer; let referer_header = req .headers() .get(REFERER) @@ -73,7 +67,7 @@ pub async fn serve_http_request( return not_found(); } } - let _hostname = &proxy_config.hostname; + let _hostname = &proxy_handler.config.hostname; let _hostname = req .uri() .authority() @@ -88,12 +82,12 @@ pub async fn serve_http_request( #[cfg(target_os = "linux")] (_, "/nt") => _count_stream(), #[cfg(target_os = "linux")] - (_, "/speed") => _speed(_net_monitor, _hostname, can_gzip).await, + (_, "/speed") => _speed(&proxy_handler.net_monitor, _hostname, can_gzip).await, #[cfg(target_os = "linux")] - (_, "/net") => _speed(_net_monitor, _hostname, can_gzip).await, + (_, "/net") => _speed(&proxy_handler.net_monitor, _hostname, can_gzip).await, (_, "/metrics") => { let (_, authed) = check_auth( - &proxy_config.basic_auth, + &proxy_handler.config.basic_auth, req, &client_socket_addr, hyper::header::AUTHORIZATION, @@ -101,15 +95,9 @@ pub async fn serve_http_request( if !authed { return Ok(build_authenticate_resp(false)); } - serve_metrics( - prom_registry, - _net_monitor, - &metrics.net_bytes, - #[cfg(feature = "bpf")] - &metrics.cgroup_bytes, - can_gzip, - ) - .await + #[cfg(feature = "bpf")] + proxy_handler.snapshot_metrics(); + serve_metrics(&proxy_handler.prom_registry, can_gzip).await } (&Method::GET, path) => { let is_outer_view_html = (path.ends_with('/') || path.ends_with(".html")) @@ -136,7 +124,7 @@ pub async fn serve_http_request( &r, is_outer_view_html, is_shell, - &metrics.http_req_counter, + &proxy_handler.metrics.http_req_counter, referer_header, path, ); @@ -191,54 +179,11 @@ fn extract_search_engine_from_referer(referer: &str) -> Result, Counter>, - #[cfg(feature = "bpf")] _cgroup_bytes: &Family, Counter>, + prom_registry: &Registry, can_gzip: bool, ) -> Result>, Error> { - #[cfg(feature = "bpf")] - { - _net_bytes - .get_or_create(&LabelImpl::new(NetDirectionLabel { - direction: "egress", - })) - .inner() - .store( - crate::net_monitor::get_egress(), - std::sync::atomic::Ordering::Relaxed, - ); - _net_bytes - .get_or_create(&LabelImpl::new(NetDirectionLabel { - direction: "ingress", - })) - .inner() - .store( - crate::net_monitor::get_ingress(), - std::sync::atomic::Ordering::Relaxed, - ); - - _cgroup_bytes - .get_or_create(&LabelImpl::new(NetDirectionLabel { - direction: "egress", - })) - .inner() - .store( - _net_monitor.get_cgroup_egress(), - std::sync::atomic::Ordering::Relaxed, - ); - _cgroup_bytes - .get_or_create(&LabelImpl::new(NetDirectionLabel { - direction: "ingress", - })) - .inner() - .store( - _net_monitor.get_cgroup_ingress(), - std::sync::atomic::Ordering::Relaxed, - ); - } let mut buffer = String::new(); - if let Err(e) = encode(&mut buffer, registry) { + if let Err(e) = encode(&mut buffer, prom_registry) { Response::builder() .status(StatusCode::INTERNAL_SERVER_ERROR) .header(http::header::SERVER, SERVER_NAME)