@@ -3,7 +3,11 @@ mod host;
33use std:: sync:: { Arc , RwLock } ;
44
55use indexmap:: IndexMap ;
6- use opentelemetry:: { global:: ObjectSafeSpan , trace:: TraceContextExt , Context } ;
6+ use opentelemetry:: {
7+ global:: { self , BoxedTracer , ObjectSafeSpan } ,
8+ trace:: TraceContextExt ,
9+ Context ,
10+ } ;
711use spin_factors:: { Factor , SelfInstanceBuilder } ;
812use tracing_opentelemetry:: OpenTelemetrySpanExt ;
913
@@ -32,14 +36,18 @@ impl Factor for ObserveFactor {
3236
3337 fn prepare < T : spin_factors:: RuntimeFactors > (
3438 & self ,
35- _ctx : spin_factors:: PrepareContext < Self > ,
39+ ctx : spin_factors:: PrepareContext < Self > ,
3640 _builders : & mut spin_factors:: InstanceBuilders < T > ,
3741 ) -> anyhow:: Result < Self :: InstanceBuilder > {
42+ // TODO(Lann): Is it fine that we're using BoxedTracer and BoxedSpan? Are there perf tradeoffs?
43+ // TODO(Lann): Would it make sense to put the tracer on the app state and just hold a reference to it in the instance state?
44+ let tracer = global:: tracer ( ctx. app_component ( ) . app . id ( ) . to_string ( ) ) ;
3845 Ok ( InstanceState {
3946 state : Arc :: new ( RwLock :: new ( State {
4047 guest_spans : table:: Table :: new ( 1024 ) ,
4148 active_spans : Default :: default ( ) ,
4249 } ) ) ,
50+ tracer,
4351 } )
4452 }
4553}
@@ -52,6 +60,7 @@ impl ObserveFactor {
5260
5361pub struct InstanceState {
5462 pub ( crate ) state : Arc < RwLock < State > > ,
63+ pub ( crate ) tracer : BoxedTracer ,
5564}
5665
5766impl SelfInstanceBuilder for InstanceState { }
@@ -65,7 +74,10 @@ impl InstanceState {
6574 }
6675}
6776
68- /// Internal state of the observe factor.
77+ /// Internal state of the ObserveFactor instance state.
78+ ///
79+ /// This data lives here rather than directly on InstanceState so that we can have multiple things
80+ /// take Arc references to it.
6981pub ( crate ) struct State {
7082 /// A resource table that holds the guest spans.
7183 pub guest_spans : table:: Table < GuestSpan > ,
@@ -78,10 +90,10 @@ pub(crate) struct State {
7890 pub active_spans : IndexMap < String , u32 > ,
7991}
8092
81- /// The WIT resource Span. Effectively wraps an [opentelemetry_sdk::trace::Span ].
93+ /// The WIT resource Span. Effectively wraps an [opentelemetry::global::BoxedSpan ].
8294pub struct GuestSpan {
83- /// The [opentelemetry_sdk::trace::Span ] we use to do the actual tracing work.
84- pub inner : opentelemetry_sdk :: trace :: Span ,
95+ /// The [opentelemetry::global::BoxedSpan ] we use to do the actual tracing work.
96+ pub inner : opentelemetry :: global :: BoxedSpan ,
8597}
8698
8799/// TODO comment
0 commit comments