Skip to content

Commit ab9972f

Browse files
authored
Context holds SynchronizedSpan directly, not via HashMap (#1268)
- lookup on TypeId and downcasting isn't necesary - Context::with_value and current_with_value are more efficient as they no longer clone and overwrite the entry in the map which represens the current span
1 parent e7fb708 commit ab9972f

File tree

3 files changed

+41
-16
lines changed

3 files changed

+41
-16
lines changed

opentelemetry/src/context.rs

+16
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use crate::trace::context::SynchronizedSpan;
12
use std::any::{Any, TypeId};
23
use std::cell::RefCell;
34
use std::collections::HashMap;
@@ -74,6 +75,7 @@ thread_local! {
7475
/// ```
7576
#[derive(Clone, Default)]
7677
pub struct Context {
78+
pub(super) span: Option<Arc<SynchronizedSpan>>,
7779
entries: HashMap<TypeId, Arc<dyn Any + Sync + Send>, BuildHasherDefault<IdHasher>>,
7880
}
7981

@@ -303,6 +305,20 @@ impl Context {
303305
_marker: PhantomData,
304306
}
305307
}
308+
309+
pub(super) fn current_with_synchronized_span(value: SynchronizedSpan) -> Self {
310+
Context {
311+
span: Some(Arc::new(value)),
312+
entries: Context::map_current(|cx| cx.entries.clone()),
313+
}
314+
}
315+
316+
pub(super) fn with_synchronized_span(&self, value: SynchronizedSpan) -> Self {
317+
Context {
318+
span: Some(Arc::new(value)),
319+
entries: self.entries.clone(),
320+
}
321+
}
306322
}
307323

308324
impl fmt::Debug for Context {

opentelemetry/src/trace/context.rs

+24-15
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,31 @@ static NOOP_SPAN: Lazy<SynchronizedSpan> = Lazy::new(|| SynchronizedSpan {
2626
pub struct SpanRef<'a>(&'a SynchronizedSpan);
2727

2828
#[derive(Debug)]
29-
struct SynchronizedSpan {
29+
pub(crate) struct SynchronizedSpan {
3030
/// Immutable span context
3131
span_context: SpanContext,
3232
/// Mutable span inner that requires synchronization
3333
inner: Option<Mutex<global::BoxedSpan>>,
3434
}
3535

36+
impl From<SpanContext> for SynchronizedSpan {
37+
fn from(value: SpanContext) -> Self {
38+
Self {
39+
span_context: value,
40+
inner: None,
41+
}
42+
}
43+
}
44+
45+
impl<T: Span + Send + Sync + 'static> From<T> for SynchronizedSpan {
46+
fn from(value: T) -> Self {
47+
Self {
48+
span_context: value.span_context().clone(),
49+
inner: Some(Mutex::new(global::BoxedSpan::new(value))),
50+
}
51+
}
52+
}
53+
3654
impl SpanRef<'_> {
3755
fn with_inner_mut<F: FnOnce(&mut global::BoxedSpan)>(&self, f: F) {
3856
if let Some(ref inner) = self.0.inner {
@@ -253,36 +271,27 @@ pub trait TraceContextExt {
253271

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

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

269281
fn span(&self) -> SpanRef<'_> {
270-
if let Some(span) = self.get::<SynchronizedSpan>() {
282+
if let Some(span) = self.span.as_ref() {
271283
SpanRef(span)
272284
} else {
273285
SpanRef(&NOOP_SPAN)
274286
}
275287
}
276288

277289
fn has_active_span(&self) -> bool {
278-
self.get::<SynchronizedSpan>().is_some()
290+
self.span.is_some()
279291
}
280292

281293
fn with_remote_span_context(&self, span_context: crate::trace::SpanContext) -> Self {
282-
self.with_value(SynchronizedSpan {
283-
span_context,
284-
inner: None,
285-
})
294+
self.with_synchronized_span(span_context.into())
286295
}
287296
}
288297

opentelemetry/src/trace/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ use std::borrow::Cow;
166166
use std::time;
167167
use thiserror::Error;
168168

169-
mod context;
169+
pub(crate) mod context;
170170
pub mod noop;
171171
mod span;
172172
mod span_context;

0 commit comments

Comments
 (0)