Skip to content

Commit

Permalink
refactor: extract const for logging targets
Browse files Browse the repository at this point in the history
And make it explicit the coupling between logs and `RunningServices`
type.
  • Loading branch information
josecelano committed Jun 25, 2024
1 parent 0388e1d commit b4b4515
Show file tree
Hide file tree
Showing 7 changed files with 42 additions and 11 deletions.
3 changes: 2 additions & 1 deletion src/bootstrap/jobs/health_check_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ use tracing::info;

use super::Started;
use crate::servers::health_check_api::{server, HEALTH_CHECK_API_LOG_TARGET};
use crate::servers::logging::STARTED_ON;
use crate::servers::registar::ServiceRegistry;
use crate::servers::signals::Halted;

Expand Down Expand Up @@ -55,7 +56,7 @@ pub async fn start_job(config: &HealthCheckApi, register: ServiceRegistry) -> Jo

// Wait until the server sends the started message
match rx_start.await {
Ok(msg) => info!(target: HEALTH_CHECK_API_LOG_TARGET, "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/console/ci/e2e/logs_parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ 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::logging::STARTED_ON;
use crate::servers::udp::UDP_TRACKER_LOG_TARGET;

const INFO_LOG_LEVEL: &str = "INFO";
Expand Down Expand Up @@ -65,9 +66,9 @@ impl RunningServices {
let mut http_trackers: Vec<String> = Vec::new();
let mut health_checks: Vec<String> = Vec::new();

let udp_re = Regex::new(r"Started on: udp://([0-9.]+:[0-9]+)").unwrap();
let http_re = Regex::new(r"Started on: (https?://[0-9.]+:[0-9]+)").unwrap(); // DevSkim: ignore DS137138
let health_re = Regex::new(r"Started on: (https?://[0-9.]+:[0-9]+)").unwrap(); // DevSkim: ignore DS137138
let udp_re = Regex::new(&format!("{STARTED_ON}: {}", r"udp://([0-9.]+:[0-9]+)")).unwrap();
let http_re = Regex::new(&format!("{STARTED_ON}: {}", r"(https?://[0-9.]+:[0-9]+)")).unwrap(); // DevSkim: ignore DS137138
let health_re = Regex::new(&format!("{STARTED_ON}: {}", r"(https?://[0-9.]+:[0-9]+)")).unwrap(); // DevSkim: ignore DS137138
let ansi_escape_re = Regex::new(r"\x1b\[[0-9;]*m").unwrap();

for line in logs.lines() {
Expand Down
3 changes: 2 additions & 1 deletion src/servers/apis/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ 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::logging::STARTED_ON;
use crate::servers::registar::{ServiceHealthCheckJob, ServiceRegistration, ServiceRegistrationForm};
use crate::servers::signals::{graceful_shutdown, Halted};

Expand Down Expand Up @@ -251,7 +252,7 @@ impl Launcher {
}
});

info!(target: API_LOG_TARGET, "Started on {protocol}://{}", address);
info!(target: API_LOG_TARGET, "{STARTED_ON} {protocol}://{}", address);

tx_start
.send(Started { address })
Expand Down
3 changes: 2 additions & 1 deletion src/servers/http/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ 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::logging::STARTED_ON;
use crate::servers::registar::{ServiceHealthCheckJob, ServiceRegistration, ServiceRegistrationForm};
use crate::servers::signals::{graceful_shutdown, Halted};

Expand Down Expand Up @@ -77,7 +78,7 @@ impl Launcher {
}
});

info!(target: HTTP_TRACKER_LOG_TARGET, "Started on: {protocol}://{}", address);
info!(target: HTTP_TRACKER_LOG_TARGET, "{STARTED_ON}: {protocol}://{}", address);

tx_start
.send(Started { address })
Expand Down
29 changes: 29 additions & 0 deletions src/servers/logging.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/// This is the prefix used in logs to identify a started service.
///
/// For example:
///
/// ```text
/// 2024-06-25T12:36:25.025312Z INFO UDP TRACKER: Started on: udp://0.0.0.0:6969
/// 2024-06-25T12:36:25.025445Z INFO HTTP TRACKER: Started on: http://0.0.0.0:7070
/// 2024-06-25T12:36:25.025527Z INFO API: Started on http://0.0.0.0:1212
/// 2024-06-25T12:36:25.025580Z INFO HEALTH CHECK API: Started on: http://127.0.0.1:1313
/// ```
pub const STARTED_ON: &str = "Started on";

/*
todo: we should use a field fot the URL.
For example, instead of:
```
2024-06-25T12:36:25.025312Z INFO UDP TRACKER: Started on: udp://0.0.0.0:6969
```
We should use something like:
```
2024-06-25T12:36:25.025312Z INFO UDP TRACKER started_at_url=udp://0.0.0.0:6969
```
*/
1 change: 1 addition & 0 deletions src/servers/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ pub mod apis;
pub mod custom_axum_server;
pub mod health_check_api;
pub mod http;
pub mod logging;
pub mod registar;
pub mod signals;
pub mod udp;
7 changes: 2 additions & 5 deletions src/servers/udp/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ use tokio::task::{AbortHandle, JoinHandle};
use super::UdpRequest;
use crate::bootstrap::jobs::Started;
use crate::core::Tracker;
use crate::servers::logging::STARTED_ON;
use crate::servers::registar::{ServiceHealthCheckJob, ServiceRegistration, ServiceRegistrationForm};
use crate::servers::signals::{shutdown_signal_with_message, Halted};
use crate::servers::udp::{handlers, UDP_TRACKER_LOG_TARGET};
Expand Down Expand Up @@ -364,11 +365,7 @@ impl Udp {
let address = bound_socket.local_addr();
let local_udp_url = format!("udp://{address}");

// note: this log message is parsed by our container. i.e:
//
// `INFO UDP TRACKER: Started on: udp://0.0.0.0:6969`
//
tracing::info!(target: UDP_TRACKER_LOG_TARGET, "Started on: {local_udp_url}");
tracing::info!(target: UDP_TRACKER_LOG_TARGET, "{STARTED_ON}: {local_udp_url}");

let receiver = Receiver::new(bound_socket.into(), tracker);

Expand Down

0 comments on commit b4b4515

Please sign in to comment.