Remove duplicate query logging that breaks Django's assertNumQueries#4367
Open
federicobond wants to merge 2 commits intoopen-telemetry:mainfrom
Open
Remove duplicate query logging that breaks Django's assertNumQueries#4367federicobond wants to merge 2 commits intoopen-telemetry:mainfrom
federicobond wants to merge 2 commits intoopen-telemetry:mainfrom
Conversation
3f6ab47 to
14a2c7e
Compare
The SqlCommenter _QueryWrapper manually appends the SQL string to queries_log when a CursorDebugWrapper is active. However, CursorDebugWrapper.debug_sql already logs the query as a dict with "sql" and "time" keys. This causes two problems: 1. Queries are double-counted in queries_log 2. The raw string entries cause a TypeError in Django's assertNumQueries, which expects dict entries with a "sql" key Remove the manual append and let Django handle query logging.
14a2c7e to
5b94cb1
Compare
...tation-django/src/opentelemetry/instrumentation/django/middleware/sqlcommenter_middleware.py
Outdated
Show resolved
Hide resolved
Contributor
|
Thanks for catching this! Could unit test coverage be added here on OTel Contrib side? Maybe it could borrow from your other-project test case that does the |
Member
Author
|
@tammy-baylis-swi I tried but found it hard to come up with a meaningful test that it does NOT mess with the query log. Could be something like this, but still feels a bit artificial: connection = MagicMock()
connection.queries_log = deque()
connection.force_debug_cursor = False
with CaptureQueriesContext(connection) as ctx:
qw_instance = _QueryWrapper(requests_mock)
execute_mock = MagicMock()
context = {"cursor": MagicMock(), "connection": connection}
qw_instance(
execute_mock, "SELECT 1", MagicMock(), MagicMock(), context
)
self.assertEqual(len(ctx), 0) |
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.
The SqlCommenter _QueryWrapper manually appends the SQL string to queries_log when a CursorDebugWrapper is active. However, CursorDebugWrapper.debug_sql already logs the query as a dict with "sql" and "time" keys. This causes two problems:
Remove the manual append and let Django handle query logging.
Type of change
How Has This Been Tested?
Does This PR Require a Core Repo Change?
Checklist:
See contributing.md for styleguide, changelog guidelines, and more.