Skip to content

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

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

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 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 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, 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 keeps the 218 generated query signatures untouched in P10.

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 means a log search joins the two halves meanwhile.

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 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 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.

And 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.

So 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:

a blocking hop drops the ambient span, so every span the closure makes becomes a
disconnected root — and nothing else fails when it does.
  er-server/src/dispatcher.rs:583

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 which stage it was in and nothing about the run, job or tenant. That's precisely the attribution this programme 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, documented in the code: 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.

Verification

make check green at exit 0, first attempt. Plus a real er run-all against the base_10 fixture, with the ancestry printed above read out of the actual log.

🤖 Generated with Claude Code

bmoore813 and others added 2 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 closed this Nov 2, 2026
@athvin athvin reopened this Nov 2, 2026
@athvin

athvin commented Nov 2, 2026

Copy link
Copy Markdown
Owner Author

Superseded by a fresh PR from the same branch: GitHub returned an Internal Server Error on the push carrying the MSRV fix, and after that no pull_request synchronize event ever fired for this PR — two further pushes and a close/reopen produced no workflow run, while other branches ran normally. Reopening as a new PR to get a clean opened event. No code difference.

@athvin athvin closed this Nov 2, 2026
athvin added a commit that referenced this pull request Nov 3, 2026
…metry P8) (#416)

* feat(obs): the ambient context spine — job, run and stage spans (telemetry 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.

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.

`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.

`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.

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]>

* fix(obs): er-obs cannot override tokio's default-features on the MSRV

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]>

---------

Co-authored-by: bmoore813 <[email protected]>
Co-authored-by: Claude Opus 5 (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