Skip to content

Commit

Permalink
chore: add missing example file
Browse files Browse the repository at this point in the history
  • Loading branch information
G4Vi committed Feb 24, 2024
1 parent 4bfa321 commit 6890140
Showing 1 changed file with 88 additions and 0 deletions.
88 changes: 88 additions & 0 deletions rust/examples/reactor-hello-world-otel-stdout-components.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
use dylibso_observe_sdk::{
adapter::otelstdout::OtelStdoutAdapter,
context::component::{ObserveSdk, ObserveSdkView},
};

use wasmtime_wasi::preview2::{ResourceTable, WasiCtx, WasiView};

struct State {
table: ResourceTable,
wasi_ctx: WasiCtx,
observe_sdk: ObserveSdk,
}

impl WasiView for State {
fn table(&self) -> &ResourceTable {
&self.table
}

fn table_mut(&mut self) -> &mut ResourceTable {
&mut self.table
}

fn ctx(&self) -> &WasiCtx {
&self.wasi_ctx
}

fn ctx_mut(&mut self) -> &mut WasiCtx {
&mut self.wasi_ctx
}
}

impl ObserveSdkView for State {
fn sdk_mut(&mut self) -> &mut ObserveSdk {
&mut self.observe_sdk
}
}

wasmtime::component::bindgen!({path: "../corpus/01-component-instr-component/wit", async: true});

#[tokio::main]
pub async fn main() -> anyhow::Result<()> {
let args: Vec<_> = std::env::args().skip(1).collect();
let data = std::fs::read(&args[0])?;
let mut config = wasmtime::Config::new();

config.async_support(true);
config.wasm_component_model(true);

// Create instance
let engine = wasmtime::Engine::new(&config)?;
let component = wasmtime::component::Component::new(&engine, &data)?;

let table = ResourceTable::new();

// Setup WASI
let wasi_ctx = wasmtime_wasi::preview2::WasiCtxBuilder::new()
.inherit_stdio()
.args(&args.clone())
.build();

let mut linker = wasmtime::component::Linker::new(&engine);

let adapter = OtelStdoutAdapter::create();
let observe_sdk = adapter.build_observe_sdk(&data, Default::default())?;

let state = State {
table,
wasi_ctx,
observe_sdk,
};
let mut store = wasmtime::Store::new(&engine, state);
wasmtime_wasi::preview2::command::add_to_linker(&mut linker)?;

dylibso_observe_sdk::context::component::add_to_linker(&mut linker)?;

let (component_instr_component, _instance) =
Example::instantiate_async(&mut store, &component, &linker).await?;

Check failure on line 77 in rust/examples/reactor-hello-world-otel-stdout-components.rs

View workflow job for this annotation

GitHub Actions / Test Rust

failed to resolve: use of undeclared type `Example`

match component_instr_component.call_hello_world(&mut store).await {
Ok(result) => println!("hello_world: {:?}", result),
_ => println!("encountered error"),
};

let state = store.into_data();
state.observe_sdk.shutdown().await?;

Ok(())
}

0 comments on commit 6890140

Please sign in to comment.