Skip to content

fix(logging): restore handlers and formatters on uninstrument - #4982

Draft
sidsri14 wants to merge 1 commit into
open-telemetry:mainfrom
sidsri14:fix/logging-uninstrument-restore-format
Draft

fix(logging): restore handlers and formatters on uninstrument#4982
sidsri14 wants to merge 1 commit into
open-telemetry:mainfrom
sidsri14:fix/logging-uninstrument-restore-format

Conversation

@sidsri14

Copy link
Copy Markdown

Description

Fixes #4949.

When LoggingInstrumentor().instrument(set_logging_format=True) is called, logging.basicConfig() configures or attaches root handlers with a format string interpolating OpenTelemetry trace context fields (%(otelTraceID)s, %(otelSpanID)s, etc.).

During _uninstrument(), the log record factory was restored to _old_factory, but the format string remained active on the root logger's handlers. Because newly created LogRecord instances no longer carried otelTraceID, any record logged after uninstrument() failed to format, causing silent log dropping and ValueError: Formatting field not found in record: 'otelTraceID' via Handler.handleError.

Changes

  1. In _instrument(): When set_logging_format is active, capture the pre-instrumentation root logger state (level, handlers, and existing formatters).
  2. In _uninstrument(): Restore the root logger level, restore previous formatters on pre-existing handlers, and remove any handlers added during instrumentation.
  3. In tests/test_logging.py: Added unit tests verifying:
    • Logging after uninstrument() succeeds without KeyError / ValueError on formatting.
    • Pre-existing handler formatters are accurately restored after an instrument/uninstrument cycle.

Type of change

  • 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 pytest instrumentation/opentelemetry-instrumentation-logging/tests: all 73 tests passed.
  • Added regression tests covering both freshly configured and pre-configured logging handlers across instrument/uninstrument cycles.

Checklist:

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

When LoggingInstrumentor.instrument(set_logging_format=True) is called, basicConfig adds or configures root handlers with a format string interpolating %(otelTraceID)s, %(otelSpanID)s, etc.

During _uninstrument, the log record factory is restored to the old factory, so subsequent log records no longer contain otelTraceID.
However, because the format string remained in place on root handlers, any record logged after uninstrument failed to format with a KeyError/ValueError.

This captures the root level, handlers, and formatters before basicConfig and restores them upon _uninstrument.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

LoggingInstrumentor: uninstrument() leaves the basicConfig format string asking for otel fields, so every later record fails to format

1 participant