8000 fix(django/sql): Run cursor.mogrify as fallback to regular formatting by untitaker · Pull Request #325 · getsentry/sentry-python · GitHub
[go: up one dir, main page]

Skip to content

fix(django/sql): Run cursor.mogrify as fallback to regular formatting #325

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Apr 5, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 30 additions & 13 deletions sentry_sdk/integrations/django/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -316,21 +316,38 @@ def record_sql(sql, params, cursor=None):
if hub.get_integration(DjangoIntegration) is None:
return

with capture_internal_exceptions():
if cursor and hasattr(cursor, "mogrify"): # psycopg2
real_sql = cursor.mogrify(sql, params)
with capture_internal_exceptions():
real_sql = None
real_params = None

try:
# Prefer our own SQL formatting logic because it's the only one that
# has proper value trimming.
real_sql, real_params = format_sql(sql, params)
if real_sql:
real_sql = format_and_strip(real_sql, real_params)
except Exception:
pass

if not real_sql and cursor and hasattr(cursor, "mogrify"):
# If formatting failed and we're using psycopg2, it could be that we're
# looking at a query that uses Composed objects. Use psycopg2's mogrify
# function to format the query. We lose per-parameter trimming but gain
# accuracy in formatting.
#
# This is intentionally the second choice because we assume Composed
# queries are not widely used, while per-parameter trimming is
# generally highly desirable.
try:
if cursor and hasattr(cursor, "mogrify"):
real_sql = cursor.mogrify(sql, params)
if isinstance(real_sql, bytes):
real_sql = real_sql.decode(cursor.connection.encoding)
else:
real_sql, real_params = format_sql(sql, params)

if real_params:
try:
real_sql = format_and_strip(real_sql, real_params)
except Exception:
pass
hub.add_breadcrumb(message=real_sql, category="query")
except Exception:
pass

if real_sql:
with capture_internal_exceptions():
hub.add_breadcrumb(message=real_sql, category="query")


def install_sql_hook():
Expand Down
4 changes: 2 additions & 2 deletions tests/integrations/django/test_basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@ def test_sql_psycopg2_string_composition(sentry_init, capture_events, query):
if "postgres" not in connections:
pytest.skip("postgres tests disabled")

import psycopg2
import psycopg2.sql

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

Expand All @@ -248,7 +248,7 @@ def test_sql_psycopg2_placeholders(sentry_init, capture_events):
if "postgres" not in connections:
pytest.skip("postgres tests disabled")

import psycopg2
import psycopg2.sql

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

Expand Down
0