diff --git a/Cargo.lock b/Cargo.lock index e4c5b173e..ef3176efb 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -5633,6 +5633,7 @@ dependencies = [ "mime", "mime_guess", "netif", + "once_cell", "pin-project-lite", "prost", "prost-build", diff --git a/Cargo.toml b/Cargo.toml index 989e7c4e2..e45251674 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -60,6 +60,7 @@ lazy_static = "1.5.0" local-ip-address = "0.6.3" mime = "0.3.17" netif = "0.1.6" +once_cell = "1.21.1" opentelemetry = { version = "0.28" } opentelemetry-appender-tracing = { version = "0.28.1", features = ["experimental_use_tracing_span_context", "experimental_metadata_attributes"] } opentelemetry_sdk = { version = "0.28" } diff --git a/packages/obs/examples/config.toml b/packages/obs/examples/config.toml index ddf376352..2ceac4397 100644 --- a/packages/obs/examples/config.toml +++ b/packages/obs/examples/config.toml @@ -1,6 +1,6 @@ [observability] endpoint = "http://localhost:4317" -use_stdout = false +use_stdout = true sample_ratio = 0.5 meter_interval = 30 service_name = "rustfs_obs_service" diff --git a/packages/obs/examples/server.rs b/packages/obs/examples/server.rs index 2d35c8d4f..15a64c8be 100644 --- a/packages/obs/examples/server.rs +++ b/packages/obs/examples/server.rs @@ -1,5 +1,6 @@ use opentelemetry::global; -use rustfs_obs::{get_logger, init_obs, load_config, log_info, ServerLogEntry}; +use rustfs_obs::{get_logger, init_obs, load_config, log_info, BaseLogEntry, ServerLogEntry}; +use std::collections::HashMap; use std::time::{Duration, SystemTime}; use tracing::{info, instrument}; use tracing_core::Level; @@ -34,7 +35,14 @@ async fn run(bucket: String, object: String, user: String, service_name: String) &[opentelemetry::KeyValue::new("operation", "run")], ); + let base_entry = BaseLogEntry::new() + .message(Some("run logger api_handler info".to_string())) + .request_id(Some("request_id".to_string())) + .timestamp(chrono::DateTime::from(start_time)) + .tags(Some(HashMap::default())); + let server_entry = ServerLogEntry::new(Level::INFO, "api_handler".to_string()) + .with_base(base_entry) .user_id(Some(user.clone())) .add_field("operation".to_string(), "login".to_string()) .add_field("bucket".to_string(), bucket.clone()) diff --git a/rustfs/Cargo.toml b/rustfs/Cargo.toml index 73e53c0bb..530cc57ae 100644 --- a/rustfs/Cargo.toml +++ b/rustfs/Cargo.toml @@ -33,6 +33,7 @@ http-body.workspace = true lock.workspace = true mime.workspace = true netif.workspace = true +once_cell.workspace = true pin-project-lite.workspace = true prost.workspace = true prost-types.workspace = true diff --git a/rustfs/src/main.rs b/rustfs/src/main.rs index 480b5db98..6f4fee1ee 100644 --- a/rustfs/src/main.rs +++ b/rustfs/src/main.rs @@ -34,6 +34,7 @@ use hyper_util::{ service::TowerToHyperService, }; use iam::init_iam_sys; +use once_cell::sync::OnceCell; use protos::proto_gen::node_service::node_service_server::NodeServiceServer; use rustfs_obs::{init_obs, load_config}; use s3s::{host::MultiDomain, service::S3ServiceBuilder}; @@ -46,6 +47,20 @@ use tracing::{debug, error, info, warn}; use tracing_error::ErrorLayer; use tracing_subscriber::{layer::SubscriberExt, util::SubscriberInitExt}; +/// Guard that holds a reference to the global observability subsystem. +/// This struct ensures that tracing/logging components remain alive for the +/// application's lifetime. The underlying guard is dropped when this struct +/// is dropped, which may flush logs or perform cleanup operations. +#[allow(dead_code)] +struct TracingGuard(Box); + +impl Drop for TracingGuard { + fn drop(&mut self) { + debug!("Dropping global tracing guard, flushing logs"); + } +} +static GLOBAL_GUARD: OnceCell = OnceCell::new(); + #[allow(dead_code)] fn setup_tracing() { use tracing_subscriber::EnvFilter; @@ -87,26 +102,28 @@ fn print_server_info() { } fn main() -> Result<()> { - //解析获得到的参数 + // Parse the obtained parameters let opt = config::Opt::parse(); - //设置 trace + // 设置 trace // setup_tracing(); let config = load_config(Some(opt.clone().obs_config)); // Initialize Observability - let (_logger, _guard) = tokio::runtime::Runtime::new()?.block_on(async { init_obs(config).await }); + let (_logger, guard) = tokio::runtime::Runtime::new()?.block_on(async { init_obs(config).await }); - //运行参数 + // Pack and store the guard + GLOBAL_GUARD.set(TracingGuard(Box::new(guard))).unwrap_or_else(|_| { + error!("Unable to set global tracing guard"); + }); + + // Run parameters run(opt) } #[tokio::main] async fn run(opt: config::Opt) -> Result<()> { debug!("opt: {:?}", &opt); - // let config = load_config(Some(opt.clone().obs_config)); - // Initialize Observability - // let (_logger, _guard) = init_obs(config).await; let mut server_addr = net::check_local_server_addr(opt.address.as_str()).unwrap();