ENG-13799: workflow metrics show how long due steps wait for a worker and how many claims find their run moved on or gone

AD
11h ago
+473-14

Nothing in reflex-workflow's telemetry covered the time between a run's step becoming due and a worker starting it, and a claim whose row had moved on (by run, deliver or cancel) or disappeared returned stale/missing from execute() before any span opened. Backlog and worker contention were invisible.

With OpenTelemetry enabled:

  • reflex.workflow.claim.delay histogram (s, seconds-scale bucket boundaries from 5 ms to an hour, matching reflex.workflow.step.duration in #8114), attribute reflex.workflow.table: now() - wake_at at claim time, one point per claimed row. The lease update's RETURNING now includes the row's wake_at and statement_timestamp(), so the delay is measured on the database clock, at the moment the claim ran rather than when its transaction began (a group claim waits on the group's advisory lock first). Claimed gains late, and the runner passes it to execute(), which sets it on the attempt span as reflex.workflow.claim.delay, so late runs can be found in traces.
  • reflex.workflow.claims counter ({claim}), attributes reflex.workflow.table and reflex.workflow.result = executed | stale | missing, recorded in execute() for every claim, including the early returns that open no span. executed: the claim was still valid and the step started; it is recorded before the attempt runs, so an attempt fenced at commit also counts as executed. stale: run, deliver or cancel moved the row on after the claim. missing: the row was deleted. Workers competing for rows do not produce stale: a claim picks with FOR UPDATE SKIP LOCKED, so contention shows only in claim.delay.
  • reflex_workflow.otel.enable() takes a meter_provider (defaults to the global one), and ReflexInstrumentor forwards its meter_provider to it, falling back to enable(tracer_provider=...) on a TypeError from an older reflex-workflow (the same shape as #8111 and #8114, so they merge in any order). Claimed also gains at, the claim's statement_timestamp(), which late is counted to.

Choices the ticket left open:

  • The delay counts from when the row became due. A row claimed to run an event it was holding has its wake_at rewritten by the claim, now to statement_timestamp() rather than now(), so its delay is exactly 0 however long the event waited. This is documented on Claimed.late and record_claim_delay. Releasing a lease or letting one expire does not move wake_at, so in this PR a re-claim after a cancelled step or a dead worker reads the time since the row first came due; the docstrings and the reflex-otel README say so, and #8119 changes it to count from the end of the replaced lease.
  • The result attribute is reflex.workflow.result, matching the key ENG-13798 (#8111) uses for its operations counter, rather than a bare result.
  • The counter is recorded in execute(), so a claim that claim() returns but the runner never starts (e.g. the worker shuts down first) is not counted.

Tests: Postgres tests in test_claim.py cover the lease update returning the delay (exactly the claim's statement_timestamp() minus the row's wake_at read back; a row claimed for a held event reads 0) and the histogram points (exact sum, min and max). test_runner.py drives execute() to ok, stale and missing and checks the counter and the span attribute (equal to taken.late). test_init.py (reflex-otel) checks the workflow metrics go through the instrumentor's meter provider, and the fallback for an older enable(). test_otel.py covers the instruments and the disabled no-op path. pytest tests/units/reflex_workflow passes with REFLEX_TEST_POSTGRES set with tests/units/reflex_otel (1279 passed, 2 skipped).

Linear: ENG-13799

šŸ¤– Generated with Claude Code