@@ -5,7 +5,7 @@ use std::sync::{Arc, RwLock};
5
5
use indexmap:: IndexMap ;
6
6
use opentelemetry:: {
7
7
global:: { self , BoxedTracer , ObjectSafeSpan } ,
8
- trace:: TraceContextExt ,
8
+ trace:: { SpanId , TraceContextExt } ,
9
9
Context ,
10
10
} ;
11
11
use spin_factors:: { Factor , SelfInstanceBuilder } ;
@@ -46,6 +46,7 @@ impl Factor for ObserveFactor {
46
46
state : Arc :: new ( RwLock :: new ( State {
47
47
guest_spans : table:: Table :: new ( 1024 ) ,
48
48
active_spans : Default :: default ( ) ,
49
+ original_host_span_id : None ,
49
50
} ) ) ,
50
51
tracer,
51
52
} )
@@ -80,14 +81,20 @@ impl InstanceState {
80
81
/// take Arc references to it.
81
82
pub ( crate ) struct State {
82
83
/// A resource table that holds the guest spans.
83
- pub guest_spans : table:: Table < GuestSpan > ,
84
+ pub ( crate ) guest_spans : table:: Table < GuestSpan > ,
84
85
85
86
/// A LIFO stack of guest spans that are currently active.
86
87
///
87
88
/// Only a reference ID to the guest span is held here. The actual guest span must be looked up
88
89
/// in the `guest_spans` table using the reference ID.
89
90
/// 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 > ,
91
98
}
92
99
93
100
/// The WIT resource Span. Effectively wraps an [opentelemetry::global::BoxedSpan].
@@ -112,6 +119,18 @@ impl ObserveContext {
112
119
return ;
113
120
}
114
121
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
+
115
134
let parent_context = Context :: new ( ) . with_remote_span_context (
116
135
state
117
136
. guest_spans
0 commit comments