From 0388e1d1439bbc1d2ef7b59bf225a2d152358a2b Mon Sep 17 00:00:00 2001 From: Jose Celano Date: Tue, 25 Jun 2024 13:20:17 +0100 Subject: [PATCH] refactor: extract consts for logging targets --- src/bootstrap/jobs/health_check_api.rs | 8 +-- src/bootstrap/jobs/udp_tracker.rs | 7 +-- src/console/ci/e2e/logs_parser.rs | 7 +-- src/servers/apis/mod.rs | 6 ++- src/servers/apis/routes.rs | 5 +- src/servers/apis/server.rs | 9 ++-- src/servers/health_check_api/mod.rs | 2 + src/servers/health_check_api/server.rs | 7 +-- src/servers/http/mod.rs | 2 + src/servers/http/server.rs | 5 +- src/servers/http/v1/routes.rs | 5 +- src/servers/udp/logging.rs | 13 ++--- src/servers/udp/mod.rs | 2 + src/servers/udp/server.rs | 54 ++++++++++--------- src/shared/bit_torrent/tracker/udp/client.rs | 12 +++-- tests/servers/health_check_api/environment.rs | 10 ++-- 16 files changed, 87 insertions(+), 67 deletions(-) diff --git a/src/bootstrap/jobs/health_check_api.rs b/src/bootstrap/jobs/health_check_api.rs index c22a4cf95..e79a6da77 100644 --- a/src/bootstrap/jobs/health_check_api.rs +++ b/src/bootstrap/jobs/health_check_api.rs @@ -20,7 +20,7 @@ use torrust_tracker_configuration::HealthCheckApi; use tracing::info; use super::Started; -use crate::servers::health_check_api::server; +use crate::servers::health_check_api::{server, HEALTH_CHECK_API_LOG_TARGET}; use crate::servers::registar::ServiceRegistry; use crate::servers::signals::Halted; @@ -44,18 +44,18 @@ pub async fn start_job(config: &HealthCheckApi, register: ServiceRegistry) -> Jo // Run the API server let join_handle = tokio::spawn(async move { - info!(target: "HEALTH CHECK API", "Starting on: {protocol}://{}", bind_addr); + info!(target: HEALTH_CHECK_API_LOG_TARGET, "Starting on: {protocol}://{}", bind_addr); let handle = server::start(bind_addr, tx_start, rx_halt, register); if let Ok(()) = handle.await { - info!(target: "HEALTH CHECK API", "Stopped server running on: {protocol}://{}", bind_addr); + info!(target: HEALTH_CHECK_API_LOG_TARGET, "Stopped server running on: {protocol}://{}", bind_addr); } }); // Wait until the server sends the started message match rx_start.await { - Ok(msg) => info!(target: "HEALTH CHECK API", "Started on: {protocol}://{}", msg.address), + Ok(msg) => info!(target: HEALTH_CHECK_API_LOG_TARGET, "Started on: {protocol}://{}", msg.address), Err(e) => panic!("the Health Check API server was dropped: {e}"), } diff --git a/src/bootstrap/jobs/udp_tracker.rs b/src/bootstrap/jobs/udp_tracker.rs index 2c09e6de2..ba39df2fe 100644 --- a/src/bootstrap/jobs/udp_tracker.rs +++ b/src/bootstrap/jobs/udp_tracker.rs @@ -15,6 +15,7 @@ use tracing::debug; use crate::core; use crate::servers::registar::ServiceRegistrationForm; use crate::servers::udp::server::{Launcher, UdpServer}; +use crate::servers::udp::UDP_TRACKER_LOG_TARGET; /// It starts a new UDP server with the provided configuration. /// @@ -35,8 +36,8 @@ pub async fn start_job(config: &UdpTracker, tracker: Arc, form: S .expect("it should be able to start the udp tracker"); tokio::spawn(async move { - debug!(target: "UDP TRACKER", "Wait for launcher (UDP service) to finish ..."); - debug!(target: "UDP TRACKER", "Is halt channel closed before waiting?: {}", server.state.halt_task.is_closed()); + debug!(target: UDP_TRACKER_LOG_TARGET, "Wait for launcher (UDP service) to finish ..."); + debug!(target: UDP_TRACKER_LOG_TARGET, "Is halt channel closed before waiting?: {}", server.state.halt_task.is_closed()); assert!( !server.state.halt_task.is_closed(), @@ -49,6 +50,6 @@ pub async fn start_job(config: &UdpTracker, tracker: Arc, form: S .await .expect("it should be able to join to the udp tracker task"); - debug!(target: "UDP TRACKER", "Is halt channel closed after finishing the server?: {}", server.state.halt_task.is_closed()); + debug!(target: UDP_TRACKER_LOG_TARGET, "Is halt channel closed after finishing the server?: {}", server.state.halt_task.is_closed()); }) } diff --git a/src/console/ci/e2e/logs_parser.rs b/src/console/ci/e2e/logs_parser.rs index 4886786de..8bf7974c1 100644 --- a/src/console/ci/e2e/logs_parser.rs +++ b/src/console/ci/e2e/logs_parser.rs @@ -2,10 +2,11 @@ use regex::Regex; use serde::{Deserialize, Serialize}; +use crate::servers::health_check_api::HEALTH_CHECK_API_LOG_TARGET; +use crate::servers::http::HTTP_TRACKER_LOG_TARGET; +use crate::servers::udp::UDP_TRACKER_LOG_TARGET; + const INFO_LOG_LEVEL: &str = "INFO"; -const UDP_TRACKER_LOG_TARGET: &str = "UDP TRACKER"; -const HTTP_TRACKER_LOG_TARGET: &str = "HTTP TRACKER"; -const HEALTH_CHECK_API_LOG_TARGET: &str = "HEALTH CHECK API"; #[derive(Serialize, Deserialize, Debug, Default)] pub struct RunningServices { diff --git a/src/servers/apis/mod.rs b/src/servers/apis/mod.rs index 6dae66c2d..b44ccab9f 100644 --- a/src/servers/apis/mod.rs +++ b/src/servers/apis/mod.rs @@ -157,6 +157,10 @@ pub mod routes; pub mod server; pub mod v1; +use serde::{Deserialize, Serialize}; + +pub const API_LOG_TARGET: &str = "API"; + /// The info hash URL path parameter. /// /// Some API endpoints require an info hash as a path parameter. @@ -169,8 +173,6 @@ pub mod v1; #[derive(Deserialize)] pub struct InfoHashParam(pub String); -use serde::{Deserialize, Serialize}; - /// The version of the HTTP Api. #[derive(Serialize, Deserialize, Copy, Clone, PartialEq, Eq, Debug)] pub enum Version { diff --git a/src/servers/apis/routes.rs b/src/servers/apis/routes.rs index 087bcfa4a..2001afc2f 100644 --- a/src/servers/apis/routes.rs +++ b/src/servers/apis/routes.rs @@ -27,6 +27,7 @@ use super::v1; use super::v1::context::health_check::handlers::health_check_handler; use super::v1::middlewares::auth::State; use crate::core::Tracker; +use crate::servers::apis::API_LOG_TARGET; const TIMEOUT: Duration = Duration::from_secs(5); @@ -60,7 +61,7 @@ pub fn router(tracker: Arc, access_tokens: Arc) -> Router .unwrap_or_default(); tracing::span!( - target: "API", + target: API_LOG_TARGET, tracing::Level::INFO, "request", method = %method, uri = %uri, request_id = %request_id); }) .on_response(|response: &Response, latency: Duration, _span: &Span| { @@ -73,7 +74,7 @@ pub fn router(tracker: Arc, access_tokens: Arc) -> Router let latency_ms = latency.as_millis(); tracing::span!( - target: "API", + target: API_LOG_TARGET, tracing::Level::INFO, "response", latency = %latency_ms, status = %status_code, request_id = %request_id); }), ) diff --git a/src/servers/apis/server.rs b/src/servers/apis/server.rs index 246660ab1..d47e5d542 100644 --- a/src/servers/apis/server.rs +++ b/src/servers/apis/server.rs @@ -37,6 +37,7 @@ use tracing::{debug, error, info}; use super::routes::router; use crate::bootstrap::jobs::Started; use crate::core::Tracker; +use crate::servers::apis::API_LOG_TARGET; use crate::servers::custom_axum_server::{self, TimeoutAcceptor}; use crate::servers::registar::{ServiceHealthCheckJob, ServiceRegistration, ServiceRegistrationForm}; use crate::servers::signals::{graceful_shutdown, Halted}; @@ -121,11 +122,11 @@ impl ApiServer { let launcher = self.state.launcher; let task = tokio::spawn(async move { - debug!(target: "API", "Starting with launcher in spawned task ..."); + debug!(target: API_LOG_TARGET, "Starting with launcher in spawned task ..."); let _task = launcher.start(tracker, access_tokens, tx_start, rx_halt).await; - debug!(target: "API", "Started with launcher in spawned task"); + debug!(target: API_LOG_TARGET, "Started with launcher in spawned task"); launcher }); @@ -231,7 +232,7 @@ impl Launcher { let tls = self.tls.clone(); let protocol = if tls.is_some() { "https" } else { "http" }; - info!(target: "API", "Starting on {protocol}://{}", address); + info!(target: API_LOG_TARGET, "Starting on {protocol}://{}", address); let running = Box::pin(async { match tls { @@ -250,7 +251,7 @@ impl Launcher { } }); - info!(target: "API", "Started on {protocol}://{}", address); + info!(target: API_LOG_TARGET, "Started on {protocol}://{}", address); tx_start .send(Started { address }) diff --git a/src/servers/health_check_api/mod.rs b/src/servers/health_check_api/mod.rs index ec608387d..24c5232c8 100644 --- a/src/servers/health_check_api/mod.rs +++ b/src/servers/health_check_api/mod.rs @@ -2,3 +2,5 @@ pub mod handlers; pub mod resources; pub mod responses; pub mod server; + +pub const HEALTH_CHECK_API_LOG_TARGET: &str = "HEALTH CHECK API"; diff --git a/src/servers/health_check_api/server.rs b/src/servers/health_check_api/server.rs index f03753573..89fbafe45 100644 --- a/src/servers/health_check_api/server.rs +++ b/src/servers/health_check_api/server.rs @@ -22,6 +22,7 @@ use tracing::{debug, Level, Span}; use crate::bootstrap::jobs::Started; use crate::servers::health_check_api::handlers::health_check_handler; +use crate::servers::health_check_api::HEALTH_CHECK_API_LOG_TARGET; use crate::servers::registar::ServiceRegistry; use crate::servers::signals::{graceful_shutdown, Halted}; @@ -56,7 +57,7 @@ pub fn start( .unwrap_or_default(); tracing::span!( - target: "HEALTH CHECK API", + target: HEALTH_CHECK_API_LOG_TARGET, tracing::Level::INFO, "request", method = %method, uri = %uri, request_id = %request_id); }) .on_response(|response: &Response, latency: Duration, _span: &Span| { @@ -69,7 +70,7 @@ pub fn start( let latency_ms = latency.as_millis(); tracing::span!( - target: "HEALTH CHECK API", + target: HEALTH_CHECK_API_LOG_TARGET, tracing::Level::INFO, "response", latency = %latency_ms, status = %status_code, request_id = %request_id); }), ) @@ -80,7 +81,7 @@ pub fn start( let handle = Handle::new(); - debug!(target: "HEALTH CHECK API", "Starting service with graceful shutdown in a spawned task ..."); + debug!(target: HEALTH_CHECK_API_LOG_TARGET, "Starting service with graceful shutdown in a spawned task ..."); tokio::task::spawn(graceful_shutdown( handle.clone(), diff --git a/src/servers/http/mod.rs b/src/servers/http/mod.rs index e50e3c351..4ef5ca7ea 100644 --- a/src/servers/http/mod.rs +++ b/src/servers/http/mod.rs @@ -309,6 +309,8 @@ pub mod percent_encoding; pub mod server; pub mod v1; +pub const HTTP_TRACKER_LOG_TARGET: &str = "HTTP TRACKER"; + /// The version of the HTTP tracker. #[derive(Serialize, Deserialize, Copy, Clone, PartialEq, Eq, Debug)] pub enum Version { diff --git a/src/servers/http/server.rs b/src/servers/http/server.rs index 5798f7c10..9199573b0 100644 --- a/src/servers/http/server.rs +++ b/src/servers/http/server.rs @@ -13,6 +13,7 @@ use super::v1::routes::router; use crate::bootstrap::jobs::Started; use crate::core::Tracker; use crate::servers::custom_axum_server::{self, TimeoutAcceptor}; +use crate::servers::http::HTTP_TRACKER_LOG_TARGET; use crate::servers::registar::{ServiceHealthCheckJob, ServiceRegistration, ServiceRegistrationForm}; use crate::servers::signals::{graceful_shutdown, Halted}; @@ -55,7 +56,7 @@ impl Launcher { let tls = self.tls.clone(); let protocol = if tls.is_some() { "https" } else { "http" }; - info!(target: "HTTP TRACKER", "Starting on: {protocol}://{}", address); + info!(target: HTTP_TRACKER_LOG_TARGET, "Starting on: {protocol}://{}", address); let app = router(tracker, address); @@ -76,7 +77,7 @@ impl Launcher { } }); - info!(target: "HTTP TRACKER", "Started on: {protocol}://{}", address); + info!(target: HTTP_TRACKER_LOG_TARGET, "Started on: {protocol}://{}", address); tx_start .send(Started { address }) diff --git a/src/servers/http/v1/routes.rs b/src/servers/http/v1/routes.rs index 14641dc1d..b2f37880c 100644 --- a/src/servers/http/v1/routes.rs +++ b/src/servers/http/v1/routes.rs @@ -20,6 +20,7 @@ use tracing::{Level, Span}; use super::handlers::{announce, health_check, scrape}; use crate::core::Tracker; +use crate::servers::http::HTTP_TRACKER_LOG_TARGET; const TIMEOUT: Duration = Duration::from_secs(5); @@ -56,7 +57,7 @@ pub fn router(tracker: Arc, server_socket_addr: SocketAddr) -> Router { .unwrap_or_default(); tracing::span!( - target:"HTTP TRACKER", + target: HTTP_TRACKER_LOG_TARGET, tracing::Level::INFO, "request", server_socket_addr= %server_socket_addr, method = %method, uri = %uri, request_id = %request_id); }) .on_response(move |response: &Response, latency: Duration, _span: &Span| { @@ -69,7 +70,7 @@ pub fn router(tracker: Arc, server_socket_addr: SocketAddr) -> Router { let latency_ms = latency.as_millis(); tracing::span!( - target: "HTTP TRACKER", + target: HTTP_TRACKER_LOG_TARGET, tracing::Level::INFO, "response", server_socket_addr= %server_socket_addr, latency = %latency_ms, status = %status_code, request_id = %request_id); }), ) diff --git a/src/servers/udp/logging.rs b/src/servers/udp/logging.rs index 9bbb48f6a..3891278d7 100644 --- a/src/servers/udp/logging.rs +++ b/src/servers/udp/logging.rs @@ -7,6 +7,7 @@ use aquatic_udp_protocol::{Request, Response, TransactionId}; use torrust_tracker_primitives::info_hash::InfoHash; use super::handlers::RequestId; +use crate::servers::udp::UDP_TRACKER_LOG_TARGET; pub fn log_request(request: &Request, request_id: &RequestId, server_socket_addr: &SocketAddr) { let action = map_action_name(request); @@ -17,7 +18,7 @@ pub fn log_request(request: &Request, request_id: &RequestId, server_socket_addr let transaction_id_str = transaction_id.0.to_string(); tracing::span!( - target: "UDP TRACKER", + target: UDP_TRACKER_LOG_TARGET, tracing::Level::INFO, "request", server_socket_addr = %server_socket_addr, action = %action, transaction_id = %transaction_id_str, request_id = %request_id); } Request::Announce(announce_request) => { @@ -27,7 +28,7 @@ pub fn log_request(request: &Request, request_id: &RequestId, server_socket_addr let info_hash_str = InfoHash::from_bytes(&announce_request.info_hash.0).to_hex_string(); tracing::span!( - target: "UDP TRACKER", + target: UDP_TRACKER_LOG_TARGET, tracing::Level::INFO, "request", server_socket_addr = %server_socket_addr, action = %action, transaction_id = %transaction_id_str, request_id = %request_id, connection_id = %connection_id_str, info_hash = %info_hash_str); } Request::Scrape(scrape_request) => { @@ -36,7 +37,7 @@ pub fn log_request(request: &Request, request_id: &RequestId, server_socket_addr let connection_id_str = scrape_request.connection_id.0.to_string(); tracing::span!( - target: "UDP TRACKER", + target: UDP_TRACKER_LOG_TARGET, tracing::Level::INFO, "request", server_socket_addr = %server_socket_addr, @@ -64,7 +65,7 @@ pub fn log_response( latency: Duration, ) { tracing::span!( - target: "UDP TRACKER", + target: UDP_TRACKER_LOG_TARGET, tracing::Level::INFO, "response", server_socket_addr = %server_socket_addr, @@ -75,12 +76,12 @@ pub fn log_response( pub fn log_bad_request(request_id: &RequestId) { tracing::span!( - target: "UDP TRACKER", + target: UDP_TRACKER_LOG_TARGET, tracing::Level::INFO, "bad request", request_id = %request_id); } pub fn log_error_response(request_id: &RequestId) { tracing::span!( - target: "UDP TRACKER", + target: UDP_TRACKER_LOG_TARGET, tracing::Level::INFO, "response", request_id = %request_id); } diff --git a/src/servers/udp/mod.rs b/src/servers/udp/mod.rs index 3062a4393..5c5460397 100644 --- a/src/servers/udp/mod.rs +++ b/src/servers/udp/mod.rs @@ -649,6 +649,8 @@ pub mod peer_builder; pub mod request; pub mod server; +pub const UDP_TRACKER_LOG_TARGET: &str = "UDP TRACKER"; + /// Number of bytes. pub type Bytes = u64; /// The port the peer is listening on. diff --git a/src/servers/udp/server.rs b/src/servers/udp/server.rs index 229729038..e60e49ace 100644 --- a/src/servers/udp/server.rs +++ b/src/servers/udp/server.rs @@ -42,7 +42,7 @@ use crate::bootstrap::jobs::Started; use crate::core::Tracker; use crate::servers::registar::{ServiceHealthCheckJob, ServiceRegistration, ServiceRegistrationForm}; use crate::servers::signals::{shutdown_signal_with_message, Halted}; -use crate::servers::udp::handlers; +use crate::servers::udp::{handlers, UDP_TRACKER_LOG_TARGET}; use crate::shared::bit_torrent::tracker::udp::client::check; use crate::shared::bit_torrent::tracker::udp::MAX_PACKET_SIZE; @@ -150,7 +150,7 @@ impl UdpServer { }, }; - tracing::trace!(target: "UDP TRACKER: UdpServer::start", local_addr, "(running)"); + tracing::trace!(target: UDP_TRACKER_LOG_TARGET, local_addr, "UdpServer::start (running)"); Ok(running_udp_server) } @@ -248,7 +248,7 @@ impl BoundSocket { }; let local_addr = format!("udp://{addr}"); - tracing::debug!(target: "UDP TRACKER: UdpSocket::new", local_addr, "(bound)"); + tracing::debug!(target: UDP_TRACKER_LOG_TARGET, local_addr, "UdpSocket::new (bound)"); Ok(Self { socket: Arc::new(socket), @@ -347,6 +347,8 @@ impl Udp { format!("Halting UDP Service Bound to Socket: {bind_to}"), )); + tracing::info!(target: UDP_TRACKER_LOG_TARGET, "Starting on: {bind_to}"); + let socket = tokio::time::timeout(Duration::from_millis(5000), BoundSocket::new(bind_to)) .await .expect("it should bind to the socket within five seconds"); @@ -354,7 +356,7 @@ impl Udp { let bound_socket = match socket { Ok(socket) => socket, Err(e) => { - tracing::error!(target: "UDP TRACKER: Udp::run_with_graceful_shutdown", addr = %bind_to, err = %e, "panic! (error when building socket)" ); + tracing::error!(target: UDP_TRACKER_LOG_TARGET, addr = %bind_to, err = %e, "Udp::run_with_graceful_shutdown panic! (error when building socket)" ); panic!("could not bind to socket!"); } }; @@ -364,18 +366,18 @@ impl Udp { // note: this log message is parsed by our container. i.e: // - // `[UDP TRACKER][INFO] Starting on: udp://` + // `INFO UDP TRACKER: Started on: udp://0.0.0.0:6969` // - tracing::info!(target: "UDP TRACKER", "Starting on: {local_udp_url}"); + tracing::info!(target: UDP_TRACKER_LOG_TARGET, "Started on: {local_udp_url}"); let receiver = Receiver::new(bound_socket.into(), tracker); - tracing::trace!(target: "UDP TRACKER: Udp::run_with_graceful_shutdown", local_udp_url, "(spawning main loop)"); + tracing::trace!(target: UDP_TRACKER_LOG_TARGET, local_udp_url, "Udp::run_with_graceful_shutdown (spawning main loop)"); let running = { let local_addr = local_udp_url.clone(); tokio::task::spawn(async move { - tracing::debug!(target: "UDP TRACKER: Udp::run_with_graceful_shutdown::task", local_addr, "(listening...)"); + tracing::debug!(target: UDP_TRACKER_LOG_TARGET, local_addr, "Udp::run_with_graceful_shutdown::task (listening...)"); let () = Self::run_udp_server_main(receiver).await; }) }; @@ -384,13 +386,13 @@ impl Udp { .send(Started { address }) .expect("the UDP Tracker service should not be dropped"); - tracing::debug!(target: "UDP TRACKER: Udp::run_with_graceful_shutdown", local_udp_url, "(started)"); + tracing::debug!(target: UDP_TRACKER_LOG_TARGET, local_udp_url, "Udp::run_with_graceful_shutdown (started)"); let stop = running.abort_handle(); select! { - _ = running => { tracing::debug!(target: "UDP TRACKER: Udp::run_with_graceful_shutdown", local_udp_url, "(stopped)"); }, - _ = halt_task => { tracing::debug!(target: "UDP TRACKER: Udp::run_with_graceful_shutdown",local_udp_url, "(halting)"); } + _ = running => { tracing::debug!(target: UDP_TRACKER_LOG_TARGET, local_udp_url, "Udp::run_with_graceful_shutdown (stopped)"); }, + _ = halt_task => { tracing::debug!(target: UDP_TRACKER_LOG_TARGET, local_udp_url, "Udp::run_with_graceful_shutdown (halting)"); } } stop.abort(); @@ -405,19 +407,19 @@ impl Udp { loop { if let Some(req) = { - tracing::trace!(target: "UDP TRACKER: Udp::run_udp_server", local_addr, "(wait for request)"); + tracing::trace!(target: UDP_TRACKER_LOG_TARGET, local_addr, "Udp::run_udp_server (wait for request)"); receiver.next().await } { - tracing::trace!(target: "UDP TRACKER: Udp::run_udp_server::loop", local_addr, "(in)"); + tracing::trace!(target: UDP_TRACKER_LOG_TARGET, local_addr, "Udp::run_udp_server::loop (in)"); let req = match req { Ok(req) => req, Err(e) => { if e.kind() == std::io::ErrorKind::Interrupted { - tracing::warn!(target: "UDP TRACKER: Udp::run_udp_server::loop", local_addr, err = %e, "(interrupted)"); + tracing::warn!(target: UDP_TRACKER_LOG_TARGET, local_addr, err = %e, "Udp::run_udp_server::loop (interrupted)"); return; } - tracing::error!(target: "UDP TRACKER: Udp::run_udp_server::loop", local_addr, err = %e, "break: (got error)"); + tracing::error!(target: UDP_TRACKER_LOG_TARGET, local_addr, err = %e, "Udp::run_udp_server::loop break: (got error)"); break; } }; @@ -450,13 +452,13 @@ impl Udp { continue; } - tracing::debug!(target: "UDP TRACKER: Udp::run_udp_server::loop", local_addr, removed_count = finished, "(got unfinished task)"); + tracing::debug!(target: UDP_TRACKER_LOG_TARGET, local_addr, removed_count = finished, "Udp::run_udp_server::loop (got unfinished task)"); if finished == 0 { // we have _no_ finished tasks.. will abort the unfinished task to make space... h.abort(); - tracing::warn!(target: "UDP TRACKER: Udp::run_udp_server::loop", local_addr, "aborting request: (no finished tasks)"); + tracing::warn!(target: UDP_TRACKER_LOG_TARGET, local_addr, "Udp::run_udp_server::loop aborting request: (no finished tasks)"); break; } @@ -476,19 +478,19 @@ impl Udp { } else { tokio::task::yield_now().await; // the request iterator returned `None`. - tracing::error!(target: "UDP TRACKER: Udp::run_udp_server", local_addr, "breaking: (ran dry, should not happen in production!)"); + tracing::error!(target: UDP_TRACKER_LOG_TARGET, local_addr, "Udp::run_udp_server breaking: (ran dry, should not happen in production!)"); break; } } } async fn process_request(request: UdpRequest, tracker: Arc, socket: Arc) { - tracing::trace!(target: "UDP TRACKER: Udp::process_request", request = %request.from, "(receiving)"); + tracing::trace!(target: UDP_TRACKER_LOG_TARGET, request = %request.from, "Udp::process_request (receiving)"); Self::process_valid_request(tracker, socket, request).await; } async fn process_valid_request(tracker: Arc, socket: Arc, udp_request: UdpRequest) { - tracing::trace!(target: "UDP TRACKER: Udp::process_valid_request", "Making Response to {udp_request:?}"); + tracing::trace!(target: UDP_TRACKER_LOG_TARGET, "Udp::process_valid_request. Making Response to {udp_request:?}"); let from = udp_request.from; let response = handlers::handle_packet(udp_request, &tracker.clone(), socket.local_addr()).await; Self::send_response(&socket.clone(), from, response).await; @@ -503,7 +505,7 @@ impl Udp { Response::Error(e) => format!("Error: {e:?}"), }; - tracing::debug!(target: "UDP TRACKER: Udp::send_response", target = ?to, response_type, "(sending)"); + tracing::debug!(target: UDP_TRACKER_LOG_TARGET, target = ?to, response_type, "Udp::send_response (sending)"); let buffer = vec![0u8; MAX_PACKET_SIZE]; let mut cursor = Cursor::new(buffer); @@ -514,21 +516,21 @@ impl Udp { let position = cursor.position() as usize; let inner = cursor.get_ref(); - tracing::debug!(target: "UDP TRACKER: Udp::send_response", ?to, bytes_count = &inner[..position].len(), "(sending...)" ); - tracing::trace!(target: "UDP TRACKER: Udp::send_response", ?to, bytes_count = &inner[..position].len(), payload = ?&inner[..position], "(sending...)"); + tracing::debug!(target: UDP_TRACKER_LOG_TARGET, ?to, bytes_count = &inner[..position].len(), "Udp::send_response (sending...)" ); + tracing::trace!(target: UDP_TRACKER_LOG_TARGET, ?to, bytes_count = &inner[..position].len(), payload = ?&inner[..position], "Udp::send_response (sending...)"); Self::send_packet(bound_socket, &to, &inner[..position]).await; - tracing::trace!(target: "UDP TRACKER: Udp::send_response", ?to, bytes_count = &inner[..position].len(), "(sent)"); + tracing::trace!(target:UDP_TRACKER_LOG_TARGET, ?to, bytes_count = &inner[..position].len(), "Udp::send_response (sent)"); } Err(e) => { - tracing::error!(target: "UDP TRACKER: Udp::send_response", ?to, response_type, err = %e, "(error)"); + tracing::error!(target: UDP_TRACKER_LOG_TARGET, ?to, response_type, err = %e, "Udp::send_response (error)"); } } } async fn send_packet(socket: &Arc, remote_addr: &SocketAddr, payload: &[u8]) { - tracing::trace!(target: "UDP TRACKER: Udp::send_response", to = %remote_addr, ?payload, "(sending)"); + tracing::trace!(target: UDP_TRACKER_LOG_TARGET, to = %remote_addr, ?payload, "Udp::send_response (sending)"); // doesn't matter if it reaches or not drop(socket.send_to(payload, remote_addr).await); diff --git a/src/shared/bit_torrent/tracker/udp/client.rs b/src/shared/bit_torrent/tracker/udp/client.rs index 900543462..dce596e08 100644 --- a/src/shared/bit_torrent/tracker/udp/client.rs +++ b/src/shared/bit_torrent/tracker/udp/client.rs @@ -13,6 +13,8 @@ use zerocopy::network_endian::I32; use crate::shared::bit_torrent::tracker::udp::{source_address, MAX_PACKET_SIZE}; +pub const UDP_CLIENT_LOG_TARGET: &str = "UDP CLIENT"; + /// Default timeout for sending and receiving packets. And waiting for sockets /// to be readable and writable. pub const DEFAULT_TIMEOUT: Duration = Duration::from_secs(5); @@ -82,7 +84,7 @@ impl UdpClient { /// - Can't write to the socket. /// - Can't send data. pub async fn send(&self, bytes: &[u8]) -> Result { - debug!(target: "UDP client", "sending {bytes:?} ..."); + debug!(target: UDP_CLIENT_LOG_TARGET, "sending {bytes:?} ..."); match time::timeout(self.timeout, self.socket.writable()).await { Ok(writable_result) => { @@ -115,7 +117,7 @@ impl UdpClient { pub async fn receive(&self) -> Result> { let mut response_buffer = [0u8; MAX_PACKET_SIZE]; - debug!(target: "UDP client", "receiving ..."); + debug!(target: UDP_CLIENT_LOG_TARGET, "receiving ..."); match time::timeout(self.timeout, self.socket.readable()).await { Ok(readable_result) => { @@ -138,7 +140,7 @@ impl UdpClient { let mut res: Vec = response_buffer.to_vec(); Vec::truncate(&mut res, size); - debug!(target: "UDP client", "{size} bytes received {res:?}"); + debug!(target: UDP_CLIENT_LOG_TARGET, "{size} bytes received {res:?}"); Ok(res) } @@ -168,7 +170,7 @@ impl UdpTrackerClient { /// /// Will return error if can't write request to bytes. pub async fn send(&self, request: Request) -> Result { - debug!(target: "UDP tracker client", "send request {request:?}"); + debug!(target: UDP_CLIENT_LOG_TARGET, "send request {request:?}"); // Write request into a buffer let request_buffer = vec![0u8; MAX_PACKET_SIZE]; @@ -196,7 +198,7 @@ impl UdpTrackerClient { pub async fn receive(&self) -> Result { let payload = self.udp_client.receive().await?; - debug!(target: "UDP tracker client", "received {} bytes. Response {payload:?}", payload.len()); + debug!(target: UDP_CLIENT_LOG_TARGET, "received {} bytes. Response {payload:?}", payload.len()); let response = Response::parse_bytes(&payload, true)?; diff --git a/tests/servers/health_check_api/environment.rs b/tests/servers/health_check_api/environment.rs index a50ad5156..cf0566d67 100644 --- a/tests/servers/health_check_api/environment.rs +++ b/tests/servers/health_check_api/environment.rs @@ -4,7 +4,7 @@ use std::sync::Arc; use tokio::sync::oneshot::{self, Sender}; use tokio::task::JoinHandle; use torrust_tracker::bootstrap::jobs::Started; -use torrust_tracker::servers::health_check_api::server; +use torrust_tracker::servers::health_check_api::{server, HEALTH_CHECK_API_LOG_TARGET}; use torrust_tracker::servers::registar::Registar; use torrust_tracker::servers::signals::{self, Halted}; use torrust_tracker_configuration::HealthCheckApi; @@ -49,21 +49,21 @@ impl Environment { let register = self.registar.entries(); - debug!(target: "HEALTH CHECK API", "Spawning task to launch the service ..."); + debug!(target: HEALTH_CHECK_API_LOG_TARGET, "Spawning task to launch the service ..."); let server = tokio::spawn(async move { - debug!(target: "HEALTH CHECK API", "Starting the server in a spawned task ..."); + debug!(target: HEALTH_CHECK_API_LOG_TARGET, "Starting the server in a spawned task ..."); server::start(self.state.bind_to, tx_start, rx_halt, register) .await .expect("it should start the health check service"); - debug!(target: "HEALTH CHECK API", "Server started. Sending the binding {} ...", self.state.bind_to); + debug!(target: HEALTH_CHECK_API_LOG_TARGET, "Server started. Sending the binding {} ...", self.state.bind_to); self.state.bind_to }); - debug!(target: "HEALTH CHECK API", "Waiting for spawning task to send the binding ..."); + debug!(target: HEALTH_CHECK_API_LOG_TARGET, "Waiting for spawning task to send the binding ..."); let binding = rx_start.await.expect("it should send service binding").address;