Skip to content

Commit a71d56b

Browse files
bobrikTheJokr
authored andcommitted
Add a new SharedSpanHandle::Inactive variant
It's expected that most of the spans are not sampled. We still clone and stack them, which adds up. Let's add a new variant for unsampled spans that is cheaper to clone.
1 parent aadf368 commit a71d56b

File tree

2 files changed

+15
-9
lines changed

2 files changed

+15
-9
lines changed

foundations/src/telemetry/tracing/internal.rs

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,38 +9,39 @@ use parking_lot::RwLock;
99
use rand::{self, Rng};
1010
use std::borrow::Cow;
1111
use std::error::Error;
12-
use std::sync::Arc;
12+
use std::sync::{Arc, LazyLock};
1313

1414
pub(crate) type Tracer = cf_rustracing::Tracer<BoxSampler<SpanContextState>, SpanContextState>;
1515

16+
static INACTIVE_SPAN: LazyLock<RwLock<Span>> = LazyLock::new(|| RwLock::new(Span::inactive()));
17+
1618
/// Shared span with mutability and additional reference tracking for
1719
/// ad-hoc inspection.
1820
#[derive(Clone, Debug)]
1921
pub(crate) enum SharedSpanHandle {
2022
Tracked(Arc<LiveReferenceHandle<Arc<RwLock<Span>>>>),
2123
Untracked(Arc<RwLock<Span>>),
24+
Inactive,
2225
}
2326

2427
impl SharedSpanHandle {
2528
pub(crate) fn new(span: Span) -> Self {
26-
let is_sampled = span.is_sampled();
27-
28-
TracingHarness::get()
29-
.active_roots
30-
.track(Arc::new(RwLock::new(span)), is_sampled)
29+
TracingHarness::get().active_roots.track(span)
3130
}
3231

3332
pub(crate) fn read(&self) -> parking_lot::RwLockReadGuard<'_, Span> {
3433
match self {
3534
SharedSpanHandle::Tracked(handle) => handle.read(),
3635
SharedSpanHandle::Untracked(rw_lock) => rw_lock.read(),
36+
SharedSpanHandle::Inactive => (*INACTIVE_SPAN).read(),
3737
}
3838
}
3939

4040
pub(crate) fn write(&self) -> parking_lot::RwLockWriteGuard<'_, Span> {
4141
match self {
4242
SharedSpanHandle::Tracked(handle) => handle.write(),
4343
SharedSpanHandle::Untracked(rw_lock) => rw_lock.write(),
44+
SharedSpanHandle::Inactive => (*INACTIVE_SPAN).write(),
4445
}
4546
}
4647
}
@@ -50,6 +51,7 @@ impl From<SharedSpanHandle> for Arc<RwLock<Span>> {
5051
match value {
5152
SharedSpanHandle::Tracked(handle) => Arc::clone(&handle),
5253
SharedSpanHandle::Untracked(rw_lock) => Arc::clone(&rw_lock),
54+
SharedSpanHandle::Inactive => Arc::new(RwLock::new(Span::inactive())),
5355
}
5456
}
5557
}

foundations/src/telemetry/tracing/live/mod.rs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,15 @@ impl ActiveRoots {
3939
event_output::spans_to_trace_events(self.start, &self.roots.get_live_references())
4040
}
4141

42-
pub(crate) fn track(&self, value: Arc<RwLock<Span>>, is_sampled: bool) -> SharedSpanHandle {
42+
pub(crate) fn track(&self, span: Span) -> SharedSpanHandle {
43+
let is_sampled = span.is_sampled();
44+
4345
if self.settings.enabled && (self.settings.track_all_spans || is_sampled) {
44-
SharedSpanHandle::Tracked(self.roots.track(value))
46+
SharedSpanHandle::Tracked(self.roots.track(Arc::new(RwLock::new(span))))
47+
} else if is_sampled {
48+
SharedSpanHandle::Untracked(Arc::new(RwLock::new(span)))
4549
} else {
46-
SharedSpanHandle::Untracked(value)
50+
SharedSpanHandle::Inactive
4751
}
4852
}
4953
}

0 commit comments

Comments
 (0)