Skip to content

Commit

Permalink
wasmtime component bindgen: when tracing is enabled, emit an event fo…
Browse files Browse the repository at this point in the history
…r args & results (bytecodealliance#6209)

* wasmtime component bindgen: when tracing is enabled, emit an event for arguments and results

This is consistient with what wiggle does (see
https://github.com/bytecodealliance/wasmtime/blob/main/crates/wiggle/generate/src/funcs.rs#L266), with the exceptions that
1. wiggle has a facility for disabling tracing on a per-function basis,
a requirement which was driven by functions which pass secrets into wasm. this will be added to wasmtime-wit-bindgen at a later date.
2. wiggle doesn't actually emit an event when calling a function which
takes no arguments (see `&& func.params.len() > 0` in predicate), in
this case we emit an event with the body `"call"`, to ensure these calls
are observable.

* review feedback: add call and return messages to events

* consistiency: dont drop `guest` from `wit-bindgen guest export` in span
  • Loading branch information
Pat Hickey authored and eduardomourar committed Apr 16, 2023
1 parent 653d306 commit 235e36b
Showing 1 changed file with 27 additions and 4 deletions.
31 changes: 27 additions & 4 deletions crates/wit-bindgen/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1112,11 +1112,12 @@ impl<'a> InterfaceGenerator<'a> {
}

if self.gen.opts.tracing {
self.src.push_str(&format!(
uwrite!(
self.src,
"
let span = tracing::span!(
tracing::Level::TRACE,
\"wit-bindgen guest import\",
\"wit-bindgen import\",
module = \"{}\",
function = \"{}\",
);
Expand All @@ -1131,7 +1132,22 @@ impl<'a> InterfaceGenerator<'a> {
TypeOwner::None => "<no owner>",
},
func.name,
));
);
let mut event_fields = func
.params
.iter()
.enumerate()
.map(|(i, (name, _ty))| {
let name = to_rust_ident(&name);
format!("{name} = tracing::field::debug(&arg{i})")
})
.collect::<Vec<String>>();
event_fields.push(format!("\"call\""));
uwrite!(
self.src,
"tracing::event!(tracing::Level::TRACE, {});\n",
event_fields.join(", ")
);
}

self.src.push_str("let host = get(caller.data_mut());\n");
Expand All @@ -1146,6 +1162,13 @@ impl<'a> InterfaceGenerator<'a> {
uwrite!(self.src, ");\n");
}

if self.gen.opts.tracing {
uwrite!(
self.src,
"tracing::event!(tracing::Level::TRACE, result = tracing::field::debug(&r), \"return\");"
);
}

if self
.special_case_trappable_error(owner, &func.results)
.is_some()
Expand Down Expand Up @@ -1272,7 +1295,7 @@ impl<'a> InterfaceGenerator<'a> {
"
let span = tracing::span!(
tracing::Level::TRACE,
\"wit-bindgen guest export\",
\"wit-bindgen export\",
module = \"{}\",
function = \"{}\",
);
Expand Down

0 comments on commit 235e36b

Please sign in to comment.