Skip to content

Commit

Permalink
refactor: extract consts for logging targets
Browse files Browse the repository at this point in the history
  • Loading branch information
josecelano committed Jun 25, 2024
1 parent 16ae4fd commit 0388e1d
Show file tree
Hide file tree
Showing 16 changed files with 87 additions and 67 deletions.
8 changes: 4 additions & 4 deletions src/bootstrap/jobs/health_check_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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}"),
}

Expand Down
7 changes: 4 additions & 3 deletions src/bootstrap/jobs/udp_tracker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
///
Expand All @@ -35,8 +36,8 @@ pub async fn start_job(config: &UdpTracker, tracker: Arc<core::Tracker>, 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(),
Expand All @@ -49,6 +50,6 @@ pub async fn start_job(config: &UdpTracker, tracker: Arc<core::Tracker>, 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());
})
}
7 changes: 4 additions & 3 deletions src/console/ci/e2e/logs_parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
6 changes: 4 additions & 2 deletions src/servers/apis/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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 {
Expand Down
5 changes: 3 additions & 2 deletions src/servers/apis/routes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down Expand Up @@ -60,7 +61,7 @@ pub fn router(tracker: Arc<Tracker>, access_tokens: Arc<AccessTokens>) -> 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| {
Expand All @@ -73,7 +74,7 @@ pub fn router(tracker: Arc<Tracker>, access_tokens: Arc<AccessTokens>) -> 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);
}),
)
Expand Down
9 changes: 5 additions & 4 deletions src/servers/apis/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -121,11 +122,11 @@ impl ApiServer<Stopped> {
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
});
Expand Down Expand Up @@ -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 {
Expand All @@ -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 })
Expand Down
2 changes: 2 additions & 0 deletions src/servers/health_check_api/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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";
7 changes: 4 additions & 3 deletions src/servers/health_check_api/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};

Expand Down Expand Up @@ -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| {
Expand All @@ -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);
}),
)
Expand All @@ -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(),
Expand Down
2 changes: 2 additions & 0 deletions src/servers/http/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
5 changes: 3 additions & 2 deletions src/servers/http/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};

Expand Down Expand Up @@ -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);

Expand All @@ -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 })
Expand Down
5 changes: 3 additions & 2 deletions src/servers/http/v1/routes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down Expand Up @@ -56,7 +57,7 @@ pub fn router(tracker: Arc<Tracker>, 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| {
Expand All @@ -69,7 +70,7 @@ pub fn router(tracker: Arc<Tracker>, 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);
}),
)
Expand Down
13 changes: 7 additions & 6 deletions src/servers/udp/logging.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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) => {
Expand All @@ -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) => {
Expand All @@ -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,
Expand Down Expand Up @@ -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,
Expand All @@ -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);
}
2 changes: 2 additions & 0 deletions src/servers/udp/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
Loading

0 comments on commit 0388e1d

Please sign in to comment.