From 1b22a1e078a3f861e8bb06f6edf1cc1a6519551c Mon Sep 17 00:00:00 2001 From: houseme Date: Wed, 5 Nov 2025 20:04:28 +0800 Subject: [PATCH] Refactor modify stdout (#797) * fix * fix --- crates/config/src/audit/mod.rs | 4 ++-- crates/config/src/constants/app.rs | 4 ++-- crates/config/src/observability/mod.rs | 12 ++-------- crates/obs/src/config.rs | 31 +++++++++++++------------- crates/obs/src/telemetry.rs | 21 +++++++++++++++-- scripts/run.sh | 2 +- 6 files changed, 42 insertions(+), 32 deletions(-) diff --git a/crates/config/src/audit/mod.rs b/crates/config/src/audit/mod.rs index 87155ac4..92a57212 100644 --- a/crates/config/src/audit/mod.rs +++ b/crates/config/src/audit/mod.rs @@ -16,8 +16,8 @@ //! This module defines the configuration for audit systems, including //! webhook and MQTT audit-related settings. -pub(crate) mod mqtt; -pub(crate) mod webhook; +mod mqtt; +mod webhook; pub use mqtt::*; pub use webhook::*; diff --git a/crates/config/src/constants/app.rs b/crates/config/src/constants/app.rs index 5be9b2f7..5686e39d 100644 --- a/crates/config/src/constants/app.rs +++ b/crates/config/src/constants/app.rs @@ -151,8 +151,8 @@ pub const DEFAULT_LOG_KEEP_FILES: u16 = 30; /// This is the default log local logging enabled for rustfs. /// It is used to enable or disable local logging of the application. /// Default value: false -/// Environment variable: RUSTFS_OBS_LOCAL_LOGGING_ENABLED -pub const DEFAULT_LOG_LOCAL_LOGGING_ENABLED: bool = false; +/// Environment variable: RUSTFS_OBS_LOGL_STDOUT_ENABLED +pub const DEFAULT_OBS_LOG_STDOUT_ENABLED: bool = false; /// Constant representing 1 Kibibyte (1024 bytes) /// Default value: 1024 diff --git a/crates/config/src/observability/mod.rs b/crates/config/src/observability/mod.rs index 0032c286..e723882c 100644 --- a/crates/config/src/observability/mod.rs +++ b/crates/config/src/observability/mod.rs @@ -22,7 +22,7 @@ pub const ENV_OBS_SERVICE_NAME: &str = "RUSTFS_OBS_SERVICE_NAME"; pub const ENV_OBS_SERVICE_VERSION: &str = "RUSTFS_OBS_SERVICE_VERSION"; pub const ENV_OBS_ENVIRONMENT: &str = "RUSTFS_OBS_ENVIRONMENT"; pub const ENV_OBS_LOGGER_LEVEL: &str = "RUSTFS_OBS_LOGGER_LEVEL"; -pub const ENV_OBS_LOCAL_LOGGING_ENABLED: &str = "RUSTFS_OBS_LOCAL_LOGGING_ENABLED"; +pub const ENV_OBS_LOG_STDOUT_ENABLED: &str = "RUSTFS_OBS_LOG_STDOUT_ENABLED"; pub const ENV_OBS_LOG_DIRECTORY: &str = "RUSTFS_OBS_LOG_DIRECTORY"; pub const ENV_OBS_LOG_FILENAME: &str = "RUSTFS_OBS_LOG_FILENAME"; pub const ENV_OBS_LOG_ROTATION_SIZE_MB: &str = "RUSTFS_OBS_LOG_ROTATION_SIZE_MB"; @@ -47,12 +47,6 @@ pub const DEFAULT_OBS_LOG_MESSAGE_CAPA: usize = 32768; /// Default values for flush interval in milliseconds pub const DEFAULT_OBS_LOG_FLUSH_MS: u64 = 200; -/// Audit logger queue capacity environment variable key -pub const ENV_AUDIT_LOGGER_QUEUE_CAPACITY: &str = "RUSTFS_AUDIT_LOGGER_QUEUE_CAPACITY"; - -/// Default values for observability configuration -pub const DEFAULT_AUDIT_LOGGER_QUEUE_CAPACITY: usize = 10000; - /// Default values for observability configuration // ### Supported Environment Values // - `production` - Secure file-only logging @@ -78,18 +72,16 @@ mod tests { assert_eq!(ENV_OBS_SERVICE_VERSION, "RUSTFS_OBS_SERVICE_VERSION"); assert_eq!(ENV_OBS_ENVIRONMENT, "RUSTFS_OBS_ENVIRONMENT"); assert_eq!(ENV_OBS_LOGGER_LEVEL, "RUSTFS_OBS_LOGGER_LEVEL"); - assert_eq!(ENV_OBS_LOCAL_LOGGING_ENABLED, "RUSTFS_OBS_LOCAL_LOGGING_ENABLED"); + assert_eq!(ENV_OBS_LOG_STDOUT_ENABLED, "RUSTFS_OBS_LOG_STDOUT_ENABLED"); assert_eq!(ENV_OBS_LOG_DIRECTORY, "RUSTFS_OBS_LOG_DIRECTORY"); assert_eq!(ENV_OBS_LOG_FILENAME, "RUSTFS_OBS_LOG_FILENAME"); assert_eq!(ENV_OBS_LOG_ROTATION_SIZE_MB, "RUSTFS_OBS_LOG_ROTATION_SIZE_MB"); assert_eq!(ENV_OBS_LOG_ROTATION_TIME, "RUSTFS_OBS_LOG_ROTATION_TIME"); assert_eq!(ENV_OBS_LOG_KEEP_FILES, "RUSTFS_OBS_LOG_KEEP_FILES"); - assert_eq!(ENV_AUDIT_LOGGER_QUEUE_CAPACITY, "RUSTFS_AUDIT_LOGGER_QUEUE_CAPACITY"); } #[test] fn test_default_values() { - assert_eq!(DEFAULT_AUDIT_LOGGER_QUEUE_CAPACITY, 10000); assert_eq!(DEFAULT_OBS_ENVIRONMENT_PRODUCTION, "production"); assert_eq!(DEFAULT_OBS_ENVIRONMENT_DEVELOPMENT, "development"); assert_eq!(DEFAULT_OBS_ENVIRONMENT_TEST, "test"); diff --git a/crates/obs/src/config.rs b/crates/obs/src/config.rs index c1d46dd8..ee225dc7 100644 --- a/crates/obs/src/config.rs +++ b/crates/obs/src/config.rs @@ -13,13 +13,14 @@ // limitations under the License. use rustfs_config::observability::{ - ENV_OBS_ENDPOINT, ENV_OBS_ENVIRONMENT, ENV_OBS_LOCAL_LOGGING_ENABLED, ENV_OBS_LOG_DIRECTORY, ENV_OBS_LOG_FILENAME, - ENV_OBS_LOG_KEEP_FILES, ENV_OBS_LOG_ROTATION_SIZE_MB, ENV_OBS_LOG_ROTATION_TIME, ENV_OBS_LOGGER_LEVEL, + ENV_OBS_ENDPOINT, ENV_OBS_ENVIRONMENT, ENV_OBS_LOG_DIRECTORY, ENV_OBS_LOG_FILENAME, ENV_OBS_LOG_KEEP_FILES, + ENV_OBS_LOG_ROTATION_SIZE_MB, ENV_OBS_LOG_ROTATION_TIME, ENV_OBS_LOG_STDOUT_ENABLED, ENV_OBS_LOGGER_LEVEL, ENV_OBS_METER_INTERVAL, ENV_OBS_SAMPLE_RATIO, ENV_OBS_SERVICE_NAME, ENV_OBS_SERVICE_VERSION, ENV_OBS_USE_STDOUT, }; use rustfs_config::{ - APP_NAME, DEFAULT_LOG_KEEP_FILES, DEFAULT_LOG_LEVEL, DEFAULT_LOG_LOCAL_LOGGING_ENABLED, DEFAULT_LOG_ROTATION_SIZE_MB, - DEFAULT_LOG_ROTATION_TIME, DEFAULT_OBS_LOG_FILENAME, ENVIRONMENT, METER_INTERVAL, SAMPLE_RATIO, SERVICE_VERSION, USE_STDOUT, + APP_NAME, DEFAULT_LOG_KEEP_FILES, DEFAULT_LOG_LEVEL, DEFAULT_LOG_ROTATION_SIZE_MB, DEFAULT_LOG_ROTATION_TIME, + DEFAULT_OBS_LOG_FILENAME, DEFAULT_OBS_LOG_STDOUT_ENABLED, ENVIRONMENT, METER_INTERVAL, SAMPLE_RATIO, SERVICE_VERSION, + USE_STDOUT, }; use rustfs_utils::dirs::get_log_directory_to_string; use serde::{Deserialize, Serialize}; @@ -53,15 +54,15 @@ use std::env; /// ``` #[derive(Debug, Deserialize, Serialize, Clone)] pub struct OtelConfig { - pub endpoint: String, // Endpoint for metric collection - pub use_stdout: Option, // Output to stdout - pub sample_ratio: Option, // Trace sampling ratio - pub meter_interval: Option, // Metric collection interval - pub service_name: Option, // Service name - pub service_version: Option, // Service version - pub environment: Option, // Environment - pub logger_level: Option, // Logger level - pub local_logging_enabled: Option, // Local logging enabled + pub endpoint: String, // Endpoint for metric collection + pub use_stdout: Option, // Output to stdout + pub sample_ratio: Option, // Trace sampling ratio + pub meter_interval: Option, // Metric collection interval + pub service_name: Option, // Service name + pub service_version: Option, // Service version + pub environment: Option, // Environment + pub logger_level: Option, // Logger level + pub log_stdout_enabled: Option, // Stdout logging enabled // Added flexi_logger related configurations pub log_directory: Option, // LOG FILE DIRECTORY pub log_filename: Option, // The name of the log file @@ -117,10 +118,10 @@ impl OtelConfig { .ok() .and_then(|v| v.parse().ok()) .or(Some(DEFAULT_LOG_LEVEL.to_string())), - local_logging_enabled: env::var(ENV_OBS_LOCAL_LOGGING_ENABLED) + log_stdout_enabled: env::var(ENV_OBS_LOG_STDOUT_ENABLED) .ok() .and_then(|v| v.parse().ok()) - .or(Some(DEFAULT_LOG_LOCAL_LOGGING_ENABLED)), + .or(Some(DEFAULT_OBS_LOG_STDOUT_ENABLED)), log_directory: Some(get_log_directory_to_string(ENV_OBS_LOG_DIRECTORY)), log_filename: env::var(ENV_OBS_LOG_FILENAME) .ok() diff --git a/crates/obs/src/telemetry.rs b/crates/obs/src/telemetry.rs index 4c7a1d3b..42d22e55 100644 --- a/crates/obs/src/telemetry.rs +++ b/crates/obs/src/telemetry.rs @@ -36,7 +36,7 @@ use opentelemetry_semantic_conventions::{ attribute::{DEPLOYMENT_ENVIRONMENT_NAME, NETWORK_LOCAL_ADDRESS, SERVICE_VERSION as OTEL_SERVICE_VERSION}, }; use rustfs_config::{ - APP_NAME, DEFAULT_LOG_KEEP_FILES, DEFAULT_LOG_LEVEL, DEFAULT_LOG_LOCAL_LOGGING_ENABLED, ENVIRONMENT, METER_INTERVAL, + APP_NAME, DEFAULT_LOG_KEEP_FILES, DEFAULT_LOG_LEVEL, DEFAULT_OBS_LOG_STDOUT_ENABLED, ENVIRONMENT, METER_INTERVAL, SAMPLE_RATIO, SERVICE_VERSION, USE_STDOUT, observability::{ DEFAULT_OBS_ENVIRONMENT_PRODUCTION, DEFAULT_OBS_LOG_FLUSH_MS, DEFAULT_OBS_LOG_MESSAGE_CAPA, DEFAULT_OBS_LOG_POOL_CAPA, @@ -304,7 +304,7 @@ pub(crate) fn init_telemetry(config: &OtelConfig) -> OtelGuard { tracing_subscriber::registry() .with(filter) .with(ErrorLayer::default()) - .with(if config.local_logging_enabled.unwrap_or(DEFAULT_LOG_LOCAL_LOGGING_ENABLED) { + .with(if config.log_stdout_enabled.unwrap_or(DEFAULT_OBS_LOG_STDOUT_ENABLED) { Some(fmt_layer) } else { None @@ -395,6 +395,23 @@ pub(crate) fn init_telemetry(config: &OtelConfig) -> OtelGuard { .with(env_filter) .with(ErrorLayer::default()) .with(fmt_layer) + .with(if config.log_stdout_enabled.unwrap_or(DEFAULT_OBS_LOG_STDOUT_ENABLED) { + let stdout_fmt_layer = tracing_subscriber::fmt::layer() + .with_timer(LocalTime::rfc_3339()) + .with_target(true) + .with_thread_names(true) + .with_thread_ids(true) + .with_file(true) + .with_line_number(true) + .with_writer(std::io::stdout) // Specify writing file + .json() + .with_current_span(true) + .with_span_list(true) + .with_span_events(FmtSpan::CLOSE); // Log span lifecycle events, including trace_id; + Some(stdout_fmt_layer) + } else { + None + }) .init(); info!("Tracing telemetry initialized for non-production with trace_id logging."); diff --git a/scripts/run.sh b/scripts/run.sh index ae327d0d..9d0ba5d4 100755 --- a/scripts/run.sh +++ b/scripts/run.sh @@ -59,7 +59,7 @@ export RUSTFS_CONSOLE_ADDRESS=":9001" #export RUSTFS_OBS_SERVICE_VERSION=0.1.0 # Service version export RUSTFS_OBS_ENVIRONMENT=develop # Environment name export RUSTFS_OBS_LOGGER_LEVEL=info # Log level, supports trace, debug, info, warn, error -export RUSTFS_OBS_LOCAL_LOGGING_ENABLED=true # Whether to enable local logging +export RUSTFS_OBS_LOG_STDOUT_ENABLED=true # Whether to enable local stdout logging export RUSTFS_OBS_LOG_DIRECTORY="$current_dir/deploy/logs" # Log directory export RUSTFS_OBS_LOG_ROTATION_TIME="hour" # Log rotation time unit, can be "second", "minute", "hour", "day" export RUSTFS_OBS_LOG_ROTATION_SIZE_MB=100 # Log rotation size in MB