Skip to content

Commit

Permalink
Fix Redis tests for potel (#3838)
Browse files Browse the repository at this point in the history
* Make sure only spans are created, not transactions
  • Loading branch information
antonpirker authored Dec 4, 2024
1 parent 8b70a66 commit a05086f
Show file tree
Hide file tree
Showing 8 changed files with 45 additions and 43 deletions.
2 changes: 2 additions & 0 deletions sentry_sdk/integrations/redis/_sync_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -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__()

Expand All @@ -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__()

Expand Down
8 changes: 4 additions & 4 deletions tests/integrations/redis/asyncio/test_redis_asyncio.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import pytest

from sentry_sdk import capture_message, start_transaction
import sentry_sdk
from sentry_sdk.consts import SPANDATA
from sentry_sdk.integrations.redis import RedisIntegration
from tests.conftest import ApproxDict
Expand All @@ -16,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"]
Expand Down Expand Up @@ -54,7 +54,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)
Expand Down Expand Up @@ -92,7 +92,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")

Expand Down
12 changes: 6 additions & 6 deletions tests/integrations/redis/cluster/test_redis_cluster.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import pytest
from sentry_sdk import capture_message

import sentry_sdk
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

Expand All @@ -27,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"]
Expand Down Expand Up @@ -68,7 +68,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)

Expand Down Expand Up @@ -117,7 +117,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)
Expand Down Expand Up @@ -152,7 +152,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")

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import pytest

from sentry_sdk import capture_message, start_transaction
import sentry_sdk
from sentry_sdk.consts import SPANDATA
from sentry_sdk.integrations.redis import RedisIntegration
from tests.conftest import ApproxDict
Expand Down Expand Up @@ -40,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"]
Expand Down Expand Up @@ -78,7 +78,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
Expand Down Expand Up @@ -120,7 +120,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)
Expand Down Expand Up @@ -156,7 +156,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")

Expand Down
34 changes: 17 additions & 17 deletions tests/integrations/redis/test_redis.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import pytest
from fakeredis import FakeStrictRedis

from sentry_sdk import capture_message, start_transaction
import sentry_sdk
from sentry_sdk.consts import SPANDATA
from sentry_sdk.integrations.redis import RedisIntegration

Expand All @@ -23,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"]
Expand Down Expand Up @@ -60,7 +60,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)
Expand Down Expand Up @@ -94,7 +94,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
Expand All @@ -103,7 +103,7 @@ def test_sensitive_data(sentry_init, capture_events, render_span_tree):
assert (
render_span_tree(event)
== """\
- op="": description=null
- op="<unlabeled span>": description=null
- op="db.redis": description="GET [Filtered]"\
"""
)
Expand All @@ -117,7 +117,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")
Expand All @@ -127,7 +127,7 @@ def test_pii_data_redacted(sentry_init, capture_events, render_span_tree):
assert (
render_span_tree(event)
== """\
- op="": description=null
- op="<unlabeled span>": description=null
- op="db.redis": description="SET 'somekey1' [Filtered]"
- op="db.redis": description="SET 'somekey2' [Filtered]"
- op="db.redis": description="GET 'somekey2'"
Expand All @@ -145,7 +145,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")
Expand All @@ -155,7 +155,7 @@ def test_pii_data_sent(sentry_init, capture_events, render_span_tree):
assert (
render_span_tree(event)
== """\
- op="": description=null
- op="<unlabeled span>": 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'"
Expand All @@ -173,7 +173,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
Expand All @@ -183,7 +183,7 @@ def test_data_truncation(sentry_init, capture_events, render_span_tree):
assert (
render_span_tree(event)
== f"""\
- op="": description=null
- op="<unlabeled span>": description=null
- op="db.redis": description="SET 'somekey1' '{long_string[: 1024 - len("...") - len("SET 'somekey1' '")]}..."
- op="db.redis": description="SET 'somekey2' 'bbbbbbbbbb'"\
""" # noqa: E221
Expand All @@ -199,7 +199,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
Expand All @@ -209,7 +209,7 @@ def test_data_truncation_custom(sentry_init, capture_events, render_span_tree):
assert (
render_span_tree(event)
== f"""\
- op="": description=null
- op="<unlabeled span>": 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
Expand All @@ -230,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"]
Expand Down Expand Up @@ -268,7 +268,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")

Expand All @@ -290,7 +290,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")
Expand All @@ -317,7 +317,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")

Expand Down
6 changes: 3 additions & 3 deletions tests/integrations/redis/test_redis_cache_module.py
Original file line number Diff line number Diff line change
Expand Up @@ -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="<unlabeled span>": description=null
- op="db.redis": description="GET 'mycachekey'"\
"""
)
Expand Down Expand Up @@ -61,7 +61,7 @@ def test_cache_basic(sentry_init, capture_events, render_span_tree):
assert (
render_span_tree(event)
== """\
- op="": description=null
- op="<unlabeled span>": description=null
- op="db.redis": description="HGET 'mycachekey' [Filtered]"
- op="cache.get": description="mycachekey"
- op="db.redis": description="GET 'mycachekey'"
Expand Down Expand Up @@ -97,7 +97,7 @@ def test_cache_keys(sentry_init, capture_events, render_span_tree):
assert (
render_span_tree(event)
== """\
- op="": description=null
- op="<unlabeled span>": description=null
- op="db.redis": description="GET 'somethingelse'"
- op="cache.get": description="blub"
- op="db.redis": description="GET 'blub'"
Expand Down
14 changes: 7 additions & 7 deletions tests/integrations/redis/test_redis_cache_module_async.py
Original file line number Diff line number Diff line change
Expand Up @@ -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="<unlabeled span>": description=null
- op="db.redis": description="GET 'myasynccachekey'"\
"""
)
Expand All @@ -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="<unlabeled span>": description=null
- op="cache.get": description="myasynccachekey"
- op="db.redis": description="GET 'myasynccachekey'"\
"""
Expand All @@ -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")
Expand All @@ -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="<unlabeled span>": description=null
- op="db.redis": description="GET 'asomethingelse'"
- op="cache.get": description="ablub"
- op="db.redis": description="GET 'ablub'"
Expand All @@ -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")
Expand Down
2 changes: 1 addition & 1 deletion tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit a05086f

Please sign in to comment.