From 18bb9ff8a6fe890a9b491a69b9061d6b1c76f716 Mon Sep 17 00:00:00 2001 From: Anton Pirker Date: Tue, 3 Dec 2024 09:58:04 +0100 Subject: [PATCH 1/3] Fixed some tests and cleanup --- .../redis/asyncio/test_redis_asyncio.py | 7 +++-- .../redis/cluster/test_redis_cluster.py | 9 +++--- .../test_redis_cluster_asyncio.py | 9 +++--- tests/integrations/redis/test_redis.py | 31 ++++++++++--------- .../redis/test_redis_cache_module.py | 6 ++-- .../redis/test_redis_cache_module_async.py | 14 ++++----- tox.ini | 2 +- 7 files changed, 41 insertions(+), 37 deletions(-) diff --git a/tests/integrations/redis/asyncio/test_redis_asyncio.py b/tests/integrations/redis/asyncio/test_redis_asyncio.py index 2a0b96b021..b0f676a5cb 100644 --- a/tests/integrations/redis/asyncio/test_redis_asyncio.py +++ b/tests/integrations/redis/asyncio/test_redis_asyncio.py @@ -1,6 +1,7 @@ import pytest -from sentry_sdk import capture_message, start_transaction +import sentry_sdk +from sentry_sdk import capture_message from sentry_sdk.consts import SPANDATA from sentry_sdk.integrations.redis import RedisIntegration from tests.conftest import ApproxDict @@ -54,7 +55,7 @@ async def test_async_redis_pipeline( events = capture_events() connection = FakeRedis() - with start_transaction(): + with sentry_sdk.start_span(): pipeline = connection.pipeline(transaction=is_transaction) pipeline.get("foo") pipeline.set("bar", 1) @@ -92,7 +93,7 @@ async def test_async_span_origin(sentry_init, capture_events): events = capture_events() connection = FakeRedis() - with start_transaction(name="custom_transaction"): + with sentry_sdk.start_span(name="custom_transaction"): # default case await connection.set("somekey", "somevalue") diff --git a/tests/integrations/redis/cluster/test_redis_cluster.py b/tests/integrations/redis/cluster/test_redis_cluster.py index 26feee1dae..e52d86051c 100644 --- a/tests/integrations/redis/cluster/test_redis_cluster.py +++ b/tests/integrations/redis/cluster/test_redis_cluster.py @@ -1,7 +1,8 @@ import pytest + +import sentry_sdk from sentry_sdk import capture_message from sentry_sdk.consts import SPANDATA -from sentry_sdk.api import start_transaction from sentry_sdk.integrations.redis import RedisIntegration from tests.conftest import ApproxDict @@ -68,7 +69,7 @@ def test_rediscluster_basic(sentry_init, capture_events, send_default_pii, descr ) events = capture_events() - with start_transaction(): + with sentry_sdk.start_span(): rc = redis.RedisCluster(host="localhost", port=6379) rc.set("bar", 1) @@ -117,7 +118,7 @@ def test_rediscluster_pipeline( events = capture_events() rc = redis.RedisCluster(host="localhost", port=6379) - with start_transaction(): + with sentry_sdk.start_span(): pipeline = rc.pipeline() pipeline.get("foo") pipeline.set("bar", 1) @@ -152,7 +153,7 @@ def test_rediscluster_span_origin(sentry_init, capture_events): events = capture_events() rc = redis.RedisCluster(host="localhost", port=6379) - with start_transaction(name="custom_transaction"): + with sentry_sdk.start_span(name="custom_transaction"): # default case rc.set("somekey", "somevalue") diff --git a/tests/integrations/redis/cluster_asyncio/test_redis_cluster_asyncio.py b/tests/integrations/redis/cluster_asyncio/test_redis_cluster_asyncio.py index b11808fb50..a38f1cb7e6 100644 --- a/tests/integrations/redis/cluster_asyncio/test_redis_cluster_asyncio.py +++ b/tests/integrations/redis/cluster_asyncio/test_redis_cluster_asyncio.py @@ -1,6 +1,7 @@ import pytest -from sentry_sdk import capture_message, start_transaction +import sentry_sdk +from sentry_sdk import capture_message from sentry_sdk.consts import SPANDATA from sentry_sdk.integrations.redis import RedisIntegration from tests.conftest import ApproxDict @@ -78,7 +79,7 @@ async def test_async_basic(sentry_init, capture_events, send_default_pii, descri events = capture_events() connection = cluster.RedisCluster(host="localhost", port=6379) - with start_transaction(): + with sentry_sdk.start_span(): await connection.set("bar", 1) (event,) = events @@ -120,7 +121,7 @@ async def test_async_redis_pipeline( events = capture_events() connection = cluster.RedisCluster(host="localhost", port=6379) - with start_transaction(): + with sentry_sdk.start_span(): pipeline = connection.pipeline() pipeline.get("foo") pipeline.set("bar", 1) @@ -156,7 +157,7 @@ async def test_async_span_origin(sentry_init, capture_events): events = capture_events() connection = cluster.RedisCluster(host="localhost", port=6379) - with start_transaction(name="custom_transaction"): + with sentry_sdk.start_span(name="custom_transaction"): # default case await connection.set("somekey", "somevalue") diff --git a/tests/integrations/redis/test_redis.py b/tests/integrations/redis/test_redis.py index 0fec23f273..4a3686d365 100644 --- a/tests/integrations/redis/test_redis.py +++ b/tests/integrations/redis/test_redis.py @@ -3,7 +3,8 @@ import pytest from fakeredis import FakeStrictRedis -from sentry_sdk import capture_message, start_transaction +import sentry_sdk +from sentry_sdk import capture_message from sentry_sdk.consts import SPANDATA from sentry_sdk.integrations.redis import RedisIntegration @@ -60,7 +61,7 @@ def test_redis_pipeline( events = capture_events() connection = FakeStrictRedis() - with start_transaction(): + with sentry_sdk.start_span(): pipeline = connection.pipeline(transaction=is_transaction) pipeline.get("foo") pipeline.set("bar", 1) @@ -94,7 +95,7 @@ def test_sensitive_data(sentry_init, capture_events, render_span_tree): events = capture_events() connection = FakeStrictRedis() - with start_transaction(): + with sentry_sdk.start_span(): connection.get( "this is super secret" ) # because fakeredis does not support AUTH we use GET instead @@ -103,7 +104,7 @@ def test_sensitive_data(sentry_init, capture_events, render_span_tree): assert ( render_span_tree(event) == """\ -- op="": description=null +- op="": description=null - op="db.redis": description="GET [Filtered]"\ """ ) @@ -117,7 +118,7 @@ def test_pii_data_redacted(sentry_init, capture_events, render_span_tree): events = capture_events() connection = FakeStrictRedis() - with start_transaction(): + with sentry_sdk.start_span(): connection.set("somekey1", "my secret string1") connection.set("somekey2", "my secret string2") connection.get("somekey2") @@ -127,7 +128,7 @@ def test_pii_data_redacted(sentry_init, capture_events, render_span_tree): assert ( render_span_tree(event) == """\ -- op="": description=null +- op="": description=null - op="db.redis": description="SET 'somekey1' [Filtered]" - op="db.redis": description="SET 'somekey2' [Filtered]" - op="db.redis": description="GET 'somekey2'" @@ -145,7 +146,7 @@ def test_pii_data_sent(sentry_init, capture_events, render_span_tree): events = capture_events() connection = FakeStrictRedis() - with start_transaction(): + with sentry_sdk.start_span(): connection.set("somekey1", "my secret string1") connection.set("somekey2", "my secret string2") connection.get("somekey2") @@ -155,7 +156,7 @@ def test_pii_data_sent(sentry_init, capture_events, render_span_tree): assert ( render_span_tree(event) == """\ -- op="": description=null +- op="": description=null - op="db.redis": description="SET 'somekey1' 'my secret string1'" - op="db.redis": description="SET 'somekey2' 'my secret string2'" - op="db.redis": description="GET 'somekey2'" @@ -173,7 +174,7 @@ def test_data_truncation(sentry_init, capture_events, render_span_tree): events = capture_events() connection = FakeStrictRedis() - with start_transaction(): + with sentry_sdk.start_span(): long_string = "a" * 100000 connection.set("somekey1", long_string) short_string = "b" * 10 @@ -183,7 +184,7 @@ def test_data_truncation(sentry_init, capture_events, render_span_tree): assert ( render_span_tree(event) == f"""\ -- op="": description=null +- op="": description=null - op="db.redis": description="SET 'somekey1' '{long_string[: 1024 - len("...") - len("SET 'somekey1' '")]}..." - op="db.redis": description="SET 'somekey2' 'bbbbbbbbbb'"\ """ # noqa: E221 @@ -199,7 +200,7 @@ def test_data_truncation_custom(sentry_init, capture_events, render_span_tree): events = capture_events() connection = FakeStrictRedis() - with start_transaction(): + with sentry_sdk.start_span(): long_string = "a" * 100000 connection.set("somekey1", long_string) short_string = "b" * 10 @@ -209,7 +210,7 @@ def test_data_truncation_custom(sentry_init, capture_events, render_span_tree): assert ( render_span_tree(event) == f"""\ -- op="": description=null +- op="": description=null - op="db.redis": description="SET 'somekey1' '{long_string[: 30 - len("...") - len("SET 'somekey1' '")]}..." - op="db.redis": description="SET 'somekey2' '{short_string}'"\ """ # noqa: E221 @@ -268,7 +269,7 @@ def test_db_connection_attributes_client(sentry_init, capture_events): ) events = capture_events() - with start_transaction(): + with sentry_sdk.start_span(): connection = FakeStrictRedis(connection_pool=MOCK_CONNECTION_POOL) connection.get("foobar") @@ -290,7 +291,7 @@ def test_db_connection_attributes_pipeline(sentry_init, capture_events): ) events = capture_events() - with start_transaction(): + with sentry_sdk.start_span(): connection = FakeStrictRedis(connection_pool=MOCK_CONNECTION_POOL) pipeline = connection.pipeline(transaction=False) pipeline.get("foo") @@ -317,7 +318,7 @@ def test_span_origin(sentry_init, capture_events): events = capture_events() connection = FakeStrictRedis() - with start_transaction(name="custom_transaction"): + with sentry_sdk.start_span(name="custom_transaction"): # default case connection.set("somekey", "somevalue") diff --git a/tests/integrations/redis/test_redis_cache_module.py b/tests/integrations/redis/test_redis_cache_module.py index 68f915c2e5..e02b1ec31a 100644 --- a/tests/integrations/redis/test_redis_cache_module.py +++ b/tests/integrations/redis/test_redis_cache_module.py @@ -31,7 +31,7 @@ def test_no_cache_basic(sentry_init, capture_events, render_span_tree): assert ( render_span_tree(event) == """\ -- op="": description=null +- op="": description=null - op="db.redis": description="GET 'mycachekey'"\ """ ) @@ -61,7 +61,7 @@ def test_cache_basic(sentry_init, capture_events, render_span_tree): assert ( render_span_tree(event) == """\ -- op="": description=null +- op="": description=null - op="db.redis": description="HGET 'mycachekey' [Filtered]" - op="cache.get": description="mycachekey" - op="db.redis": description="GET 'mycachekey'" @@ -97,7 +97,7 @@ def test_cache_keys(sentry_init, capture_events, render_span_tree): assert ( render_span_tree(event) == """\ -- op="": description=null +- op="": description=null - op="db.redis": description="GET 'somethingelse'" - op="cache.get": description="blub" - op="db.redis": description="GET 'blub'" diff --git a/tests/integrations/redis/test_redis_cache_module_async.py b/tests/integrations/redis/test_redis_cache_module_async.py index a6ea06a973..d4ce4936bb 100644 --- a/tests/integrations/redis/test_redis_cache_module_async.py +++ b/tests/integrations/redis/test_redis_cache_module_async.py @@ -31,14 +31,14 @@ async def test_no_cache_basic(sentry_init, capture_events, render_span_tree): events = capture_events() connection = FakeRedisAsync() - with sentry_sdk.start_transaction(): + with sentry_sdk.start_span(): await connection.get("myasynccachekey") (event,) = events assert ( render_span_tree(event) == """\ -- op="": description=null +- op="": description=null - op="db.redis": description="GET 'myasynccachekey'"\ """ ) @@ -57,14 +57,14 @@ async def test_cache_basic(sentry_init, capture_events, render_span_tree): events = capture_events() connection = FakeRedisAsync() - with sentry_sdk.start_transaction(): + with sentry_sdk.start_span(): await connection.get("myasynccachekey") (event,) = events assert ( render_span_tree(event) == """\ -- op="": description=null +- op="": description=null - op="cache.get": description="myasynccachekey" - op="db.redis": description="GET 'myasynccachekey'"\ """ @@ -84,7 +84,7 @@ async def test_cache_keys(sentry_init, capture_events, render_span_tree): events = capture_events() connection = FakeRedisAsync() - with sentry_sdk.start_transaction(): + with sentry_sdk.start_span(): await connection.get("asomethingelse") await connection.get("ablub") await connection.get("ablubkeything") @@ -94,7 +94,7 @@ async def test_cache_keys(sentry_init, capture_events, render_span_tree): assert ( render_span_tree(event) == """\ -- op="": description=null +- op="": description=null - op="db.redis": description="GET 'asomethingelse'" - op="cache.get": description="ablub" - op="db.redis": description="GET 'ablub'" @@ -118,7 +118,7 @@ async def test_cache_data(sentry_init, capture_events): events = capture_events() connection = FakeRedisAsync(host="mycacheserver.io", port=6378) - with sentry_sdk.start_transaction(): + with sentry_sdk.start_span(): await connection.get("myasynccachekey") await connection.set("myasynccachekey", "事实胜于雄辩") await connection.get("myasynccachekey") diff --git a/tox.ini b/tox.ini index 60036adf98..ad1591f91e 100644 --- a/tox.ini +++ b/tox.ini @@ -403,7 +403,7 @@ deps = # Django django: psycopg2-binary - django-v{1.11,2.0,2.1,2.2,3.0,3.1,3.2}: djangorestframework>=3.0.0,<4.0.0 + django-v{1.11,2.0,2.2,3.0,3.1,3.2}: djangorestframework>=3.0.0,<4.0.0 django-v{2.0,2.2,3.0,3.2,4.0,4.1,4.2,5.0,5.1}: channels[daphne] django-v{2.2,3.0}: six django-v{1.11,2.0,2.2,3.0,3.2}: Werkzeug<2.1.0 From 291743fed8a72eb8d3e6ecfd15b8fbe067e76deb Mon Sep 17 00:00:00 2001 From: Anton Pirker Date: Tue, 3 Dec 2024 10:04:17 +0100 Subject: [PATCH 2/3] cleanup --- tests/integrations/redis/asyncio/test_redis_asyncio.py | 3 +-- tests/integrations/redis/cluster/test_redis_cluster.py | 3 +-- .../redis/cluster_asyncio/test_redis_cluster_asyncio.py | 3 +-- tests/integrations/redis/test_redis.py | 5 ++--- 4 files changed, 5 insertions(+), 9 deletions(-) diff --git a/tests/integrations/redis/asyncio/test_redis_asyncio.py b/tests/integrations/redis/asyncio/test_redis_asyncio.py index b0f676a5cb..e735d478c9 100644 --- a/tests/integrations/redis/asyncio/test_redis_asyncio.py +++ b/tests/integrations/redis/asyncio/test_redis_asyncio.py @@ -1,7 +1,6 @@ import pytest import sentry_sdk -from sentry_sdk import capture_message from sentry_sdk.consts import SPANDATA from sentry_sdk.integrations.redis import RedisIntegration from tests.conftest import ApproxDict @@ -17,7 +16,7 @@ async def test_async_basic(sentry_init, capture_events): connection = FakeRedis() await connection.get("foobar") - capture_message("hi") + sentry_sdk.capture_message("hi") (event,) = events (crumb,) = event["breadcrumbs"]["values"] diff --git a/tests/integrations/redis/cluster/test_redis_cluster.py b/tests/integrations/redis/cluster/test_redis_cluster.py index e52d86051c..43bd3e3392 100644 --- a/tests/integrations/redis/cluster/test_redis_cluster.py +++ b/tests/integrations/redis/cluster/test_redis_cluster.py @@ -1,7 +1,6 @@ import pytest import sentry_sdk -from sentry_sdk import capture_message from sentry_sdk.consts import SPANDATA from sentry_sdk.integrations.redis import RedisIntegration from tests.conftest import ApproxDict @@ -28,7 +27,7 @@ def test_rediscluster_breadcrumb(sentry_init, capture_events): rc = redis.RedisCluster(host="localhost", port=6379) rc.get("foobar") - capture_message("hi") + sentry_sdk.capture_message("hi") (event,) = events crumbs = event["breadcrumbs"]["values"] diff --git a/tests/integrations/redis/cluster_asyncio/test_redis_cluster_asyncio.py b/tests/integrations/redis/cluster_asyncio/test_redis_cluster_asyncio.py index a38f1cb7e6..85970978dd 100644 --- a/tests/integrations/redis/cluster_asyncio/test_redis_cluster_asyncio.py +++ b/tests/integrations/redis/cluster_asyncio/test_redis_cluster_asyncio.py @@ -1,7 +1,6 @@ import pytest import sentry_sdk -from sentry_sdk import capture_message from sentry_sdk.consts import SPANDATA from sentry_sdk.integrations.redis import RedisIntegration from tests.conftest import ApproxDict @@ -41,7 +40,7 @@ async def test_async_breadcrumb(sentry_init, capture_events): connection = cluster.RedisCluster(host="localhost", port=6379) await connection.get("foobar") - capture_message("hi") + sentry_sdk.capture_message("hi") (event,) = events (crumb,) = event["breadcrumbs"]["values"] diff --git a/tests/integrations/redis/test_redis.py b/tests/integrations/redis/test_redis.py index 4a3686d365..4afee93c59 100644 --- a/tests/integrations/redis/test_redis.py +++ b/tests/integrations/redis/test_redis.py @@ -4,7 +4,6 @@ from fakeredis import FakeStrictRedis import sentry_sdk -from sentry_sdk import capture_message from sentry_sdk.consts import SPANDATA from sentry_sdk.integrations.redis import RedisIntegration @@ -24,7 +23,7 @@ def test_basic(sentry_init, capture_events): connection = FakeStrictRedis() connection.get("foobar") - capture_message("hi") + sentry_sdk.capture_message("hi") (event,) = events (crumb,) = event["breadcrumbs"]["values"] @@ -231,7 +230,7 @@ def test_breadcrumbs(sentry_init, capture_events): short_string = "b" * 10 connection.set("somekey2", short_string) - capture_message("hi") + sentry_sdk.capture_message("hi") (event,) = events crumbs = event["breadcrumbs"]["values"] From b1d1ff5b5cd74dd5fe32dc203da0f58de518d298 Mon Sep 17 00:00:00 2001 From: Anton Pirker Date: Tue, 3 Dec 2024 10:08:32 +0100 Subject: [PATCH 3/3] Make sure only spans are created, not transactions --- sentry_sdk/integrations/redis/_sync_common.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/sentry_sdk/integrations/redis/_sync_common.py b/sentry_sdk/integrations/redis/_sync_common.py index 63738ea7cb..c2509eea9c 100644 --- a/sentry_sdk/integrations/redis/_sync_common.py +++ b/sentry_sdk/integrations/redis/_sync_common.py @@ -88,6 +88,7 @@ def sentry_patched_execute_command(self, name, *args, **kwargs): op=cache_properties["op"], name=cache_properties["description"], origin=SPAN_ORIGIN, + only_if_parent=True, ) cache_span.__enter__() @@ -97,6 +98,7 @@ def sentry_patched_execute_command(self, name, *args, **kwargs): op=db_properties["op"], name=db_properties["description"], origin=SPAN_ORIGIN, + only_if_parent=True, ) db_span.__enter__()