instrumentation/logging: deprecate camelCase log record attrs, add OTel-spec snake_case equivalents#4758
Open
rajdhruvsingh wants to merge 4 commits into
Conversation
…el-spec snake_case equivalents (open-telemetry#4643)
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.
Description
Renames the camelCase log record attributes injected by
opentelemetry-instrumentation-logging(otelSpanID,otelTraceID,otelTraceSampled,otelServiceName) to the OTel-spec-compliant snake_case equivalents (span_id,trace_id,trace_flags,service_name).DEFAULT_LOGGING_FORMATis updated to match.Per the OTel spec for trace context in non-OTLP log formats,
trace_id,span_id, andtrace_flagsare the field names that should appear in legacy/JSON log formats. The old camelCase names didn't surface as a problem with text formatters (sinceDEFAULT_LOGGING_FORMATalready mapped fields explicitly), but JSON formatters (e.g. python-json-logger) dump record attributes verbatim, so the wrong field names leaked straight into JSON log output.To avoid a breaking change, the old camelCase names are kept working via a deprecation proxy (new
_deprecated.py) installed onlogging.LogRecordfor the duration of instrumentation. Reading an old name still works but emits aDeprecationWarning; writing an old name forwards to the new one. The proxy is installed in_instrument()and removed in_uninstrument()so it doesn't leak onto the stdlib class outside the instrumentation's lifecycle.Fixes #4643
Type of change
Please delete options that are not relevant.
How Has This Been Tested?
Ran the full existing test suite (
tests/test_logging.py,tests/test_handler.py,tests/test_logging_handler_recursion.py) unmodified — all 25 pre-existing assertions intest_logging.pystill pass as-is, proving the old attribute names remain functional.Added 5 new tests in
test_logging.py:test_spec_compliant_attribute_names_are_injected— new snake_case names are set correctly with the right values.test_json_formatter_uses_spec_compliant_field_names— a minimal JSON formatter (mirroring python-json-logger's behavior) now producestrace_id/span_id/trace_flags/service_nameas top-level JSON keys.test_legacy_attribute_names_still_work_but_warn— reading an old camelCase name returns the correct value and emits exactly oneDeprecationWarningreferencing both old and new names.test_legacy_attribute_write_forwards_to_new_name— writing the old name (e.g. inside a customlog_hook) correctly forwards to the new attribute.test_uninstrument_removes_deprecated_attribute_proxies— afteruninstrument(), the proxy is fully removed fromlogging.LogRecord, so it doesn't leak into unrelated code.Full suite:
python3 -m pytest instrumentation/opentelemetry-instrumentation-logging/tests/ -v→ 70 passed, 0 failed.ruff checkandruff formatpass clean on all changed/new files.Test A:
python3 -m pytest instrumentation/opentelemetry-instrumentation-logging/tests/(70 passed)Does This PR Require a Core Repo Change?
Checklist:
See contributing.md for styleguide, changelog guidelines, and more.