Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Performance: Context holds SynchronizedSpan directly, not via HashMap #1268

Merged
merged 1 commit into from
Sep 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions opentelemetry/src/context.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use crate::trace::context::SynchronizedSpan;
use std::any::{Any, TypeId};
use std::cell::RefCell;
use std::collections::HashMap;
Expand Down Expand Up @@ -74,6 +75,7 @@ thread_local! {
/// ```
#[derive(Clone, Default)]
pub struct Context {
pub(super) span: Option<Arc<SynchronizedSpan>>,
entries: HashMap<TypeId, Arc<dyn Any + Sync + Send>, BuildHasherDefault<IdHasher>>,
}

Expand Down Expand Up @@ -303,6 +305,20 @@ impl Context {
_marker: PhantomData,
}
}

pub(super) fn current_with_synchronized_span(value: SynchronizedSpan) -> Self {
Context {
span: Some(Arc::new(value)),
entries: Context::map_current(|cx| cx.entries.clone()),
}
}

pub(super) fn with_synchronized_span(&self, value: SynchronizedSpan) -> Self {
Context {
span: Some(Arc::new(value)),
entries: self.entries.clone(),
}
}
}

impl fmt::Debug for Context {
Expand Down
39 changes: 24 additions & 15 deletions opentelemetry/src/trace/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,31 @@ static NOOP_SPAN: Lazy<SynchronizedSpan> = Lazy::new(|| SynchronizedSpan {
pub struct SpanRef<'a>(&'a SynchronizedSpan);

#[derive(Debug)]
struct SynchronizedSpan {
pub(crate) struct SynchronizedSpan {
/// Immutable span context
span_context: SpanContext,
/// Mutable span inner that requires synchronization
inner: Option<Mutex<global::BoxedSpan>>,
}

impl From<SpanContext> for SynchronizedSpan {
fn from(value: SpanContext) -> Self {
Self {
span_context: value,
inner: None,
}
}
}

impl<T: Span + Send + Sync + 'static> From<T> for SynchronizedSpan {
fn from(value: T) -> Self {
Self {
span_context: value.span_context().clone(),
inner: Some(Mutex::new(global::BoxedSpan::new(value))),
}
}
}

impl SpanRef<'_> {
fn with_inner_mut<F: FnOnce(&mut global::BoxedSpan)>(&self, f: F) {
if let Some(ref inner) = self.0.inner {
Expand Down Expand Up @@ -253,36 +271,27 @@ pub trait TraceContextExt {

impl TraceContextExt for Context {
fn current_with_span<T: crate::trace::Span + Send + Sync + 'static>(span: T) -> Self {
Context::current_with_value(SynchronizedSpan {
span_context: span.span_context().clone(),
inner: Some(Mutex::new(global::BoxedSpan::new(span))),
})
Context::current_with_synchronized_span(span.into())
}

fn with_span<T: crate::trace::Span + Send + Sync + 'static>(&self, span: T) -> Self {
self.with_value(SynchronizedSpan {
span_context: span.span_context().clone(),
inner: Some(Mutex::new(global::BoxedSpan::new(span))),
})
self.with_synchronized_span(span.into())
}

fn span(&self) -> SpanRef<'_> {
if let Some(span) = self.get::<SynchronizedSpan>() {
if let Some(span) = self.span.as_ref() {
SpanRef(span)
} else {
SpanRef(&NOOP_SPAN)
}
}

fn has_active_span(&self) -> bool {
self.get::<SynchronizedSpan>().is_some()
self.span.is_some()
}

fn with_remote_span_context(&self, span_context: crate::trace::SpanContext) -> Self {
self.with_value(SynchronizedSpan {
span_context,
inner: None,
})
self.with_synchronized_span(span_context.into())
}
}

Expand Down
2 changes: 1 addition & 1 deletion opentelemetry/src/trace/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ use std::borrow::Cow;
use std::time;
use thiserror::Error;

mod context;
pub(crate) mod context;
pub mod noop;
mod span;
mod span_context;
Expand Down