Skip to content

feat(obs): the ambient context spine — job, run and stage spans (telemetry P8) - #414

Closed
athvin wants to merge 3 commits into
mainfrom
bm/telemetry-p8-spine-v2
Closed

athvin wants to merge 3 commits into
mainfrom
bm/telemetry-p8-spine-v2

Conversation

@athvin

@athvin athvin commented Nov 2, 2026

Copy link
Copy Markdown
Owner

Third attempt at a PR for these commits. GitHub stopped delivering pull_request events for bm/telemetry-p8-context-spine after returning an Internal Server Error on a push — two further pushes, a close/reopen and a brand-new PR from that ref all produced zero workflow runs while other branches ran normally. Same commits, fresh ref. No code difference.

The piece everything else in the programme hangs off. Verified against a real er run-all:

process (service.name=er-cli) → run (run_id, mode, model_version) → stage (ingest, 356ms, ok)

A statement span nests one level deeper, so every SQL record carries its stage, its run and its service without any of them being passed as an argument. That is the point of ambient context — a generated query function three layers down is attributed to its tenant without taking one.

What I dropped from the plan, and why

The plan said to add org_id to PipelineInputs. That was wrong: 21 struct-literal construction sites edited to thread an identifier the dispatcher already has.

Instead dispatch_claimed — the single funnel every claimed job passes through — enters a job span with org, kind and attempt, and the run and stage spans are created inside it. Org is inherited, not threaded. The same reasoning keeps the 245 generated query signatures untouched in #411.

The job span also records jobs.trace_context, which #384 started populating. Making it a true child of that remote context needs the OTel propagator and so arrives with the exporter; recording it as a field joins the two halves meanwhile.

StageObserver finally carries timing

observed() now measures the stage's wall clock. The trait never has — despite duration_ms being one of the eleven promoted counters — so the ledger has been taking timing from SQL now() at each boundary. Measuring it once means the span and the ledger agree rather than approximately agree.

The blocking-hop gate, and why it isn't a convention

tokio::task::spawn_blocking moves its closure to another thread, and tracing's current span is thread-local, so the bare form silently drops the ambient span. Because DuckDB is synchronous the entire pipeline runs inside those closures — one missed hop orphans every stage span and every statement span beneath it from the job that caused them.

Nothing catches it. No test fails, no type is wrong, every span is still produced — they're parentless. It doesn't read as a bug, it reads as tracing not working.

All 9 hops move to er_obs::spawn_blocking_in_span (same signature, same JoinHandle, same panic-as-JoinError contract), and er-schema/tests/span_boundaries.rs bans the bare form in the shape freeform_sql.rs established.

The gate was observed failing before being trusted — reintroducing one bare site named er-server/src/dispatcher.rs:583 exactly. It also asserts the wrapper is still used ≥9 times, because proving the bare form absent means nothing if the hops quietly disappeared.

with_span_list, turned on

Found while verifying, not while writing: with the span list off a record carried only its innermost span — so a statement-level event would have reported its stage and nothing about the run, job or tenant. That's the attribution this programme exists for.

It costs bytes, and the lever against that is the filter — a cheap line nobody can attribute is not actually cheap. One documented limitation: a span filtered out by target drops from the ancestry, so a narrow RUST_LOG=er_cli=info loses the process span's service identity.

Also

er-obs cannot override tokio's default-features on the MSRV. The cargo-deny job pins RUSTUP_TOOLCHAIN=1.85.0 deliberately, and on 1.85 an inherited workspace dependency may not override default-features — it fails to parse the manifest at all, which is why that job died in 21s without reaching a check. Newer local toolchains accept it silently. Fixed to tokio.workspace = true, the house pattern, and re-verified green under that exact toolchain.

make check green at exit 0.

🤖 Generated with Claude Code

bmoore813 and others added 3 commits November 2, 2026 14:49
…metry P8)

The piece everything else in the programme hangs off. Verified against a real
`er run-all`, the ancestry is now:

  process (service.name=er-cli) -> run (run_id, mode, model_version)
    -> stage (ingest, 356ms, ok)

A statement span nests one level deeper, so when the emitters land every SQL
record will carry its stage, its run and its service **without any of them
being passed as an argument**. That is the whole point: `tracing` context is
ambient, so a generated query function three layers down is attributed to its
tenant without taking one.

## What this replaces from the plan, and why

