improve code for observability

This commit is contained in:
houseme
2025-03-18 22:57:26 +08:00
parent f1c6f276e6
commit 197ae72e93
6 changed files with 37 additions and 9 deletions
Generated
+1
View File
@@ -5633,6 +5633,7 @@ dependencies = [
"mime",
"mime_guess",
"netif",
"once_cell",
"pin-project-lite",
"prost",
"prost-build",
+1
View File
@@ -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" }
+1 -1
View File
@@ -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"
+9 -1
View File
@@ -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())
+1
View File
@@ -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
+24 -7
View File
@@ -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<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;
@@ -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();