Skip to content

Remove duplicate query logging that breaks Django's assertNumQueries#4367

Merged
lzchen merged 3 commits into
open-telemetry:mainfrom
federicobond:fix/remove-redundant-query-log
May 19, 2026
Merged

Remove duplicate query logging that breaks Django's assertNumQueries#4367
lzchen merged 3 commits into
open-telemetry:mainfrom
federicobond:fix/remove-redundant-query-log

Conversation

@federicobond
Copy link
Copy Markdown
Member

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.

Type of change

  • Bug fix (non-breaking change which fixes an issue)

How Has This Been Tested?

  • Tested against a Django project that was failing a test case:
         with self.assertNumQueries(14):
              ^^^^^^^^^^^^^^^^^^^^^^^^^
     E   TypeError: string indices must be integers, not 'str'

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

@federicobond federicobond force-pushed the fix/remove-redundant-query-log branch from 3f6ab47 to 14a2c7e Compare March 27, 2026 03:01
@federicobond federicobond requested a review from a team as a code owner March 27, 2026 03:01
@federicobond federicobond force-pushed the fix/remove-redundant-query-log branch from 14a2c7e to 5b94cb1 Compare March 27, 2026 03:06
@tammy-baylis-swi tammy-baylis-swi moved this to Ready for review in Python PR digest Mar 27, 2026
@tammy-baylis-swi
Copy link
Copy Markdown
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 self.assertNumQueries(14).

@federicobond
Copy link
Copy Markdown
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)

@tammy-baylis-swi
Copy link
Copy Markdown
Contributor

@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:

Agree that it's quite mock-y @federicobond , which reminds me that we've been wanting to add proper db client testing for a while but haven't gotten to it with all else going on: #3032

Maybe a quick unit test is better than nothing for now? Would be good to get other Approvers' thoughts.

@tammy-baylis-swi tammy-baylis-swi requested a review from a team March 30, 2026 16:51
@federicobond
Copy link
Copy Markdown
Member Author

I'm not sure it's worth it to add a test here to be honest. There was never a test for the original query log behavior to begin with.

@federicobond federicobond force-pushed the fix/remove-redundant-query-log branch from b9af128 to 52e0fd4 Compare April 13, 2026 23:21
@github-actions
Copy link
Copy Markdown

This PR has been automatically marked as stale because it has not had any activity for 14 days. It will be closed if no further activity occurs within 14 days of this comment.
If you're still working on this, please add a comment or push new commits.

@github-actions github-actions Bot added the Stale label Apr 28, 2026
@federicobond
Copy link
Copy Markdown
Member Author

This issue is still relevant. Pull request is just waiting for a second approval

@github-actions github-actions Bot removed the Stale label Apr 29, 2026
@tammy-baylis-swi tammy-baylis-swi moved this from Ready for review to Approved PRs in Python PR digest May 8, 2026
@tammy-baylis-swi
Copy link
Copy Markdown
Contributor

Maintainers, please have a look.

@emdneto
Copy link
Copy Markdown
Member

emdneto commented May 12, 2026

Thanks for the PR!

Just a heads-up: we no longer update CHANGELOG.md directly. The changelog is now generated from changelog fragments using Towncrier.

Please add the appropriate changelog fragment for this change instead of editing CHANGELOG.md manually. You can find the instructions and expected format in CONTRIBUTING.md.

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.
@federicobond federicobond force-pushed the fix/remove-redundant-query-log branch from 52e0fd4 to e1d3647 Compare May 13, 2026 02:33
@federicobond
Copy link
Copy Markdown
Member Author

Done, let me know if I've correctly added the changelog entry.

@lzchen lzchen added this pull request to the merge queue May 19, 2026
Merged via the queue into open-telemetry:main with commit f918859 May 19, 2026
1500 of 1502 checks passed
@github-project-automation github-project-automation Bot moved this from Approved PRs to Done in Python PR digest May 19, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

4 participants