From 6e4095bd811a53cb75092516ef303b746e9aafba Mon Sep 17 00:00:00 2001 From: metamuffin Date: Fri, 6 Oct 2023 10:22:31 +0200 Subject: some basic reporting for mond --- src/main.rs | 33 +++++++++++++++++++++------------ 1 file changed, 21 insertions(+), 12 deletions(-) (limited to 'src/main.rs') diff --git a/src/main.rs b/src/main.rs index 4d14bec..a1b04d4 100644 --- a/src/main.rs +++ b/src/main.rs @@ -6,6 +6,8 @@ pub mod error; pub mod files; pub mod helper; pub mod proxy; +#[cfg(feature = "mond")] +pub mod reporting; use crate::{ config::{Config, HostConfig}, @@ -26,6 +28,8 @@ use hyper::{ Request, Response, StatusCode, }; use log::{debug, error, info, warn}; +#[cfg(feature = "mond")] +use reporting::Reporting; use std::{fs::File, io::BufReader, net::SocketAddr, path::Path, process::exit, sync::Arc}; use tokio::{net::TcpListener, signal::ctrl_c, sync::Semaphore}; use tokio_rustls::TlsAcceptor; @@ -34,6 +38,8 @@ pub struct State { pub config: Config, pub l_incoming: Semaphore, pub l_outgoing: Semaphore, + #[cfg(feature = "mond")] + pub reporting: Reporting, } #[tokio::main] @@ -54,6 +60,8 @@ async fn main() -> anyhow::Result<()> { let state = Arc::new(State { l_incoming: Semaphore::new(config.limits.max_incoming_connections), l_outgoing: Semaphore::new(config.limits.max_outgoing_connections), + #[cfg(feature = "mond")] + reporting: Reporting::new(&config), config, }); @@ -204,18 +212,19 @@ async fn service( addr: SocketAddr, ) -> Result>, ServiceError> { debug!("{addr} ~> {:?} {}", req.headers().get(HOST), req.uri()); - - let route = state - .config - .hosts - .get(remove_port( - &req.headers() - .get(HOST) - .and_then(|e| e.to_str().ok()) - .map(String::from) - .unwrap_or(String::from("")), - )) - .ok_or(ServiceError::NoHost)?; + #[cfg(feature = "mond")] + state.reporting.request_in.inc(); + + let host = req + .headers() + .get(HOST) + .and_then(|e| e.to_str().ok()) + .map(String::from) + .unwrap_or(String::from("")); + let host = remove_port(&host); + let route = state.config.hosts.get(host).ok_or(ServiceError::NoHost)?; + #[cfg(feature = "mond")] + state.reporting.hosts.get(host).unwrap().requests_in.inc(); let mut resp = match route { HostConfig::Backend { backend } => proxy_request(&state, req, addr, backend).await, -- cgit v1.2.3-70-g09d2