-
Why talking about an opentelemetry update in tracing-opentelemetry?I already opened a discussion in open-telemetry/opentelemetry-rust, then someone there told me that I should open a discussion here instead. What is the issue?My issue is about how to properly shutdown the Here is an example of what it can look like to build a simple wrapper library over OpenTelemetry: pub fn build_metrics_layer() -> tracing_opentelemetry::MetricsLayer<S>
where
S: tracing::Subscriber + for<'span> tracing_subscriber::registry::LookupSpan<'span>,
{
let meter_provider = opentelemetry_otlp::new_pipeline()
.metrics(opentelemetry_sdk::runtime::Tokio)
.with_exporter(some_exporter())
.build()
.expect("Failed to build OTLP meter provider");
MetricsLayer::new(meter_provider)
}
pub fn shutdown_meter_provider() {
// this work in opentelemetry 0.21,
// but in opentelemetry 0.22, this feature is gone! :'(
opentelemetry::global::shutdown_meter_provider();
} Then we would use this lib in a project, such as: let registry = tracing_subscriber::registry()
.with(tracing_subscriber::filter::EnvFilter::from_default_env())
.with(build_metrics_layer());
tracing::subscriber::set_global_default(registry).expect("Failed to set global tracing subscribers");
// do our stuff, such as run an HTTP API server
// ...
shutdown_meter_provider(); So how are we supposed to shutdown the SdkMeterProvider used internally, without using Currently, I found a workaround which is to keep my own static instance of the use std::sync::OnceLock;
use opentelemetry_sdk::metrics::SdkMeterProvider;
static METER_PROVIDER: OnceLock<SdkMeterProvider> = OnceLock::new();
pub fn build_metrics_layer() -> tracing_opentelemetry::MetricsLayer<S>
where
S: tracing::Subscriber + for<'span> tracing_subscriber::registry::LookupSpan<'span>,
{
let meter_provider = opentelemetry_otlp::new_pipeline()
.metrics(opentelemetry_sdk::runtime::Tokio)
.with_exporter(some_exporter())
.build()
.expect("Failed to build OTLP meter provider");
METER_PROVIDER.set(meter_provider.clone()).expect(
"build_metrics_layer() is not meant to be used more than once in the same process",
);
MetricsLayer::new(meter_provider)
}
pub fn shutdown_meter_provider() {
if let Some(meter_provider) = METER_PROVIDER.get() {
meter_provider
.shutdown()
.expect("Failed to shutdown meter provider");
}
} This workaround seems to work fine, however I don't know if this is a good practice... Do you understand why lalitb told me to open the discussion on the tracing-opentelemetry lib instead of the opentelemetry libs? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
Update: see answer below |
Beta Was this translation helpful? Give feedback.
Closing this issue since this has been fixed on the OpenTelemetry side in this PRUpdate: see answer below