From 2478a05c898f1b6be7a85e0b908d6aeeaa4b2066 Mon Sep 17 00:00:00 2001 From: LittleSheep Date: Tue, 13 Feb 2024 01:20:27 +0800 Subject: [PATCH] :sparkles: More details trace --- src/main.rs | 2 ++ src/proxies/metrics.rs | 44 +++++++++++++++++++++++++++++------------- src/proxies/route.rs | 16 ++++++++++++--- 3 files changed, 46 insertions(+), 16 deletions(-) diff --git a/src/main.rs b/src/main.rs index 6373103..72ae36a 100644 --- a/src/main.rs +++ b/src/main.rs @@ -4,6 +4,7 @@ mod sideload; pub mod warden; use actix_web::{App, HttpServer, web}; +use actix_web::middleware::Logger; use actix_web_httpauth::extractors::AuthenticationError; use actix_web_httpauth::headers::www_authenticate::basic::Basic; use actix_web_httpauth::middleware::HttpAuthentication; @@ -44,6 +45,7 @@ async fn main() -> Result<(), std::io::Error> { // Proxies let proxies_server = HttpServer::new(|| { App::new() + .wrap(Logger::default()) .app_data(web::Data::new(Client::default())) .route("/", web::to(route::handle)) }).bind( diff --git a/src/proxies/metrics.rs b/src/proxies/metrics.rs index a464ed7..ba5c6c8 100644 --- a/src/proxies/metrics.rs +++ b/src/proxies/metrics.rs @@ -9,12 +9,22 @@ pub struct RoadTrace { pub region: String, pub location: String, pub destination: String, + pub ip_address: String, + pub user_agent: String, pub error: Option, } impl RoadTrace { - pub fn from_structs(reg: Region, loc: Location, end: Destination) -> RoadTrace { + pub fn from_structs( + ip: String, + ua: String, + reg: Region, + loc: Location, + end: Destination, + ) -> RoadTrace { RoadTrace { + ip_address: ip, + user_agent: ua, region: reg.id, location: loc.id, destination: end.id, @@ -23,17 +33,16 @@ impl RoadTrace { } pub fn from_structs_with_error( + ip: String, + ua: String, reg: Region, loc: Location, end: Destination, err: String, ) -> RoadTrace { - RoadTrace { - region: reg.id, - location: loc.id, - destination: end.id, - error: Some(err), - } + let mut trace = Self::from_structs(ip, ua, reg, loc, end); + trace.error = Some(err); + return trace; } } @@ -46,7 +55,7 @@ pub struct RoadMetrics { pub recent_errors: VecDeque, } -const MAX_TRACE_COUNT: usize = 10; +const MAX_TRACE_COUNT: usize = 32; impl RoadMetrics { pub fn new() -> RoadMetrics { @@ -66,26 +75,35 @@ impl RoadMetrics { } } - pub fn add_success_request(&mut self, reg: Region, loc: Location, end: Destination) { + pub fn add_success_request( + &mut self, + ip: String, + ua: String, + reg: Region, + loc: Location, + end: Destination, + ) { self.requests_count += 1; self.recent_successes - .push_back(RoadTrace::from_structs(reg, loc, end)); + .push_back(RoadTrace::from_structs(ip, ua, reg, loc, end)); if self.recent_successes.len() > MAX_TRACE_COUNT { self.recent_successes.pop_front(); } } - pub fn add_faliure_request( + pub fn add_failure_request( &mut self, + ip: String, + ua: String, reg: Region, loc: Location, end: Destination, - err: String, // For some reason error is rarely clonable, so we use preformatted message + err: String, // For some reason error is rarely cloneable, so we use preformatted message ) { self.requests_count += 1; self.failures_count += 1; self.recent_errors - .push_back(RoadTrace::from_structs_with_error(reg, loc, end, err)); + .push_back(RoadTrace::from_structs_with_error(ip, ua, reg, loc, end, err)); if self.recent_errors.len() > MAX_TRACE_COUNT { self.recent_errors.pop_front(); } diff --git a/src/proxies/route.rs b/src/proxies/route.rs index ccb881b..4f968ef 100644 --- a/src/proxies/route.rs +++ b/src/proxies/route.rs @@ -1,4 +1,5 @@ use actix_web::{HttpRequest, HttpResponse, web}; +use actix_web::http::header; use awc::Client; use rand::seq::SliceRandom; @@ -59,11 +60,20 @@ pub async fn handle(req: HttpRequest, client: web::Data) -> HttpResponse let loc = location.clone(); let end = destination.clone(); - return match forward(&end, req, client).await { + let ip = match req.peer_addr() { + None => "unknown".to_string(), + Some(val) => val.ip().to_string() + }; + let ua = match req.headers().get(header::USER_AGENT) { + None => "unknown".to_string(), + Some(val) => val.to_str().unwrap().to_string(), + }; + + match forward(&end, req, client).await { Ok(resp) => { tokio::spawn(async move { let writable_app = &mut ROAD.lock().await; - writable_app.metrics.add_success_request(reg, loc, end); + writable_app.metrics.add_success_request(ip, ua, reg, loc, end); }); resp } @@ -72,7 +82,7 @@ pub async fn handle(req: HttpRequest, client: web::Data) -> HttpResponse let writable_app = &mut ROAD.lock().await; writable_app .metrics - .add_faliure_request(reg, loc, end, "TODO".to_owned()); + .add_failure_request(ip, ua, reg, loc, end, "TODO".to_owned()); }); resp }