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 2633f5a072..f881f3adb2 100644 --- a/opentelemetry-stdout/src/logs/exporter.rs +++ b/opentelemetry-stdout/src/logs/exporter.rs @@ -82,6 +82,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);