From 6d1a765c41e9f21951aae19665ae569bc8978abc Mon Sep 17 00:00:00 2001 From: David Pitoniak <84917393+pitoniak32@users.noreply.github.com> Date: Wed, 4 Dec 2024 09:03:46 -0500 Subject: [PATCH 1/2] chore: remove the global::shutdown_tracer_provider function (#2369) --- README.md | 2 +- examples/tracing-grpc/src/client.rs | 16 ++++----- examples/tracing-grpc/src/server.rs | 9 ++--- examples/tracing-jaeger/src/main.rs | 4 +-- .../examples/basic-otlp-http/src/main.rs | 2 +- .../examples/basic-otlp/src/main.rs | 3 +- .../tests/integration_test/tests/traces.rs | 3 +- opentelemetry-otlp/tests/smoke.rs | 7 ++-- opentelemetry-sdk/CHANGELOG.md | 1 + opentelemetry-sdk/src/lib.rs | 2 +- opentelemetry-sdk/src/trace/provider.rs | 4 +-- opentelemetry-sdk/src/trace/runtime_tests.rs | 33 ++++++++++++------- opentelemetry-stdout/examples/basic.rs | 9 ++--- opentelemetry-zipkin/README.md | 2 +- opentelemetry-zipkin/examples/zipkin.rs | 6 ++-- opentelemetry-zipkin/src/exporter/mod.rs | 17 ++++++---- opentelemetry-zipkin/src/lib.rs | 8 ++--- opentelemetry/src/global/trace.rs | 13 -------- 18 files changed, 72 insertions(+), 69 deletions(-) diff --git a/README.md b/README.md index fb54e62066..c98987773f 100644 --- a/README.md +++ b/README.md @@ -83,7 +83,7 @@ fn main() { }); // Shutdown trace pipeline - global::shutdown_tracer_provider(); + provider.shutdown().expect("TracerProvider should shutdown successfully") } ``` diff --git a/examples/tracing-grpc/src/client.rs b/examples/tracing-grpc/src/client.rs index 0f24e23710..c871e9ca8d 100644 --- a/examples/tracing-grpc/src/client.rs +++ b/examples/tracing-grpc/src/client.rs @@ -1,9 +1,7 @@ use hello_world::greeter_client::GreeterClient; use hello_world::HelloRequest; use opentelemetry::{global, propagation::Injector}; -use opentelemetry_sdk::{ - propagation::TraceContextPropagator, runtime::Tokio, trace::TracerProvider, -}; +use opentelemetry_sdk::{propagation::TraceContextPropagator, runtime::Tokio, trace as sdktrace}; use opentelemetry_stdout::SpanExporter; use opentelemetry::{ @@ -11,14 +9,15 @@ use opentelemetry::{ Context, KeyValue, }; -fn init_tracer() { +fn init_tracer() -> sdktrace::TracerProvider { global::set_text_map_propagator(TraceContextPropagator::new()); // Install stdout exporter pipeline to be able to retrieve the collected spans. - let provider = TracerProvider::builder() + let provider = sdktrace::TracerProvider::builder() .with_batch_exporter(SpanExporter::default(), Tokio) .build(); - global::set_tracer_provider(provider); + global::set_tracer_provider(provider.clone()); + provider } struct MetadataMap<'a>(&'a mut tonic::metadata::MetadataMap); @@ -75,9 +74,10 @@ async fn greet() -> Result<(), Box Result<(), Box> { - init_tracer(); + let provider = init_tracer(); greet().await?; - opentelemetry::global::shutdown_tracer_provider(); + + provider.shutdown()?; Ok(()) } diff --git a/examples/tracing-grpc/src/server.rs b/examples/tracing-grpc/src/server.rs index 3831907cf0..aadb77b6e6 100644 --- a/examples/tracing-grpc/src/server.rs +++ b/examples/tracing-grpc/src/server.rs @@ -11,14 +11,15 @@ use opentelemetry_sdk::{ use opentelemetry_stdout::SpanExporter; use tonic::{transport::Server, Request, Response, Status}; -fn init_tracer() { +fn init_tracer() -> TracerProvider { global::set_text_map_propagator(TraceContextPropagator::new()); // Install stdout exporter pipeline to be able to retrieve the collected spans. let provider = TracerProvider::builder() .with_batch_exporter(SpanExporter::default(), Tokio) .build(); - global::set_tracer_provider(provider); + global::set_tracer_provider(provider.clone()); + provider } #[allow(clippy::derive_partial_eq_without_eq)] // tonic don't derive Eq for generated types. We shouldn't manually change it. @@ -82,7 +83,7 @@ impl Greeter for MyGreeter { #[tokio::main] async fn main() -> Result<(), Box> { - init_tracer(); + let provider = init_tracer(); let addr = "[::1]:50051".parse()?; let greeter = MyGreeter::default(); @@ -92,7 +93,7 @@ async fn main() -> Result<(), Box .serve(addr) .await?; - opentelemetry::global::shutdown_tracer_provider(); + provider.shutdown()?; Ok(()) } diff --git a/examples/tracing-jaeger/src/main.rs b/examples/tracing-jaeger/src/main.rs index 27e8bda8f8..47d8f2615b 100644 --- a/examples/tracing-jaeger/src/main.rs +++ b/examples/tracing-jaeger/src/main.rs @@ -1,4 +1,3 @@ -use opentelemetry::global::shutdown_tracer_provider; use opentelemetry::{ global, trace::{TraceContextExt, TraceError, Tracer}, @@ -43,6 +42,7 @@ async fn main() -> Result<(), Box> { }); }); - shutdown_tracer_provider(); + tracer_provider.shutdown()?; + Ok(()) } diff --git a/opentelemetry-otlp/examples/basic-otlp-http/src/main.rs b/opentelemetry-otlp/examples/basic-otlp-http/src/main.rs index 4a9d862b59..bba642e657 100644 --- a/opentelemetry-otlp/examples/basic-otlp-http/src/main.rs +++ b/opentelemetry-otlp/examples/basic-otlp-http/src/main.rs @@ -156,7 +156,7 @@ async fn main() -> Result<(), Box> { info!(target: "my-target", "hello from {}. My price is {}", "apple", 1.99); - global::shutdown_tracer_provider(); + tracer_provider.shutdown()?; logger_provider.shutdown()?; meter_provider.shutdown()?; diff --git a/opentelemetry-otlp/examples/basic-otlp/src/main.rs b/opentelemetry-otlp/examples/basic-otlp/src/main.rs index e2163ee2ef..77dd615b85 100644 --- a/opentelemetry-otlp/examples/basic-otlp/src/main.rs +++ b/opentelemetry-otlp/examples/basic-otlp/src/main.rs @@ -139,8 +139,7 @@ async fn main() -> Result<(), Box> { }); info!(name: "my-event", target: "my-target", "hello from {}. My price is {}", "apple", 1.99); - - global::shutdown_tracer_provider(); + tracer_provider.shutdown()?; meter_provider.shutdown()?; logger_provider.shutdown()?; diff --git a/opentelemetry-otlp/tests/integration_test/tests/traces.rs b/opentelemetry-otlp/tests/integration_test/tests/traces.rs index fe5d185536..28649d1fcc 100644 --- a/opentelemetry-otlp/tests/integration_test/tests/traces.rs +++ b/opentelemetry-otlp/tests/integration_test/tests/traces.rs @@ -2,7 +2,6 @@ use integration_test_runner::trace_asserter::{read_spans_from_json, TraceAsserter}; use opentelemetry::global; -use opentelemetry::global::shutdown_tracer_provider; use opentelemetry::trace::TraceError; use opentelemetry::{ trace::{TraceContextExt, Tracer}, @@ -65,7 +64,7 @@ pub async fn traces() -> Result<(), Box> { }); }); - shutdown_tracer_provider(); + tracer_provider.shutdown()?; Ok(()) } diff --git a/opentelemetry-otlp/tests/smoke.rs b/opentelemetry-otlp/tests/smoke.rs index f2dcc3bbfd..ba09407e1e 100644 --- a/opentelemetry-otlp/tests/smoke.rs +++ b/opentelemetry-otlp/tests/smoke.rs @@ -1,6 +1,5 @@ use futures_util::StreamExt; use opentelemetry::global; -use opentelemetry::global::shutdown_tracer_provider; use opentelemetry::trace::{Span, SpanKind, Tracer}; use opentelemetry_otlp::{WithExportConfig, WithTonicConfig}; use opentelemetry_proto::tonic::collector::trace::v1::{ @@ -105,7 +104,7 @@ async fn smoke_tracer() { ) .build(); - global::set_tracer_provider(tracer_provider); + global::set_tracer_provider(tracer_provider.clone()); let tracer = global::tracer("smoke"); @@ -117,7 +116,9 @@ async fn smoke_tracer() { span.add_event("my-test-event", vec![]); span.end(); - shutdown_tracer_provider(); + tracer_provider + .shutdown() + .expect("tracer_provider should shutdown successfully"); } println!("Waiting for request..."); diff --git a/opentelemetry-sdk/CHANGELOG.md b/opentelemetry-sdk/CHANGELOG.md index e8223c625b..bec13eb9dc 100644 --- a/opentelemetry-sdk/CHANGELOG.md +++ b/opentelemetry-sdk/CHANGELOG.md @@ -9,6 +9,7 @@ transparent change. [#2338](https://github.com/open-telemetry/opentelemetry-rust/pull/2338) - `ResourceDetector.detect()` no longer supports timeout option. + - `opentelemetry::global::shutdown_tracer_provider()` Removed from the API, should now use `tracer_provider.shutdown()` see [#2369](https://github.com/open-telemetry/opentelemetry-rust/pull/2369) for a migration example. "Tracer provider" is cheaply cloneable, so users are encouraged to set a clone of it as the global (ex: `global::set_tracer_provider(provider.clone()))`, so that instrumentations and other components can obtain tracers from `global::tracer()`. The tracer_provider must be kept around to call shutdown on it at the end of application (ex: `tracer_provider.shutdown()`) ## 0.27.1 diff --git a/opentelemetry-sdk/src/lib.rs b/opentelemetry-sdk/src/lib.rs index aeeaa229c7..4afda7deb7 100644 --- a/opentelemetry-sdk/src/lib.rs +++ b/opentelemetry-sdk/src/lib.rs @@ -30,7 +30,7 @@ //! }); //! //! // Shutdown trace pipeline -//! global::shutdown_tracer_provider(); +//! provider.shutdown().expect("TracerProvider should shutdown successfully") //! # } //! } //! # } diff --git a/opentelemetry-sdk/src/trace/provider.rs b/opentelemetry-sdk/src/trace/provider.rs index f8c65d59c9..1e1e586123 100644 --- a/opentelemetry-sdk/src/trace/provider.rs +++ b/opentelemetry-sdk/src/trace/provider.rs @@ -219,10 +219,8 @@ impl TracerProvider { /// /// // create more spans.. /// - /// // dropping provider and shutting down global provider ensure all - /// // remaining spans are exported + /// // dropping provider ensures all remaining spans are exported /// drop(provider); - /// global::shutdown_tracer_provider(); /// } /// ``` pub fn force_flush(&self) -> Vec> { diff --git a/opentelemetry-sdk/src/trace/runtime_tests.rs b/opentelemetry-sdk/src/trace/runtime_tests.rs index f9f3c105f9..75cb1b4475 100644 --- a/opentelemetry-sdk/src/trace/runtime_tests.rs +++ b/opentelemetry-sdk/src/trace/runtime_tests.rs @@ -66,15 +66,18 @@ fn build_simple_tracer_provider(exporter: SpanCountExporter) -> crate::trace::Tr } #[cfg(any(feature = "rt-tokio", feature = "rt-tokio-current-thread"))] -async fn test_set_provider_in_tokio(runtime: R) -> Arc { +async fn test_set_provider_in_tokio( + runtime: R, +) -> (Arc, crate::trace::TracerProvider) { let exporter = SpanCountExporter::new(); let span_count = exporter.span_count.clone(); - let _ = set_tracer_provider(build_batch_tracer_provider(exporter, runtime)); + let tracer_provider = build_batch_tracer_provider(exporter, runtime); + let _ = set_tracer_provider(tracer_provider.clone()); let tracer = tracer("opentelemetery"); tracer.in_span("test", |_cx| {}); - span_count + (span_count, tracer_provider) } // When using `tokio::spawn` to spawn the worker task in batch processor @@ -97,7 +100,7 @@ async fn test_set_provider_in_tokio(runtime: R) -> Arc 0); } @@ -119,12 +124,15 @@ async fn test_set_provider_multiple_thread_tokio_shutdown() { async fn test_set_provider_single_thread_tokio_with_simple_processor() { let exporter = SpanCountExporter::new(); let span_count = exporter.span_count.clone(); - let _ = set_tracer_provider(build_simple_tracer_provider(exporter)); + let tracer_provider = build_simple_tracer_provider(exporter); + let _ = set_tracer_provider(tracer_provider.clone()); let tracer = tracer("opentelemetry"); tracer.in_span("test", |_cx| {}); - shutdown_tracer_provider(); + tracer_provider + .shutdown() + .expect("TracerProvider should shutdown properly"); assert!(span_count.load(Ordering::SeqCst) > 0); } @@ -134,7 +142,7 @@ async fn test_set_provider_single_thread_tokio_with_simple_processor() { #[ignore = "requires --test-threads=1"] #[cfg(feature = "rt-tokio-current-thread")] async fn test_set_provider_single_thread_tokio() { - let span_count = test_set_provider_in_tokio(runtime::TokioCurrentThread).await; + let (span_count, _) = test_set_provider_in_tokio(runtime::TokioCurrentThread).await; assert_eq!(span_count.load(Ordering::SeqCst), 0) } @@ -143,7 +151,10 @@ async fn test_set_provider_single_thread_tokio() { #[ignore = "requires --test-threads=1"] #[cfg(feature = "rt-tokio-current-thread")] async fn test_set_provider_single_thread_tokio_shutdown() { - let span_count = test_set_provider_in_tokio(runtime::TokioCurrentThread).await; - shutdown_tracer_provider(); + let (span_count, tracer_provider) = + test_set_provider_in_tokio(runtime::TokioCurrentThread).await; + tracer_provider + .shutdown() + .expect("TracerProvider should shutdown properly"); assert!(span_count.load(Ordering::SeqCst) > 0) } diff --git a/opentelemetry-stdout/examples/basic.rs b/opentelemetry-stdout/examples/basic.rs index 824541d3aa..fd4e78e0d5 100644 --- a/opentelemetry-stdout/examples/basic.rs +++ b/opentelemetry-stdout/examples/basic.rs @@ -24,13 +24,14 @@ static RESOURCE: Lazy = Lazy::new(|| { }); #[cfg(feature = "trace")] -fn init_trace() { +fn init_trace() -> TracerProvider { let exporter = opentelemetry_stdout::SpanExporter::default(); let provider = TracerProvider::builder() .with_simple_exporter(exporter) .with_resource(RESOURCE.clone()) .build(); - global::set_tracer_provider(provider); + global::set_tracer_provider(provider.clone()); + provider } #[cfg(feature = "metrics")] @@ -198,7 +199,7 @@ fn emit_log() { #[tokio::main] async fn main() -> Result<(), Box> { #[cfg(feature = "trace")] - init_trace(); + let tracer_provider = init_trace(); #[cfg(feature = "metrics")] let meter_provider = init_metrics(); @@ -216,7 +217,7 @@ async fn main() -> Result<(), Box> { emit_metrics(); #[cfg(feature = "trace")] - global::shutdown_tracer_provider(); + tracer_provider.shutdown()?; #[cfg(feature = "metrics")] meter_provider.shutdown()?; diff --git a/opentelemetry-zipkin/README.md b/opentelemetry-zipkin/README.md index 53a7e92fff..dda83b5620 100644 --- a/opentelemetry-zipkin/README.md +++ b/opentelemetry-zipkin/README.md @@ -56,7 +56,7 @@ fn main() -> Result<(), Box> { // Traced app logic here... }); - global::shutdown_tracer_provider(); + provider.shutdown().expect("TracerProvider should shutdown successfully"); Ok(()) } diff --git a/opentelemetry-zipkin/examples/zipkin.rs b/opentelemetry-zipkin/examples/zipkin.rs index 48c902a928..09b755f3fa 100644 --- a/opentelemetry-zipkin/examples/zipkin.rs +++ b/opentelemetry-zipkin/examples/zipkin.rs @@ -1,5 +1,5 @@ use opentelemetry::{ - global::{self, shutdown_tracer_provider}, + global::{self}, trace::{Span, Tracer}, }; use std::thread; @@ -13,7 +13,7 @@ fn bar() { } fn main() -> Result<(), Box> { - let tracer = opentelemetry_zipkin::new_pipeline() + let (tracer, provider) = opentelemetry_zipkin::new_pipeline() .with_service_name("trace-demo") .install_simple()?; @@ -23,6 +23,6 @@ fn main() -> Result<(), Box> { thread::sleep(Duration::from_millis(6)); }); - shutdown_tracer_provider(); + provider.shutdown()?; Ok(()) } diff --git a/opentelemetry-zipkin/src/exporter/mod.rs b/opentelemetry-zipkin/src/exporter/mod.rs index 06b02b3ff0..c9f03104df 100644 --- a/opentelemetry-zipkin/src/exporter/mod.rs +++ b/opentelemetry-zipkin/src/exporter/mod.rs @@ -140,7 +140,9 @@ impl ZipkinPipelineBuilder { /// Install the Zipkin trace exporter pipeline with a simple span processor. #[allow(deprecated)] - pub fn install_simple(mut self) -> Result { + pub fn install_simple( + mut self, + ) -> Result<(Tracer, opentelemetry_sdk::trace::TracerProvider), TraceError> { let (config, endpoint) = self.init_config_and_endpoint(); let exporter = self.init_exporter_with_endpoint(endpoint)?; let mut provider_builder = TracerProvider::builder().with_simple_exporter(exporter); @@ -151,14 +153,17 @@ impl ZipkinPipelineBuilder { .with_schema_url(semcov::SCHEMA_URL) .build(); let tracer = opentelemetry::trace::TracerProvider::tracer_with_scope(&provider, scope); - let _ = global::set_tracer_provider(provider); - Ok(tracer) + let _ = global::set_tracer_provider(provider.clone()); + Ok((tracer, provider)) } /// Install the Zipkin trace exporter pipeline with a batch span processor using the specified /// runtime. #[allow(deprecated)] - pub fn install_batch(mut self, runtime: R) -> Result { + pub fn install_batch( + mut self, + runtime: R, + ) -> Result<(Tracer, opentelemetry_sdk::trace::TracerProvider), TraceError> { let (config, endpoint) = self.init_config_and_endpoint(); let exporter = self.init_exporter_with_endpoint(endpoint)?; let mut provider_builder = TracerProvider::builder().with_batch_exporter(exporter, runtime); @@ -169,8 +174,8 @@ impl ZipkinPipelineBuilder { .with_schema_url(semcov::SCHEMA_URL) .build(); let tracer = opentelemetry::trace::TracerProvider::tracer_with_scope(&provider, scope); - let _ = global::set_tracer_provider(provider); - Ok(tracer) + let _ = global::set_tracer_provider(provider.clone()); + Ok((tracer, provider)) } /// Assign the service name under which to group traces. diff --git a/opentelemetry-zipkin/src/lib.rs b/opentelemetry-zipkin/src/lib.rs index 0e8db47dd3..020aee4ea1 100644 --- a/opentelemetry-zipkin/src/lib.rs +++ b/opentelemetry-zipkin/src/lib.rs @@ -26,13 +26,13 @@ //! //! fn main() -> Result<(), TraceError> { //! global::set_text_map_propagator(opentelemetry_zipkin::Propagator::new()); -//! let tracer = opentelemetry_zipkin::new_pipeline().install_simple()?; +//! let (tracer, provider) = opentelemetry_zipkin::new_pipeline().install_simple()?; //! //! tracer.in_span("doing_work", |cx| { //! // Traced app logic here... //! }); //! -//! global::shutdown_tracer_provider(); // sending remaining spans +//! provider.shutdown().expect("TracerProvider should shutdown successfully"); // sending remaining spans //! //! Ok(()) //! } @@ -131,7 +131,7 @@ //! //! fn main() -> Result<(), Box> { //! global::set_text_map_propagator(opentelemetry_zipkin::Propagator::new()); -//! let tracer = opentelemetry_zipkin::new_pipeline() +//! let (tracer, provider) = opentelemetry_zipkin::new_pipeline() //! .with_http_client( //! HyperClient( //! Client::builder(TokioExecutor::new()) @@ -156,7 +156,7 @@ //! // Traced app logic here... //! }); //! -//! global::shutdown_tracer_provider(); // sending remaining spans +//! provider.shutdown()?; // sending remaining spans //! //! Ok(()) //! } diff --git a/opentelemetry/src/global/trace.rs b/opentelemetry/src/global/trace.rs index 68e7813a05..8121e4fd9a 100644 --- a/opentelemetry/src/global/trace.rs +++ b/opentelemetry/src/global/trace.rs @@ -437,16 +437,3 @@ where GlobalTracerProvider::new(new_provider), ) } - -/// Shut down the current tracer provider. This will invoke the shutdown method on all span processors. -/// span processors should export remaining spans before return -pub fn shutdown_tracer_provider() { - let mut tracer_provider = global_tracer_provider() - .write() - .expect("GLOBAL_TRACER_PROVIDER RwLock poisoned"); - - let _ = mem::replace( - &mut *tracer_provider, - GlobalTracerProvider::new(NoopTracerProvider::new()), - ); -} From 957659fac9217534e64d36f80bddc8360345fde5 Mon Sep 17 00:00:00 2001 From: Lalit Kumar Bhasin Date: Wed, 4 Dec 2024 09:48:31 -0800 Subject: [PATCH 2/2] chore: ARM64 CI fix. (#2382) --- .github/workflows/ci.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 3136073add..376730ac7b 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -25,7 +25,7 @@ jobs: - rust: stable os: macos-latest - rust: stable - os: actuated-arm64-4cpu-16gb + os: otel-linux-arm64 runs-on: ${{ matrix.os }} continue-on-error: ${{ matrix.rust == 'beta' }} steps: