Skip to content

instrumentation/logging: deprecate camelCase log record attrs, add OTel-spec snake_case equivalents#4758

Open
rajdhruvsingh wants to merge 4 commits into
open-telemetry:mainfrom
rajdhruvsingh:fix/4643-deprecate-camelcase-log-attrs
Open

instrumentation/logging: deprecate camelCase log record attrs, add OTel-spec snake_case equivalents#4758
rajdhruvsingh wants to merge 4 commits into
open-telemetry:mainfrom
rajdhruvsingh:fix/4643-deprecate-camelcase-log-attrs

Conversation

@rajdhruvsingh

Copy link
Copy Markdown
Contributor

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_FORMAT is updated to match.

Per the OTel spec for trace context in non-OTLP log formats, trace_id, span_id, and trace_flags are the field names that should appear in legacy/JSON log formats. The old camelCase names didn't surface as a problem with text formatters (since DEFAULT_LOGGING_FORMAT already 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 on logging.LogRecord for the duration of instrumentation. Reading an old name still works but emits a DeprecationWarning; 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.

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • This change requires a documentation update

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 in test_logging.py still 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 produces trace_id/span_id/trace_flags/service_name as top-level JSON keys.
    • test_legacy_attribute_names_still_work_but_warn — reading an old camelCase name returns the correct value and emits exactly one DeprecationWarning referencing both old and new names.
    • test_legacy_attribute_write_forwards_to_new_name — writing the old name (e.g. inside a custom log_hook) correctly forwards to the new attribute.
    • test_uninstrument_removes_deprecated_attribute_proxies — after uninstrument(), the proxy is fully removed from logging.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 check and ruff format pass 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?

  • Yes. - Link to PR:
  • No.

Checklist:

See contributing.md for styleguide, changelog guidelines, and more.

  • Followed the style guidelines of this project
  • Changelogs have been updated
  • Unit tests have been added
  • Documentation has been updated

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

Labels

None yet

Projects

Status: No status

Development

Successfully merging this pull request may close these issues.

opentelemetry-instrumentation-logging JSON Formatter compatibility with spec

1 participant