Repository navigation
feat(log): add trace_id/span_id to JSON log records inside a span - #7668
Open
gaurav0107 wants to merge 2 commits into
Open
gaurav0107 wants to merge 2 commits into
gaurav0107 wants to merge 2 commits into
Conversation
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
Contributor
[Low impact] The PR appears safe to merge; the previous console-output mismatch is fixed. SummaryAdds
Reviews (2) · Last reviewed commit: "Add trace ids to console output in JSON ..." · Reviewed by Greptile |
Merging this PR will not alter performance1 warning
Comparing |
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
marked this pull request as ready for review
October 27, 2026 06:41
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
All Submissions:
Type of change
Description
Closes #6903.
When OpenTelemetry tracing is enabled,
JsonHandler.emit(reflex_base/utils/log.py) now adds the current span's W3Ctrace_id(32 hex) andspan_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, orNonewhen tracing is off or no valid span is current. It uses thetracemodule thatenable()binds, so it has no import statement, and with tracing off nothing fromopentelemetryis imported (test_disabled_path_imports_no_opentelemetrystill passes). I added it to the "trace points carry no import statement" test as well, since it runs once per log record.JsonHandler.emitmerges those fields into the payload. Records emitted outside a span, or with tracing off, are unchanged.reflex_otelwas already inPACKAGE_LOGGER_NAMESon main, so this PR doesn't touch that part of the issue.docs/api-reference/observability.md.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:
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 withKeyError: '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 --covpasses except 3 tests that also fail on main in my environment (two dataeditor tests neednode, 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.