Skip to content
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

chore: remove the global::shutdown_tracer_provider function #2369

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ fn main() {
});

// Shutdown trace pipeline
global::shutdown_tracer_provider();
provider.shutdown().expect("TracerProvider should shutdown successfully")
}
```

Expand Down
16 changes: 8 additions & 8 deletions examples/tracing-grpc/src/client.rs
Original file line number Diff line number Diff line change
@@ -1,24 +1,23 @@
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::{
trace::{SpanKind, TraceContextExt, Tracer},
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);
Expand Down Expand Up @@ -75,9 +74,10 @@ async fn greet() -> Result<(), Box<dyn std::error::Error + Send + Sync + 'static

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync + 'static>> {
init_tracer();
let provider = init_tracer();
greet().await?;
opentelemetry::global::shutdown_tracer_provider();

provider.shutdown()?;

Ok(())
}
9 changes: 5 additions & 4 deletions examples/tracing-grpc/src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -82,7 +83,7 @@ impl Greeter for MyGreeter {

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync + 'static>> {
init_tracer();
let provider = init_tracer();

let addr = "[::1]:50051".parse()?;
let greeter = MyGreeter::default();
Expand All @@ -92,7 +93,7 @@ async fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync + 'static>
.serve(addr)
.await?;

opentelemetry::global::shutdown_tracer_provider();
provider.shutdown()?;

Ok(())
}
4 changes: 2 additions & 2 deletions examples/tracing-jaeger/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use opentelemetry::global::shutdown_tracer_provider;
use opentelemetry::{
global,
trace::{TraceContextExt, TraceError, Tracer},
Expand Down Expand Up @@ -43,6 +42,7 @@ async fn main() -> Result<(), Box<dyn Error + Send + Sync + 'static>> {
});
});

shutdown_tracer_provider();
tracer_provider.shutdown()?;

Ok(())
}
2 changes: 1 addition & 1 deletion opentelemetry-otlp/examples/basic-otlp-http/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ async fn main() -> Result<(), Box<dyn Error + Send + Sync + 'static>> {

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()?;

Expand Down
3 changes: 1 addition & 2 deletions opentelemetry-otlp/examples/basic-otlp/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -139,8 +139,7 @@ async fn main() -> Result<(), Box<dyn Error + Send + Sync + 'static>> {
});

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()?;

Expand Down
3 changes: 1 addition & 2 deletions opentelemetry-otlp/tests/integration_test/tests/traces.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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},
Expand Down Expand Up @@ -53,7 +52,7 @@ pub async fn traces() -> Result<(), Box<dyn Error + Send + Sync + 'static>> {
});
});

shutdown_tracer_provider();
tracer_provider.shutdown()?;

Ok(())
}
Expand Down
7 changes: 4 additions & 3 deletions opentelemetry-otlp/tests/smoke.rs
Original file line number Diff line number Diff line change
@@ -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::{
Expand Down Expand Up @@ -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");

Expand All @@ -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...");
Expand Down
2 changes: 1 addition & 1 deletion opentelemetry-sdk/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
//! });
//!
//! // Shutdown trace pipeline
//! global::shutdown_tracer_provider();
//! provider.shutdown().expect("TracerProvider should shutdown successfully")
//! # }
//! }
//! # }
Expand Down
4 changes: 1 addition & 3 deletions opentelemetry-sdk/src/trace/provider.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<TraceResult<()>> {
Expand Down
33 changes: 22 additions & 11 deletions opentelemetry-sdk/src/trace/runtime_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,15 +66,18 @@
}

#[cfg(any(feature = "rt-tokio", feature = "rt-tokio-current-thread"))]
async fn test_set_provider_in_tokio<R: RuntimeChannel>(runtime: R) -> Arc<AtomicUsize> {
async fn test_set_provider_in_tokio<R: RuntimeChannel>(
runtime: R,
) -> (Arc<AtomicUsize>, crate::trace::TracerProvider) {

Check warning on line 71 in opentelemetry-sdk/src/trace/runtime_tests.rs

View check run for this annotation

Codecov / codecov/patch

opentelemetry-sdk/src/trace/runtime_tests.rs#L69-L71

Added lines #L69 - L71 were not covered by tests
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());

Check warning on line 75 in opentelemetry-sdk/src/trace/runtime_tests.rs

View check run for this annotation

Codecov / codecov/patch

opentelemetry-sdk/src/trace/runtime_tests.rs#L74-L75

Added lines #L74 - L75 were not covered by tests
let tracer = tracer("opentelemetery");

tracer.in_span("test", |_cx| {});

span_count
(span_count, tracer_provider)

Check warning on line 80 in opentelemetry-sdk/src/trace/runtime_tests.rs

View check run for this annotation

Codecov / codecov/patch

opentelemetry-sdk/src/trace/runtime_tests.rs#L80

Added line #L80 was not covered by tests
}

// When using `tokio::spawn` to spawn the worker task in batch processor
Expand All @@ -97,7 +100,7 @@
#[ignore = "requires --test-threads=1"]
#[cfg(feature = "rt-tokio")]
async fn test_set_provider_multiple_thread_tokio() {
let span_count = test_set_provider_in_tokio(runtime::Tokio).await;
let (span_count, _) = test_set_provider_in_tokio(runtime::Tokio).await;

Check warning on line 103 in opentelemetry-sdk/src/trace/runtime_tests.rs

View check run for this annotation

Codecov / codecov/patch

opentelemetry-sdk/src/trace/runtime_tests.rs#L103

Added line #L103 was not covered by tests
assert_eq!(span_count.load(Ordering::SeqCst), 0);
}

Expand All @@ -106,8 +109,10 @@
#[ignore = "requires --test-threads=1"]
#[cfg(feature = "rt-tokio")]
async fn test_set_provider_multiple_thread_tokio_shutdown() {
let span_count = test_set_provider_in_tokio(runtime::Tokio).await;
shutdown_tracer_provider();
let (span_count, tracer_provider) = test_set_provider_in_tokio(runtime::Tokio).await;
tracer_provider
.shutdown()
.expect("TracerProvider should shutdown properly");

Check warning on line 115 in opentelemetry-sdk/src/trace/runtime_tests.rs

View check run for this annotation

Codecov / codecov/patch

opentelemetry-sdk/src/trace/runtime_tests.rs#L112-L115

Added lines #L112 - L115 were not covered by tests
assert!(span_count.load(Ordering::SeqCst) > 0);
}

Expand All @@ -119,12 +124,15 @@
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());

Check warning on line 128 in opentelemetry-sdk/src/trace/runtime_tests.rs

View check run for this annotation

Codecov / codecov/patch

opentelemetry-sdk/src/trace/runtime_tests.rs#L127-L128

Added lines #L127 - L128 were not covered by tests
let tracer = tracer("opentelemetry");

tracer.in_span("test", |_cx| {});

shutdown_tracer_provider();
tracer_provider
.shutdown()
.expect("TracerProvider should shutdown properly");

Check warning on line 135 in opentelemetry-sdk/src/trace/runtime_tests.rs

View check run for this annotation

Codecov / codecov/patch

opentelemetry-sdk/src/trace/runtime_tests.rs#L133-L135

Added lines #L133 - L135 were not covered by tests

assert!(span_count.load(Ordering::SeqCst) > 0);
}
Expand All @@ -134,7 +142,7 @@
#[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;

Check warning on line 145 in opentelemetry-sdk/src/trace/runtime_tests.rs

View check run for this annotation

Codecov / codecov/patch

opentelemetry-sdk/src/trace/runtime_tests.rs#L145

Added line #L145 was not covered by tests
assert_eq!(span_count.load(Ordering::SeqCst), 0)
}

Expand All @@ -143,7 +151,10 @@
#[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");

Check warning on line 158 in opentelemetry-sdk/src/trace/runtime_tests.rs

View check run for this annotation

Codecov / codecov/patch

opentelemetry-sdk/src/trace/runtime_tests.rs#L154-L158

Added lines #L154 - L158 were not covered by tests
assert!(span_count.load(Ordering::SeqCst) > 0)
}
9 changes: 5 additions & 4 deletions opentelemetry-stdout/examples/basic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,14 @@ static RESOURCE: Lazy<Resource> = 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")]
Expand Down Expand Up @@ -198,7 +199,7 @@ fn emit_log() {
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
#[cfg(feature = "trace")]
init_trace();
let tracer_provider = init_trace();

#[cfg(feature = "metrics")]
let meter_provider = init_metrics();
Expand All @@ -216,7 +217,7 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
emit_metrics();

#[cfg(feature = "trace")]
global::shutdown_tracer_provider();
tracer_provider.shutdown()?;

#[cfg(feature = "metrics")]
meter_provider.shutdown()?;
Expand Down
2 changes: 1 addition & 1 deletion opentelemetry-zipkin/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync + 'static>> {
// Traced app logic here...
});

global::shutdown_tracer_provider();
provider.shutdown().expect("TracerProvider should shutdown successfully");

Ok(())
}
Expand Down
6 changes: 3 additions & 3 deletions opentelemetry-zipkin/examples/zipkin.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use opentelemetry::{
global::{self, shutdown_tracer_provider},
global::{self},
trace::{Span, Tracer},
};
use std::thread;
Expand All @@ -13,7 +13,7 @@ fn bar() {
}

fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync + 'static>> {
let tracer = opentelemetry_zipkin::new_pipeline()
let (tracer, provider) = opentelemetry_zipkin::new_pipeline()
.with_service_name("trace-demo")
.install_simple()?;

Expand All @@ -23,6 +23,6 @@ fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync + 'static>> {
thread::sleep(Duration::from_millis(6));
});

shutdown_tracer_provider();
provider.shutdown()?;
Ok(())
}
17 changes: 11 additions & 6 deletions opentelemetry-zipkin/src/exporter/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,9 @@

/// Install the Zipkin trace exporter pipeline with a simple span processor.
#[allow(deprecated)]
pub fn install_simple(mut self) -> Result<Tracer, TraceError> {
pub fn install_simple(
Copy link
Member

@lalitb lalitb Dec 2, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not related to this PR - but ZipkinPipelineBuilder should be replaced with ZipkinBuilder and the implementation should be made consistent to OTLP as done in #2221. The crate be only responsible to provide builder to create Zipkin exporter, and no convenience wrappers to create TracerProvider / Processors.

mut self,
) -> Result<(Tracer, opentelemetry_sdk::trace::TracerProvider), TraceError> {

Check warning on line 146 in opentelemetry-zipkin/src/exporter/mod.rs

View check run for this annotation

Codecov / codecov/patch

opentelemetry-zipkin/src/exporter/mod.rs#L144-L146

Added lines #L144 - L146 were not covered by tests
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);
Expand All @@ -152,14 +154,17 @@
.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))

Check warning on line 158 in opentelemetry-zipkin/src/exporter/mod.rs

View check run for this annotation

Codecov / codecov/patch

opentelemetry-zipkin/src/exporter/mod.rs#L157-L158

Added lines #L157 - L158 were not covered by tests
}

/// Install the Zipkin trace exporter pipeline with a batch span processor using the specified
/// runtime.
#[allow(deprecated)]
pub fn install_batch<R: RuntimeChannel>(mut self, runtime: R) -> Result<Tracer, TraceError> {
pub fn install_batch<R: RuntimeChannel>(
mut self,
runtime: R,
) -> Result<(Tracer, opentelemetry_sdk::trace::TracerProvider), TraceError> {

Check warning on line 167 in opentelemetry-zipkin/src/exporter/mod.rs

View check run for this annotation

Codecov / codecov/patch

opentelemetry-zipkin/src/exporter/mod.rs#L164-L167

Added lines #L164 - L167 were not covered by tests
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);
Expand All @@ -170,8 +175,8 @@
.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))

Check warning on line 179 in opentelemetry-zipkin/src/exporter/mod.rs

View check run for this annotation

Codecov / codecov/patch

opentelemetry-zipkin/src/exporter/mod.rs#L178-L179

Added lines #L178 - L179 were not covered by tests
}

/// Assign the service name under which to group traces.
Expand Down
Loading
Loading