Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix asyncpg spans #3639

Merged
merged 13 commits into from
Oct 15, 2024
45 changes: 30 additions & 15 deletions sentry_sdk/integrations/asyncpg.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ def setup_once() -> None:
asyncpg.Connection.execute = _wrap_execute(
asyncpg.Connection.execute,
)

asyncpg.Connection._execute = _wrap_connection_method(
asyncpg.Connection._execute
)
Expand Down Expand Up @@ -80,8 +79,8 @@ async def _inner(*args: Any, **kwargs: Any) -> T:
) as span:
res = await f(*args, **kwargs)

with capture_internal_exceptions():
add_query_source(span)
with capture_internal_exceptions():
add_query_source(span)
Comment on lines +82 to +83
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the query source change from the PR description


return res

Expand Down Expand Up @@ -148,7 +147,7 @@ def _inner(*args: Any, **kwargs: Any) -> T: # noqa: N807
) as span:
_set_db_data(span, args[0])
res = f(*args, **kwargs)
span.set_data("db.cursor", res)
span.set_attribute("db.cursor", str(res))

return res

Expand All @@ -168,21 +167,37 @@ async def _inner(*args: Any, **kwargs: Any) -> T:
name="connect",
origin=AsyncPGIntegration.origin,
) as span:
span.set_data(SPANDATA.DB_SYSTEM, "postgresql")
span.set_attribute(SPANDATA.DB_SYSTEM, "postgresql")
addr = kwargs.get("addr")
if addr:
try:
span.set_data(SPANDATA.SERVER_ADDRESS, addr[0])
span.set_data(SPANDATA.SERVER_PORT, addr[1])
span.set_attribute(SPANDATA.SERVER_ADDRESS, addr[0])
span.set_attribute(SPANDATA.SERVER_PORT, addr[1])
except IndexError:
pass
span.set_data(SPANDATA.DB_NAME, database)
span.set_data(SPANDATA.DB_USER, user)

span.set_attribute(SPANDATA.DB_NAME, database)
span.set_attribute(SPANDATA.DB_USER, user)

with capture_internal_exceptions():
data = {}
for attr in (
"db.cursor",
"db.params",
"db.paramstyle",
SPANDATA.DB_NAME,
SPANDATA.DB_SYSTEM,
SPANDATA.DB_USER,
SPANDATA.SERVER_ADDRESS,
SPANDATA.SERVER_PORT,
):
if span.get_attribute(attr):
data[attr] = span.get_attribute(attr)

sentry_sdk.add_breadcrumb(
message="connect", category="query", data=span._data
message="connect", category="query", data=data
)

res = await f(*args, **kwargs)

return res
Expand All @@ -191,20 +206,20 @@ async def _inner(*args: Any, **kwargs: Any) -> T:


def _set_db_data(span: Span, conn: Any) -> None:
span.set_data(SPANDATA.DB_SYSTEM, "postgresql")
span.set_attribute(SPANDATA.DB_SYSTEM, "postgresql")

addr = conn._addr
if addr:
try:
span.set_data(SPANDATA.SERVER_ADDRESS, addr[0])
span.set_data(SPANDATA.SERVER_PORT, addr[1])
span.set_attribute(SPANDATA.SERVER_ADDRESS, addr[0])
span.set_attribute(SPANDATA.SERVER_PORT, addr[1])
except IndexError:
pass

database = conn._params.database
if database:
span.set_data(SPANDATA.DB_NAME, database)
span.set_attribute(SPANDATA.DB_NAME, database)

user = conn._params.user
if user:
span.set_data(SPANDATA.DB_USER, user)
span.set_attribute(SPANDATA.DB_USER, user)
15 changes: 9 additions & 6 deletions sentry_sdk/tracing_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import re
import sys
from collections.abc import Mapping
from datetime import timedelta
from datetime import datetime, timedelta, timezone
from functools import wraps
from urllib.parse import quote, unquote
import uuid
Expand Down Expand Up @@ -133,13 +133,13 @@ def record_sql_queries(

data = {}
if params_list is not None:
data["db.params"] = params_list
data["db.params"] = str(params_list)
if paramstyle is not None:
data["db.paramstyle"] = paramstyle
data["db.paramstyle"] = str(paramstyle)
if executemany:
data["db.executemany"] = True
if record_cursor_repr and cursor is not None:
data["db.cursor"] = cursor
data["db.cursor"] = str(cursor)

with capture_internal_exceptions():
sentry_sdk.add_breadcrumb(message=query, category="query", data=data)
Expand Down Expand Up @@ -209,14 +209,17 @@ def add_query_source(span):
if not client.is_active():
return

if span.timestamp is None or span.start_timestamp is None:
if span.start_timestamp is None:
return

should_add_query_source = client.options.get("enable_db_query_source", True)
if not should_add_query_source:
return

duration = span.timestamp - span.start_timestamp
# We assume here that the span is just ending now. We can't use
# the actual end timestamp of the span because the span can't be
# finished in order to set any attributes on it.
duration = datetime.now(tz=timezone.utc) - span.start_timestamp
threshold = client.options.get("db_query_source_threshold_ms", 0)
slow_query = duration / timedelta(milliseconds=1) > threshold

Expand Down
Loading
Loading