Skip to content

Commit 4b7d222

Browse files
committed
Re-introduce patch for _bind, just don't add query source to those spans since they're not fetching rows
1 parent ede0537 commit 4b7d222

2 files changed

Lines changed: 29 additions & 7 deletions

File tree

sentry_sdk/integrations/asyncpg.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,9 @@ def setup_once() -> None:
5353
)
5454
asyncpg.Connection.prepare = _wrap_connection_method(asyncpg.Connection.prepare)
5555

56+
BaseCursor._bind = _wrap_cursor_method(
57+
BaseCursor._bind, record_query_source=False
58+
)
5659
BaseCursor._bind_exec = _wrap_cursor_method(BaseCursor._bind_exec)
5760
BaseCursor._exec = _wrap_cursor_method(BaseCursor._exec)
5861

@@ -163,6 +166,8 @@ async def _inner(*args: "Any", **kwargs: "Any") -> "T":
163166

164167
def _wrap_cursor_method(
165168
f: "Callable[..., Awaitable[T]]",
169+
*,
170+
record_query_source: bool = True,
166171
) -> "Callable[..., Awaitable[T]]":
167172
async def _inner(*args: "Any", **kwargs: "Any") -> "T":
168173
if sentry_sdk.get_client().get_integration(AsyncPGIntegration) is None:
@@ -182,11 +187,11 @@ async def _inner(*args: "Any", **kwargs: "Any") -> "T":
182187
_set_db_data(span, cursor._connection)
183188
res = await f(*args, **kwargs)
184189

185-
if isinstance(span, StreamedSpan):
190+
if record_query_source and isinstance(span, StreamedSpan):
186191
with capture_internal_exceptions():
187192
add_query_source(span)
188193

189-
if not isinstance(span, StreamedSpan):
194+
if record_query_source and not isinstance(span, StreamedSpan):
190195
with capture_internal_exceptions():
191196
add_query_source(span)
192197

tests/integrations/asyncpg/test_asyncpg.py

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -410,6 +410,12 @@ async def test_cursor_manual(sentry_init, capture_events) -> None:
410410
"message": "SELECT * FROM users WHERE dob > $1",
411411
"type": "default",
412412
},
413+
{
414+
"category": "query",
415+
"data": {"db.cursor": mock.ANY},
416+
"message": "SELECT * FROM users WHERE dob > $1",
417+
"type": "default",
418+
},
413419
{"category": "query", "data": {}, "message": "COMMIT;", "type": "default"},
414420
]
415421

@@ -1462,7 +1468,9 @@ async def test_cursor__bind_exec_creates_spans(
14621468

14631469

14641470
@pytest.mark.asyncio
1465-
async def test_cursor__exec_methods_create_spans(sentry_init, capture_events) -> None:
1471+
async def test_cursor_bind_and_exec_methods_create_spans(
1472+
sentry_init, capture_events
1473+
) -> None:
14661474
sentry_init(
14671475
integrations=[AsyncPGIntegration()],
14681476
traces_sample_rate=1.0,
@@ -1494,25 +1502,34 @@ async def test_cursor__exec_methods_create_spans(sentry_init, capture_events) ->
14941502

14951503
(event,) = events
14961504

1497-
assert len(event["spans"]) == 6
1505+
assert len(event["spans"]) == 7
14981506

14991507
connect_span = event["spans"][0]
15001508
executemany_span = event["spans"][1]
15011509
begin_span = event["spans"][2]
1502-
fetchrow_span_1 = event["spans"][3]
1503-
fetchrow_span_2 = event["spans"][4]
1504-
commit_span = event["spans"][5]
1510+
bind_span = event["spans"][3]
1511+
fetchrow_span_1 = event["spans"][4]
1512+
fetchrow_span_2 = event["spans"][5]
1513+
commit_span = event["spans"][6]
15051514

15061515
assert connect_span["description"] == "connect"
15071516
assert (
15081517
executemany_span["description"]
15091518
== "INSERT INTO users(name, password, dob) VALUES($1, $2, $3)"
15101519
)
15111520
assert begin_span["description"] == "BEGIN;"
1521+
assert bind_span["description"] == "SELECT * FROM users WHERE dob > $1"
15121522
assert fetchrow_span_1["description"] == "SELECT * FROM users WHERE dob > $1"
15131523
assert fetchrow_span_2["description"] == "SELECT * FROM users WHERE dob > $1"
15141524
assert commit_span["description"] == "COMMIT;"
15151525

1526+
assert bind_span["data"]["db.cursor"] is not None
1527+
assert bind_span["data"]["db.system"] == "postgresql"
1528+
assert bind_span["data"]["db.driver.name"] == "asyncpg"
1529+
assert bind_span["origin"] == "auto.db.asyncpg"
1530+
assert SPANDATA.CODE_LINENO not in bind_span.get("data", {})
1531+
assert SPANDATA.CODE_FILEPATH not in bind_span.get("data", {})
1532+
15161533
for span in (fetchrow_span_1, fetchrow_span_2):
15171534
assert span["data"]["db.cursor"] is not None
15181535
assert span["data"]["db.system"] == "postgresql"

0 commit comments

Comments
 (0)