Skip to content

Commit

Permalink
[PAPAY-1395]: Explicitely shutdown the tracer provider (#113)
Browse files Browse the repository at this point in the history
* shutdown tracer provider properly

* add comment

* Bump 0.11.1 & update changelog

---------

Co-authored-by: Simone Cottini <[email protected]>
  • Loading branch information
enerdgumen and cottinisimone authored Sep 12, 2024
1 parent 7c03025 commit 50d9dfd
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 5 deletions.
11 changes: 10 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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 = []
Expand Down Expand Up @@ -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"
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/subscriber.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
31 changes: 30 additions & 1 deletion src/telemetry.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use once_cell::sync::Lazy;
use opentelemetry::global;
use opentelemetry::trace::TracerProvider;
use opentelemetry::KeyValue;
Expand All @@ -6,6 +7,8 @@ use opentelemetry_sdk::{
trace::{self, Tracer},
Resource,
};
use std::mem;
use std::sync::Mutex;

use crate::SubscriberConfig;

Expand Down Expand Up @@ -64,14 +67,40 @@ pub fn configure<T>(config: &SubscriberConfig<T>) -> 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")
.with_version(env!("CARGO_PKG_VERSION"))
.build()
}

// Consider to remove this wrapper when https://github.com/open-telemetry/opentelemetry-rust/issues/1961 is resolved
static TRACER_PROVIDER: Lazy<Mutex<Option<trace::TracerProvider>>> = 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::*;
Expand Down

0 comments on commit 50d9dfd

Please sign in to comment.