Skip to content

feat(log): add trace_id/span_id to JSON log records inside a span - #7668

Open
gaurav0107 wants to merge 2 commits into
reflex-dev:mainfrom
gaurav0107:fix/6903-otel-log-correlation-trace-id-span-id-on
Open

gaurav0107 wants to merge 2 commits into
reflex-dev:mainfrom
gaurav0107:fix/6903-otel-log-correlation-trace-id-span-id-on

Conversation

@gaurav0107

Copy link
Copy Markdown
Contributor

All Submissions:

  • Have you followed the guidelines stated in CONTRIBUTING.md file?
  • Have you checked to ensure there aren't any other open Pull Requests for the desired changed?

Type of change

  • New feature (non-breaking change which adds functionality)
  • This change requires a documentation update (included)

Description

Closes #6903.

When OpenTelemetry tracing is enabled, JsonHandler.emit (reflex_base/utils/log.py) now adds the current span's W3C trace_id (32 hex) and span_id (16 hex) to each JSON log record, so a log line can be looked up in the tracing UI and vice versa.

  • reflex_base.otel.trace_fields() (new) returns {"trace_id", "span_id"} for the current valid span, or None when tracing is off or no valid span is current. It uses the trace module that enable() binds, so it has no import statement, and with tracing off nothing from opentelemetry is imported (test_disabled_path_imports_no_opentelemetry still passes). I added it to the "trace points carry no import statement" test as well, since it runs once per log record.
  • JsonHandler.emit merges those fields into the payload. Records emitted outside a span, or with tracing off, are unchanged.
  • reflex_otel was already in PACKAGE_LOGGER_NAMES on main, so this PR doesn't touch that part of the issue.
  • Docs: added a "Logs" section to docs/api-reference/observability.md.
  • News fragment: packages/reflex-base/news/+json-log-trace-ids.feature.md.

I left the optional rich console/file formatter change out to keep this PR small.

Changes To Core Features:

  • Have you added an explanation of what your changes do and why you'd like us to include them?
  • Have you written new tests for your core changes, as applicable?
  • Have you successfully ran tests with your changes locally?

Tests:

  • tests/units/reflex_base/utils/test_log.py: test_json_mode_adds_trace_ids (info and error records inside a span carry exactly that span's ids; this fails on main with KeyError: 'trace_id'), test_json_mode_omits_trace_ids_outside_span, test_json_mode_omits_trace_ids_when_tracing_disabled.
  • tests/units/reflex_base/test_otel.py: trace_fields() when disabled, when enabled with no current span, and inside a span.

Local results: uv run pytest tests/units --cov passes except 3 tests that also fail on main in my environment (two dataeditor tests need node, and one bench HMR test). ruff check, ruff format --check, and the pre-commit hooks (pyright, ty, codespell) pass on the changed files.


🤖 Opened with Superhuman, an open-source contribution agent.

When OpenTelemetry tracing is enabled, JsonHandler now adds the current
span's W3C trace_id (32 hex) and span_id (16 hex) to each record, so a
log line can be looked up in the tracing backend and vice versa.

The ids come from a new reflex_base.otel.trace_fields() helper, which
returns None while tracing is off or no valid span is current. It uses
the API module bound by enable(), so the disabled path still imports
nothing from opentelemetry.

Closes reflex-dev#6903
@greptile-apps

greptile-apps Bot commented Oct 27, 2026 •

Copy link
Copy Markdown
Contributor

RetriggerView in GreptileConfidence Score: 5/5

[Low impact] The PR appears safe to merge; the previous console-output mismatch is fixed.

Summary

Adds trace_id and span_id to JSON output inside a valid OpenTelemetry span.

  • Reflex JSON output now carries the current span’s IDs.

Reviews (2) · Last reviewed commit: "Add trace ids to console output in JSON ..." · Reviewed by Greptile

Comment thread docs/api-reference/observability.md Outdated
@codspeed

codspeed Bot commented Oct 27, 2026 •

Copy link
Copy Markdown

Merging this PR will not alter performance

1 warning

⚠️ 12 benchmarks are dominated by system calls

System calls cannot be simulated consistently and are left out of the measure. The reported value understates the real cost. Switch to the Walltime instrument to measure them.

Learn more about system calls

See affected benchmarks

✅ 148 untouched benchmarks


Comparing gaurav0107:fix/6903-otel-log-correlation-trace-id-span-id-on (a20f3cc) with main (9c64f1e)

Open in CodSpeed

console.print() in JSON mode writes through emit_json_print(), which
bypasses JsonHandler, so its records lacked trace_id/span_id. Merge the
ids in _write_json(), the one writer both paths share.
@gaurav0107
gaurav0107 marked this pull request as ready for review October 27, 2026 06:41
@gaurav0107
gaurav0107 requested review from a team and Alek99 as code owners October 27, 2026 06:41

@cubic-dev-ai cubic-dev-ai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No issues found across 6 files

View guided diff | Re-trigger cubic

@masenf masenf added the documentation Improvements or additions to documentation label Oct 27, 2026

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

documentation Improvements or additions to documentation

Projects

None yet

Development

Successfully merging this pull request may close these issues.

OTel: log correlation — trace_id/span_id on JSON log records

2 participants