Skip to content

Commit 99e60c4

Browse files
committed
fix: Additional safeguards for tracing code
1 parent d460727 commit 99e60c4

File tree

4 files changed

+25
-16
lines changed

4 files changed

+25
-16
lines changed

sentry_sdk/hub.py

+6-3
Original file line numberDiff line numberDiff line change
@@ -446,9 +446,12 @@ def span(
446446
else:
447447
span.set_tag("error", False)
448448
finally:
449-
span.finish()
450-
maybe_create_breadcrumbs_from_span(self, span)
451-
self.finish_span(span)
449+
try:
450+
span.finish()
451+
maybe_create_breadcrumbs_from_span(self, span)
452+
self.finish_span(span)
453+
except Exception:
454+
self._capture_internal_exception(sys.exc_info())
452455
scope.span = old_span
453456

454457
def start_span(

sentry_sdk/integrations/_sql_common.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ def format_sql(sql, params, cursor):
6161
except Exception:
6262
pass
6363

64-
if not real_sql and cursor and hasattr(cursor, "mogrify"):
64+
if not real_sql and hasattr(cursor, "mogrify"):
6565
# If formatting failed and we're using psycopg2, it could be that we're
6666
# looking at a query that uses Composed objects. Use psycopg2's mogrify
6767
# function to format the query. We lose per-parameter trimming but gain
@@ -71,7 +71,7 @@ def format_sql(sql, params, cursor):
7171
# queries are not widely used, while per-parameter trimming is
7272
# generally highly desirable.
7373
try:
74-
if cursor and hasattr(cursor, "mogrify"):
74+
if hasattr(cursor, "mogrify"):
7575
real_sql = cursor.mogrify(sql, params)
7676
if isinstance(real_sql, bytes):
7777
real_sql = real_sql.decode(cursor.connection.encoding)

sentry_sdk/integrations/django/__init__.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -387,7 +387,7 @@ def install_sql_hook():
387387
def execute(self, sql, params=None):
388388
hub = Hub.current
389389
if hub.get_integration(DjangoIntegration) is None:
390-
return
390+
return real_execute(self, sql, params)
391391

392392
with record_sql_queries(
393393
hub, [format_sql(sql, params, self.cursor)], label="Django: "
@@ -397,7 +397,7 @@ def execute(self, sql, params=None):
397397
def executemany(self, sql, param_list):
398398
hub = Hub.current
399399
if hub.get_integration(DjangoIntegration) is None:
400-
return
400+
return real_executemany(self, sql, param_list)
401401

402402
with record_sql_queries(
403403
hub,

sentry_sdk/tracing.py

+15-9
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
from datetime import datetime
77

8-
from sentry_sdk.utils import concat_strings
8+
from sentry_sdk.utils import capture_internal_exceptions, concat_strings
99

1010

1111
if False:
@@ -211,14 +211,20 @@ def record_sql_queries(hub, queries, label=""):
211211
if not queries:
212212
yield None
213213
else:
214-
strings = [label]
215-
for query in queries:
216-
hub.add_breadcrumb(message=query, category="query")
217-
strings.append(query)
218-
219-
description = concat_strings(strings)
220-
with hub.span(op="db", description=description) as span:
221-
yield span
214+
description = None
215+
with capture_internal_exceptions():
216+
strings = [label]
217+
for query in queries:
218+
hub.add_breadcrumb(message=query, category="query")
219+
strings.append(query)
220+
221+
description = concat_strings(strings)
222+
223+
if description is None:
224+
yield None
225+
else:
226+
with hub.span(op="db", description=description) as span:
227+
yield span
222228

223229

224230
@contextlib.contextmanager

0 commit comments

Comments
 (0)