Skip to content

Commit 3b30f8d

Browse files
committed
add query source needs a live span
1 parent 79aa789 commit 3b30f8d

File tree

3 files changed

+10
-6
lines changed

3 files changed

+10
-6
lines changed

sentry_sdk/integrations/asyncpg.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,8 +79,9 @@ async def _inner(*args: Any, **kwargs: Any) -> T:
7979
) as span:
8080
res = await f(*args, **kwargs)
8181

82-
with capture_internal_exceptions():
83-
add_query_source(span)
82+
with capture_internal_exceptions():
83+
print("ADDING QUERY SOURCE")
84+
add_query_source(span)
8485

8586
return res
8687

sentry_sdk/tracing_utils.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
import re
55
import sys
66
from collections.abc import Mapping
7-
from datetime import timedelta
7+
from datetime import datetime, timedelta, timezone
88
from functools import wraps
99
from urllib.parse import quote, unquote
1010
import uuid
@@ -209,14 +209,17 @@ def add_query_source(span):
209209
if not client.is_active():
210210
return
211211

212-
if span.timestamp is None or span.start_timestamp is None:
212+
if span.start_timestamp is None:
213213
return
214214

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

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

tests/integrations/asyncpg/test_asyncpg.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -264,7 +264,7 @@ async def test_cursor(sentry_init, capture_events) -> None:
264264
async for record in conn.cursor(
265265
"SELECT * FROM users WHERE dob > $1", datetime.date(1970, 1, 1)
266266
):
267-
print(record)
267+
pass
268268

269269
await conn.close()
270270

0 commit comments

Comments
 (0)