Fix logger name misattributed to sentry_sdk.integrations.logging - #677
Closed
pgetta wants to merge 1 commit into
Closed
Fix logger name misattributed to sentry_sdk.integrations.logging#677pgetta wants to merge 1 commit into
pgetta wants to merge 1 commit into
Conversation
Sentry's default LoggingIntegration patches logging.Logger.callHandlers, inserting a sentry_sdk frame into the call stack. InterceptHandler's frame walk stopped at that frame, so loguru attributed every intercepted stdlib log (httpx, uvicorn, ...) to sentry_sdk.integrations.logging. Skip sentry_sdk frames in the walk to restore correct caller attribution.
Codecov Report✅ All modified and coverable lines are covered by tests.
Flags with carried forward coverage won't be shown. Click here to find out more.
🚀 New features to boost your workflow:
|
Contributor
Author
|
Closing in favour of #678 |
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.
Problem
Since the Sentry integration (#667), every log emitted by third-party libraries through stdlib
loggingis reported with"name":"sentry_sdk.integrations.logging"instead of the originating module:Cause
Sentry's default
LoggingIntegrationmonkeypatcheslogging.Logger.callHandlers, inserting asentry_sdkframe into the call stack between the stdlibloggingframes.InterceptHandler.emitlocates the original caller by walking frames and skipping only frames from theloggingmodule, so the walk now stops at Sentry's wrapper. Loguru then derivesrecord["name"](andfunction/line) from that frame instead of the real caller. Known issue with the loguru intercept recipe: Delgan/loguru#509.Fix
Extend the frame walk to also skip frames whose module is
sentry_sdk.*. Checked via the frame's__name__soapp/logger.pydoesn't need to importsentry_sdk, preserving the init ordering documented inapp/sentry.py.Testing
tests/test_logger.py: initializes sentry (assertingLogger.callHandlersis actually patched), routes a stdlib log throughInterceptHandler, and asserts the loguru record is attributed to the calling module/function. Fails with'sentry_sdk.integrations.logging' == 'tests.test_logger'without the fix.