From 11615861232ef80d0e72e5e66588f9c69f3e0bdd Mon Sep 17 00:00:00 2001 From: Ivana Kellyer Date: Thu, 10 Oct 2024 11:58:30 +0200 Subject: [PATCH 01/11] fixes --- sentry_sdk/integrations/asyncpg.py | 40 +++++++++++++++++++++--------- 1 file changed, 28 insertions(+), 12 deletions(-) diff --git a/sentry_sdk/integrations/asyncpg.py b/sentry_sdk/integrations/asyncpg.py index b05d5615ba..3303b7b6a3 100644 --- a/sentry_sdk/integrations/asyncpg.py +++ b/sentry_sdk/integrations/asyncpg.py @@ -148,7 +148,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", res) return res @@ -168,21 +168,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 @@ -191,20 +207,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) From 13bb7b2bcfb2b356c821066cd0e17fbf75c8eaea Mon Sep 17 00:00:00 2001 From: Ivana Kellyer Date: Thu, 10 Oct 2024 12:08:18 +0200 Subject: [PATCH 02/11] . --- tests/integrations/asyncpg/test_asyncpg.py | 40 +++++++++++++++++----- 1 file changed, 32 insertions(+), 8 deletions(-) diff --git a/tests/integrations/asyncpg/test_asyncpg.py b/tests/integrations/asyncpg/test_asyncpg.py index 8996c8dd1a..72040de91d 100644 --- a/tests/integrations/asyncpg/test_asyncpg.py +++ b/tests/integrations/asyncpg/test_asyncpg.py @@ -279,14 +279,24 @@ async def test_cursor(sentry_init, capture_events) -> None: "message": "INSERT INTO users(name, password, dob) VALUES($1, $2, $3)", "type": "default", }, - {"category": "query", "data": {}, "message": "BEGIN;", "type": "default"}, + { + "category": "query", + "data": {}, + "message": "BEGIN;", + "type": "default", + }, { "category": "query", "data": {}, "message": "SELECT * FROM users WHERE dob > $1", "type": "default", }, - {"category": "query", "data": {}, "message": "COMMIT;", "type": "default"}, + { + "category": "query", + "data": {}, + "message": "COMMIT;", + "type": "default", + }, ] @@ -337,14 +347,24 @@ async def test_cursor_manual(sentry_init, capture_events) -> None: "message": "INSERT INTO users(name, password, dob) VALUES($1, $2, $3)", "type": "default", }, - {"category": "query", "data": {}, "message": "BEGIN;", "type": "default"}, + { + "category": "query", + "data": {}, + "message": "BEGIN;", + "type": "default", + }, { "category": "query", "data": {}, "message": "SELECT * FROM users WHERE dob > $1", "type": "default", }, - {"category": "query", "data": {}, "message": "COMMIT;", "type": "default"}, + { + "category": "query", + "data": {}, + "message": "COMMIT;", + "type": "default", + }, ] @@ -654,9 +674,11 @@ async def test_no_query_source_if_duration_too_short(sentry_init, capture_events @contextmanager def fake_record_sql_queries(*args, **kwargs): - with freeze_time(datetime(2024, 1, 1, microsecond=0)): + with freeze_time(datetime.datetime(2024, 1, 1, microsecond=0)): with record_sql_queries(*args, **kwargs) as span: - freezer = freeze_time(datetime(2024, 1, 1, microsecond=99999)) + freezer = freeze_time( + datetime.datetime(2024, 1, 1, microsecond=99999) + ) freezer.start() freezer.stop() @@ -702,9 +724,11 @@ async def test_query_source_if_duration_over_threshold(sentry_init, capture_even @contextmanager def fake_record_sql_queries(*args, **kwargs): - with freeze_time(datetime(2024, 1, 1, microsecond=0)): + with freeze_time(datetime.datetime(2024, 1, 1, microsecond=0)): with record_sql_queries(*args, **kwargs) as span: - freezer = freeze_time(datetime(2024, 1, 1, microsecond=100001)) + freezer = freeze_time( + datetime.datetime(2024, 1, 1, microsecond=100001) + ) freezer.start() freezer.stop() From d1e57a3e2e568fa34f2e81d572e87af7307a5b2d Mon Sep 17 00:00:00 2001 From: Ivana Kellyer Date: Thu, 10 Oct 2024 12:26:47 +0200 Subject: [PATCH 03/11] clean up tests --- sentry_sdk/integrations/asyncpg.py | 1 - tests/integrations/asyncpg/test_asyncpg.py | 27 ++++++++++------------ 2 files changed, 12 insertions(+), 16 deletions(-) diff --git a/sentry_sdk/integrations/asyncpg.py b/sentry_sdk/integrations/asyncpg.py index 3303b7b6a3..7fa6c86c63 100644 --- a/sentry_sdk/integrations/asyncpg.py +++ b/sentry_sdk/integrations/asyncpg.py @@ -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 ) diff --git a/tests/integrations/asyncpg/test_asyncpg.py b/tests/integrations/asyncpg/test_asyncpg.py index 72040de91d..6317bb0a02 100644 --- a/tests/integrations/asyncpg/test_asyncpg.py +++ b/tests/integrations/asyncpg/test_asyncpg.py @@ -10,14 +10,6 @@ """ import os - - -PG_HOST = os.getenv("SENTRY_PYTHON_TEST_POSTGRES_HOST", "localhost") -PG_PORT = int(os.getenv("SENTRY_PYTHON_TEST_POSTGRES_PORT", "5432")) -PG_USER = os.getenv("SENTRY_PYTHON_TEST_POSTGRES_USER", "postgres") -PG_PASSWORD = os.getenv("SENTRY_PYTHON_TEST_POSTGRES_PASSWORD", "sentry") -PG_NAME = os.getenv("SENTRY_PYTHON_TEST_POSTGRES_NAME", "postgres") - import datetime from contextlib import contextmanager from unittest import mock @@ -35,9 +27,16 @@ from tests.conftest import ApproxDict +PG_HOST = os.getenv("SENTRY_PYTHON_TEST_POSTGRES_HOST", "localhost") +PG_PORT = int(os.getenv("SENTRY_PYTHON_TEST_POSTGRES_PORT", "5432")) +PG_USER = os.getenv("SENTRY_PYTHON_TEST_POSTGRES_USER", "postgres") +PG_PASSWORD = os.getenv("SENTRY_PYTHON_TEST_POSTGRES_PASSWORD", "sentry") +PG_NAME = os.getenv("SENTRY_PYTHON_TEST_POSTGRES_NAME", "postgres") + PG_CONNECTION_URI = "postgresql://{}:{}@{}/{}".format( PG_USER, PG_PASSWORD, PG_HOST, PG_NAME ) + CRUMBS_CONNECT = { "category": "query", "data": ApproxDict( @@ -317,18 +316,16 @@ async def test_cursor_manual(sentry_init, capture_events) -> None: ("Alice", "pw", datetime.date(1990, 12, 25)), ], ) - # + async with conn.transaction(): # Postgres requires non-scrollable cursors to be created # and used in a transaction. cur = await conn.cursor( "SELECT * FROM users WHERE dob > $1", datetime.date(1970, 1, 1) ) - record = await cur.fetchrow() - print(record) + await cur.fetchrow() while await cur.forward(1): - record = await cur.fetchrow() - print(record) + await cur.fetchrow() await conn.close() @@ -388,8 +385,8 @@ async def test_prepared_stmt(sentry_init, capture_events) -> None: stmt = await conn.prepare("SELECT * FROM users WHERE name = $1") - print(await stmt.fetchval("Bob")) - print(await stmt.fetchval("Alice")) + await stmt.fetchval("Bob") + await stmt.fetchval("Alice") await conn.close() From 339b4b795daecba0addc01a53e32ffee0aed74f9 Mon Sep 17 00:00:00 2001 From: Ivana Kellyer Date: Thu, 10 Oct 2024 15:08:01 +0200 Subject: [PATCH 04/11] no start_transaction --- tests/integrations/asyncpg/test_asyncpg.py | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/tests/integrations/asyncpg/test_asyncpg.py b/tests/integrations/asyncpg/test_asyncpg.py index 6317bb0a02..b97958654b 100644 --- a/tests/integrations/asyncpg/test_asyncpg.py +++ b/tests/integrations/asyncpg/test_asyncpg.py @@ -20,7 +20,7 @@ from asyncpg import connect, Connection from freezegun import freeze_time -from sentry_sdk import capture_message, start_transaction +from sentry_sdk import capture_message, start_span from sentry_sdk.integrations.asyncpg import AsyncPGIntegration from sentry_sdk.consts import SPANDATA from sentry_sdk.tracing_utils import record_sql_queries @@ -498,7 +498,7 @@ async def test_query_source_disabled(sentry_init, capture_events): events = capture_events() - with start_transaction(name="test_transaction", sampled=True): + with start_span(name="test_span"): conn: Connection = await connect(PG_CONNECTION_URI) await conn.execute( @@ -537,7 +537,7 @@ async def test_query_source_enabled( events = capture_events() - with start_transaction(name="test_transaction", sampled=True): + with start_span(name="test_span"): conn: Connection = await connect(PG_CONNECTION_URI) await conn.execute( @@ -570,7 +570,7 @@ async def test_query_source(sentry_init, capture_events): events = capture_events() - with start_transaction(name="test_transaction", sampled=True): + with start_span(name="test_span"): conn: Connection = await connect(PG_CONNECTION_URI) await conn.execute( @@ -622,7 +622,7 @@ async def test_query_source_with_module_in_search_path(sentry_init, capture_even from asyncpg_helpers.helpers import execute_query_in_connection - with start_transaction(name="test_transaction", sampled=True): + with start_span(name="test_span"): conn: Connection = await connect(PG_CONNECTION_URI) await execute_query_in_connection( @@ -666,7 +666,7 @@ async def test_no_query_source_if_duration_too_short(sentry_init, capture_events events = capture_events() - with start_transaction(name="test_transaction", sampled=True): + with start_span(name="test_span"): conn: Connection = await connect(PG_CONNECTION_URI) @contextmanager @@ -716,7 +716,7 @@ async def test_query_source_if_duration_over_threshold(sentry_init, capture_even events = capture_events() - with start_transaction(name="test_transaction", sampled=True): + with start_span(name="test_span"): conn: Connection = await connect(PG_CONNECTION_URI) @contextmanager @@ -781,7 +781,7 @@ async def test_span_origin(sentry_init, capture_events): events = capture_events() - with start_transaction(name="test_transaction"): + with start_span(name="test_span"): conn: Connection = await connect(PG_CONNECTION_URI) await conn.execute("SELECT 1") From 0ca30361aa5ef770c3eca9e40dbb50fa1b36e780 Mon Sep 17 00:00:00 2001 From: Ivana Kellyer Date: Thu, 10 Oct 2024 15:57:53 +0200 Subject: [PATCH 05/11] . --- sentry_sdk/integrations/asyncpg.py | 2 +- tests/integrations/asyncpg/test_asyncpg.py | 20 ++++++++++++++------ 2 files changed, 15 insertions(+), 7 deletions(-) diff --git a/sentry_sdk/integrations/asyncpg.py b/sentry_sdk/integrations/asyncpg.py index 7fa6c86c63..15c62035f5 100644 --- a/sentry_sdk/integrations/asyncpg.py +++ b/sentry_sdk/integrations/asyncpg.py @@ -147,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_attribute("db.cursor", res) + span.set_attribute("db.cursor", str(res)) return res diff --git a/tests/integrations/asyncpg/test_asyncpg.py b/tests/integrations/asyncpg/test_asyncpg.py index b97958654b..d3c16ca927 100644 --- a/tests/integrations/asyncpg/test_asyncpg.py +++ b/tests/integrations/asyncpg/test_asyncpg.py @@ -74,6 +74,7 @@ async def _clean_pg(): async def test_connect(sentry_init, capture_events) -> None: sentry_init( integrations=[AsyncPGIntegration()], + traces_sample_rate=1.0, _experiments={"record_sql_params": True}, ) events = capture_events() @@ -84,7 +85,7 @@ async def test_connect(sentry_init, capture_events) -> None: capture_message("hi") - (event,) = events + event = events[-1] for crumb in event["breadcrumbs"]["values"]: del crumb["timestamp"] @@ -96,6 +97,7 @@ async def test_connect(sentry_init, capture_events) -> None: async def test_execute(sentry_init, capture_events) -> None: sentry_init( integrations=[AsyncPGIntegration()], + traces_sample_rate=1.0, _experiments={"record_sql_params": True}, ) events = capture_events() @@ -123,7 +125,7 @@ async def test_execute(sentry_init, capture_events) -> None: capture_message("hi") - (event,) = events + event = events[-1] for crumb in event["breadcrumbs"]["values"]: del crumb["timestamp"] @@ -161,6 +163,7 @@ async def test_execute(sentry_init, capture_events) -> None: async def test_execute_many(sentry_init, capture_events) -> None: sentry_init( integrations=[AsyncPGIntegration()], + traces_sample_rate=1.0, _experiments={"record_sql_params": True}, ) events = capture_events() @@ -179,7 +182,7 @@ async def test_execute_many(sentry_init, capture_events) -> None: capture_message("hi") - (event,) = events + event = events[-1] for crumb in event["breadcrumbs"]["values"]: del crumb["timestamp"] @@ -199,6 +202,7 @@ async def test_execute_many(sentry_init, capture_events) -> None: async def test_record_params(sentry_init, capture_events) -> None: sentry_init( integrations=[AsyncPGIntegration(record_params=True)], + traces_sample_rate=1.0, _experiments={"record_sql_params": True}, ) events = capture_events() @@ -216,7 +220,7 @@ async def test_record_params(sentry_init, capture_events) -> None: capture_message("hi") - (event,) = events + event = events[-1] for crumb in event["breadcrumbs"]["values"]: del crumb["timestamp"] @@ -239,6 +243,7 @@ async def test_record_params(sentry_init, capture_events) -> None: async def test_cursor(sentry_init, capture_events) -> None: sentry_init( integrations=[AsyncPGIntegration()], + traces_sample_rate=1.0, _experiments={"record_sql_params": True}, ) events = capture_events() @@ -265,7 +270,7 @@ async def test_cursor(sentry_init, capture_events) -> None: capture_message("hi") - (event,) = events + event = events[-1] for crumb in event["breadcrumbs"]["values"]: del crumb["timestamp"] @@ -303,6 +308,7 @@ async def test_cursor(sentry_init, capture_events) -> None: async def test_cursor_manual(sentry_init, capture_events) -> None: sentry_init( integrations=[AsyncPGIntegration()], + traces_sample_rate=1.0, _experiments={"record_sql_params": True}, ) events = capture_events() @@ -331,7 +337,7 @@ async def test_cursor_manual(sentry_init, capture_events) -> None: capture_message("hi") - (event,) = events + event = events[-1] for crumb in event["breadcrumbs"]["values"]: del crumb["timestamp"] @@ -369,6 +375,7 @@ async def test_cursor_manual(sentry_init, capture_events) -> None: async def test_prepared_stmt(sentry_init, capture_events) -> None: sentry_init( integrations=[AsyncPGIntegration()], + traces_sample_rate=1.0, _experiments={"record_sql_params": True}, ) events = capture_events() @@ -418,6 +425,7 @@ async def test_prepared_stmt(sentry_init, capture_events) -> None: async def test_connection_pool(sentry_init, capture_events) -> None: sentry_init( integrations=[AsyncPGIntegration()], + traces_sample_rate=1.0, _experiments={"record_sql_params": True}, ) events = capture_events() From 02dc96dd4fd3d58d286e3587f64f248ce55ff70d Mon Sep 17 00:00:00 2001 From: Ivana Kellyer Date: Thu, 10 Oct 2024 16:08:06 +0200 Subject: [PATCH 06/11] . --- sentry_sdk/integrations/asyncpg.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/sentry_sdk/integrations/asyncpg.py b/sentry_sdk/integrations/asyncpg.py index 15c62035f5..dc743cdac0 100644 --- a/sentry_sdk/integrations/asyncpg.py +++ b/sentry_sdk/integrations/asyncpg.py @@ -191,8 +191,8 @@ async def _inner(*args: Any, **kwargs: Any) -> T: SPANDATA.SERVER_ADDRESS, SPANDATA.SERVER_PORT, ): - if span._get_attribute(attr): - data[attr] = span._get_attribute(attr) + if span.get_attribute(attr): + data[attr] = span.get_attribute(attr) sentry_sdk.add_breadcrumb( message="connect", category="query", data=data From 8b2d7bc794128361fdce0a58cdb3fadf72e5c71d Mon Sep 17 00:00:00 2001 From: Ivana Kellyer Date: Thu, 10 Oct 2024 16:35:15 +0200 Subject: [PATCH 07/11] stringify all the things --- sentry_sdk/tracing_utils.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/sentry_sdk/tracing_utils.py b/sentry_sdk/tracing_utils.py index 2f4dad738a..1745f6854e 100644 --- a/sentry_sdk/tracing_utils.py +++ b/sentry_sdk/tracing_utils.py @@ -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) From 79aa789765decb0ec1069a8ccf3fb860acce9e02 Mon Sep 17 00:00:00 2001 From: Ivana Kellyer Date: Thu, 10 Oct 2024 16:45:47 +0200 Subject: [PATCH 08/11] fix --- tests/integrations/asyncpg/test_asyncpg.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tests/integrations/asyncpg/test_asyncpg.py b/tests/integrations/asyncpg/test_asyncpg.py index d3c16ca927..af977c1dff 100644 --- a/tests/integrations/asyncpg/test_asyncpg.py +++ b/tests/integrations/asyncpg/test_asyncpg.py @@ -230,7 +230,7 @@ async def test_record_params(sentry_init, capture_events) -> None: { "category": "query", "data": { - "db.params": ["Bob", "secret_pw", "datetime.date(1984, 3, 1)"], + "db.params": "('Bob', 'secret_pw', datetime.date(1984, 3, 1))", "db.paramstyle": "format", }, "message": "INSERT INTO users(name, password, dob) VALUES($1, $2, $3)", @@ -399,7 +399,7 @@ async def test_prepared_stmt(sentry_init, capture_events) -> None: capture_message("hi") - (event,) = events + event = events[-1] for crumb in event["breadcrumbs"]["values"]: del crumb["timestamp"] @@ -452,7 +452,7 @@ async def test_connection_pool(sentry_init, capture_events) -> None: capture_message("hi") - (event,) = events + event = events[-1] for crumb in event["breadcrumbs"]["values"]: del crumb["timestamp"] From 3b30f8d8b20bca882e8dc46554d7e475ef0a27e9 Mon Sep 17 00:00:00 2001 From: Ivana Kellyer Date: Thu, 10 Oct 2024 18:06:03 +0200 Subject: [PATCH 09/11] add query source needs a live span --- sentry_sdk/integrations/asyncpg.py | 5 +++-- sentry_sdk/tracing_utils.py | 9 ++++++--- tests/integrations/asyncpg/test_asyncpg.py | 2 +- 3 files changed, 10 insertions(+), 6 deletions(-) diff --git a/sentry_sdk/integrations/asyncpg.py b/sentry_sdk/integrations/asyncpg.py index dc743cdac0..9fe378bebc 100644 --- a/sentry_sdk/integrations/asyncpg.py +++ b/sentry_sdk/integrations/asyncpg.py @@ -79,8 +79,9 @@ 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(): + print("ADDING QUERY SOURCE") + add_query_source(span) return res diff --git a/sentry_sdk/tracing_utils.py b/sentry_sdk/tracing_utils.py index 1745f6854e..5b69a75499 100644 --- a/sentry_sdk/tracing_utils.py +++ b/sentry_sdk/tracing_utils.py @@ -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 @@ -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 diff --git a/tests/integrations/asyncpg/test_asyncpg.py b/tests/integrations/asyncpg/test_asyncpg.py index af977c1dff..7a08ecf5f6 100644 --- a/tests/integrations/asyncpg/test_asyncpg.py +++ b/tests/integrations/asyncpg/test_asyncpg.py @@ -264,7 +264,7 @@ async def test_cursor(sentry_init, capture_events) -> None: async for record in conn.cursor( "SELECT * FROM users WHERE dob > $1", datetime.date(1970, 1, 1) ): - print(record) + pass await conn.close() From e1cf67c3bd3cdfa34c38793f9a10caf4d5c0b322 Mon Sep 17 00:00:00 2001 From: Ivana Kellyer Date: Thu, 10 Oct 2024 18:06:51 +0200 Subject: [PATCH 10/11] . --- sentry_sdk/integrations/asyncpg.py | 1 - 1 file changed, 1 deletion(-) diff --git a/sentry_sdk/integrations/asyncpg.py b/sentry_sdk/integrations/asyncpg.py index 9fe378bebc..71740cb3aa 100644 --- a/sentry_sdk/integrations/asyncpg.py +++ b/sentry_sdk/integrations/asyncpg.py @@ -80,7 +80,6 @@ async def _inner(*args: Any, **kwargs: Any) -> T: res = await f(*args, **kwargs) with capture_internal_exceptions(): - print("ADDING QUERY SOURCE") add_query_source(span) return res From 5758fd72b36cfd94ab4fb8388deab1b1ddaa5f2e Mon Sep 17 00:00:00 2001 From: Ivana Kellyer Date: Fri, 11 Oct 2024 11:00:54 +0200 Subject: [PATCH 11/11] fix last test --- sentry_sdk/tracing_utils.py | 6 +-- tests/integrations/asyncpg/test_asyncpg.py | 50 ++++++++++------------ 2 files changed, 25 insertions(+), 31 deletions(-) diff --git a/sentry_sdk/tracing_utils.py b/sentry_sdk/tracing_utils.py index 5b69a75499..b8f7288374 100644 --- a/sentry_sdk/tracing_utils.py +++ b/sentry_sdk/tracing_utils.py @@ -216,9 +216,9 @@ def add_query_source(span): if not should_add_query_source: return - # 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. + # We assume here that the query is just ending now. We can't use + # the actual end timestamp of the span because in OTel 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 diff --git a/tests/integrations/asyncpg/test_asyncpg.py b/tests/integrations/asyncpg/test_asyncpg.py index 7a08ecf5f6..adeef37d38 100644 --- a/tests/integrations/asyncpg/test_asyncpg.py +++ b/tests/integrations/asyncpg/test_asyncpg.py @@ -679,24 +679,21 @@ async def test_no_query_source_if_duration_too_short(sentry_init, capture_events @contextmanager def fake_record_sql_queries(*args, **kwargs): - with freeze_time(datetime.datetime(2024, 1, 1, microsecond=0)): + with freeze_time(datetime.datetime(2024, 1, 1, microsecond=99999)): with record_sql_queries(*args, **kwargs) as span: - freezer = freeze_time( - datetime.datetime(2024, 1, 1, microsecond=99999) - ) - freezer.start() - - freezer.stop() - - yield span + yield span with mock.patch( - "sentry_sdk.integrations.asyncpg.record_sql_queries", - fake_record_sql_queries, + "sentry_sdk.tracing.POTelSpan.start_timestamp", + datetime.datetime(2024, 1, 1, microsecond=0, tzinfo=datetime.timezone.utc), ): - await conn.execute( - "INSERT INTO users(name, password, dob) VALUES ('Alice', 'secret', '1990-12-25')", - ) + with mock.patch( + "sentry_sdk.integrations.asyncpg.record_sql_queries", + fake_record_sql_queries, + ): + await conn.execute( + "INSERT INTO users(name, password, dob) VALUES ('Alice', 'secret', '1990-12-25')", + ) await conn.close() @@ -729,24 +726,21 @@ async def test_query_source_if_duration_over_threshold(sentry_init, capture_even @contextmanager def fake_record_sql_queries(*args, **kwargs): - with freeze_time(datetime.datetime(2024, 1, 1, microsecond=0)): + with freeze_time(datetime.datetime(2024, 1, 1, microsecond=100001)): with record_sql_queries(*args, **kwargs) as span: - freezer = freeze_time( - datetime.datetime(2024, 1, 1, microsecond=100001) - ) - freezer.start() - - freezer.stop() - - yield span + yield span with mock.patch( - "sentry_sdk.integrations.asyncpg.record_sql_queries", - fake_record_sql_queries, + "sentry_sdk.tracing.POTelSpan.start_timestamp", + datetime.datetime(2024, 1, 1, microsecond=0, tzinfo=datetime.timezone.utc), ): - await conn.execute( - "INSERT INTO users(name, password, dob) VALUES ('Alice', 'secret', '1990-12-25')", - ) + with mock.patch( + "sentry_sdk.integrations.asyncpg.record_sql_queries", + fake_record_sql_queries, + ): + await conn.execute( + "INSERT INTO users(name, password, dob) VALUES ('Alice', 'secret', '1990-12-25')", + ) await conn.close()