8000 fix(django/sql): Run cursor.mogrify as fallback to regular formatting… · etherscan-io/sentry-python@46f8527 · GitHub
[go: up one dir, main page]

Skip to content

Commit 46f8527

Browse files
authored
fix(django/sql): Run cursor.mogrify as fallback to regular formatting (getsentry#325)
* fix(django/sql): Run cursor.mogrify as fallback to regular formatting * fix: Fix imports
1 parent f5cd47f commit 46f8527

File tree

2 files changed

+32
-15
lines changed

2 files changed

+32
-15
lines changed

sentry_sdk/integrations/django/__init__.py

Lines changed: 30 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -322,21 +322,38 @@ def record_sql(sql, params, cursor=None):
322322
if hub.get_integration(DjangoIntegration) is None:
323323
return
324324

325-
with capture_internal_exceptions():
326-
if cursor and hasattr(cursor, "mogrify"): # psycopg2
327-
real_sql = cursor.mogrify(sql, params)
328-
with capture_internal_exceptions():
325+
real_sql = None
326+
real_params = None
327+
328+
try:
329+
# Prefer our own SQL formatting logic because it's the only one that
330+
# has proper value trimming.
331+
real_sql, real_params = format_sql(sql, params)
332+
if real_sql:
333+
real_sql = format_and_strip(real_sql, real_params)
334+
except Exception:
335+
pass
336+
337+
if not real_sql and cursor and hasattr(cursor, "mogrify"):
338+
# If formatting failed and we're using psycopg2, it could be that we're
339+
# looking at a query that uses Composed objects. Use psycopg2's mogrify
340+
# function to format the query. We lose per-parameter trimming but gain
341+
# accuracy in formatting.
342+
#
343+
# This is intentionally the second choice because we assume Composed
344+
# queries are not widely used, while per-parameter trimming is
345+
# generally highly desirable.
346+
try:
347+
if cursor and hasattr(cursor, "mogrify"):
348+
real_sql = cursor.mogrify(sql, params)
329349
if isinstance(real_sql, bytes):
330350
real_sql = real_sql.decode(cursor.connection.encoding)
331-
else:
3 10000 32-
real_sql, real_params = format_sql(sql, params)
333-
334-
if real_params:
335-
try:
336-
real_sql = format_and_strip(real_sql, real_params)
337-
except Exception:
338-
pass
339-
hub.add_breadcrumb(message=real_sql, category="query")
351+
except Exception:
352+
pass
353+
354+
if real_sql:
355+
with capture_internal_exceptions():
356+
hub.add_breadcrumb(message=real_sql, category="query")
340357

341358

342359
def install_sql_hook():

tests/integrations/django/test_basic.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,7 @@ def test_sql_psycopg2_string_composition(sentry_init, capture_events, query):
226226
if "postgres" not in connections:
227227
pytest.skip("postgres tests disabled")
228228

229-
import psycopg2
229+
import psycopg2.sql
230230

231231
sql = connections["postgres"].cursor()
232232

@@ -249,7 +249,7 @@ def test_sql_psycopg2_placeholders(sentry_init, capture_events):
249249
if "postgres" not in connections:
250250
pytest.skip("postgres tests disabled")
251251

252-
import psycopg2
252+
import psycopg2.sql
253253

254254
sql = connections["postgres"].cursor()
255255

0 commit comments

Comments
 (0)
0