From 147cbce0859719e8b0d67b6353cce5544315c4b7 Mon Sep 17 00:00:00 2001 From: 4t145 Date: Mon, 30 Oct 2023 18:00:30 +0800 Subject: [PATCH 1/2] add standalone mode for tardis tracing --- tardis/src/basic/tracing.rs | 126 ++++++++++++++++++++++++----- tardis/src/consts.rs | 3 + tardis/src/utils.rs | 1 + tardis/src/utils/package_name.rs | 6 ++ tardis/tests/test_basic_tracing.rs | 6 +- tardis/tests/test_os_client.rs | 1 - 6 files changed, 121 insertions(+), 22 deletions(-) create mode 100644 tardis/src/utils/package_name.rs diff --git a/tardis/src/basic/tracing.rs b/tardis/src/basic/tracing.rs index 5a03ca0e..223186f3 100644 --- a/tardis/src/basic/tracing.rs +++ b/tardis/src/basic/tracing.rs @@ -26,14 +26,14 @@ pub struct TardisTracing { } // create a configurable layer, recieve a layer and a configer, return a reload layer and a config function -fn create_configurable_layer(layer: L, configer: impl Fn(&C) -> TardisResult + Send + Sync) -> TardisResult<(ReloadLayer, impl Fn(&C) -> TardisResult<()>)> { +fn create_configurable_layer(layer: L, configer: impl Fn(&C) -> TardisResult + Send + Sync) -> (ReloadLayer, impl Fn(&C) -> TardisResult<()>) { let (reload_layer, reload_handle) = ReloadLayer::new(layer); let config_layer_fn = move |conf: &C| -> TardisResult<()> { let layer = configer(conf)?; reload_handle.reload(layer)?; Ok(()) }; - Ok((reload_layer, config_layer_fn)) + (reload_layer, config_layer_fn) } /// Tardis tracing initializer @@ -79,17 +79,17 @@ where mut self, layer: L, configer: impl Fn(&C) -> TardisResult + 'static + Send + Sync, - ) -> TardisResult, L0>, C>> + ) -> TardisTracingInitializer, L0>, C> where ReloadLayer: tracing_subscriber::Layer, L: 'static + Send + Sync, { - let (reload_layer, config_layer_fn) = create_configurable_layer::(layer, configer)?; + let (reload_layer, config_layer_fn) = create_configurable_layer::(layer, configer); self.configers.push(Box::new(config_layer_fn)); - Ok(TardisTracingInitializer { + TardisTracingInitializer { configers: self.configers, layered: self.layered.with(reload_layer), - }) + } } pub fn with_layer(self, layer: L) -> TardisTracingInitializer, C> @@ -102,6 +102,86 @@ where } } } +// Box + Send + Sync + 'static> + +type BoxLayer = Box + Send + Sync + 'static>; +impl TardisTracingInitializer +where + L0: SubscriberExt, +{ + pub fn with_fmt_layer(self) -> TardisTracingInitializer, L0>, LogConfig> + where + S: tracing::Subscriber + for<'span> tracing_subscriber::registry::LookupSpan<'span> + Send + Sync, + BoxLayer: tracing_subscriber::Layer, + { + self.with_layer(FmtLayer::default().boxed()) + } + pub fn with_env_layer(self) -> TardisTracingInitializer, S>, L0>, LogConfig> + where + S: tracing::Subscriber, + ReloadLayer, S>: tracing_subscriber::Layer, + { + self.with_configurable_layer(EnvFilter::from_default_env().boxed(), |config: &LogConfig| { + let mut env_filter = EnvFilter::from_default_env(); + env_filter = env_filter.add_directive(config.level.clone()); + for directive in &config.directives { + env_filter = env_filter.add_directive(directive.clone()); + } + std::env::set_var("RUST_LOG", env_filter.to_string()); + Ok(env_filter.boxed()) + }) + } + + #[cfg(feature = "tracing")] + pub fn with_opentelemetry_layer(self) -> TardisTracingInitializer, S>, L0>, LogConfig> + where + S: tracing::Subscriber + for<'span> tracing_subscriber::registry::LookupSpan<'span> + Send + Sync, + ReloadLayer, S>: tracing_subscriber::Layer, + { + self.with_configurable_layer( + tracing_opentelemetry::layer().with_tracer(TardisTracing::::create_otlp_tracer()).boxed(), + |conf: &LogConfig| { + if std::env::var_os(OTEL_EXPORTER_OTLP_ENDPOINT).is_none() { + std::env::set_var(OTEL_EXPORTER_OTLP_ENDPOINT, conf.tracing.endpoint.as_str()); + } + if std::env::var_os(OTEL_EXPORTER_OTLP_PROTOCOL).is_none() { + std::env::set_var(OTEL_EXPORTER_OTLP_PROTOCOL, conf.tracing.protocol.to_string()); + } + if std::env::var_os(OTEL_SERVICE_NAME).is_none() { + std::env::set_var(OTEL_SERVICE_NAME, conf.tracing.server_name.as_str()); + } + Ok(tracing_opentelemetry::layer().with_tracer(TardisTracing::::create_otlp_tracer()).boxed()) + }, + ) + } + + #[cfg(feature = "console-subscriber")] + pub fn with_console_layer(self) -> TardisResult, L0>, LogConfig>> + where + S: tracing::Subscriber + for<'span> tracing_subscriber::registry::LookupSpan<'span> + Send + Sync, + BoxLayer: tracing_subscriber::Layer, + { + Ok(self.with_layer(console_subscriber::ConsoleLayer::builder().with_default_env().spawn().boxed())) + } + + #[cfg(feature = "tracing-appender")] + pub fn with_appender_layer(self) -> TardisTracingInitializer, S>, L0>, LogConfig> + where + S: tracing::Subscriber + for<'span> tracing_subscriber::registry::LookupSpan<'span> + Send + Sync, + ReloadLayer, S>: tracing_subscriber::Layer, + { + use crate::config::config_dto::log::TracingAppenderConfig; + let config_file_layer = |cfg: Option<&TracingAppenderConfig>| { + if let Some(cfg) = &cfg { + let file_appender = tracing_appender::rolling::RollingFileAppender::new(cfg.rotation.into(), &cfg.dir, &cfg.filename); + FmtLayer::default().with_writer(file_appender).boxed() + } else { + FmtLayer::default().with_writer(std::io::sink).boxed() + } + }; + self.with_configurable_layer(config_file_layer(None), move |cfg| TardisResult::Ok(config_file_layer(cfg.tracing_appender.as_ref()))) + } +} impl TardisTracingInitializer where @@ -117,11 +197,21 @@ where TARDIS_INST.tracing.set(TardisTracing { configer: configer_list }); } } + + /// Initialize tardis tracing as standalone, this will not set the global tardis tracing instance. + /// # Warning + /// Config this standalong instance will also change the value of env variable `RUST_LOG`, + /// if you are using the global tardis tracing instance, you should use [`TardisTracingInitializer::init`] instead. + pub fn init_standalone(self) -> TardisTracing { + let configer_list = self.configers; + self.layered.init(); + TardisTracing { configer: configer_list } + } } impl TardisTracing { /// Get an initializer for tardis tracing - pub fn init() -> TardisTracingInitializer { + pub fn initializer() -> TardisTracingInitializer { TardisTracingInitializer::default() } @@ -140,19 +230,19 @@ impl TardisTracing { tracing::info!("[Tardis.Tracing] Initializing by defualt initializer."); let initializer = TardisTracingInitializer::default(); let initializer = initializer.with_layer(FmtLayer::default()); - let initializer = initializer.with_configurable_layer(EnvFilter::from_default_env(), |config: &LogConfig| { + let initializer = initializer.with_configurable_layer(EnvFilter::from_default_env().boxed(), |config: &LogConfig| { let mut env_filter = EnvFilter::from_default_env(); env_filter = env_filter.add_directive(config.level.clone()); for directive in &config.directives { env_filter = env_filter.add_directive(directive.clone()); } std::env::set_var("RUST_LOG", env_filter.to_string()); - Ok(env_filter) - })?; + Ok(env_filter.boxed()) + }); tracing::debug!("[Tardis.Tracing] Added fmt layer and env filter."); #[cfg(feature = "tracing")] let initializer = { - let initializer = initializer.with_configurable_layer(tracing_opentelemetry::layer().with_tracer(Self::create_otlp_tracer()?), |conf: &LogConfig| { + let initializer = initializer.with_configurable_layer(tracing_opentelemetry::layer().with_tracer(Self::create_otlp_tracer()), |conf: &LogConfig| { if std::env::var_os(OTEL_EXPORTER_OTLP_ENDPOINT).is_none() { std::env::set_var(OTEL_EXPORTER_OTLP_ENDPOINT, conf.tracing.endpoint.as_str()); } @@ -162,8 +252,8 @@ impl TardisTracing { if std::env::var_os(OTEL_SERVICE_NAME).is_none() { std::env::set_var(OTEL_SERVICE_NAME, conf.tracing.server_name.as_str()); } - Ok(tracing_opentelemetry::layer().with_tracer(Self::create_otlp_tracer()?)) - })?; + Ok(tracing_opentelemetry::layer().with_tracer(Self::create_otlp_tracer())) + }); tracing::debug!("[Tardis.Tracing] Added fmt layer and env filter."); initializer }; @@ -185,7 +275,7 @@ impl TardisTracing { FmtLayer::default().with_writer(std::io::sink).boxed() } }; - initializer.with_configurable_layer(config_file_layer(None), move |cfg| TardisResult::Ok(config_file_layer(cfg.tracing_appender.as_ref())))? + initializer.with_configurable_layer(config_file_layer(None), move |cfg| TardisResult::Ok(config_file_layer(cfg.tracing_appender.as_ref()))) }; tracing::info!("[Tardis.Tracing] Initialize finished."); initializer.init(); @@ -193,12 +283,12 @@ impl TardisTracing { } #[cfg(feature = "tracing")] - fn create_otlp_tracer() -> TardisResult { + fn create_otlp_tracer() -> opentelemetry::sdk::trace::Tracer { use opentelemetry_otlp::WithExportConfig; use crate::config::config_dto::OtlpProtocol; tracing::debug!("[Tardis.Tracing] Initializing otlp tracer"); - let protocol = std::env::var(OTEL_EXPORTER_OTLP_PROTOCOL).ok().map(|s| s.parse::()).transpose()?.unwrap_or_default(); + let protocol = std::env::var(OTEL_EXPORTER_OTLP_PROTOCOL).ok().map(|s| s.parse::().unwrap_or_default()).unwrap_or_default(); let mut tracer = opentelemetry_otlp::new_pipeline().tracing(); match protocol { OtlpProtocol::Grpc => { @@ -218,9 +308,9 @@ impl TardisTracing { } }; tracing::debug!("[Tardis.Tracing] Batch installing tracer. If you are blocked here, try running tokio in multithread."); - let tracer = tracer.install_batch(opentelemetry::runtime::Tokio)?; + let tracer = tracer.install_batch(opentelemetry::runtime::Tokio).expect("fail to install otlp tracer"); tracing::debug!("[Tardis.Tracing] Initialized otlp tracer"); - Ok(tracer) + tracer } #[cfg(feature = "tracing")] diff --git a/tardis/src/consts.rs b/tardis/src/consts.rs index 018bbf64..f1be1b31 100644 --- a/tardis/src/consts.rs +++ b/tardis/src/consts.rs @@ -15,3 +15,6 @@ pub const OTEL_EXPORTER_OTLP_ENDPOINT: &str = "OTEL_EXPORTER_OTLP_ENDPOINT"; pub const OTEL_EXPORTER_OTLP_PROTOCOL: &str = "OTEL_EXPORTER_OTLP_PROTOCOL"; pub const OTEL_EXPORTER_OTLP_HEADERS: &str = "OTEL_EXPORTER_OTLP_HEADERS"; pub const OTEL_SERVICE_NAME: &str = "OTEL_SERVICE_NAME"; + +// shortcuts for build info +pub const TARDIS_VERSION: &str = env!("CARGO_PKG_VERSION"); diff --git a/tardis/src/utils.rs b/tardis/src/utils.rs index 468e3660..46de33f1 100644 --- a/tardis/src/utils.rs +++ b/tardis/src/utils.rs @@ -4,4 +4,5 @@ pub mod tardis_component; pub(crate) use tardis_component::*; pub mod initializer; pub mod mapper; +pub mod package_name; pub mod tardis_static; diff --git a/tardis/src/utils/package_name.rs b/tardis/src/utils/package_name.rs new file mode 100644 index 00000000..b9ea114a --- /dev/null +++ b/tardis/src/utils/package_name.rs @@ -0,0 +1,6 @@ +#[macro_export] +macro_rules! pkg { + () => { + env!("CARGO_PKG_NAME") + }; +} diff --git a/tardis/tests/test_basic_tracing.rs b/tardis/tests/test_basic_tracing.rs index ca6d33f9..bae6bb75 100644 --- a/tardis/tests/test_basic_tracing.rs +++ b/tardis/tests/test_basic_tracing.rs @@ -1,13 +1,13 @@ use crate::app::req::test_req; -use tardis::basic::result::TardisResult; -use tardis::TardisFuns; +use tardis::basic::{result::TardisResult, tracing::TardisTracing}; use tracing::{error, info}; #[tokio::test(flavor = "multi_thread", worker_threads = 1)] async fn test_basic_tracing() -> TardisResult<()> { // env::set_var("RUST_LOG", "OFF"); // env::set_var("RUST_LOG", "info"); - TardisFuns::init(Some("tests/config")).await?; + let _tardis_tracing = TardisTracing::initializer().with_fmt_layer().with_env_layer().with_opentelemetry_layer().init_standalone(); + // TardisFuns::init(Some("tests/config")).await?; let _g = tracing::trace_span!("main"); let _g = _g.enter(); info!("main info..."); diff --git a/tardis/tests/test_os_client.rs b/tardis/tests/test_os_client.rs index 8517f224..034ee5b1 100644 --- a/tardis/tests/test_os_client.rs +++ b/tardis/tests/test_os_client.rs @@ -11,7 +11,6 @@ use tardis::TardisFuns; async fn test_os_client() -> TardisResult<()> { env::set_var("RUST_LOG", "info,tardis=trace"); TardisFuns::init_log()?; - TardisTestContainer::minio(|url| async move { let os_module_config = OSModuleConfig::builder().kind("s3").endpoint(url).ak("minioadmin").sk("minioadmin").region("us-east-1").build(); TardisFuns::init_conf(TardisConfig::builder().fw(FrameworkConfig::builder().os(os_module_config).build()).build()).await?; From 53c23f2300360bcd0412f29861f7e47968d5ef15 Mon Sep 17 00:00:00 2001 From: 4t145 Date: Tue, 31 Oct 2023 10:06:40 +0800 Subject: [PATCH 2/2] tardis tracing using boxed initializer --- tardis/src/basic/tracing.rs | 81 +++++++++++-------------------------- 1 file changed, 23 insertions(+), 58 deletions(-) diff --git a/tardis/src/basic/tracing.rs b/tardis/src/basic/tracing.rs index 223186f3..e1452c19 100644 --- a/tardis/src/basic/tracing.rs +++ b/tardis/src/basic/tracing.rs @@ -1,4 +1,4 @@ -use std::sync::Once; +use std::sync::{Arc, Once}; use crate::basic::result::TardisResult; use crate::config::config_dto::LogConfig; @@ -7,11 +7,16 @@ use crate::config::config_dto::LogConfig; use crate::consts::*; use crate::TARDIS_INST; pub use tracing_subscriber::filter::Directive; -use tracing_subscriber::layer::Layered; -use tracing_subscriber::util::SubscriberInitExt; -use tracing_subscriber::EnvFilter; #[allow(unused_imports)] -use tracing_subscriber::{fmt::Layer as FmtLayer, layer::SubscriberExt, prelude::*, reload::Layer as ReloadLayer, Registry}; +use tracing_subscriber::{ + fmt::Layer as FmtLayer, + layer::{Layered, SubscriberExt}, + prelude::*, + registry::LookupSpan, + reload::Layer as ReloadLayer, + util::SubscriberInitExt, + EnvFilter, Registry, +}; /// # Tardis Tracing /// Tardis tracing is a wrapper of tracing-subscriber. It provides configurable layers as runtime. @@ -102,7 +107,6 @@ where } } } -// Box + Send + Sync + 'static> type BoxLayer = Box + Send + Sync + 'static>; impl TardisTracingInitializer @@ -111,7 +115,7 @@ where { pub fn with_fmt_layer(self) -> TardisTracingInitializer, L0>, LogConfig> where - S: tracing::Subscriber + for<'span> tracing_subscriber::registry::LookupSpan<'span> + Send + Sync, + S: tracing::Subscriber + for<'span> LookupSpan<'span> + Send + Sync, BoxLayer: tracing_subscriber::Layer, { self.with_layer(FmtLayer::default().boxed()) @@ -135,7 +139,7 @@ where #[cfg(feature = "tracing")] pub fn with_opentelemetry_layer(self) -> TardisTracingInitializer, S>, L0>, LogConfig> where - S: tracing::Subscriber + for<'span> tracing_subscriber::registry::LookupSpan<'span> + Send + Sync, + S: tracing::Subscriber + for<'span> LookupSpan<'span> + Send + Sync, ReloadLayer, S>: tracing_subscriber::Layer, { self.with_configurable_layer( @@ -156,18 +160,18 @@ where } #[cfg(feature = "console-subscriber")] - pub fn with_console_layer(self) -> TardisResult, L0>, LogConfig>> + pub fn with_console_layer(self) -> TardisTracingInitializer, L0>, LogConfig> where - S: tracing::Subscriber + for<'span> tracing_subscriber::registry::LookupSpan<'span> + Send + Sync, + S: tracing::Subscriber + for<'span> LookupSpan<'span> + Send + Sync, BoxLayer: tracing_subscriber::Layer, { - Ok(self.with_layer(console_subscriber::ConsoleLayer::builder().with_default_env().spawn().boxed())) + self.with_layer(console_subscriber::ConsoleLayer::builder().with_default_env().spawn().boxed()) } #[cfg(feature = "tracing-appender")] pub fn with_appender_layer(self) -> TardisTracingInitializer, S>, L0>, LogConfig> where - S: tracing::Subscriber + for<'span> tracing_subscriber::registry::LookupSpan<'span> + Send + Sync, + S: tracing::Subscriber + for<'span> LookupSpan<'span> + Send + Sync, ReloadLayer, S>: tracing_subscriber::Layer, { use crate::config::config_dto::log::TracingAppenderConfig; @@ -187,7 +191,8 @@ impl TardisTracingInitializer where L: SubscriberInitExt + 'static, { - pub fn init(self) { + /// Initialize tardis tracing, this will set the global tardis tracing instance. + pub(crate) fn init(self) -> Arc { static INITIALIZED: Once = Once::new(); let configer_list = self.configers; if INITIALIZED.is_completed() { @@ -196,6 +201,7 @@ where INITIALIZED.call_once(|| self.layered.init()); TARDIS_INST.tracing.set(TardisTracing { configer: configer_list }); } + crate::TardisFuns::tracing() } /// Initialize tardis tracing as standalone, this will not set the global tardis tracing instance. @@ -228,55 +234,14 @@ impl TardisTracing { pub(crate) fn init_default() -> TardisResult<()> { tracing::info!("[Tardis.Tracing] Initializing by defualt initializer."); - let initializer = TardisTracingInitializer::default(); - let initializer = initializer.with_layer(FmtLayer::default()); - let initializer = initializer.with_configurable_layer(EnvFilter::from_default_env().boxed(), |config: &LogConfig| { - let mut env_filter = EnvFilter::from_default_env(); - env_filter = env_filter.add_directive(config.level.clone()); - for directive in &config.directives { - env_filter = env_filter.add_directive(directive.clone()); - } - std::env::set_var("RUST_LOG", env_filter.to_string()); - Ok(env_filter.boxed()) - }); + let initializer = TardisTracingInitializer::default().with_fmt_layer().with_env_layer(); tracing::debug!("[Tardis.Tracing] Added fmt layer and env filter."); #[cfg(feature = "tracing")] - let initializer = { - let initializer = initializer.with_configurable_layer(tracing_opentelemetry::layer().with_tracer(Self::create_otlp_tracer()), |conf: &LogConfig| { - if std::env::var_os(OTEL_EXPORTER_OTLP_ENDPOINT).is_none() { - std::env::set_var(OTEL_EXPORTER_OTLP_ENDPOINT, conf.tracing.endpoint.as_str()); - } - if std::env::var_os(OTEL_EXPORTER_OTLP_PROTOCOL).is_none() { - std::env::set_var(OTEL_EXPORTER_OTLP_PROTOCOL, conf.tracing.protocol.to_string()); - } - if std::env::var_os(OTEL_SERVICE_NAME).is_none() { - std::env::set_var(OTEL_SERVICE_NAME, conf.tracing.server_name.as_str()); - } - Ok(tracing_opentelemetry::layer().with_tracer(Self::create_otlp_tracer())) - }); - tracing::debug!("[Tardis.Tracing] Added fmt layer and env filter."); - initializer - }; + let initializer = initializer.with_opentelemetry_layer(); #[cfg(feature = "console-subscriber")] - let initializer = { - use console_subscriber::ConsoleLayer; - tracing::info!("[Tardis.Tracing] Initializing console subscriber. To make it work, you need to enable tokio and runtime tracing targets at **TRACE** level."); - let layer = ConsoleLayer::builder().with_default_env().spawn(); - initializer.with_layer(layer) - }; + let initializer = initializer.with_console_layer(); #[cfg(feature = "tracing-appender")] - let initializer = { - use crate::config::config_dto::log::TracingAppenderConfig; - let config_file_layer = |cfg: Option<&TracingAppenderConfig>| { - if let Some(cfg) = &cfg { - let file_appender = tracing_appender::rolling::RollingFileAppender::new(cfg.rotation.into(), &cfg.dir, &cfg.filename); - FmtLayer::default().with_writer(file_appender).boxed() - } else { - FmtLayer::default().with_writer(std::io::sink).boxed() - } - }; - initializer.with_configurable_layer(config_file_layer(None), move |cfg| TardisResult::Ok(config_file_layer(cfg.tracing_appender.as_ref()))) - }; + let initializer = initializer.with_appender_layer(); tracing::info!("[Tardis.Tracing] Initialize finished."); initializer.init(); Ok(())