From cc1c14ac913be13329e5cfc91c33e053ab722132 Mon Sep 17 00:00:00 2001 From: Pat Hickey Date: Thu, 13 Apr 2023 15:58:53 -0700 Subject: [PATCH] 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 --- crates/wit-bindgen/src/lib.rs | 31 +++++++++++++++++++++++++++---- 1 file changed, 27 insertions(+), 4 deletions(-) diff --git a/crates/wit-bindgen/src/lib.rs b/crates/wit-bindgen/src/lib.rs index f564a5a956..5af48778c1 100644 --- a/crates/wit-bindgen/src/lib.rs +++ b/crates/wit-bindgen/src/lib.rs @@ -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 = \"{}\", ); @@ -1131,7 +1132,22 @@ impl<'a> InterfaceGenerator<'a> { TypeOwner::None => "", }, 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::>(); + 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"); @@ -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() @@ -1272,7 +1295,7 @@ impl<'a> InterfaceGenerator<'a> { " let span = tracing::span!( tracing::Level::TRACE, - \"wit-bindgen guest export\", + \"wit-bindgen export\", module = \"{}\", function = \"{}\", );