ENG-13799: workflow metrics show how long due steps wait for a worker and how many claims find their run moved on or gone
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.delayhistogram (s, seconds-scale bucket boundaries from 5 ms to an hour, matchingreflex.workflow.step.durationin #8114), attributereflex.workflow.table:now() - wake_atat claim time, one point per claimed row. The lease update'sRETURNINGnow includes the row'swake_atandstatement_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).Claimedgainslate, and the runner passes it toexecute(), which sets it on the attempt span asreflex.workflow.claim.delay, so late runs can be found in traces.reflex.workflow.claimscounter ({claim}), attributesreflex.workflow.tableandreflex.workflow.result=executed|stale|missing, recorded inexecute()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 asexecuted.stale:run,deliverorcancelmoved the row on after the claim.missing: the row was deleted. Workers competing for rows do not producestale: a claim picks withFOR UPDATE SKIP LOCKED, so contention shows only inclaim.delay.reflex_workflow.otel.enable()takes ameter_provider(defaults to the global one), andReflexInstrumentorforwards itsmeter_providerto it, falling back toenable(tracer_provider=...)on aTypeErrorfrom an older reflex-workflow (the same shape as #8111 and #8114, so they merge in any order).Claimedalso gainsat, the claim'sstatement_timestamp(), whichlateis 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_atrewritten by the claim, now tostatement_timestamp()rather thannow(), so its delay is exactly 0 however long the event waited. This is documented onClaimed.lateandrecord_claim_delay. Releasing a lease or letting one expire does not movewake_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 bareresult. - The counter is recorded in
execute(), so a claim thatclaim()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