Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 25 additions & 1 deletion src/component.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use {
crate::linking::ComponentCtx,
crate::linking::{ComponentCtx, FuncCall},
wasmtime::component::{self, HasSelf},
};

Expand Down Expand Up @@ -131,6 +131,30 @@ pub(crate) mod bindings {
});
}

impl bindings::proxy::recorder::record::Host for ComponentCtx {
fn record_args(&mut self, method: Option<String>, args: Vec<String>, is_export: bool) {
let call = if is_export {
FuncCall::ExportArgs {
method: method.unwrap(),
args,
}
} else {
FuncCall::ImportArgs { method, args }
};
println!("{:?}", call);
self.logger.push(call);
}
fn record_ret(&mut self, method: Option<String>, ret: Option<String>, is_export: bool) {
let call = if is_export {
FuncCall::ExportRet { method, ret }
} else {
FuncCall::ImportRet { method, ret }
};
println!("{:?}", call);
self.logger.push(call);
}
}

pub fn link_host_functions(linker: &mut component::Linker<ComponentCtx>) -> anyhow::Result<()> {
let options = bindings::LinkOptions::default();

Expand Down
6 changes: 6 additions & 0 deletions src/execute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -671,6 +671,12 @@ impl ExecuteCtx {
}
};

// If we collected a recording trace, write to a file
if !store.data().logger.is_empty() {
let trace = serde_json::to_string(&store.data().logger).unwrap();
std::fs::write("trace.out", &trace).unwrap();
}

// Ensure the downstream response channel is closed, whether or not a response was
// sent during execution.
let resp = outcome
Expand Down
22 changes: 22 additions & 0 deletions src/linking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,26 @@ impl wasmtime::ResourceLimiter for Limiter {
}
}

#[derive(serde::Serialize, serde::Deserialize, Debug)]
pub(crate) enum FuncCall {
ExportArgs {
method: String,
args: Vec<String>,
},
ExportRet {
method: Option<String>,
ret: Option<String>,
},
ImportArgs {
method: Option<String>,
args: Vec<String>,
},
ImportRet {
method: Option<String>,
ret: Option<String>,
},
}

#[allow(unused)]
pub struct ComponentCtx {
pub wasi_ctx: wasmtime_wasi::WasiCtx,
Expand All @@ -100,6 +120,7 @@ pub struct ComponentCtx {
pub(crate) session: Session,
guest_profiler: Option<Box<GuestProfiler>>,
limiter: Limiter,
pub(crate) logger: Vec<FuncCall>,
}

/// An extension trait for users of `ComponentCtx` to access the session.
Expand Down Expand Up @@ -160,6 +181,7 @@ impl ComponentCtx {
session,
guest_profiler: guest_profiler.map(Box::new),
limiter: Limiter::new(100, 100, 100),
logger: Vec::new(),
};
let mut store = Store::new(ctx.engine(), wasm_ctx);
store.set_epoch_deadline(1);
Expand Down
1 change: 1 addition & 0 deletions wasm_abi/wit/deps/fastly/compute.wit
Original file line number Diff line number Diff line change
Expand Up @@ -2940,6 +2940,7 @@ world service-imports {
import secret-store;
import security;
import shielding;
import proxy:recorder/%record@0.1.0;
}

/// A Fastly Compute service.
Expand Down
16 changes: 16 additions & 0 deletions wasm_abi/wit/deps/proxy/recorder.wit
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package proxy:recorder@0.1.0;

interface %record {
record-args: func(method: option<string>, args: list<string>, is-export: bool);
record-ret: func(method: option<string>, ret: option<string>, is-export: bool);
}

interface replay {
replay-export: func() -> option<tuple<string, list<string>>>;
assert-export-ret: func(method: option<string>, ret: option<string>);
replay-import: func(method: option<string>, args: option<list<string>>) -> option<string>;
}

world host {
import %record;
}
Loading