1
- pub mod future;
2
1
mod host;
3
2
4
3
use std:: sync:: { Arc , RwLock } ;
5
4
6
- use future:: ObserveContext ;
7
5
use indexmap:: IndexMap ;
6
+ use opentelemetry:: { global:: ObjectSafeSpan , trace:: TraceContextExt , Context } ;
8
7
use spin_factors:: { Factor , SelfInstanceBuilder } ;
8
+ use tracing_opentelemetry:: OpenTelemetrySpanExt ;
9
9
10
10
#[ derive( Default ) ]
11
11
pub struct ObserveFactor { }
@@ -57,29 +57,6 @@ pub struct InstanceState {
57
57
impl SelfInstanceBuilder for InstanceState { }
58
58
59
59
impl InstanceState {
60
- // /// Close the span associated with the given resource and optionally drop the resource
61
- // /// from the table. Additionally close any other active spans that are more recent on the stack
62
- // /// in reverse order.
63
- // ///
64
- // /// Exiting any spans that were already closed will not cause this to error.
65
- // fn safely_close(&mut self, resource_id: u32, drop_resource: bool) {
66
- // let mut state: std::sync::RwLockWriteGuard<State> = self.state.write().unwrap();
67
-
68
- // if let Some(index) = state
69
- // .active_spans
70
- // .iter()
71
- // .rposition(|(_, id)| *id == resource_id)
72
- // {
73
- // state.close_from_back_to(index);
74
- // } else {
75
- // tracing::debug!("found no active spans to close")
76
- // }
77
-
78
- // if drop_resource {
79
- // state.guest_spans.remove(resource_id).unwrap();
80
- // }
81
- // }
82
-
83
60
pub fn get_observe_context ( & self ) -> ObserveContext {
84
61
ObserveContext {
85
62
state : self . state . clone ( ) ,
@@ -100,65 +77,37 @@ pub(crate) struct State {
100
77
pub active_spans : IndexMap < String , u32 > ,
101
78
}
102
79
103
- // impl State {
104
- // /// Close all active spans from the top of the stack to the given index. Closing entails exiting
105
- // /// the inner [tracing] span and removing it from the active spans stack.
106
- // pub(crate) fn close_from_back_to(&mut self, index: usize) {
107
- // self.active_spans
108
- // .split_off(index)
109
- // .iter()
110
- // .rev()
111
- // .for_each(|(_, id)| {
112
- // if let Some(guest_span) = self.guest_spans.get(*id) {
113
- // guest_span.exit();
114
- // } else {
115
- // tracing::debug!("active_span {id:?} already removed from resource table");
116
- // }
117
- // });
118
- // }
119
-
120
- // /// Enter the inner [tracing] span for all active spans.
121
- // pub(crate) fn enter_all(&self) {
122
- // for (_, guest_span_id) in self.active_spans.iter() {
123
- // if let Some(span_resource) = self.guest_spans.get(*guest_span_id) {
124
- // span_resource.enter();
125
- // } else {
126
- // tracing::debug!("guest span already dropped")
127
- // }
128
- // }
129
- // }
130
-
131
- // /// Exit the inner [tracing] span for all active spans.
132
- // pub(crate) fn exit_all(&self) {
133
- // for (_, guest_span_id) in self.active_spans.iter().rev() {
134
- // if let Some(span_resource) = self.guest_spans.get(*guest_span_id) {
135
- // span_resource.exit();
136
- // } else {
137
- // tracing::debug!("guest span already dropped")
138
- // }
139
- // }
140
- // }
141
- // }
142
-
143
80
/// The WIT resource Span. Effectively wraps an [opentelemetry_sdk::trace::Span].
144
81
pub struct GuestSpan {
145
82
/// The [opentelemetry_sdk::trace::Span] we use to do the actual tracing work.
146
83
pub inner : opentelemetry_sdk:: trace:: Span ,
147
84
}
148
85
149
- // // Note: We use tracing enter instead of Entered because Entered is not Send
150
- // impl GuestSpan {
151
- // /// Enter the inner [tracing] span.
152
- // pub fn enter(&self) {
153
- // self.inner.with_subscriber(|(id, dispatch)| {
154
- // dispatch.enter(id);
155
- // });
156
- // }
157
-
158
- // /// Exits the inner [tracing] span.
159
- // pub fn exit(&self) {
160
- // self.inner.with_subscriber(|(id, dispatch)| {
161
- // dispatch.exit(id);
162
- // });
163
- // }
164
- // }
86
+ pub struct ObserveContext {
87
+ pub ( crate ) state : Arc < RwLock < State > > ,
88
+ }
89
+
90
+ impl ObserveContext {
91
+ /// TODO comment
92
+ pub fn oh_dear_i_better_get_renamed ( & self ) {
93
+ // TODO: Move this duplicate logic into its own impl
94
+ let state = self . state . read ( ) . unwrap ( ) ;
95
+ if state. active_spans . is_empty ( ) {
96
+ return ;
97
+ }
98
+
99
+ let parent_context = Context :: new ( ) . with_remote_span_context (
100
+ state
101
+ . guest_spans
102
+ . get ( * state. active_spans . last ( ) . unwrap ( ) . 1 )
103
+ . unwrap ( )
104
+ . inner
105
+ . span_context ( )
106
+ . clone ( ) ,
107
+ ) ;
108
+ tracing:: Span :: current ( ) . set_parent ( parent_context) ;
109
+ }
110
+ }
111
+
112
+ // TODO(Caleb): Reorder things
113
+ // TODO(Caleb): Make otel a workspace dependency
0 commit comments