Skip to content

Commit

Permalink
Merged
Browse files Browse the repository at this point in the history
  • Loading branch information
scottgerring committed Dec 5, 2024
2 parents d2a88ab + 067be7b commit 33cb755
Show file tree
Hide file tree
Showing 19 changed files with 74 additions and 72 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
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 @@ -65,7 +64,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
4 changes: 2 additions & 2 deletions opentelemetry-sdk/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@
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()`)
- The trait functions `LogExporter.shutdown` and `TraceExporter.shutdown` now explicitly return a result. The
semantics of the method have not changed, but you will have a new lint encouraging you to consume these results.

## 0.27.1
## 0.27.1

Released 2024-Nov-27

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 @@ 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<R: RuntimeChannel>(runtime: R) -> Arc<AtomicUsize> {
async fn test_set_provider_in_tokio<R: RuntimeChannel>(
runtime: R,
) -> (Arc<AtomicUsize>, 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
Expand All @@ -97,7 +100,7 @@ async fn test_set_provider_in_tokio<R: RuntimeChannel>(runtime: R) -> Arc<Atomic
#[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;
assert_eq!(span_count.load(Ordering::SeqCst), 0);
}

Expand All @@ -106,8 +109,10 @@ async fn test_set_provider_multiple_thread_tokio() {
#[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");
assert!(span_count.load(Ordering::SeqCst) > 0);
}

Expand All @@ -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);
}
Expand All @@ -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)
}

Expand All @@ -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)
}
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(())
}
Loading

0 comments on commit 33cb755

Please sign in to comment.