Skip to content

Commit 970353a

Browse files
committed
Remove duplicate query logging that breaks Django's assertNumQueries
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.
1 parent a912524 commit 970353a

File tree

2 files changed

+2
-5
lines changed

2 files changed

+2
-5
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
3535

3636
### Fixed
3737

38+
- `opentelemetry-instrumentation-django`: Remove duplicate query logging in SQLCommenter middleware that broke Django's `assertNumQueries`
39+
([#4367](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/4367))
3840
- `opentelemetry-docker-tests`: Replace deprecated `SpanAttributes` from `opentelemetry.semconv.trace` with `opentelemetry.semconv._incubating.attributes`
3941
([#4339](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/4339))
4042
- `opentelemetry-instrumentation-confluent-kafka`: Skip `recv` span creation when `poll()` returns no message or `consume()` returns an empty list, avoiding empty spans on idle polls

instrumentation/opentelemetry-instrumentation-django/src/opentelemetry/instrumentation/django/middleware/sqlcommenter_middleware.py

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
# pylint: disable=no-name-in-module
1919
from django import conf, get_version
2020
from django.db import connections
21-
from django.db.backends.utils import CursorDebugWrapper
2221

2322
from opentelemetry.instrumentation.sqlcommenter_utils import _add_sql_comment
2423
from opentelemetry.instrumentation.utils import _get_opentelemetry_values
@@ -122,8 +121,4 @@ def __call__(self, execute: Type[T], sql, params, many, context) -> T:
122121
# * https://github.com/basecamp/marginalia/issues/61
123122
# * https://github.com/basecamp/marginalia/pull/80
124123

125-
# Add the query to the query log if debugging.
126-
if isinstance(context["cursor"], CursorDebugWrapper):
127-
context["connection"].queries_log.append(sql)
128-
129124
return execute(sql, params, many, context)

0 commit comments

Comments
 (0)