diff --git a/opentelemetry-api/src/trace/tracer.rs b/opentelemetry-api/src/trace/tracer.rs index b400733d29..c46704944e 100644 --- a/opentelemetry-api/src/trace/tracer.rs +++ b/opentelemetry-api/src/trace/tracer.rs @@ -169,7 +169,7 @@ pub trait Tracer { /// Start a [`Span`] from a [`SpanBuilder`]. fn build(&self, builder: SpanBuilder) -> Self::Span { - self.build_with_context(builder, &Context::current()) + Context::map_current(|cx| self.build_with_context(builder, cx)) } /// Start a span from a [`SpanBuilder`] with a parent context. @@ -382,7 +382,7 @@ impl SpanBuilder { /// Builds a span with the given tracer from this configuration. pub fn start(self, tracer: &T) -> T::Span { - tracer.build_with_context(self, &Context::current()) + Context::map_current(|cx| tracer.build_with_context(self, cx)) } /// Builds a span with the given tracer from this configuration and parent. diff --git a/opentelemetry-sdk/src/logs/log_emitter.rs b/opentelemetry-sdk/src/logs/log_emitter.rs index f4a03e89dc..83b576e7ab 100644 --- a/opentelemetry-sdk/src/logs/log_emitter.rs +++ b/opentelemetry-sdk/src/logs/log_emitter.rs @@ -5,7 +5,7 @@ use crate::{ }; use opentelemetry_api::{ global::{self}, - logs::{LogRecord, LogResult}, + logs::{LogRecord, LogResult, TraceContext}, trace::TraceContextExt, Context, InstrumentationLibrary, }; @@ -206,16 +206,19 @@ impl opentelemetry_api::logs::Logger for Logger { Some(provider) => provider, None => return, }; - + let trace_context = if self.include_trace_context { + Context::map_current(|cx| { + cx.has_active_span() + .then(|| TraceContext::from(cx.span().span_context())) + }) + } else { + None + }; let config = provider.config(); for processor in provider.log_processors() { let mut record = record.clone(); - if self.include_trace_context { - let ctx = Context::current(); - if ctx.has_active_span() { - let span = ctx.span(); - record.trace_context = Some(span.span_context().into()); - } + if let Some(ref trace_context) = trace_context { + record.trace_context = Some(trace_context.clone()) } let data = LogData { record,