The plan said to add `org_id` to `PipelineInputs`. That was wrong, and doing it
would have defeated the design: 21 struct-literal construction sites edited to
thread an identifier the dispatcher already has. Instead `dispatch_claimed` —
the single funnel every claimed job passes through — enters a `job` span with
org, kind and attempt, and the run and stage spans are created inside it. Org
is inherited, not threaded. The same reasoning is what will keep the 218
generated query signatures untouched in the emitter change.

The `job` span also records `jobs.trace_context`, which P3 started populating.
Making this span a true CHILD of that remote context needs the OTel context
propagator and so arrives with the exporter; recording it as a field means a
log search joins the two halves in the meantime.

## StageObserver finally carries timing

`observed()` now measures the stage's own wall clock. The trait never has —
despite `duration_ms` being one of the eleven promoted counters — so the ledger
has been taking its timing from SQL `now()` at each boundary. Measuring it in
one place means the span and the ledger agree rather than approximately agree.

## The blocking-hop gate, and why it is not a convention

`tokio::task::spawn_blocking` moves its closure to another thread, and
`tracing`'s current span is thread-local, so the bare form silently drops the
ambient span. Because DuckDB is synchronous the ENTIRE pipeline runs inside
those closures: one missed hop orphans every stage span and every statement
span beneath it from the job that caused them.

Nothing else catches it. No test fails, no type is wrong, every span is still
produced — they are parentless. It does not read as a bug, it reads as tracing
not working. So all 9 hops in er-server move to
`er_obs::spawn_blocking_in_span` (same signature, same JoinHandle, same
panic-as-JoinError contract) and `er-schema/tests/span_boundaries.rs` bans the
bare form, in the shape `freeform_sql.rs` established.

The gate was observed FAILING before being trusted: reintroducing one bare site
named `er-server/src/dispatcher.rs:583` exactly. It also asserts the wrapper is
still used at least 9 times, because proving the bare form absent means nothing
if the hops quietly disappeared.

## with_span_list, turned on

Found while verifying: with the span list off, a record carried only its
innermost span — so a statement-level event would have said which STAGE it was
in and nothing about the run, job or tenant. That is the attribution the whole
exercise exists for. Every record is now self-describing. It costs bytes, and
the lever against that is the filter, because a cheap line nobody can attribute
is not actually cheap.

One honest limitation: a span filtered out by target drops from the ancestry
too, so a narrow `RUST_LOG=er_cli=info` loses the service identity the process
span carries. The default filter enables it, and Fluent Bit attaches pod
identity independently.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
cargo-deny failed on #408 while passing locally. The CI job pins
RUSTUP_TOOLCHAIN=1.85.0 deliberately — the workflow comment says it is there
"to keep the check evaluating the lockfile" — and on 1.85 an inherited
workspace dependency may not override default-features:

  error inheriting `tokio` from workspace root manifest's
  workspace.dependencies.tokio
  `default-features = false` cannot override workspace's `default-features`

A newer local toolchain accepts it, which is exactly the gap the pin exists to
close. Reproduced verbatim with RUSTUP_TOOLCHAIN=1.85.0 and now green under it:
advisories ok, bans ok, licenses ok, sources ok.

The override was unnecessary anyway. The workspace entry already carries
rt-multi-thread and macros, which is everything `spawn_blocking` and
`#[tokio::test]` need, so `tokio.workspace = true` is both correct and the
house pattern (er-k8s and er-server both inherit and add features, neither
overrides defaults). The dev-dependency went with it as redundant.

Zero lock change; er-obs's 12 tests still pass.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
@athvin

athvin commented Nov 3, 2026

Copy link
Copy Markdown
Owner Author

Superseded by #416, which merged this same work as a71f3ce0 — git log -S confirms both the run/stage spans in er-cli/src/lib.rs and the job span in dispatcher.rs came from that commit, and the tokio.workspace = true MSRV fix is on main too.

This branch is the pre-#416 attempt that the GitHub push incident stranded: it went DIRTY/CONFLICTING and therefore ran zero workflows, so it has sat 19 commits behind reporting "no checks reported" rather than anything actionable. Closing rather than rebasing — a rebase would resolve conflicts against content that is already upstream.

Worth recording because it nearly cost real time: grepping er-cli/src/lib.rs for er_span::/stage_span returns 0 on main and reads as "P8 never landed". The spans are tracing::info_span!("stage", …), so the token to search for is the span name or a comment, not a helper that was never used.

@athvin athvin closed this Nov 3, 2026
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