Skip to content

feat(server): the trace-context hop — jobs.trace_context carries the click (telemetry P3) - #384

Merged
athvin merged 1 commit into
mainfrom
bm/telemetry-p1-trace-context
Nov 2, 2026
Merged

athvin merged 1 commit into
mainfrom
bm/telemetry-p1-trace-context

Conversation

@athvin

@athvin athvin commented Nov 2, 2026

Copy link
Copy Markdown
Owner

First PR of the telemetry program (plan: 24 PRs, three waves). This one is Wave 1 — measurement substrate, no new dependencies, nothing observable changes yet.

Why this goes first

jobs.trace_context / task_instances.trace_context have existed since the orchestration work, are threaded through ~20 statements, and already propagate job → task_instance (taskpg.rs:92). Every writer passed None. So the column infrastructure.md §10.2 calls "the highest-value single decision in this section" has been built and empty.

It ships before any OTel SDK exists anywhere, which is the point: the column is a string and the header is a string. Once a subscriber lands, the dispatcher starts the run's span as a child of this context and one trace spans the click → API call → queue wait → dispatch → every stage. Without it you get two unrelated traces and a run_id to join by hand.

What's here

The extractor mirrors the forwarded_actor / acceptable_actor pair beside it, including the split that makes the rule testable without a header. W3C level-1 only:

Refused Why
all-zero trace or span id the spec calls both invalid; a trace nothing can join to is worse than none
version ff forbidden by the spec
uppercase hex one context would otherwise arrive as two distinct strings
version 01+ may carry fields we'd be guessing at

A malformed value is ignored, not refused — a bad telemetry header should not fail an otherwise valid enqueue. Same reasoning forwarded_actor already documents.

It rides on Principal, already the one thing extracted from headers on every authenticated request, so all 11 enqueue sites get it in scope with no new plumbing.

pg::Provenance bundles created_by with trace_context. Not cosmetic — widening pg::enqueue to 8 arguments trips clippy's too_many_arguments, and an allow would have been the wrong answer. The two are the same fact from two angles (durable attribution a human reads; correlation handle a tool follows) and always travel off the same request. The call sites got shorter.

Deliberately no context: the S3 poll tick, the cron tick, the dispatcher's own drain, and the migrate-tenants sweep. None has a caller to inherit from, and claiming a parent they don't have is worse than rooting their own trace. A user-initiated import does link to the run it causes.

Testing

The in-memory store now matches the Postgres path, which is what makes the hop testable end to end without a database:

  • http::an_enqueued_job_carries_the_requests_trace_context — header → stored; no header → null; malformed → null, request still 202.
  • tests::a_well_formed_traceparent_is_stored_verbatim, tests::a_malformed_traceparent_is_ignored_rather_than_stored — the validator against 10 malformed shapes.

make check green, including entities-check (119 generated files current, every statement plans, 16 rules over 42 entities). No DDL change — the columns already exist, so the reconciler has nothing to do.

Note on trace ids and auth

A trace id is correlation only and never an authorization input, so the spoofability x-acting-user already documents is tolerable here for the same reason. What a forged value can do is attach a run to someone else's trace, so durable attribution stays created_by / audit_log. The doc comments say this at both sites.

🤖 Generated with Claude Code

…click (telemetry P3)

`jobs.trace_context` and `task_instances.trace_context` have existed since the
orchestration work, are threaded through ~20 statements, and already propagate
job -> task_instance (taskpg.rs:92). Every writer passed `None`, so the column
infrastructure.md §10.2 calls "the highest-value single decision in this
section" has been built and empty. This fills it.

Shippable before any OTel SDK exists anywhere, which is why it goes first: the
column is a string and the header is a string. Once a subscriber lands, the
dispatcher starts the run's span as a child of this context and one trace spans
the click, the API call, the queue wait, the dispatch and every stage. Without
it you get two unrelated traces and a run_id to join by hand.

- `forwarded_traceparent` / `acceptable_traceparent` mirror the
  `forwarded_actor` / `acceptable_actor` pair beside them, including the split
  so the rule is testable without a header. W3C level-1 only: an all-zero trace
  or span id is refused (the spec calls both invalid), version `ff` is refused
  (the spec forbids it), uppercase hex is refused so one context cannot arrive
  as two distinct strings, and a future version is refused rather than stored
  half-understood. A malformed value is ignored, not refused — a bad telemetry
  header should not fail an otherwise valid enqueue.

- The context rides on `Principal`, which is already the one thing extracted
  from headers on every authenticated request, so all 11 enqueue sites get it
  in scope with no new plumbing.

- `pg::Provenance` bundles `created_by` with `trace_context`. Not cosmetic:
  widening `pg::enqueue` to 8 arguments trips clippy's `too_many_arguments`,
  and an `allow` would have been the wrong answer — the two are the same fact
  from two angles (durable attribution a human reads, correlation handle a tool
  follows) and always travel off the same request. The call sites shrank.

- The in-memory store matches the Postgres path, which is what makes the hop
  testable end to end without a database: POST with a `traceparent` and the job
  row carries it; no header and it stays null; malformed and it stays null with
  the request still accepted.

- A user-initiated import links to the run it causes (`finish_delivery` carries
  the context), while the S3 poll tick, the cron tick, the dispatcher's own
  drain and the `migrate-tenants` sweep pass no context — none of them has a
  caller to inherit from, and claiming a parent they do not have would be worse
  than rooting their own trace.

Trace ids are correlation only and never an authorization input, so the
spoofability `x-acting-user` already documents is tolerable here for the same
reason; the doc comments say so at both sites.

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