wasmtime component bindgen: when tracing is enabled, emit an event for args & results (#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
This commit is contained in:
Pat Hickey
2023-04-13 15:58:53 -07:00
committed by GitHub
parent 9425a252bb
commit cc1c14ac91

View File

@@ -1112,11 +1112,12 @@ impl<'a> InterfaceGenerator<'a> {
} }
if self.gen.opts.tracing { if self.gen.opts.tracing {
self.src.push_str(&format!( uwrite!(
self.src,
" "
let span = tracing::span!( let span = tracing::span!(
tracing::Level::TRACE, tracing::Level::TRACE,
\"wit-bindgen guest import\", \"wit-bindgen import\",
module = \"{}\", module = \"{}\",
function = \"{}\", function = \"{}\",
); );
@@ -1131,7 +1132,22 @@ impl<'a> InterfaceGenerator<'a> {
TypeOwner::None => "<no owner>", TypeOwner::None => "<no owner>",
}, },
func.name, 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"); self.src.push_str("let host = get(caller.data_mut());\n");
@@ -1146,6 +1162,13 @@ impl<'a> InterfaceGenerator<'a> {
uwrite!(self.src, ");\n"); 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 if self
.special_case_trappable_error(owner, &func.results) .special_case_trappable_error(owner, &func.results)
.is_some() .is_some()
@@ -1272,7 +1295,7 @@ impl<'a> InterfaceGenerator<'a> {
" "
let span = tracing::span!( let span = tracing::span!(
tracing::Level::TRACE, tracing::Level::TRACE,
\"wit-bindgen guest export\", \"wit-bindgen export\",
module = \"{}\", module = \"{}\",
function = \"{}\", function = \"{}\",
); );