diff --git a/opentelemetry-otlp/examples/basic-otlp-http/Cargo.toml b/opentelemetry-otlp/examples/basic-otlp-http/Cargo.toml index c56c5502c3..4974c8a223 100644 --- a/opentelemetry-otlp/examples/basic-otlp-http/Cargo.toml +++ b/opentelemetry-otlp/examples/basic-otlp-http/Cargo.toml @@ -6,11 +6,9 @@ license = "Apache-2.0" publish = false [features] -default = ["reqwest", "experimental_metrics_periodicreader_with_async_runtime"] -reqwest = ["opentelemetry-otlp/reqwest-client"] +default = ["reqwest-blocking"] +reqwest-blocking = ["opentelemetry-otlp/reqwest-blocking-client"] hyper = ["opentelemetry-otlp/hyper-client"] -experimental_metrics_periodicreader_with_async_runtime = ["opentelemetry_sdk/experimental_metrics_periodicreader_with_async_runtime"] - [dependencies] once_cell = { workspace = true } diff --git a/opentelemetry-otlp/examples/basic-otlp-http/README.md b/opentelemetry-otlp/examples/basic-otlp-http/README.md index eb65c74160..2d06e6a8fe 100644 --- a/opentelemetry-otlp/examples/basic-otlp-http/README.md +++ b/opentelemetry-otlp/examples/basic-otlp-http/README.md @@ -13,11 +13,9 @@ applications, these filters should be adjusted appropriately. The example employs a `BatchExporter` for logs and traces, which is the recommended approach when using OTLP exporters. While it can be modified to use -a `SimpleExporter`, this requires enabling feature flag `reqwest-blocking-client` and -making the `main()` a normal main and *not* `tokio::main` +a `SimpleExporter`, this requires making the main function a regular main and +*not* tokio main. -// TODO: Metrics does not work with non tokio main when using `reqwest-blocking-client` today, fix that when switching -// default to use own thread. // TODO: Document `hyper` feature flag when using SimpleProcessor. ## Usage diff --git a/opentelemetry-otlp/examples/basic-otlp-http/src/main.rs b/opentelemetry-otlp/examples/basic-otlp-http/src/main.rs index 6b3dee3f07..3e564ee5c5 100644 --- a/opentelemetry-otlp/examples/basic-otlp-http/src/main.rs +++ b/opentelemetry-otlp/examples/basic-otlp-http/src/main.rs @@ -11,7 +11,6 @@ use opentelemetry_otlp::{LogExporter, MetricExporter, Protocol, SpanExporter}; use opentelemetry_sdk::{ logs::LoggerProvider, metrics::{MetricError, SdkMeterProvider}, - runtime, trace::{self as sdktrace, TracerProvider}, }; use opentelemetry_sdk::{ @@ -50,7 +49,9 @@ fn init_traces() -> Result { .build()?; Ok(TracerProvider::builder() - .with_batch_exporter(exporter, runtime::Tokio) + // TODO: Enable BatchExporter after + // https://github.com/open-telemetry/opentelemetry-rust/pull/2456 + .with_simple_exporter(exporter) .with_resource(RESOURCE.clone()) .build()) } @@ -62,15 +63,6 @@ fn init_metrics() -> Result Result Result<(), Box> { +// #[tokio::main] +// TODO: Re-enable tokio::main, if needed, after +// https://github.com/open-telemetry/opentelemetry-rust/pull/2456 +fn main() -> Result<(), Box> { let logger_provider = init_logs()?; // Create a new OpenTelemetryTracingBridge using the above LoggerProvider. diff --git a/opentelemetry-stdout/examples/basic.rs b/opentelemetry-stdout/examples/basic.rs index 3934be9727..0c5ebd87b9 100644 --- a/opentelemetry-stdout/examples/basic.rs +++ b/opentelemetry-stdout/examples/basic.rs @@ -4,7 +4,7 @@ use once_cell::sync::Lazy; use opentelemetry::{global, KeyValue}; #[cfg(feature = "trace")] -use opentelemetry::trace::{Span, Tracer}; +use opentelemetry::trace::Tracer; #[cfg(feature = "metrics")] use opentelemetry_sdk::metrics::{PeriodicReader, SdkMeterProvider}; @@ -60,10 +60,7 @@ fn init_logs() -> opentelemetry_sdk::logs::LoggerProvider { #[cfg(feature = "trace")] fn emit_span() { - use opentelemetry::{ - trace::{SpanContext, SpanId, TraceFlags, TraceId, TraceState}, - InstrumentationScope, - }; + use opentelemetry::{trace::TraceContextExt, InstrumentationScope}; let scope = InstrumentationScope::builder("stdout-example") .with_version("v1") @@ -71,41 +68,15 @@ fn emit_span() { .build(); let tracer = global::tracer_with_scope(scope); - let mut span = tracer.start("example-span"); - span.set_attribute(KeyValue::new("attribute_key1", "attribute_value1")); - span.set_attribute(KeyValue::new("attribute_key2", "attribute_value2")); - span.add_event( - "example-event-name", - vec![KeyValue::new("event_attribute1", "event_value1")], - ); - span.add_link( - SpanContext::new( - TraceId::from_hex("58406520a006649127e371903a2de979").expect("invalid"), - SpanId::from_hex("b6d7d7f6d7d6d7f6").expect("invalid"), - TraceFlags::default(), - false, - TraceState::NONE, - ), - vec![ - KeyValue::new("link_attribute1", "link_value1"), - KeyValue::new("link_attribute2", "link_value2"), - ], - ); - - span.add_link( - SpanContext::new( - TraceId::from_hex("23401120a001249127e371903f2de971").expect("invalid"), - SpanId::from_hex("cd37d765d743d7f6").expect("invalid"), - TraceFlags::default(), - false, - TraceState::NONE, - ), - vec![ - KeyValue::new("link_attribute1", "link_value1"), - KeyValue::new("link_attribute2", "link_value2"), - ], - ); - span.end(); + tracer.in_span("example-span", |cx| { + let span = cx.span(); + span.set_attribute(KeyValue::new("my-attribute", "my-value")); + span.add_event( + "example-event-name", + vec![KeyValue::new("event_attribute1", "event_value1")], + ); + emit_log(); + }) } #[cfg(feature = "metrics")] diff --git a/opentelemetry-stdout/src/logs/exporter.rs b/opentelemetry-stdout/src/logs/exporter.rs index 56b6818e6c..cbc651b348 100644 --- a/opentelemetry-stdout/src/logs/exporter.rs +++ b/opentelemetry-stdout/src/logs/exporter.rs @@ -85,6 +85,9 @@ fn print_logs(batch: &LogBatch<'_>) { if let Some(trace_context) = record.trace_context() { println!("\t TraceId: {:?}", trace_context.trace_id); println!("\t SpanId: {:?}", trace_context.span_id); + if let Some(trace_flags) = trace_context.trace_flags { + println!("\t TraceFlags: {:?}", trace_flags); + } } if let Some(timestamp) = record.timestamp() { let datetime: DateTime = timestamp.into(); diff --git a/opentelemetry-stdout/src/trace/exporter.rs b/opentelemetry-stdout/src/trace/exporter.rs index e2e0fbace9..bf00909890 100644 --- a/opentelemetry-stdout/src/trace/exporter.rs +++ b/opentelemetry-stdout/src/trace/exporter.rs @@ -96,6 +96,7 @@ fn print_spans(batch: Vec) { println!("\tName : {}", &span.name); println!("\tTraceId : {}", &span.span_context.trace_id()); println!("\tSpanId : {}", &span.span_context.span_id()); + println!("\tTraceFlags : {:?}", &span.span_context.trace_flags()); println!("\tParentSpanId: {}", &span.parent_span_id); println!("\tKind : {:?}", &span.span_kind);