Skip to content

Commit

Permalink
Merge #941: Config overhaul: improve admin experience
Browse files Browse the repository at this point in the history
397ef0f feat: log final config after processing all config sources (Jose Celano)
ddfbde3 feat: the configuration can be serialized as JSON (Jose Celano)
46c3263 feat: normalize log nessages (Jose Celano)
e299792 feat: warn adming when no service is enabled in the configration (Jose Celano)

Pull request description:

  - Add a warning when the configuration does not enable any service (UDP tracker, HTTP tracker, or tracker API). It does not make sense to run the app without services enabled.
  - Write the final used config to the logs when the tracker starts. We are only writing the config values when the source is an env var. We should do it always and print out the final configuration after merging sources (defaults -> TOML file -> env vars).

ACKs for top commit:
  josecelano:
    ACK 397ef0f

Tree-SHA512: c35b8b7e20909e31cb0fc5754a2ad39bfb16685b77e7451e264713f355682a72d604492a76d4637250bb1ef77f54d037ce9bfae435c836f83e07eea510b2d8eb
  • Loading branch information
josecelano committed Jul 1, 2024
2 parents de8ed61 + 397ef0f commit b66adcc
Show file tree
Hide file tree
Showing 14 changed files with 41 additions and 12 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions packages/configuration/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ camino = { version = "1.1.6", features = ["serde", "serde1"] }
derive_more = "0"
figment = { version = "0.10.18", features = ["env", "test", "toml"] }
serde = { version = "1", features = ["derive"] }
serde_json = { version = "1", features = ["preserve_order"] }
serde_with = "3"
thiserror = "1"
toml = "0"
Expand Down
6 changes: 3 additions & 3 deletions packages/configuration/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,17 +111,17 @@ impl Info {
let env_var_config_toml_path = ENV_VAR_CONFIG_TOML_PATH.to_string();

let config_toml = if let Ok(config_toml) = env::var(env_var_config_toml) {
println!("Loading configuration from environment variable:\n {config_toml}");
println!("Loading extra configuration from environment variable:\n {config_toml}");
Some(config_toml)
} else {
None
};

let config_toml_path = if let Ok(config_toml_path) = env::var(env_var_config_toml_path) {
println!("Loading configuration from file: `{config_toml_path}` ...");
println!("Loading extra configuration from file: `{config_toml_path}` ...");
config_toml_path
} else {
println!("Loading configuration from default configuration file: `{default_config_toml_path}` ...");
println!("Loading extra configuration from default configuration file: `{default_config_toml_path}` ...");
default_config_toml_path
};

Expand Down
16 changes: 16 additions & 0 deletions packages/configuration/src/v2/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -346,10 +346,26 @@ impl Configuration {
}

/// Encodes the configuration to TOML.
///
/// # Panics
///
/// Will panic if it can't be converted to TOML.
#[must_use]
fn to_toml(&self) -> String {
// code-review: do we need to use Figment also to serialize into toml?
toml::to_string(self).expect("Could not encode TOML value")
}

/// Encodes the configuration to JSON.
///
/// # Panics
///
/// Will panic if it can't be converted to JSON.
#[must_use]
pub fn to_json(&self) -> String {
// code-review: do we need to use Figment also to serialize into json?
serde_json::to_string_pretty(self).expect("Could not encode JSON value")
}
}

#[cfg(test)]
Expand Down
7 changes: 7 additions & 0 deletions src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,13 @@ use crate::{core, servers};
/// - Can't retrieve tracker keys from database.
/// - Can't load whitelist from database.
pub async fn start(config: &Configuration, tracker: Arc<core::Tracker>) -> Vec<JoinHandle<()>> {
if config.http_api.is_none()
&& (config.udp_trackers.is_none() || config.udp_trackers.as_ref().map_or(true, std::vec::Vec::is_empty))
&& (config.http_trackers.is_none() || config.http_trackers.as_ref().map_or(true, std::vec::Vec::is_empty))
{
warn!("No services enabled in configuration");
}

let mut jobs: Vec<JoinHandle<()>> = Vec::new();

let registar = Registar::default();
Expand Down
4 changes: 4 additions & 0 deletions src/bootstrap/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ use std::sync::Arc;

use torrust_tracker_clock::static_time;
use torrust_tracker_configuration::Configuration;
use tracing::info;

use super::config::initialize_configuration;
use crate::bootstrap;
Expand All @@ -26,8 +27,11 @@ use crate::shared::crypto::ephemeral_instance_keys;
#[must_use]
pub fn setup() -> (Configuration, Arc<Tracker>) {
let configuration = initialize_configuration();

let tracker = initialize_with_configuration(&configuration);

info!("Configuration:\n{}", configuration.to_json());

(configuration, tracker)
}

Expand Down
2 changes: 1 addition & 1 deletion src/bootstrap/logging.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ fn tracing_stdout_init(filter: LevelFilter, style: &TraceStyle) {
TraceStyle::Json => builder.json().init(),
};

info!("logging initialized.");
info!("Logging initialized");
}

