Skip to content

[WIP] new: Support structured tracing logs.#876

Open
milesj wants to merge 5 commits into
extism:mainfrom
milesj:struct-logs
Open

[WIP] new: Support structured tracing logs.#876
milesj wants to merge 5 commits into
extism:mainfrom
milesj:struct-logs

Conversation

@milesj

@milesj milesj commented Jul 22, 2025

Copy link
Copy Markdown
Contributor

Sibling PR to extism/rust-pdk#81

Extism has logging/tracing support currently through explicit macro usage, but what it doesn't support is inheriting tracing logs that come from 3rd-party crates. Right now all those messages are lost, which is a huge bummer.

I wanted to figure it out if inheriting these 3rd-party logs is even possible... and it is! Turns out you can set tracing::set_global_default in WASM to read these logs, and then pipe them to extism. Here's an example I've been experimenting with: https://github.com/moonrepo/proto/blob/master/crates/warpgate-pdk/src/tracing.rs#L98

While this works, there is a major downside, and that is that field information is lost. Extism only supports the message string, so to pipe field data through, you need to include it in the message. This results in ugly logs, as seen here (notice the extism::pdk lines):

Screenshot 2025-07-16 at 3 00 27 PM Screenshot 2025-07-16 at 2 42 41 PM

Solution

So in this PR, I wanted to figure out a way to support this field data, and a custom target (to override extism::pdk). I'm doing this by introducing a new struct, TracingLog, that includes this information. I've stored this struct in extism-convert so that it can also be used in the PDK (there will be a sibling PR to this one).

I then use the tracing-dynamic crate (https://github.com/BrynCooke/tracing-dynamic) to send the event, because it's currently impossible to set dynamic fields on an event (why tracing why???).

@milesj milesj requested a review from zshipko as a code owner July 22, 2025 18:26
Comment thread runtime/Cargo.toml
http = ["ureq"] # enables extism_http_request
wasmtime-default-features = [
'wasmtime/default',
default = [

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

my editor did this

@zshipko zshipko left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is neat! it looks like a great improvement. i will test it out a little more at some point today, but i don't see why not to move forward with this since it won't immediately require PDK changes beyond what you've done in the rust-pdk

Comment thread runtime/src/pdk.rs
Ok(buf) => {
use tracing_dynamic::EventFactory;

let log = if buf.starts_with('{') {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice, so this makes it backward compatible? i think that was my only real concern about a change like this

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ya it should support a message string, and the event as a json object

@milesj

milesj commented Jul 24, 2025

Copy link
Copy Markdown
Contributor Author

@zshipko I haven't tested this yet because I can't compile the project:

   Compiling extism-sys v0.0.0+replaced-by-ci (/Users/miles/Projects/extism/extism-maturin)
error: failed to run custom build command for `extism-sys v0.0.0+replaced-by-ci (/Users/miles/Projects/extism/extism-maturin)`

Caused by:
  process didn't exit successfully: `/Users/miles/.cargo/shared-target/debug/build/extism-sys-5924b66f727e8df4/build-script-build` (exit status: 101)
  --- stdout
  cargo:rerun-if-changed=src/extism.c
  cargo:rerun-if-changed=../runtime/extism.h
  OPT_LEVEL = Some(0)
  OUT_DIR = Some(/Users/miles/.cargo/shared-target/debug/build/extism-sys-c4948024f0f7da27/out)
  TARGET = Some(aarch64-apple-darwin)
  HOST = Some(aarch64-apple-darwin)
  cargo:rerun-if-env-changed=CC_aarch64-apple-darwin
  CC_aarch64-apple-darwin = None
  cargo:rerun-if-env-changed=CC_aarch64_apple_darwin
  CC_aarch64_apple_darwin = None
  cargo:rerun-if-env-changed=HOST_CC
  HOST_CC = None
  cargo:rerun-if-env-changed=CC
  CC = None
  cargo:rerun-if-env-changed=CC_ENABLE_DEBUG_OUTPUT
  RUSTC_WRAPPER = None
  cargo:rerun-if-env-changed=CRATE_CC_NO_DEFAULTS
  CRATE_CC_NO_DEFAULTS = None
  DEBUG = Some(true)
  cargo:rerun-if-env-changed=MACOSX_DEPLOYMENT_TARGET
  MACOSX_DEPLOYMENT_TARGET = None
  cargo:rerun-if-env-changed=CFLAGS
  CFLAGS = None
  cargo:rerun-if-env-changed=HOST_CFLAGS
  HOST_CFLAGS = None
  cargo:rerun-if-env-changed=CFLAGS_aarch64_apple_darwin
  CFLAGS_aarch64_apple_darwin = None
  cargo:rerun-if-env-changed=CFLAGS_aarch64-apple-darwin
  CFLAGS_aarch64-apple-darwin = None
  CARGO_ENCODED_RUSTFLAGS = Some()

  --- stderr

  thread 'main' panicked at extism-maturin/build.rs:56:48:
  called `Result::unwrap()` on an `Err` value: Os { code: 2, kind: NotFound, message: "No such file or directory" }
  note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
warning: build failed, waiting for other jobs to finish...

@zshipko

zshipko commented Jul 24, 2025

Copy link
Copy Markdown
Contributor

@milesj - I haven't seen that before, is this using make from the root of the repo?

It looks like something related to the maturin bindings, you should be able to at least build/test the runtime if you run cargo build/cargo test from the runtime/ directory.

@milesj

milesj commented Jul 24, 2025

Copy link
Copy Markdown
Contributor Author

This is just cargo check --workspace from the root.

@milesj

milesj commented Aug 2, 2025

Copy link
Copy Markdown
Contributor Author

@zshipko any suggestions on how I could test this?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants