fix(logging): restore handlers and formatters on uninstrument - #4982
Draft
sidsri14 wants to merge 1 commit into
Draft
fix(logging): restore handlers and formatters on uninstrument#4982sidsri14 wants to merge 1 commit into
sidsri14 wants to merge 1 commit into
Conversation
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.
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
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 createdLogRecordinstances no longer carriedotelTraceID, any record logged afteruninstrument()failed to format, causing silent log dropping andValueError: Formatting field not found in record: 'otelTraceID'viaHandler.handleError.Changes
_instrument(): Whenset_logging_formatis active, capture the pre-instrumentation root logger state (level,handlers, and existingformatters)._uninstrument(): Restore the root logger level, restore previous formatters on pre-existing handlers, and remove any handlers added during instrumentation.tests/test_logging.py: Added unit tests verifying:uninstrument()succeeds withoutKeyError/ValueErroron formatting.Type of change
How Has This Been Tested?
pytest instrumentation/opentelemetry-instrumentation-logging/tests: all 73 tests passed.Checklist: