Skip to content

feat(trace): Span RAII guard for in-process attribution - #2539

Merged
max-sixty merged 1 commit into
mainfrom
json-lazy
May 3, 2026
Merged

max-sixty merged 1 commit into
mainfrom
json-lazy

Conversation

@max-sixty

Copy link
Copy Markdown
Owner

Adds in-process spans to the existing `[wt-trace]` infrastructure so per-phase wall-time is attributable, not just subprocess time. Until now subprocess records (`cmd="git …"`) accounted for child-process work; everything else — config load, repo open, template render, fork+exec wait — was opaque. The new `Span` RAII guard fills that gap.

What this adds

  • `worktrunk::trace::Span` — construct with `let _span = Span::new("name");` at the top of a block; the span fires on drop, recorded as `[wt-trace] span="name" dur_us=…`. `log_enabled!` is checked at drop time (not construction) so spans bracketing logger init still emit correctly.
  • Parser + chrome-trace renderer extended for the new `span="…"` records. Spans render as `ph: "X"` Complete events with `cat: "wt"`, distinct from the existing `git`/`network` categories.
  • Spans wired through alias dispatch: `try_alias`, `run_alias`, the config-load phases, `build_hook_context` plus per-var sub-spans (`var_default_branch`, `var_commit`, `var_remote`, `var_primary_worktree`), `template_render`, `execute_shell_command`.

Pre-init subprocess visibility

Reorders `main.rs` so `init_logging(verbose)` (which registers env_logger) runs before `init_command_log` (which spawns `Repository::current()` → `git rev-parse --git-common-dir`). With the previous order that subprocess fired before any logger was registered, leaving a 4ms hole in the trace where the work actually ran. Same reason `init_logging` reorders internally so `env_logger.init()` runs before `log_files::init()`.

How to use

```bash
RUST_LOG=debug wt myalias 2>trace.log
wt-perf trace trace.log > trace.json

Open trace.json in https://ui.perfetto.dev

```

Spans nest as parent/child stacks in Perfetto. Subprocess records (`cat=git`) interleave with span records (`cat=wt`) so cold-cache attribution down to ~µs is straightforward.

Notable trade-off

`execute_shell_command` is spanned externally because `Cmd::stream()` doesn't currently emit a per-subprocess `[wt-trace] cmd=` record (only `Cmd::run()` does). The external span captures the wall-time correctly; replacing it with native `Cmd::stream` tracing is a follow-up.

🤖 Generated with Claude Code

Adds a `Span` guard alongside the existing `[wt-trace]` command/instant
records. Construct with `let _span = Span::new("name");` at the top of a
block; the span fires when it goes out of scope, attributing the elapsed
time as a Complete event with category `wt`. The `log_enabled!` check
runs at drop time so spans constructed before `init_logging` still emit
correctly when their drop runs after the logger is up.

Reorders main.rs so `init_logging(verbose)` registers env_logger before
`init_command_log` runs. The latter spawns
`Repository::current()` → `git rev-parse --git-common-dir`; with the
previous order that subprocess fired before any logger was registered,
leaving a 4ms hole in the trace. Same reason `init_logging` itself
reorders so env_logger.init() runs before log_files::init().

Spans wired through alias dispatch:
- main.rs: `init_logging`, `init_command_log`
- repository/mod.rs: `resolve_git_common_dir`
- alias.rs: `try_alias`, `user_config_load`, `project_config_load`,
  `load_aliases`, `referenced_vars`, `parse_alias_args`, `run_alias`,
  `alias_approve`, `build_hook_context`, `prepare_steps`
- command_executor.rs: per-var spans inside `build_hook_context`
  (`var_default_branch`, `var_primary_worktree`, `var_commit`,
  `var_remote`); `template_render`; `execute_shell_command`
  (Cmd::stream doesn't currently emit a `[wt-trace] cmd=` record, so we
  span the call externally for now)

Parser + chrome-trace renderer extended for the new `span="..."` records.

Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
@max-sixty
max-sixty merged commit e47761d into main May 3, 2026
29 checks passed
@max-sixty
max-sixty deleted the json-lazy branch May 3, 2026 01:33
max-sixty added a commit that referenced this pull request May 3, 2026
Three small consolidations in the trace-trigger surface, following up on
#2539.

## What this changes

**1. `Cmd::stream()` emits `[wt-trace] cmd=...` natively.** Previously
only `Cmd::run()` and `Cmd::pipe_into()` emitted per-subprocess records
— `stream()` was a hole, which #2539 patched with a
`Span::new(\"execute_shell_command\")` wrapper at the foreground
hook/alias call site. The two surrogates aren't equivalent: spans render
under `cat: \"wt\"`, subprocess records under `cat:
\"git\"`/`\"network\"`, and spans don't carry the `ok` flag, so
hook/alias child status was invisible in chrome traces. Stream now emits
a record at every exit point (spawn fail, stdin write, wait fail,
signal-derived exit, SIGPIPE-as-success, non-zero status, success) via a
small `WtTraceLog` helper that mirrors `ExternalCommandLog`'s shape.

**2. Drops the `Span::new(\"execute_shell_command\")` workaround** in
`commands/command_executor.rs`. With `Cmd::stream()` emitting natively,
the wrapper is redundant — foreground hook/alias step time is now
captured by the canonical subprocess record (cat=`git`/`network`/none)
instead of a generic span (cat=`wt`).

**3. Re-exports `trace::instant` from `trace::mod`** alongside `Span`.
Deletes the `shell_exec::trace_instant` shim (a one-line re-export of
`trace::emit::instant`) and migrates all 14 callers in
`commands/picker/mod.rs` and `commands/list/collect/mod.rs` to
`worktrunk::trace::instant`. Symmetric public API: `trace::Span` for
scopes, `trace::instant` for milestones — neither lives under
`shell_exec` anymore, since neither has anything to do with shell
execution.

## Verification

Smoke-tested with `RUST_LOG=debug wt <alias>`: `cmd=\"echo
hello-from-stream\" ok=true` and `cmd=\"exit 7\" ok=false` both fire
correctly. `Span(\"execute_shell_command\")` no longer appears in the
trace. Full pre-merge hook (3430 tests + clippy + lints) passes locally.

---------

Co-authored-by: Claude Opus 4.7 (1M context) <[email protected]>
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