From d8e950ced92a2650cb8d2ae20d96a7708cdf14af Mon Sep 17 00:00:00 2001 From: Mauro Rocchi Date: Wed, 11 Sep 2024 15:23:12 +0200 Subject: [PATCH 1/3] shutdown tracer provider properly --- Cargo.toml | 1 + src/subscriber.rs | 2 +- src/telemetry.rs | 30 +++++++++++++++++++++++++++++- 3 files changed, 31 insertions(+), 2 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index adcf0b6..2d8a081 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -53,6 +53,7 @@ serde_json = "^1.0" # dates chrono = {version = "^0.4", default-features = false, features = ["serde", "clock"]} url = "2.5.0" +once_cell = "1.19.0" [dev-dependencies] actix-web = "4.0.1" diff --git a/src/subscriber.rs b/src/subscriber.rs index 867d685..5de70a0 100644 --- a/src/subscriber.rs +++ b/src/subscriber.rs @@ -84,7 +84,7 @@ pub struct Uninstall; impl Drop for Uninstall { fn drop(&mut self) { #[cfg(feature = "traces")] - opentelemetry::global::shutdown_tracer_provider(); + crate::telemetry::shutdown_tracer_provider(); } } /// Information about the current app context like name or environment diff --git a/src/telemetry.rs b/src/telemetry.rs index f280858..cb5fb7b 100644 --- a/src/telemetry.rs +++ b/src/telemetry.rs @@ -1,3 +1,4 @@ +use once_cell::sync::Lazy; use opentelemetry::global; use opentelemetry::trace::TracerProvider; use opentelemetry::KeyValue; @@ -6,6 +7,8 @@ use opentelemetry_sdk::{ trace::{self, Tracer}, Resource, }; +use std::mem; +use std::sync::Mutex; use crate::SubscriberConfig; @@ -64,7 +67,7 @@ pub fn configure(config: &SubscriberConfig) -> Tracer { .install_batch(runtime) .expect("Failed to configure the OpenTelemetry tracer provider"); - global::set_tracer_provider(tracer_provider.clone()); + set_tracer_provider(tracer_provider.clone()); tracer_provider .tracer_builder("prima-tracing") @@ -72,6 +75,31 @@ pub fn configure(config: &SubscriberConfig) -> Tracer { .build() } +static TRACER_PROVIDER: Lazy>> = Lazy::new(Default::default); + +fn set_tracer_provider(new_provider: trace::TracerProvider) { + global::set_tracer_provider(new_provider.clone()); + + let mut tracer_provider = TRACER_PROVIDER + .lock() + .expect("OpenTelemetry tracer provider mutex poisoned"); + _ = mem::replace(&mut *tracer_provider, Some(new_provider)); +} + +pub(crate) fn shutdown_tracer_provider() { + global::shutdown_tracer_provider(); + + let tracer_provider = TRACER_PROVIDER + .lock() + .expect("OpenTelemetry tracer provider mutex poisoned") + .take() + .expect("OpenTelemetry tracer provider is missing, cannot shutdown"); + + if let Err(err) = tracer_provider.shutdown() { + eprintln!("Failed to shutdown the OpenTelemetry tracer provider: {err:?}"); + } +} + #[cfg(test)] mod test { use super::*; From 7642522dc219f7bf1d59bdc1af4db4156c3d6bf1 Mon Sep 17 00:00:00 2001 From: Mauro Rocchi Date: Thu, 12 Sep 2024 09:25:19 +0200 Subject: [PATCH 2/3] add comment --- src/telemetry.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/src/telemetry.rs b/src/telemetry.rs index cb5fb7b..f29330d 100644 --- a/src/telemetry.rs +++ b/src/telemetry.rs @@ -75,6 +75,7 @@ pub fn configure(config: &SubscriberConfig) -> Tracer { .build() } +// Consider to remove this wrapper when https://github.com/open-telemetry/opentelemetry-rust/issues/1961 is resolved static TRACER_PROVIDER: Lazy>> = Lazy::new(Default::default); fn set_tracer_provider(new_provider: trace::TracerProvider) { From 57abf1dde8e930bc236e5482e0383b37d6a0c5de Mon Sep 17 00:00:00 2001 From: Simone Cottini Date: Thu, 12 Sep 2024 09:30:21 +0200 Subject: [PATCH 3/3] Bump 0.11.1 & update changelog --- CHANGELOG.md | 11 ++++++++++- Cargo.toml | 2 +- README.md | 2 +- 3 files changed, 12 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index f12a47b..bfbe6e8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,14 @@ and this project adheres to --- +## [0.11.1] - 2024-09-12 + +### Fixed + +- Shutdown the tracing provider before exiting ([bug](https://github.com/open-telemetry/opentelemetry-rust/issues/1961)) + +--- + ## [0.11.0] - 2024-07-24 ### Updated @@ -231,7 +239,8 @@ jaeger: -[Unreleased]: https://github.com/primait/prima_tracing.rs/compare/0.11.0...HEAD +[Unreleased]: https://github.com/primait/prima_tracing.rs/compare/0.11.1...HEAD +[0.11.1]: https://github.com/primait/prima_tracing.rs/compare/0.11.0...0.11.1 [0.11.0]: https://github.com/primait/prima_tracing.rs/compare/0.10.0...0.11.0 [0.10.0]: https://github.com/primait/prima_tracing.rs/compare/0.9.5...0.10.0 [0.9.4]: https://github.com/primait/prima_tracing.rs/compare/0.9.3...0.9.4 diff --git a/Cargo.toml b/Cargo.toml index 2d8a081..3fa1130 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -6,7 +6,7 @@ license = "MIT OR Apache-2.0" name = "prima-tracing" readme = "README.md" repository = "https://github.com/primait/prima_tracing.rs" -version = "0.11.0" +version = "0.11.1" [features] default = [] diff --git a/README.md b/README.md index cada7d0..832a505 100644 --- a/README.md +++ b/README.md @@ -12,7 +12,7 @@ Install from [crates.io](https://crates.io/crates/prima-tracing) ```toml -prima-tracing = "0.9" +prima-tracing = "0.11" ``` ### Cargo features