diff --git a/examples/tracing-otlp/config/conf-default.toml b/examples/tracing-otlp/config/conf-default.toml index 92f3e674..4e4ee51e 100644 --- a/examples/tracing-otlp/config/conf-default.toml +++ b/examples/tracing-otlp/config/conf-default.toml @@ -20,4 +20,4 @@ level = "debug" endpoint = "http://localhost:4317" # protocol = "http/protobuf" protocol = "grpc" -server_name = "tracing示例" +server_name = "tracing-示例" diff --git a/examples/tracing-otlp/src/route.rs b/examples/tracing-otlp/src/route.rs index 536bce56..70b4e973 100644 --- a/examples/tracing-otlp/src/route.rs +++ b/examples/tracing-otlp/src/route.rs @@ -11,7 +11,6 @@ pub struct Api; #[poem_openapi::OpenApi] impl Api { - #[instrument] #[oai(path = "/send_email", method = "get")] async fn send_email(&self) -> TardisApiResult { let params = gen_params(&TaskKind::SendEmail); diff --git a/tardis/src/basic/tracing.rs b/tardis/src/basic/tracing.rs index da3e8748..05b8f75d 100644 --- a/tardis/src/basic/tracing.rs +++ b/tardis/src/basic/tracing.rs @@ -216,7 +216,10 @@ where TardisTracing { configure: configer_list } } } - +#[cfg(feature = "tracing")] +pub(crate) fn tracing_service_name() -> String { + std::env::var_os(OTEL_SERVICE_NAME).map(|s| s.to_string_lossy().to_string()).unwrap_or_else(|| "tardis-tracing".to_string()) +} impl TardisTracing { /// Get an initializer for tardis tracing pub fn initializer() -> TardisTracingInitializer { @@ -249,39 +252,41 @@ impl TardisTracing { #[cfg(feature = "tracing")] fn create_otlp_tracer() -> opentelemetry_sdk::trace::Tracer { use crate::config::config_dto::OtlpProtocol; - use std::sync::OnceLock; - static INITIALIZED: OnceLock = OnceLock::new(); use opentelemetry::trace::TracerProvider; - INITIALIZED - .get_or_init(|| { - tracing::debug!("[Tardis.Tracing] Initializing otlp tracer"); - let protocol = std::env::var(OTEL_EXPORTER_OTLP_PROTOCOL).ok().map(|s| s.parse::().unwrap_or_default()).unwrap_or_default(); - let tracer = opentelemetry_otlp::new_pipeline().tracing(); - let tracer = match protocol { - OtlpProtocol::Grpc => { - let mut exporter = opentelemetry_otlp::new_exporter().tonic(); - // Check if we need TLS - if let Ok(endpoint) = std::env::var(OTEL_EXPORTER_OTLP_ENDPOINT) { - if endpoint.to_lowercase().starts_with("https") { - exporter = exporter.with_tls_config(Default::default()); - } - } - tracer.with_exporter(exporter) + tracing::debug!("[Tardis.Tracing] Initializing otlp tracer"); + let protocol = std::env::var(OTEL_EXPORTER_OTLP_PROTOCOL).ok().map(|s| s.parse::().unwrap_or_default()).unwrap_or_default(); + let tracer = opentelemetry_otlp::new_pipeline().tracing(); + let tracer = match protocol { + OtlpProtocol::Grpc => { + let mut exporter = opentelemetry_otlp::new_exporter().tonic(); + // Check if we need TLS + if let Ok(endpoint) = std::env::var(OTEL_EXPORTER_OTLP_ENDPOINT) { + if endpoint.to_lowercase().starts_with("https") { + exporter = exporter.with_tls_config(Default::default()); } - OtlpProtocol::HttpProtobuf => { - let headers = Self::parse_otlp_headers_from_env(); - let exporter = opentelemetry_otlp::new_exporter().http().with_headers(headers.into_iter().collect()); - tracer.with_exporter(exporter) - } - }; - tracing::debug!("[Tardis.Tracing] Batch installing tracer. If you are blocked here, try running tokio in multithread."); - let provider = tracer.install_batch(opentelemetry_sdk::runtime::Tokio).expect("fail to install otlp tracer"); - tracing::debug!("[Tardis.Tracing] Initialized otlp tracer"); - opentelemetry::global::set_tracer_provider(provider.clone()); - provider - }) - .clone() - .tracer("") + } + tracer.with_exporter(exporter) + } + OtlpProtocol::HttpProtobuf => { + let headers = Self::parse_otlp_headers_from_env(); + let exporter = opentelemetry_otlp::new_exporter().http().with_headers(headers.into_iter().collect()); + tracer.with_exporter(exporter) + } + }; + tracing::debug!("[Tardis.Tracing] Batch installing tracer. If you are blocked here, try running tokio in multithread."); + let provider = tracer + .with_trace_config( + opentelemetry_sdk::trace::Config::default() + .with_resource(opentelemetry_sdk::Resource::new([opentelemetry::KeyValue::new("service.name", tracing_service_name())])), + ) + .install_batch(opentelemetry_sdk::runtime::Tokio) + .expect("fail to install otlp tracer"); + tracing::debug!("[Tardis.Tracing] Initialized otlp tracer"); + opentelemetry::global::shutdown_tracer_provider(); + opentelemetry::global::set_tracer_provider(provider.clone()); + provider.tracer(tracing_service_name()) + + } #[cfg(feature = "tracing")] diff --git a/tardis/src/web/web_server.rs b/tardis/src/web/web_server.rs index de691367..f77c02fb 100644 --- a/tardis/src/web/web_server.rs +++ b/tardis/src/web/web_server.rs @@ -320,7 +320,7 @@ impl TardisWebServer { let route = route.with(middleware); #[cfg(feature = "tracing")] let route = { - let tracer = opentelemetry::global::tracer(""); + let tracer = opentelemetry::global::tracer(crate::basic::tracing::tracing_service_name()); route.with(poem::middleware::OpenTelemetryTracing::new(tracer)) }; if module_options.uniform_error || module_config.uniform_error {