@@ -5,7 +5,7 @@ use std::sync::{Arc, RwLock};
55use indexmap:: IndexMap ;
66use opentelemetry:: {
77 global:: { self , BoxedTracer , ObjectSafeSpan } ,
8- trace:: TraceContextExt ,
8+ trace:: { SpanId , TraceContextExt } ,
99 Context ,
1010} ;
1111use 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.
8182pub ( 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