ENG-13796: workflow traces can name the run, parent run and event key of each step attempt (opt-in)

AD
11h ago
+843-34

Stacked on #8113 (ENG-13795). It extends the README "Telemetry" section that PR renames. Review only the commits above #8113; this retargets to main once #8113 lands.

What a trace consumer gets

An attempt span carried no run identity, so two attempts of the same step on different rows looked identical, and a run's history could not be assembled from its spans. Links only reach backwards, and they break at every wait, because a delivered event links to the request that delivered it. Behind one opt-in, an attempt span now records:

  • reflex.workflow.run: the primary key as a JSON array in column order, even for a single column: the text of AttemptLog.run's jsonb (via json_pk, so UUIDs and other non-JSON keys match), so run = '<attribute>'::jsonb joins a span to its attempts.
  • reflex.workflow.parent.table and reflex.workflow.parent.run on a fanned-out child.
  • reflex.workflow.event.key when the attempt runs a keyed event.

Opt in with ReflexInstrumentor().instrument(workflow_identifiers=True) or OTEL_PYTHON_REFLEX_WORKFLOW_IDENTIFIERS=true, which is forwarded to reflex_workflow.otel.enable(identifiers=True). It is off by default, so the reflex-otel README's promise that row columns never leave the process holds unless someone asks. Both READMEs document it as "turn on when your keys are surrogate ids".

Choices the ticket left open

  • Default off, as the ticket proposes. The proposal had left this open.
  • Event key on both delivery paths. The ticket points at taken, but a claim only sets that when it takes an event held for a wait that wasn't armed yet. For held events the worker reads the key from that claim, whoever delivered the event. An event delivered to a run that is already waiting is applied directly and never passes through taken. For that path deliver stamps the key into the step's stored arguments under delivered_key, the same way traceparent already travels (otel.stamp_event). It is stamped only while the delivering process has tracing and identifiers on, so an app that never opts in stores nothing beyond what it stored before. The cost: a direct-applied event delivered from a process with identifiers off has no event.key on the worker's span. The README says to turn identifiers on in every process that delivers events. Deriving the key on the worker from recent_event_keys was rejected. That list's newest entry is still there for every later step of the run, so it would attach an event's key to steps the event never ran. Steps are called with only the stored args and kwargs, so no step ever sees the key. Dedupe semantics are unchanged. The key stays in the stored arguments for the step's retries.
  • A key JSON cannot hold leaves out reflex.workflow.run rather than raising while the span opens. Such a key already fails where the engine records it. Any other error in _add_identifiers is logged by the same guard as the other recorders, and the step still runs.
  • Older reflex-workflow releases: reflex-otel calls enable(..., identifiers=...) and, on TypeError, calls it again without the argument, warning if identifiers were asked for.
  • disable() also turns identifiers off, so a later enable() without the flag does not inherit it.

Tests

  • tests/units/reflex_workflow/test_otel.py: off by default even when keys are passed; on for one column, several columns (with a UUID), a child, a keyed event, and a key JSON cannot hold. Each compares the full attribute set. Also covers stamp_event writing the key only with identifiers on, identifiers that cannot be read (a raising key encoder, a parent without a key) never failing the span, and disable() clearing the flag.
  • tests/units/reflex_otel/test_init.py: the keyword and the env var (true, TRUE, 1, unset, and keyword-over-env) reach reflex_workflow.otel, and uninstrument() clears it. With a reflex-workflow whose enable takes no identifiers, tracing still turns on, and the warning appears only when identifiers were asked for.
  • tests/units/reflex_workflow/engine/test_runner.py (Postgres): an event applied to a waiting run and an event held for a wait both put their key on the decide span, and the step still never sees the key. For {untraced, traced, identifying} deliverer Ɨ {held, applied}: the key is stored only by an identifying deliverer, and a held event's key is always on the worker's span. A resend is still refused. A retried event step names its event on every attempt. A fan-out's children record their parent's table and key, and the parent records its own run.
  • uv run pytest tests/units/reflex_workflow tests/units/reflex_otel against Postgres 16, with the session timezone UTC and again with America/Los_Angeles: 1333 passed, 2 skipped (UTC, after the review changes). pre-commit passes on the changed files.

Linear: ENG-13796

šŸ¤– Generated with Claude Code