Skip to content

Commit

Permalink
Improve performance emitting LogRecords
Browse files Browse the repository at this point in the history
- no need to clone current context for each processor
- use Context::map_current instead, outside loop, to
  compute optional TraceContext once
  • Loading branch information
shaun-cox committed Jul 4, 2023
1 parent 8c2c38b commit e8cd389
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 10 deletions.
4 changes: 2 additions & 2 deletions opentelemetry-api/src/trace/tracer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -382,7 +382,7 @@ impl SpanBuilder {

/// Builds a span with the given tracer from this configuration.
pub fn start<T: Tracer>(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.
Expand Down
19 changes: 11 additions & 8 deletions opentelemetry-sdk/src/logs/log_emitter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use crate::{
};
use opentelemetry_api::{
global::{self},
logs::{LogRecord, LogResult},
logs::{LogRecord, LogResult, TraceContext},
trace::TraceContextExt,
Context, InstrumentationLibrary,
};
Expand Down Expand Up @@ -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()))
})

Check warning on line 213 in opentelemetry-sdk/src/logs/log_emitter.rs

View check run for this annotation

Codecov / codecov/patch

opentelemetry-sdk/src/logs/log_emitter.rs#L209-L213

Added lines #L209 - L213 were not covered by tests
} else {
None

Check warning on line 215 in opentelemetry-sdk/src/logs/log_emitter.rs

View check run for this annotation

Codecov / codecov/patch

opentelemetry-sdk/src/logs/log_emitter.rs#L215

Added line #L215 was not covered by tests
};
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())

Check warning on line 221 in opentelemetry-sdk/src/logs/log_emitter.rs

View check run for this annotation

Codecov / codecov/patch

opentelemetry-sdk/src/logs/log_emitter.rs#L220-L221

Added lines #L220 - L221 were not covered by tests
}
let data = LogData {
record,
Expand Down

0 comments on commit e8cd389

Please sign in to comment.