Skip to content

Commit 0bbb37b

Browse files
committed
Catch reparent being used outside of factor
Signed-off-by: Caleb Schoepp <[email protected]>
1 parent 11c9314 commit 0bbb37b

File tree

4 files changed

+32
-5
lines changed

4 files changed

+32
-5
lines changed

Cargo.lock

Lines changed: 0 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/factor-observe/Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ thiserror = "1"
2626
tokio = { version = "1", features = ["rt-multi-thread"] }
2727
tracing = "0.1.40"
2828
tracing-opentelemetry = "0.23.0"
29-
vaultrs = "0.6.2"
3029

3130
[dev-dependencies]
3231
toml = "0.5"

crates/factor-observe/src/host.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,16 @@ impl traces::HostSpan for InstanceState {
2020
async fn start(&mut self, name: String) -> Result<Resource<WitSpan>> {
2121
let mut state = self.state.write().unwrap();
2222

23+
if state.active_spans.is_empty() {
24+
state.original_host_span_id = Some(
25+
tracing::Span::current()
26+
.context()
27+
.span()
28+
.span_context()
29+
.span_id(),
30+
);
31+
}
32+
2333
// TODO(Caleb): Make this cleaner
2434
let parent_context = match state.active_spans.is_empty() {
2535
true => tracing::Span::current().context(),

crates/factor-observe/src/lib.rs

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use std::sync::{Arc, RwLock};
55
use indexmap::IndexMap;
66
use opentelemetry::{
77
global::{self, BoxedTracer, ObjectSafeSpan},
8-
trace::TraceContextExt,
8+
trace::{SpanId, TraceContextExt},
99
Context,
1010
};
1111
use spin_factors::{Factor, SelfInstanceBuilder};
@@ -46,6 +46,7 @@ impl Factor for ObserveFactor {
4646
state: Arc::new(RwLock::new(State {
4747
guest_spans: table::Table::new(1024),
4848
active_spans: Default::default(),
49+
original_host_span_id: None,
4950
})),
5051
tracer,
5152
})
@@ -80,14 +81,20 @@ impl InstanceState {
8081
/// take Arc references to it.
8182
pub(crate) struct State {
8283
/// A resource table that holds the guest spans.
83-
pub guest_spans: table::Table<GuestSpan>,
84+
pub(crate) guest_spans: table::Table<GuestSpan>,
8485

8586
/// A LIFO stack of guest spans that are currently active.
8687
///
8788
/// Only a reference ID to the guest span is held here. The actual guest span must be looked up
8889
/// in the `guest_spans` table using the reference ID.
8990
/// TODO: Fix comment
90-
pub active_spans: IndexMap<String, u32>,
91+
pub(crate) active_spans: IndexMap<String, u32>,
92+
93+
/// Id of the last span emitted from within the host before entering the guest.
94+
///
95+
/// We use this to avoid accidentally reparenting the original host span as a child of a guest
96+
/// span.
97+
pub(crate) original_host_span_id: Option<SpanId>,
9198
}
9299

93100
/// The WIT resource Span. Effectively wraps an [opentelemetry::global::BoxedSpan].
@@ -112,6 +119,18 @@ impl ObserveContext {
112119
return;
113120
}
114121

122+
if let Some(original_host_span_id) = state.original_host_span_id {
123+
if tracing::Span::current()
124+
.context()
125+
.span()
126+
.span_context()
127+
.span_id()
128+
.eq(&original_host_span_id)
129+
{
130+
panic!("TODO This should not happen")
131+
}
132+
}
133+
115134
let parent_context = Context::new().with_remote_span_context(
116135
state
117136
.guest_spans

0 commit comments

Comments
 (0)