ENG-13801: Reflex's own log records, including workflow engine errors, reach an OpenTelemetry logs pipeline under the reflex CLI
What
Under the reflex CLI, no record from a reflex.* or reflex_workflow.* logger reached an OTLP logs pipeline. That included [Reflex Backend Exception] and the engine's commit-failure records, while the app's own loggers next to them got through. reflex_base.utils.log.configure() sets propagate=False on the reflex logger in every CLI-managed process, so the handler the SDK or opentelemetry-instrumentation-logging puts on the root logger never sees those records. Also, OTEL_LOGS_EXPORTER=otlp on its own configured nothing.
Change
-
New
reflex_otel/logs.py:ReflexLogHandler, alogging.Handlerbuilt on theopentelemetry._logsAPI. Each record goes out on an OTel logger named after its stdlib logger, in the current context, so it carries the current span's trace and span ids. It sets:- severity text and number, mapped the way the SDK maps them (Reflex's
SUCCESS= INFO4); code.file.path,code.function.name,code.line.number;exception.type,exception.message,exception.stacktrace;- Reflex's own extras renamed to
reflex.log.kind,reflex.log.locationandreflex.deprecation.*.
It drops the rendering-only extras (
rich,rich_kwargs,progress,end,dedupe,dedupe_key) and any value that is not a valid attribute, such as a RichProgress, which used to trigger the SDK's recursive-logging warning. Arichmessage has its markup stripped from the body. - severity text and number, mapped the way the SDK maps them (Reflex's
-
ReflexInstrumentor.instrument()attaches that handler tologging.getLogger("reflex")when a logs pipeline is configured (logger_provider=orOTEL_LOGS_EXPORTERnaming an exporter), The handler emits only while thereflexlogger does not propagate, as under the CLI. Outside it the logger propagates and a root handler already sees each record, so the bridge would send it twice. The check runs per record, not at attach time, becauseopentelemetry-instrument reflex runinstruments before the CLI turns managed logging on. Extras never overwrite the bridge's owncode.*andexception.*attributes.uninstrument()removes the handler. -
OTEL_LOGS_EXPORTERjoins the variables that makeinstrument()configure the SDK. AnOTEL_*_EXPORTERset tonone(or empty) no longer counts as asking for an exporter, which is how the SDK reads it too. -
opentelemetry-apifloor raised from 1.30 to 1.37 for reflex-otel. 1.35 is the first release whereLogRecordtakescontext=andevent_name=, but SDK 1.35/1.36 (which pin the API exactly) pass the APILogRecordto exporters unconverted, soConsoleLogExporterand the OTLP encoder raise and the whole batch is lost; 1.37 converts it. Inuv.lockonly that specifier line changed. The min-deps check is a pyright delta and does not run the exporter, so the floor is recorded in apyproject.tomlcomment. -
The export level is independent of the terminal. The bridge exports
INFOand above by default;OTEL_PYTHON_REFLEX_LOG_LEVELorinstrument(log_level=...)sets another.reflex_base.utils.log.configure()now sets thereflexlogger to the lowest level any of its handlers sets (the CLI level,DEBUGwith full logging, and any handler's own level), while the terminal sink keeps filtering at the CLI level, as it already had a level of its own. A bridge attached after the CLI configured logging lowers the logger itself. So--loglevel warningstill exportsINFO,OTEL_PYTHON_REFLEX_LOG_LEVEL=DEBUGexportsDEBUGwithout--loglevel debug, and neither changes what the terminal prints. It deduplicatesdedupe/dedupe_keyrecords like the console and JSON sinks, with its ownDedupeFilterinstance: the shared one marks a record seen as the console handler passes it, so it would drop the first occurrence here. The level check runs before the dedupe filter so a record dropped for its level never claims its key. -
The bridge exports only under the reflex CLI (managed mode,
reflexlogger not propagating). While silent it hands a record no other handler sees tologging.lastResort, since a handler on the hierarchy otherwise suppresses Python's last-resort stderr output; for that the handler's own level is capped at WARNING (what lastResort prints) and a higher export level is a filter. -
instrument()given a tracer or meter provider but nologger_providerdoes not configure the SDK, soOTEL_LOGS_EXPORTERalone sets up no logs. When the global logger provider is still the API's proxy at that point, it now logs a warning saying so and how to fix it. The bridge still attaches, because the proxy forwards to a provider set later. -
instrument()registers anatexithook that detaches the bridge. It is registered after the SDK's providers, so it runs before their shutdown:reflex run --env prodno longer printsProcessor is already shutdown, ignoring callwhen the CLI logsReflex app stopped.at exit. -
Exception text is exported verbatim, as on spans. The docs say so, name SQLAlchemy's
hide_parameters=Truefor the bound parameters a failed write quotes, andOTEL_LOGRECORD_ATTRIBUTE_VALUE_LENGTH_LIMITfor length. Truncating in the bridge would not help: the parameters are near the start of the message. -
Docs (
docs/api-reference/observability.md, the reflex-otel README) cover:- how to enable the bridge;
- the root logger's default WARNING level for app loggers;
- that an OTel-only root handler stops the app's own terminal output, while Reflex's terminal output is unaffected.
Design choices the ticket left open
- When "a logs pipeline is configured". The handler attaches only when
logger_provider=is passed orOTEL_LOGS_EXPORTERnames an exporter. It does not attach just because some SDKLoggerProvideris installed: the SDK configurator installs one even with no log exporters, and every Reflex record would then be translated into an empty pipeline. - Export level independent of
--loglevel. The handler sets the level it needs and the logger passes the lowest one. A level set on a package logger itself (reflex_workflow, say) still applies to both the terminal and the export. - Attribute names for Reflex's extras:
reflex.log.kind,reflex.log.location,reflex.deprecation.feature_name,.version,.removal_version. Any other extra passes through under its own name if it is a valid attribute value.
Tests
tests/units/reflex_otel/test_logs.py, in a CLI-configured process (managed mode,log.configure()): records fromreflex.stateandreflex_workflow.engine.executereach anInMemoryLogRecordExporterwith the current span's ids, an exact attribute set and severity, and stop once uninstrumented. Also covered: the exception attributes, no handler without a logs pipeline (OTEL_LOGS_EXPORTERunset, empty ornone), a handler attached before managed logging that stays silent until the CLI cuts propagation, and the severity mapping.- Levels:
INFOexported at--loglevel warningand under full logging; aDEBUGexport level lowers thereflexlogger, which stays lowered acrossset_log_level, with nothing extra printed (exact terminal output);test_a_handler_below_the_cli_level_gets_its_recordsintests/units/reflex_base/utils/test_log.py. - The missing-provider warning (exact message), and the exit hook: once it runs, a record logged after the provider shut down is not handed to it, and
uninstrument()unregisters it. tests/units/reflex_otel/test_init.py, in a fresh interpreter:log.enable_managed_logging()plusOTEL_LOGS_EXPORTER=consolealone configures the SDK, and records fromreflex.*andreflex_workflow.*reach the global logger provider.- Locally:
tests/units/reflex_workflow,tests/units/reflex_otelandtests/units/reflex_base/utils/test_log.pyagainst Postgres 16: 1388 passed, 2 skipped; the fulltests/unitsran on #8117 with this merged in;uv run pre-commit run --all-filesclean;scripts/check_min_deps.py reflex-otelPASS.
ENG-13802 (one structured log event per workflow attempt) is stacked on this branch.
Linear: ENG-13801
š¤ Generated with Claude Code