ENG-13801: Reflex's own log records, including workflow engine errors, reach an OpenTelemetry logs pipeline under the reflex CLI

AD
10h ago
+1196-33

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, a logging.Handler built on the opentelemetry._logs API. 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.location and reflex.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 Rich Progress, which used to trigger the SDK's recursive-logging warning. A rich message has its markup stripped from the body.

  • ReflexInstrumentor.instrument() attaches that handler to logging.getLogger("reflex") when a logs pipeline is configured (logger_provider= or OTEL_LOGS_EXPORTER naming an exporter), The handler emits only while the reflex logger 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, because opentelemetry-instrument reflex run instruments before the CLI turns managed logging on. Extras never overwrite the bridge's own code.* and exception.* attributes. uninstrument() removes the handler.

  • OTEL_LOGS_EXPORTER joins the variables that make instrument() configure the SDK. An OTEL_*_EXPORTER set to none (or empty) no longer counts as asking for an exporter, which is how the SDK reads it too.

  • opentelemetry-api floor raised from 1.30 to 1.37 for reflex-otel. 1.35 is the first release where LogRecord takes context= and event_name=, but SDK 1.35/1.36 (which pin the API exactly) pass the API LogRecord to exporters unconverted, so ConsoleLogExporter and the OTLP encoder raise and the whole batch is lost; 1.37 converts it. In uv.lock only that specifier line changed. The min-deps check is a pyright delta and does not run the exporter, so the floor is recorded in a pyproject.toml comment.

  • The export level is independent of the terminal. The bridge exports INFO and above by default; OTEL_PYTHON_REFLEX_LOG_LEVEL or instrument(log_level=...) sets another. reflex_base.utils.log.configure() now sets the reflex logger to the lowest level any of its handlers sets (the CLI level, DEBUG with 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 warning still exports INFO, OTEL_PYTHON_REFLEX_LOG_LEVEL=DEBUG exports DEBUG without --loglevel debug, and neither changes what the terminal prints. It deduplicates dedupe/dedupe_key records like the console and JSON sinks, with its own DedupeFilter instance: 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, reflex logger not propagating). While silent it hands a record no other handler sees to logging.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 no logger_provider does not configure the SDK, so OTEL_LOGS_EXPORTER alone 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 an atexit hook that detaches the bridge. It is registered after the SDK's providers, so it runs before their shutdown: reflex run --env prod no longer prints Processor is already shutdown, ignoring call when the CLI logs Reflex app stopped. at exit.

  • Exception text is exported verbatim, as on spans. The docs say so, name SQLAlchemy's hide_parameters=True for the bound parameters a failed write quotes, and OTEL_LOGRECORD_ATTRIBUTE_VALUE_LENGTH_LIMIT for 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 or OTEL_LOGS_EXPORTER names an exporter. It does not attach just because some SDK LoggerProvider is 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 from reflex.state and reflex_workflow.engine.execute reach an InMemoryLogRecordExporter with 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_EXPORTER unset, empty or none), a handler attached before managed logging that stays silent until the CLI cuts propagation, and the severity mapping.
  • Levels: INFO exported at --loglevel warning and under full logging; a DEBUG export level lowers the reflex logger, which stays lowered across set_log_level, with nothing extra printed (exact terminal output); test_a_handler_below_the_cli_level_gets_its_records in tests/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() plus OTEL_LOGS_EXPORTER=console alone configures the SDK, and records from reflex.* and reflex_workflow.* reach the global logger provider.
  • Locally: tests/units/reflex_workflow, tests/units/reflex_otel and tests/units/reflex_base/utils/test_log.py against Postgres 16: 1388 passed, 2 skipped; the full tests/units ran on #8117 with this merged in; uv run pre-commit run --all-files clean; scripts/check_min_deps.py reflex-otel PASS.

ENG-13802 (one structured log event per workflow attempt) is stacked on this branch.

Linear: ENG-13801

šŸ¤– Generated with Claude Code