feat(obs): enhance OpenTelemetry configuration and logging

Improve observability setup with the following changes:

- Replace static OnceCell with tokio::sync::OnceCell for guard management
- Add logger_level to OtelConfig for configurable tracing verbosity
- Improve telemetry initialization with better error handling
- Enhance logging filters and span configuration

Breaking Changes:
- Configuration now requires logger_level field
- Global guard management uses async-safe primitives

Example config:
observability:
  endpoint: "http://localhost:4317"
  logger_level: "debug"  # New required field
This commit is contained in:
houseme
2025-03-27 18:03:47 +08:00
parent 6f706c102e
commit c4c6d439bc
7 changed files with 281 additions and 98 deletions
+9 -38
View File
@@ -34,35 +34,18 @@ 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, BaseLogEntry, ServerLogEntry};
use rustfs_obs::{init_obs, load_config, set_global_guard, InitLogStatus};
use s3s::{host::MultiDomain, service::S3ServiceBuilder};
use service::hybrid;
use std::time::SystemTime;
use std::{io::IsTerminal, net::SocketAddr};
use tokio::net::TcpListener;
use tonic::{metadata::MetadataValue, Request, Status};
use tower_http::cors::CorsLayer;
use tracing::{debug, error, info, info_span, warn};
use tracing_core::Level;
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<dyn std::any::Any + Send + Sync>);
impl Drop for TracingGuard {
fn drop(&mut self) {
debug!("Dropping global tracing guard, flushing logs");
}
}
static GLOBAL_GUARD: OnceCell<TracingGuard> = OnceCell::new();
#[allow(dead_code)]
fn setup_tracing() {
use tracing_subscriber::EnvFilter;
@@ -107,34 +90,23 @@ fn print_server_info() {
async fn main() -> Result<()> {
// Parse the obtained parameters
let opt = config::Opt::parse();
// println!("config: {:?}", &opt);
// Load the configuration file
let config = load_config(Some(opt.clone().obs_config));
// Initialize Observability
let (logger, guard) = init_obs(config).await;
let (_logger, guard) = init_obs(config.clone()).await;
// Pack and store the guard
GLOBAL_GUARD.set(TracingGuard(Box::new(guard))).unwrap_or_else(|_| {
error!("Unable to set global tracing guard");
});
// Store in global storage
set_global_guard(guard)?;
// Initialize the logger
let start_time = SystemTime::now();
let base_entry = BaseLogEntry::new()
.timestamp(chrono::DateTime::from(start_time))
.message(Some("main init obs start".to_string()))
.request_id(Some("main".to_string()));
let server_entry = ServerLogEntry::new(Level::INFO, "main_server_entry".to_string())
.with_base(base_entry)
.user_id(Some("user_id".to_string()));
let _r = logger.lock().await.log_server_entry(server_entry).await;
// Log initialization status
InitLogStatus::init_start_log(&config.observability).await?;
// Run parameters
run(opt).await
}
//
// #[tokio::main]
async fn run(opt: config::Opt) -> Result<()> {
let span = info_span!("trace-main-run");
@@ -142,7 +114,7 @@ async fn run(opt: config::Opt) -> Result<()> {
debug!("opt: {:?}", &opt);
let mut server_addr = net::check_local_server_addr(opt.address.as_str()).unwrap();
let mut server_addr = net::check_local_server_addr(opt.address.as_str())?;
if server_addr.port() == 0 {
server_addr.set_port(get_available_port());
@@ -155,8 +127,7 @@ async fn run(opt: config::Opt) -> Result<()> {
debug!("server_address {}", &server_address);
//设置 AK 和 SK
iam::init_global_action_cred(Some(opt.access_key.clone()), Some(opt.secret_key.clone())).unwrap();
iam::init_global_action_cred(Some(opt.access_key.clone()), Some(opt.secret_key.clone()))?;
set_global_rustfs_port(server_port);
//监听地址,端口从参数中获取