ENG-13797: workflow workers report step durations, running steps and time until the next run

AD
12h ago
+973-28

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:

MetricTypeUnitAttributes
reflex.workflow.step.durationhistogram, buckets extended to 3600 sstable, step, lane, outcome, error.type
reflex.workflow.step.activeup-down counter{step}table, lane
reflex.workflow.next_dueobservable gaugesnone
  • step.duration: one point per executed attempt. otel.measuring(table, step, lane) wraps the run_attempt call in execute, so run_attempt keeps main's shape and complexity. The point carries the outcome run_attempt returns (ok, retry, failed, fenced), or cancelled/failed for an attempt that raised: one cancelled on shutdown, or one whose give-up raised. error.type is 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 same measuring block in execute once 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 a disable() mid-step can't unbalance the count.
  • next_due: each worker's until_something_is_due publishes the wait next_due measured 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 two run_workflows in 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() takes meter_provider. ReflexInstrumentor forwards it, and if enable() raises TypeError it 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.drain force-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_DELAY for 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 an InMemoryMetricReader (exact attributes, buckets, overflow past an hour), raising attempts measured as cancelled/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 (retry with ConnectionError, then ok). Also: cancelled and failed-give-up attempts measured, the latter under the driver's exact error type; a schedule failure measured; next_due reported 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; an enable() that refuses meter_provider is 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