Skip to content

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

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

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

Conversation

@athvin

@athvin athvin commented Nov 2, 2026

Copy link
Copy Markdown
Owner

Reopened from #408, which stopped receiving CI events after GitHub returned an Internal Server Error on a push. 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 2, 2026

Copy link
Copy Markdown
Owner Author

Replaced by a PR from a fresh branch — GitHub never delivered a pull_request event for bm/telemetry-p8-context-spine after the Internal Server Error on push. Two pushes, a close/reopen and an entirely new PR from that same ref all produced zero workflow runs, while other branches ran normally throughout. The breakage is attached to the ref, so the same commits now live on bm/telemetry-p8-spine-v2. No code difference.

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