Skip to content

Commit fdcd7d4

Browse files
committed
fix query source
1 parent 7ce2acc commit fdcd7d4

File tree

2 files changed

+20
-31
lines changed

2 files changed

+20
-31
lines changed

sentry_sdk/integrations/sqlalchemy.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -51,17 +51,17 @@ def _before_cursor_execute(
5151
conn, cursor, statement, parameters, context, executemany, *args
5252
):
5353
# type: (Any, Any, Any, Any, Any, bool, *Any) -> None
54-
ctx_mgr = record_sql_queries(
54+
record_sql_queries_ctx_mngr = record_sql_queries(
5555
cursor,
5656
statement,
5757
parameters,
5858
paramstyle=context and context.dialect and context.dialect.paramstyle or None,
5959
executemany=executemany,
6060
span_origin=SqlalchemyIntegration.origin,
6161
)
62-
context._sentry_sql_span_manager = ctx_mgr
62+
context._sentry_sql_span_manager = record_sql_queries_ctx_mngr
6363

64-
span = ctx_mgr.__enter__()
64+
span = record_sql_queries_ctx_mngr.__enter__()
6565

6666
if span is not None:
6767
_set_db_data(span, conn)
@@ -76,13 +76,13 @@ def _after_cursor_execute(conn, cursor, statement, parameters, context, *args):
7676
with capture_internal_exceptions():
7777
add_query_source(span)
7878

79-
ctx_mgr = getattr(
79+
record_sql_queries_ctx_mngr = getattr(
8080
context, "_sentry_sql_span_manager", None
8181
) # type: Optional[ContextManager[Any]]
8282

83-
if ctx_mgr is not None:
83+
if record_sql_queries_ctx_mngr is not None:
8484
context._sentry_sql_span_manager = None
85-
ctx_mgr.__exit__(None, None, None)
85+
record_sql_queries_ctx_mngr.__exit__(None, None, None)
8686

8787

8888
def _handle_error(context, *args):
@@ -99,13 +99,13 @@ def _handle_error(context, *args):
9999
# _after_cursor_execute does not get called for crashing SQL stmts. Judging
100100
# from SQLAlchemy codebase it does seem like any error coming into this
101101
# handler is going to be fatal.
102-
ctx_mgr = getattr(
102+
record_sql_queries_ctx_mngr = getattr(
103103
execution_context, "_sentry_sql_span_manager", None
104104
) # type: Optional[ContextManager[Any]]
105105

106-
if ctx_mgr is not None:
106+
if record_sql_queries_ctx_mngr is not None:
107107
execution_context._sentry_sql_span_manager = None
108-
ctx_mgr.__exit__(None, None, None)
108+
record_sql_queries_ctx_mngr.__exit__(None, None, None)
109109

110110

111111
# See: https://docs.sqlalchemy.org/en/20/dialects/index.html

tests/integrations/sqlalchemy/test_sqlalchemy.py

Lines changed: 11 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import os
2+
from contextlib import contextmanager
23
from datetime import datetime, timezone
34
from unittest import mock
45

@@ -552,17 +553,11 @@ class Person(Base):
552553
bob = Person(name="Bob")
553554
session.add(bob)
554555

555-
class fake_record_sql_queries: # noqa: N801
556-
def __init__(self, *args, **kwargs):
557-
with freeze_time(datetime(2024, 1, 1, microsecond=99999)):
558-
with record_sql_queries(*args, **kwargs) as span:
559-
self.span = span
560-
561-
def __enter__(self):
562-
return self.span
563-
564-
def __exit__(self, type, value, traceback):
565-
pass
556+
@contextmanager
557+
def fake_record_sql_queries(*args, **kwargs): # noqa: N801
558+
with freeze_time(datetime(2024, 1, 1, microsecond=99999)):
559+
with record_sql_queries(*args, **kwargs) as span:
560+
yield span
566561

567562
with mock.patch(
568563
"sentry_sdk.tracing.POTelSpan.start_timestamp",
@@ -620,17 +615,11 @@ class Person(Base):
620615
bob = Person(name="Bob")
621616
session.add(bob)
622617

623-
class fake_record_sql_queries: # noqa: N801
624-
def __init__(self, *args, **kwargs):
625-
with freeze_time(datetime(2024, 1, 1, microsecond=100001)):
626-
with record_sql_queries(*args, **kwargs) as span:
627-
self.span = span
628-
629-
def __enter__(self):
630-
return self.span
631-
632-
def __exit__(self, type, value, traceback):
633-
pass
618+
@contextmanager
619+
def fake_record_sql_queries(*args, **kwargs): # noqa: N801
620+
with freeze_time(datetime(2024, 1, 1, microsecond=100001)):
621+
with record_sql_queries(*args, **kwargs) as span:
622+
yield span
634623

635624
with mock.patch(
636625
"sentry_sdk.tracing.POTelSpan.start_timestamp",

0 commit comments

Comments
 (0)