Skip to content

Commit 25c756b

Browse files
committed
fix(asyncpg): Use Sentry span attribute name conventions
Update span attribute names to align with Sentry standards: - db.system → db.system.name - db.name → db.namespace Refactor _set_db_data to handle StreamedSpan (using set_attribute) and regular Span (using set_data) separately. The deprecated attributes are retained in the non-streamed path for backwards compatibility. Fixes PY-2424 Fixes #6299
1 parent 46c6f01 commit 25c756b

2 files changed

Lines changed: 61 additions & 24 deletions

File tree

sentry_sdk/integrations/asyncpg.py

Lines changed: 36 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -211,9 +211,9 @@ async def _inner(*args: "Any", **kwargs: "Any") -> "T":
211211
span_attributes = {
212212
"sentry.op": OP.DB,
213213
"sentry.origin": AsyncPGIntegration.origin,
214-
SPANDATA.DB_SYSTEM: "postgresql",
214+
SPANDATA.DB_SYSTEM_NAME: "postgresql",
215215
SPANDATA.DB_USER: user,
216-
SPANDATA.DB_NAME: database,
216+
SPANDATA.DB_NAMESPACE: database,
217217
SPANDATA.DB_DRIVER_NAME: "asyncpg",
218218
}
219219
if addr:
@@ -261,23 +261,40 @@ async def _inner(*args: "Any", **kwargs: "Any") -> "T":
261261

262262

263263
def _set_db_data(span: "Union[Span, StreamedSpan]", conn: "Any") -> None:
264-
set_value = span.set_attribute if isinstance(span, StreamedSpan) else span.set_data
265-
266-
set_value(SPANDATA.DB_SYSTEM, "postgresql")
267-
set_value(SPANDATA.DB_DRIVER_NAME, "asyncpg")
268-
269264
addr = conn._addr
270-
if addr:
271-
try:
272-
set_value(SPANDATA.SERVER_ADDRESS, addr[0])
273-
set_value(SPANDATA.SERVER_PORT, addr[1])
274-
except IndexError:
275-
pass
276-
277265
database = conn._params.database
278-
if database:
279-
set_value(SPANDATA.DB_NAME, database)
280-
281266
user = conn._params.user
282-
if user:
283-
set_value(SPANDATA.DB_USER, user)
267+
268+
if isinstance(span, StreamedSpan):
269+
span.set_attribute(SPANDATA.DB_SYSTEM_NAME, "postgresql")
270+
span.set_attribute(SPANDATA.DB_DRIVER_NAME, "asyncpg")
271+
if addr:
272+
try:
273+
span.set_attribute(SPANDATA.SERVER_ADDRESS, addr[0])
274+
span.set_attribute(SPANDATA.SERVER_PORT, addr[1])
275+
except IndexError:
276+
pass
277+
278+
if database:
279+
span.set_attribute(SPANDATA.DB_NAMESPACE, database)
280+
281+
if user:
282+
span.set_attribute(SPANDATA.DB_USER, user)
283+
else:
284+
# Remove this else block once we've completely migrated to streamed spans
285+
# The use of deprecated attributes here is to ensure backwards compatibility
286+
span.set_data(SPANDATA.DB_SYSTEM, "postgresql")
287+
span.set_data(SPANDATA.DB_DRIVER_NAME, "asyncpg")
288+
289+
if addr:
290+
try:
291+
span.set_data(SPANDATA.SERVER_ADDRESS, addr[0])
292+
span.set_data(SPANDATA.SERVER_PORT, addr[1])
293+
except IndexError:
294+
pass
295+
296+
if database:
297+
span.set_data(SPANDATA.DB_NAME, database)
298+
299+
if user:
300+
span.set_data(SPANDATA.DB_USER, user)

tests/integrations/asyncpg/test_asyncpg.py

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,23 @@ def _get_db_name():
5858
"message": "connect",
5959
"type": "default",
6060
}
61+
CRUMBS_CONNECT_STREAMING = {
62+
"category": "query",
63+
"data": ApproxDict(
64+
{
65+
"sentry.op": "db",
66+
"sentry.origin": "auto.db.asyncpg",
67+
"db.system.name": "postgresql",
68+
"db.namespace": PG_NAME,
69+
"db.user": PG_USER,
70+
"db.driver.name": "asyncpg",
71+
"server.address": PG_HOST,
72+
"server.port": PG_PORT,
73+
}
74+
),
75+
"message": "connect",
76+
"type": "default",
77+
}
6178

6279

6380
@pytest_asyncio.fixture(autouse=True)
@@ -124,7 +141,8 @@ async def test_connect(
124141
for crumb in event["breadcrumbs"]["values"]:
125142
del crumb["timestamp"]
126143

127-
assert event["breadcrumbs"]["values"] == [CRUMBS_CONNECT]
144+
expected_crumbs_connect = CRUMBS_CONNECT_STREAMING if span_streaming else CRUMBS_CONNECT
145+
assert event["breadcrumbs"]["values"] == [expected_crumbs_connect]
128146

129147

130148
@pytest.mark.asyncio
@@ -176,8 +194,9 @@ async def test_execute(
176194
for crumb in event["breadcrumbs"]["values"]:
177195
del crumb["timestamp"]
178196

197+
expected_crumbs_connect = CRUMBS_CONNECT_STREAMING if span_streaming else CRUMBS_CONNECT
179198
assert event["breadcrumbs"]["values"] == [
180-
CRUMBS_CONNECT,
199+
expected_crumbs_connect,
181200
{
182201
"category": "query",
183202
"data": {},
@@ -245,8 +264,9 @@ async def test_execute_many(
245264
for crumb in event["breadcrumbs"]["values"]:
246265
del crumb["timestamp"]
247266

267+
expected_crumbs_connect = CRUMBS_CONNECT_STREAMING if span_streaming else CRUMBS_CONNECT
248268
assert event["breadcrumbs"]["values"] == [
249-
CRUMBS_CONNECT,
269+
expected_crumbs_connect,
250270
{
251271
"category": "query",
252272
"data": {"db.executemany": True},
@@ -1398,11 +1418,11 @@ async def test_cursor__bind_exec_creates_spans(
13981418

13991419
assert bind_exec_span["attributes"]["sentry.origin"] == "auto.db.asyncpg"
14001420
assert bind_exec_span["attributes"]["sentry.op"] == "db"
1401-
assert bind_exec_span["attributes"]["db.system"] == "postgresql"
1421+
assert bind_exec_span["attributes"]["db.system.name"] == "postgresql"
14021422
assert bind_exec_span["attributes"]["db.driver.name"] == "asyncpg"
14031423
assert bind_exec_span["attributes"]["server.address"] == PG_HOST
14041424
assert bind_exec_span["attributes"]["server.port"] == PG_PORT
1405-
assert bind_exec_span["attributes"]["db.name"] == PG_NAME
1425+
assert bind_exec_span["attributes"]["db.namespace"] == PG_NAME
14061426
assert bind_exec_span["attributes"]["db.user"] == PG_USER
14071427
else:
14081428
events = capture_events()

0 commit comments

Comments
 (0)