Skip to content

Commit 53a276c

Browse files
tonalclaude
andcommitted
fix(aiomysql): Handle add_query_source for StreamedSpan and fix test mocks
- Call add_query_source inside context for StreamedSpan, outside for regular Span (following asyncpg pattern) - Fix tests to mock record_sql_queries_supporting_streaming instead of record_sql_queries Addresses cursor bot review comments. Co-Authored-By: Claude Opus 4.7 <[email protected]>
1 parent a0143fd commit 53a276c

2 files changed

Lines changed: 17 additions & 9 deletions

File tree

sentry_sdk/integrations/aiomysql.py

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -96,9 +96,13 @@ async def _inner(*args: Any, **kwargs: Any) -> T:
9696
if conn:
9797
_set_db_data(span, conn)
9898
res = await f(*args, **kwargs)
99+
if isinstance(span, StreamedSpan):
100+
with capture_internal_exceptions():
101+
add_query_source(span)
99102

100-
with capture_internal_exceptions():
101-
add_query_source(span)
103+
if not isinstance(span, StreamedSpan):
104+
with capture_internal_exceptions():
105+
add_query_source(span)
102106

103107
return res
104108

@@ -139,9 +143,13 @@ async def _inner(*args: Any, **kwargs: Any) -> T:
139143
if conn:
140144
_set_db_data(span, conn)
141145
res = await f(*args, **kwargs)
146+
if isinstance(span, StreamedSpan):
147+
with capture_internal_exceptions():
148+
add_query_source(span)
142149

143-
with capture_internal_exceptions():
144-
add_query_source(span)
150+
if not isinstance(span, StreamedSpan):
151+
with capture_internal_exceptions():
152+
add_query_source(span)
145153

146154
return res
147155
finally:

tests/integrations/aiomysql/test_aiomysql.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
from sentry_sdk import capture_message, start_transaction
2323
from sentry_sdk.consts import SPANDATA
2424
from sentry_sdk.integrations.aiomysql import AioMySQLIntegration
25-
from sentry_sdk.tracing_utils import record_sql_queries
25+
from sentry_sdk.tracing_utils import record_sql_queries_supporting_streaming
2626
from tests.conftest import ApproxDict
2727

2828
MYSQL_HOST = os.getenv("SENTRY_PYTHON_TEST_MYSQL_HOST", "localhost")
@@ -569,15 +569,15 @@ async def test_no_query_source_if_duration_too_short(sentry_init, capture_events
569569

570570
@contextmanager
571571
def fake_record_sql_queries(*args, **kwargs):
572-
with record_sql_queries(*args, **kwargs) as span:
572+
with record_sql_queries_supporting_streaming(*args, **kwargs) as span:
573573
pass
574574
span.start_timestamp = datetime.datetime(2024, 1, 1, microsecond=0)
575575
span.timestamp = datetime.datetime(2024, 1, 1, microsecond=99999)
576576
yield span
577577

578578
async with conn.cursor() as cur:
579579
with mock.patch(
580-
"sentry_sdk.integrations.aiomysql.record_sql_queries",
580+
"sentry_sdk.integrations.aiomysql.record_sql_queries_supporting_streaming",
581581
fake_record_sql_queries,
582582
):
583583
await cur.execute(
@@ -615,15 +615,15 @@ async def test_query_source_if_duration_over_threshold(sentry_init, capture_even
615615

616616
@contextmanager
617617
def fake_record_sql_queries(*args, **kwargs):
618-
with record_sql_queries(*args, **kwargs) as span:
618+
with record_sql_queries_supporting_streaming(*args, **kwargs) as span:
619619
pass
620620
span.start_timestamp = datetime.datetime(2024, 1, 1, microsecond=0)
621621
span.timestamp = datetime.datetime(2024, 1, 1, microsecond=100001)
622622
yield span
623623

624624
async with conn.cursor() as cur:
625625
with mock.patch(
626-
"sentry_sdk.integrations.aiomysql.record_sql_queries",
626+
"sentry_sdk.integrations.aiomysql.record_sql_queries_supporting_streaming",
627627
fake_record_sql_queries,
628628
):
629629
await cur.execute(

0 commit comments

Comments
 (0)