ENG-13797: workflow workers report step durations, running steps and time until the next run
Ā
AD
12h ago+973+973+012345678901234567890123456789-28-28-01234567890123456789
reflex-workflow emitted no metrics, so "how long do steps take", "how many are running" and "when does this process next have work" had no aggregated answer (spans are sampled and batched). With reflex-otel instrumented, the engine now records on a reflex_workflow meter:
| Metric | Type | Unit | Attributes |
|---|---|---|---|
reflex.workflow.step.duration | histogram, buckets extended to 3600 s | s | table, step, lane, outcome, error.type |
reflex.workflow.step.active | up-down counter | {step} | table, lane |
reflex.workflow.next_due | observable gauge | s | none |
- step.duration: one point per executed attempt.
otel.measuring(table, step, lane)wraps therun_attemptcall inexecute, sorun_attemptkeeps main's shape and complexity. The point carries the outcomerun_attemptreturns (ok,retry,failed,fenced), orcancelled/failedfor an attempt that raised: one cancelled on shutdown, or one whose give-up raised.error.typeis the exception that failed the attempt (the step's, its schedule's, or the refused commit's), or failing that, what escaped. - step.active (an active count, as in
http.server.active_requests): held up by the samemeasuringblock inexecuteonce the row is loaded. The ticket suggested_claim_from/_finished, but the step (and so its lane) isn't known at claim time; counting where the step actually runs gives the right lane and also covers cancellation. The instruments captured at entry are the ones used at exit, so adisable()mid-step can't unbalance the count. - next_due: each worker's
until_something_is_duepublishes the waitnext_duemeasured on the database clock. It is kept per worker as a deadline on the process's monotonic clock, so app/database clock skew can't leak in. The callback reports seconds until the soonest worker's deadline, clamped at 0, and nothing when no worker has an answer. A worker that drains, or can't reach the database, clears only its own entry, so tworun_workflowsin one process don't overwrite each other. A run the worker itself is running doesn't count as one it could take. reflex_workflow.otel.enable()takesmeter_provider.ReflexInstrumentorforwards it, and ifenable()raisesTypeErrorit calls again without it, so an older reflex-workflow still works. This is the same shape #8111/#8112 use, so merge order doesn't matter.Runner.drainforce-flushes the tracer and meter providers, each on its own thread with one 5 s deadline, so the last attempts leave before the process exits. A provider whose flush raises or hangs is logged and skipped.- README documents the metrics and
OTEL_BSP_MAX_QUEUE_SIZE/OTEL_BSP_SCHEDULE_DELAYfor busy workers.
Instruments are created in otel._create_instruments(meter), so sibling metrics (claim delay, claims counter) can be added next to these on the same meter.
Attribute keys reuse the span attribute constants (reflex.workflow.table, etc.) so spans and metrics share names.
Tests
tests/units/reflex_workflow/test_otel.py: points per instrument on anInMemoryMetricReader(exact attributes, buckets, overflow past an hour), raising attempts measured ascancelled/failed, the active count balanced through exceptions and a disable, next_due clamped, absent, and the soonest across workers with each clearing its own, both providers flushed, the disabled path a no-op.tests/units/reflex_workflow/engine/test_runner.py(Postgres): a real step counted as running while it runs and back to 0 after, one histogram point per attempt (retrywithConnectionError, thenok). Also: cancelled and failed-give-up attempts measured, the latter under the driver's exact error type; a schedule failure measured;next_duereported from what the worker learned, and cleared by this worker's drain only.tests/units/reflex_otel/test_init.py: instrumentor forwards the meter provider; anenable()that refusesmeter_provideris called again without it.
uv run pytest tests/units/reflex_workflow tests/units/reflex_otel with REFLEX_TEST_POSTGRES: 1294 passed, 2 skipped. Pre-commit clean on the changed files.
Linear: ENG-13797
š¤ Generated with Claude Code