Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: WASI Observe prototype #2485

Draft
wants to merge 6 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
32 changes: 32 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions crates/core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -483,7 +483,9 @@ impl<T: OutboundWasiHttpHandler + Send + Sync> Engine<T> {
let inner = self.module_linker.instantiate_pre(module)?;
Ok(ModuleInstancePre { inner })
}
}

impl<T> Engine<T> {
/// Find the [`HostComponentDataHandle`] for a [`HostComponent`] if configured for this engine.
/// Note: [`DynamicHostComponent`]s are implicitly wrapped in `Arc`s and need to be explicitly
/// typed as such here, e.g. `find_host_component_handle::<Arc<MyDynamicHostComponent>>()`.
Expand Down
32 changes: 32 additions & 0 deletions crates/observe/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
[package]
name = "spin-observe"
version = { workspace = true }
authors = { workspace = true }
edition = { workspace = true }

[dependencies]
anyhow = "1.0"
async-trait = "0.1"
dotenvy = "0.15"
once_cell = "1"
spin-app = { path = "../app" }
spin-core = { path = "../core" }
spin-expressions = { path = "../expressions" }
spin-world = { path = "../world" }
spin-telemetry = { path = "../telemetry" }
table = { path = "../table" }
thiserror = "1"
tokio = { version = "1", features = ["rt-multi-thread"] }
vaultrs = "0.6.2"
serde = "1.0.188"
tracing = "0.1.40"
tracing-opentelemetry = "0.23.0"
pin-project-lite = "0.2"
opentelemetry = { version = "0.22.0", features = [ "metrics", "trace"] }
opentelemetry_sdk = { version = "0.22.1", features = ["rt-tokio"] }
opentelemetry-otlp = { version = "0.15.0", default-features=false, features = ["http-proto", "trace", "http", "reqwest-client", "metrics", "grpc-tonic"] }
futures-executor = "0.3"
indexmap = "2.2.6"

[dev-dependencies]
toml = "0.5"
95 changes: 95 additions & 0 deletions crates/observe/src/future.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
use anyhow::{Context, Result};
use pin_project_lite::pin_project;
use std::{
future::Future,
sync::{Arc, RwLock},
};

use spin_core::{Engine, Store};

use crate::{host_component::State, ObserveHostComponent};

pin_project! {
struct Instrumented<F> {
#[pin]
inner: F,
observe_context: ObserveContext,
}

impl<F> PinnedDrop for Instrumented<F> {
fn drop(this: Pin<&mut Self>) {
this.project().observe_context.drop_all();
}
}
}

pub trait FutureExt: Future + Sized {
/// Manage WASI Observe guest spans.
fn manage_guest_spans(
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe manage_wasi_observe_spans? At the call site it won't be obvious where this method comes from.

self,
observe_context: ObserveContext,
) -> Result<impl Future<Output = Self::Output>>;
}

impl<F: Future> FutureExt for F {
fn manage_guest_spans(
self,
observe_context: ObserveContext,
) -> Result<impl Future<Output = Self::Output>> {
Ok(Instrumented {
inner: self,
observe_context,
})
}
}

impl<F: Future> Future for Instrumented<F> {
type Output = F::Output;

/// Maintains the invariant that all active spans are entered before polling the inner future
/// and exited otherwise. If we don't do this then the timing (among many other things) of the
/// spans becomes wildly incorrect.
fn poll(
self: std::pin::Pin<&mut Self>,
cx: &mut std::task::Context<'_>,
) -> std::task::Poll<Self::Output> {
let this = self.project();

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

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

// Exit the active spans after exiting the inner poll
{
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>>,
}

impl ObserveContext {
pub fn new<T>(store: &mut Store<T>, engine: &Engine<T>) -> Result<Self> {
let handle = engine
.find_host_component_handle::<Arc<ObserveHostComponent>>()
.context("host component handle not found")?;
let state = store
.host_components_data()
.get_or_insert(handle)
.state
.clone();
Ok(Self { state })
}

fn drop_all(&self) {
self.state.write().unwrap().close_from_back_to(0);
}
}