-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[PAPAY-1395]: Explicitely shutdown the tracer provider #113
Merged
cottinisimone
merged 3 commits into
master
from
PAPAY-1395/task/tokio-error-on-shutdown
Sep 12, 2024
Merged
Changes from 2 commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
|
@@ -6,6 +7,8 @@ use opentelemetry_sdk::{ | |
trace::{self, Tracer}, | ||
Resource, | ||
}; | ||
use std::mem; | ||
use std::sync::Mutex; | ||
|
||
use crate::SubscriberConfig; | ||
|
||
|
@@ -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") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same here |
||
.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::*; | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What about gracefully handle the poison error recovering the old state?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It can be poisoned if another thread panicked while it has the mutex locked. But this lock is used only 2 times, at startup and at shutdown.
At startup should not be possible to have a poisoned lock, because noone kept the lock before. At shutdown, it can be poisoned only if the startup panicked (precisely, if
mem::replace(&mut *tracer_provider, Some(new_provider))
panicked). But in this caseshutdown_tracer_provider
will not be called.So, this seems a good case to use
expect
, like done byopentelemetry::global::set_tracer_provider
...