Skip to content

Commit 2c2ad6e

Browse files
committed
fix: drop on the TracingGuard also shutdown the wrapped TracerProvider
FIX #213
1 parent 2539d4c commit 2c2ad6e

File tree

1 file changed

+17
-6
lines changed

1 file changed

+17
-6
lines changed

init-tracing-opentelemetry/src/tracing_subscriber_ext.rs

+17-6
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ where
9494
//.with_fallback_service_name(env!("CARGO_PKG_NAME"))
9595
//.with_fallback_service_version(env!("CARGO_PKG_VERSION"))
9696
.build();
97-
let tracerprovider = otlp::init_tracerprovider(otel_rsrc, otlp::identity)?;
97+
let tracer_provider = otlp::init_tracerprovider(otel_rsrc, otlp::identity)?;
9898
// to not send trace somewhere, but continue to create and propagate,...
9999
// then send them to `axum_tracing_opentelemetry::stdio::WriteNoWhere::default()`
100100
// or to `std::io::stdout()` to print
@@ -107,20 +107,31 @@ where
107107
init_propagator()?;
108108
let layer = tracing_opentelemetry::layer()
109109
.with_error_records_to_exceptions(true)
110-
.with_tracer(tracerprovider.tracer(""));
111-
global::set_tracer_provider(tracerprovider.clone());
112-
Ok((layer, TracingGuard { tracerprovider }))
110+
.with_tracer(tracer_provider.tracer(""));
111+
global::set_tracer_provider(tracer_provider.clone());
112+
Ok((layer, TracingGuard { tracer_provider }))
113113
}
114114

115+
/// On Drop of the `TracingGuard` instance,
116+
/// the wrapped Tracer Provider is force to flush and to shutdown (ignoring error).
115117
#[must_use = "Recommend holding with 'let _guard = ' pattern to ensure final traces are sent to the server"]
116118
pub struct TracingGuard {
117-
tracerprovider: SdkTracerProvider,
119+
tracer_provider: SdkTracerProvider,
120+
}
121+
122+
impl TracingGuard {
123+
/// the wrapped Tracer Provider
124+
#[must_use]
125+
pub fn tracer_provider(&self) -> &impl TracerProvider {
126+
&self.tracer_provider
127+
}
118128
}
119129

120130
impl Drop for TracingGuard {
121131
fn drop(&mut self) {
122132
#[allow(unused_must_use)]
123-
let _ = self.tracerprovider.force_flush();
133+
let _ = self.tracer_provider.force_flush();
134+
let _ = self.tracer_provider.shutdown();
124135
}
125136
}
126137

0 commit comments

Comments
 (0)