Repository navigation
feat(workflow): trace step attempts with OpenTelemetry - #7635
Conversation
reflex-workflow had no OpenTelemetry code: a run started from a traced event handler produced steps invisible to the trace, anything a step traced itself started an unrelated trace, and every step task inherited whatever context was current when run_workflows was entered, so a worker started inside a span parented every later step under that long-ended span. Add reflex_workflow.otel on the reflex_base.otel pattern: an `enabled` flag the engine checks at each trace point, with the opentelemetry imports confined to enable(), so the package keeps its SQLAlchemy-only dependency list and imports nothing from opentelemetry until instrumented. ReflexInstrumentor enables and disables it alongside the framework trace points when reflex-workflow is importable. Each step attempt gets a CONSUMER span named `<table>.<step>`, started from an explicit empty parent context so it is a trace root whatever the worker inherited. It links to the span that scheduled it rather than nesting under it, since a step may run days after wake_in, on another worker after a lease expiry, or as one of many fanned-out children. Call.encode() stamps the current W3C traceparent into the payload under a reserved key of next_args, next to the claim's event key, so start, run, deliver, chaining, schedules and fan-out all carry it without a schema change; execute() strips it before the step sees its arguments, and a retry re-stamps the failed attempt's span so retries chain. Attributes are the table, step, attempt number, lane and outcome, plus error.type and the recorded exception on a failed attempt. Arguments and row columns are never recorded. Co-Authored-By: Claude Fable 5.1 <[email protected]> Claude-Session: https://claude.ai/code/session_01SjgvPv7AqSycFLeXwSfB6D
There was a problem hiding this comment.
All reported issues were addressed across 11 files
Reply with feedback, questions, or to request a fix.
View guided diff | Turn on auto-fix | Re-trigger cubic
[Medium impact] The PR appears safe to merge; no new actionable issues remain. SummaryAdds OpenTelemetry spans for workflow step attempts.
No new actionable issues were found. The previous unnumbered finding is fixed: Reviews (3) · Last reviewed commit: "refactor(otel): tidy workflow trace-poin..." · Reviewed by Greptile |
Co-Authored-By: Claude Fable 5.1 <[email protected]> Claude-Session: https://claude.ai/code/session_01SjgvPv7AqSycFLeXwSfB6D
wake_on_schedule turns the exception into the row's last_error, so the span ended as failed with no exception or error.type on it. Co-Authored-By: Claude Fable 5.1 <[email protected]> Claude-Session: https://claude.ai/code/session_01SjgvPv7AqSycFLeXwSfB6D
Co-Authored-By: Claude Fable 5.1 <[email protected]> Claude-Session: https://claude.ai/code/session_01SjgvPv7AqSycFLeXwSfB6D
Merging this PR will not alter performance1 warning
Comparing |
Merge the doubled TYPE_CHECKING block in reflex_workflow.otel, drop the stale/missing outcomes from record_outcome's docs (execute returns them before the span opens), make run_attempt's arguments keyword-only, and toggle the workflow trace points through one helper so _instrument keeps its complexity.
* docs: add a Workflows section for reflex-workflow Thirteen pages under docs/workflows, grouped in the Backend sidebar: an overview, a tutorial, guides to defining workflows, steps, runs, events, schedules, fan-out, concurrency, workers and inspecting runs, an explanation of how the engine works and what it guarantees, and an API reference. Also fixes the package README's wake route, which used an app.api attribute Reflex does not have. * docs: qualify lease-bound claims and complete example imports * docs: retryable webhook response, precise cancel status, and what history records * docs: acknowledge webhooks only on known delivery, guard the cancel status, describe history as committed attempts * reflex-workflow: answer a message whose wake was refused while a held one ran A run holds one event at a time. When the conversation example's turn was running a held wake, a wake for a message that arrived after the turn read the transcript was refused, and nothing read that message until the four-hour reminder. That is what timed out test_a_burst_after_a_restart_is_answered_in_order in CI: a message landing while the previous reply step was still committing is held, and the next one can land in that gap. reply now reads the transcript again under the arrival lock before it waits, as remind already does, and takes another turn for anything new. A message that lands after that read finds the run still stepping, so its wake is held. The regression test drives the race deterministically: it fails on a thirty-second timeout before this change and passes in under a second after. * reflex-workflow: arm every conversation wait in one step that rereads first Checking for unread messages in reply was not enough. When reply found one it took another turn with the earlier wake still held, and the turn that finally ran that wake found nothing new and armed the wait directly: a message whose wake was refused while it ran then waited for the reminder. Every wait is now armed by settle, a step of its own that rereads the transcript under the arrival lock. reply and a turn with nothing new both go through it. The regression test now also drives a message into the turn that runs the stale wake, and fails without this change. The reminder race test waited on the status, which is set as soon as a wait is armed; it now waits until no wake is held and no step is running, since a held wake replaying through settle takes the lock the test holds. * docs: give Workflows its own top-level sidebar section Moved out of the State section, between State and Recipes as masenf suggested, with the guides grouped into Building and Running Workflows. Single pages are one-child groups, as elsewhere in the sidebar: the index that opens the active group counts top-level entries that way. * docs: keep inline code out of link text, which the docs site does not render * Revert "docs: keep inline code out of link text, which the docs site does not render" This reverts commit efcdd2e. * Name this PR's news fragments by its number * docs: wake also waits for the steps it started to finish Matches #7385. * Revert "reflex-workflow: arm every conversation wait in one step that rereads first" This reverts commit 47588c9. * Revert "reflex-workflow: answer a message whose wake was refused while a held one ran" This reverts commit 16cf37e. * docs(workflows): bring the Workflows docs up to date with main - Stop registering the workflow base with rx.ModelRegistry.register. Since #7797 a registered base turns off detection of the app's other DeclarativeBase bases, so reflex db makemigrations would write a revision dropping their tables. Explain detection, when registering is needed, and point MappedAsDataclass bases (the database docs' default) at reflex_workflow.dataclass. Same fix in the package README. - Describe the held event being released once a worker takes it (#7396). - Document OpenTelemetry tracing of step attempts (#7635). - Require Python 3.11 (#7449), spell Cron's default zone as UTC, say wake waits for the work to run (#7382), and import rx in the wake example. Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_01ESN8DiahHo8P4FLFNr6fHK * docs(workflows): say which bases Reflex detects, and why registering drops others Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_01ESN8DiahHo8P4FLFNr6fHK * docs(reflex-workflow): rewrap the README's Reflex setup paragraph Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_01ESN8DiahHo8P4FLFNr6fHK --------- Co-authored-by: Masen Furer <[email protected]> Co-authored-by: Claude <[email protected]>
Requested by Masen · project thread
First PR for ENG-13483 (plan items A, B, C).
Type of change
What and why
reflex-workflow had no OpenTelemetry code. Checked on main with the SDK, an in-memory exporter and
ReflexInstrumentor().instrument():run_workflowswas entered (asyncio.create_taskcopies contextvars for the runner loop and each step), so a worker entered inside a span parented every later step under that long-ended span.This PR:
reflex_workflow.otel, on thereflex_base.otelpattern: anenabledflag the engine checks at each trace point,opentelemetryimports confined toenable(). reflex-workflow keeps its SQLAlchemy-only dependency list and imports nothing fromopentelemetryuntil instrumented (a test pins that).ReflexInstrumentorenables and disables the workflow trace points alongside the framework's whenreflex_workflow.otelis importable, so oneinstrument()call, oropentelemetry-instrument, covers both.CONSUMERspan per step attempt, named<table>.<step>, started from an explicit empty parent context so it is a trace root whatever the worker inherited. It links to the span that scheduled it rather than nesting under it: a step may run days afterwake_in, on another worker after a lease expiry, or as one of many fanned-out children. Attributes:reflex.workflow.table,.step,.attempt,.lane,.outcome;error.typeand the recorded exception on a failed attempt (statusERRORforretry/failed). Step arguments and row columns are never recorded.Call.encode()stamps the current W3Ctraceparentinto the payload under a reserved top-level key ofnext_args, next to the claim's existingevent_key, sostart,run,deliver, chaining, schedules andfan_outall carry it.execute()strips it before the step sees its arguments; a retry re-stamps the failed attempt's span, so retries chain attempt → attempt.Not in this PR (later items on the ticket): suppressing the engine's bookkeeping queries from DB instrumentations, and worker metrics.
Tests
tests/units/reflex_workflow/test_otel.py(no Postgres): disabled path imports noopentelemetryand changes nothing;stamp/encodecarry the current span; the step span is a root even when entered under another span, with the link, kind and attributes; unusabletraceparentvalues yield no link; failed attempts record the exception and status.test_runner.py::test_steps_are_traced_as_linked_root_spans(Postgres): a chain started under a handler span linksfirst→ handler andsecond→first; a flaky step's three attempts areretry,retry,ok, each linked to the previous attempt;next_argsis clean afterwards.tests/units/reflex_otel/test_init.py: the instrumentor toggles the workflow trace points.Ran locally: full
tests/units/reflex_workflowagainst Postgres 16 plustests/units/reflex_otelandtests/units/reflex_base/test_otel.py(1122 passed),ruff check/format,pyrighton the touched packages and tests, codespell.Generated by Claude Code