#[derive(Debug)]
Expand Down
4 changes: 2 additions & 2 deletions src/console/ci/e2e/logs_parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ impl RunningServices {
///
/// ```text
/// Loading configuration from default configuration file: `./share/default/config/tracker.development.sqlite3.toml` ...
/// 2024-06-10T16:07:39.989540Z INFO torrust_tracker::bootstrap::logging: logging initialized.
/// 2024-06-10T16:07:39.989540Z INFO torrust_tracker::bootstrap::logging: Logging initialized
/// 2024-06-10T16:07:39.990205Z INFO UDP TRACKER: Starting on: udp://0.0.0.0:6868
/// 2024-06-10T16:07:39.990215Z INFO UDP TRACKER: Started on: udp://0.0.0.0:6868
/// 2024-06-10T16:07:39.990244Z INFO UDP TRACKER: Starting on: udp://0.0.0.0:6969
Expand Down Expand Up @@ -116,7 +116,7 @@ mod tests {
fn it_should_parse_from_logs_with_valid_logs() {
let logs = r"
Loading configuration from default configuration file: `./share/default/config/tracker.development.sqlite3.toml` ...
2024-06-10T16:07:39.989540Z INFO torrust_tracker::bootstrap::logging: logging initialized.
2024-06-10T16:07:39.989540Z INFO torrust_tracker::bootstrap::logging: Logging initialized
2024-06-10T16:07:39.990244Z INFO UDP TRACKER: Starting on: udp://0.0.0.0:6969
2024-06-10T16:07:39.990255Z INFO UDP TRACKER: Started on: udp://0.0.0.0:6969
2024-06-10T16:07:39.990261Z INFO torrust_tracker::bootstrap::jobs: TLS not enabled
Expand Down
2 changes: 1 addition & 1 deletion src/console/ci/e2e/runner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ pub fn run() -> anyhow::Result<()> {

fn tracing_stdout_init(filter: LevelFilter) {
tracing_subscriber::fmt().with_max_level(filter).init();
info!("Logging initialized.");
info!("Logging initialized");
}

fn load_tracker_configuration(args: &Args) -> anyhow::Result<String> {
Expand Down
2 changes: 1 addition & 1 deletion src/console/clients/checker/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ pub async fn run() -> Result<Vec<CheckResult>> {

fn tracing_stdout_init(filter: LevelFilter) {
tracing_subscriber::fmt().with_max_level(filter).init();
debug!("logging initialized.");
debug!("Logging initialized");
}

fn setup_config(args: Args) -> Result<Configuration> {
Expand Down
2 changes: 1 addition & 1 deletion src/console/clients/udp/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ pub async fn run() -> anyhow::Result<()> {

fn tracing_stdout_init(filter: LevelFilter) {
tracing_subscriber::fmt().with_max_level(filter).init();
debug!("logging initialized.");
debug!("Logging initialized");
}

async fn handle_announce(remote_addr: SocketAddr, info_hash: &TorrustInfoHash) -> Result<Response, Error> {
Expand Down
2 changes: 1 addition & 1 deletion src/console/profiling.rs
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ pub async fn run() {
info!("Torrust timed shutdown..");
},
_ = tokio::signal::ctrl_c() => {
info!("Torrust shutting down via Ctrl+C..");
info!("Torrust shutting down via Ctrl+C ...");
// Await for all jobs to shutdown
futures::future::join_all(jobs).await;
}
Expand Down
2 changes: 1 addition & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ async fn main() {
// handle the signals
tokio::select! {
_ = tokio::signal::ctrl_c() => {
info!("Torrust shutting down..");
info!("Torrust shutting down ...");

// Await for all jobs to shutdown
futures::future::join_all(jobs).await;
Expand Down
2 changes: 1 addition & 1 deletion src/servers/apis/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
//!
//! ```text
//! Loading configuration from config file ./tracker.toml
//! 023-03-28T12:19:24.963054069+01:00 [torrust_tracker::bootstrap::logging][INFO] logging initialized.
//! 023-03-28T12:19:24.963054069+01:00 [torrust_tracker::bootstrap::logging][INFO] Logging initialized
//! ...
//! 023-03-28T12:19:24.964138723+01:00 [torrust_tracker::bootstrap::jobs::tracker_apis][INFO] Starting Torrust APIs server on: http://0.0.0.0:1212
//! ```
Expand Down

0 comments on commit b66adcc

Please sign in to comment.