Skip to content

Commit

Permalink
Refactoring 2
Browse files Browse the repository at this point in the history
Signed-off-by: Caleb Schoepp <caleb.schoepp@fermyon.com>
  • Loading branch information
calebschoepp committed May 2, 2024
1 parent db12ff3 commit 4b578a5
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 24 deletions.
27 changes: 4 additions & 23 deletions crates/observe/src/future.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,38 +57,21 @@ impl<F: Future> Future for Instrumented<F> {

// Enter the active spans before entering the inner poll
{
let state = this.observe_context.state.write().unwrap();

// TODO: Make it a method on state
for guest_span_id in state.active_spans.iter() {
if let Some(span_resource) = state.guest_spans.get(*guest_span_id) {
span_resource.enter();
} else {
tracing::error!("No span to enter")
}
}
this.observe_context.state.write().unwrap().enter_all();
}

let ret = this.inner.poll(cx);

// Exit the active spans after exiting the inner poll
{
let state = this.observe_context.state.write().unwrap();

// TODO: Make it a method on state
for span_id in state.active_spans.iter().rev() {
if let Some(span_resource) = state.guest_spans.get(*span_id) {
span_resource.exit();
} else {
tracing::error!("span already dropped")
}
}
this.observe_context.state.write().unwrap().exit_all();
}

ret
}
}

/// The context necessary for the observe host component to function.
pub struct ObserveContext {
state: Arc<RwLock<State>>,
}
Expand All @@ -107,8 +90,6 @@ impl ObserveContext {
}

fn drop_all(&self) {
let mut state: std::sync::RwLockWriteGuard<State> = self.state.write().unwrap();

state.close_from_back_to(0);
self.state.write().unwrap().close_from_back_to(0);
}
}
24 changes: 23 additions & 1 deletion crates/observe/src/host_component.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ pub(crate) struct State {
impl State {
/// Close all active spans from the top of the stack to the given index. Closing entails exiting
/// the inner [tracing] span and removing it from the active spans stack.
pub fn close_from_back_to(&mut self, index: usize) {
pub(crate) fn close_from_back_to(&mut self, index: usize) {
self.active_spans
.split_off(index)
.iter()
Expand All @@ -133,6 +133,28 @@ impl State {
}
});
}

/// Enter the inner [tracing] span for all active spans.
pub(crate) fn enter_all(&self) {
for guest_span_id in self.active_spans.iter() {
if let Some(span_resource) = self.guest_spans.get(*guest_span_id) {
span_resource.enter();
} else {
tracing::debug!("guest span already dropped")
}
}
}

/// Exit the inner [tracing] span for all active spans.
pub(crate) fn exit_all(&self) {
for guest_span_id in self.active_spans.iter().rev() {
if let Some(span_resource) = self.guest_spans.get(*guest_span_id) {
span_resource.exit();
} else {
tracing::debug!("guest span already dropped")
}
}
}
}

/// The WIT resource Span. Effectively wraps a [tracing] span.
Expand Down

0 comments on commit 4b578a5

Please sign in to comment.