Skip to content

Commit b66adcc

Browse files
committed
Merge #941: Config overhaul: improve admin experience
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
2 parents de8ed61 + 397ef0f commit b66adcc

14 files changed

Lines changed: 41 additions & 12 deletions

File tree

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/configuration/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ camino = { version = "1.1.6", features = ["serde", "serde1"] }
1919
derive_more = "0"
2020
figment = { version = "0.10.18", features = ["env", "test", "toml"] }
2121
serde = { version = "1", features = ["derive"] }
22+
serde_json = { version = "1", features = ["preserve_order"] }
2223
serde_with = "3"
2324
thiserror = "1"
2425
toml = "0"

packages/configuration/src/lib.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -111,17 +111,17 @@ impl Info {
111111
let env_var_config_toml_path = ENV_VAR_CONFIG_TOML_PATH.to_string();
112112

113113
let config_toml = if let Ok(config_toml) = env::var(env_var_config_toml) {
114-
println!("Loading configuration from environment variable:\n {config_toml}");
114+
println!("Loading extra configuration from environment variable:\n {config_toml}");
115115
Some(config_toml)
116116
} else {
117117
None
118118
};
119119

120120
let config_toml_path = if let Ok(config_toml_path) = env::var(env_var_config_toml_path) {
121-
println!("Loading configuration from file: `{config_toml_path}` ...");
121+
println!("Loading extra configuration from file: `{config_toml_path}` ...");
122122
config_toml_path
123123
} else {
124-
println!("Loading configuration from default configuration file: `{default_config_toml_path}` ...");
124+
println!("Loading extra configuration from default configuration file: `{default_config_toml_path}` ...");
125125
default_config_toml_path
126126
};
127127

packages/configuration/src/v2/mod.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -346,10 +346,26 @@ impl Configuration {
346346
}
347347

348348
/// Encodes the configuration to TOML.
349+
///
350+
/// # Panics
351+
///
352+
/// Will panic if it can't be converted to TOML.
353+
#[must_use]
349354
fn to_toml(&self) -> String {
350355
// code-review: do we need to use Figment also to serialize into toml?
351356
toml::to_string(self).expect("Could not encode TOML value")
352357
}
358+
359+
/// Encodes the configuration to JSON.
360+
///
361+
/// # Panics
362+
///
363+
/// Will panic if it can't be converted to JSON.
364+
#[must_use]
365+
pub fn to_json(&self) -> String {
366+
// code-review: do we need to use Figment also to serialize into json?
367+
serde_json::to_string_pretty(self).expect("Could not encode JSON value")
368+
}
353369
}
354370

355371
#[cfg(test)]

src/app.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,13 @@ use crate::{core, servers};
3838
/// - Can't retrieve tracker keys from database.
3939
/// - Can't load whitelist from database.
4040
pub async fn start(config: &Configuration, tracker: Arc<core::Tracker>) -> Vec<JoinHandle<()>> {
41+
if config.http_api.is_none()
42+
&& (config.udp_trackers.is_none() || config.udp_trackers.as_ref().map_or(true, std::vec::Vec::is_empty))
43+
&& (config.http_trackers.is_none() || config.http_trackers.as_ref().map_or(true, std::vec::Vec::is_empty))
44+
{
45+
warn!("No services enabled in configuration");
46+
}
47+
4148
let mut jobs: Vec<JoinHandle<()>> = Vec::new();
4249

4350
let registar = Registar::default();

src/bootstrap/app.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ use std::sync::Arc;
1515

1616
use torrust_tracker_clock::static_time;
1717
use torrust_tracker_configuration::Configuration;
18+
use tracing::info;
1819

1920
use super::config::initialize_configuration;
2021
use crate::bootstrap;
@@ -26,8 +27,11 @@ use crate::shared::crypto::ephemeral_instance_keys;
2627
#[must_use]
2728
pub fn setup() -> (Configuration, Arc<Tracker>) {
2829
let configuration = initialize_configuration();
30+
2931
let tracker = initialize_with_configuration(&configuration);
3032

33+
info!("Configuration:\n{}", configuration.to_json());
34+
3135
(configuration, tracker)
3236
}
3337

src/bootstrap/logging.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ fn tracing_stdout_init(filter: LevelFilter, style: &TraceStyle) {
5252
TraceStyle::Json => builder.json().init(),
5353
};
5454

55-
info!("logging initialized.");
55+
info!("Logging initialized");
5656
}
5757

5858
#[derive(Debug)]

src/console/ci/e2e/logs_parser.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ impl RunningServices {
2323
///
2424
/// ```text
2525
/// Loading configuration from default configuration file: `./share/default/config/tracker.development.sqlite3.toml` ...
26-
/// 2024-06-10T16:07:39.989540Z INFO torrust_tracker::bootstrap::logging: logging initialized.
26+
/// 2024-06-10T16:07:39.989540Z INFO torrust_tracker::bootstrap::logging: Logging initialized
2727
/// 2024-06-10T16:07:39.990205Z INFO UDP TRACKER: Starting on: udp://0.0.0.0:6868
2828
/// 2024-06-10T16:07:39.990215Z INFO UDP TRACKER: Started on: udp://0.0.0.0:6868
2929
/// 2024-06-10T16:07:39.990244Z INFO UDP TRACKER: Starting on: udp://0.0.0.0:6969
@@ -116,7 +116,7 @@ mod tests {
116116
fn it_should_parse_from_logs_with_valid_logs() {
117117
let logs = r"
118118
Loading configuration from default configuration file: `./share/default/config/tracker.development.sqlite3.toml` ...
119-
2024-06-10T16:07:39.989540Z INFO torrust_tracker::bootstrap::logging: logging initialized.
119+
2024-06-10T16:07:39.989540Z INFO torrust_tracker::bootstrap::logging: Logging initialized
120120
2024-06-10T16:07:39.990244Z INFO UDP TRACKER: Starting on: udp://0.0.0.0:6969
121121
2024-06-10T16:07:39.990255Z INFO UDP TRACKER: Started on: udp://0.0.0.0:6969
122122
2024-06-10T16:07:39.990261Z INFO torrust_tracker::bootstrap::jobs: TLS not enabled

src/console/ci/e2e/runner.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ pub fn run() -> anyhow::Result<()> {
117117

118118
fn tracing_stdout_init(filter: LevelFilter) {
119119
tracing_subscriber::fmt().with_max_level(filter).init();
120-
info!("Logging initialized.");
120+
info!("Logging initialized");
121121
}
122122

123123
fn load_tracker_configuration(args: &Args) -> anyhow::Result<String> {

src/console/clients/checker/app.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ pub async fn run() -> Result<Vec<CheckResult>> {
103103

104104
fn tracing_stdout_init(filter: LevelFilter) {
105105
tracing_subscriber::fmt().with_max_level(filter).init();
106-
debug!("logging initialized.");
106+
debug!("Logging initialized");
107107
}
108108

109109
fn setup_config(args: Args) -> Result<Configuration> {

0 commit comments

Comments
 (0)