Skip to content

Commit a05086f

Browse files
authored
Fix Redis tests for potel (#3838)
* Make sure only spans are created, not transactions
1 parent 8b70a66 commit a05086f

File tree

8 files changed

+45
-43
lines changed

8 files changed

+45
-43
lines changed

sentry_sdk/integrations/redis/_sync_common.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ def sentry_patched_execute_command(self, name, *args, **kwargs):
8888
op=cache_properties["op"],
8989
name=cache_properties["description"],
9090
origin=SPAN_ORIGIN,
91+
only_if_parent=True,
9192
)
9293
cache_span.__enter__()
9394

@@ -97,6 +98,7 @@ def sentry_patched_execute_command(self, name, *args, **kwargs):
9798
op=db_properties["op"],
9899
name=db_properties["description"],
99100
origin=SPAN_ORIGIN,
101+
only_if_parent=True,
100102
)
101103
db_span.__enter__()
102104

tests/integrations/redis/asyncio/test_redis_asyncio.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import pytest
22

3-
from sentry_sdk import capture_message, start_transaction
3+
import sentry_sdk
44
from sentry_sdk.consts import SPANDATA
55
from sentry_sdk.integrations.redis import RedisIntegration
66
from tests.conftest import ApproxDict
@@ -16,7 +16,7 @@ async def test_async_basic(sentry_init, capture_events):
1616
connection = FakeRedis()
1717

1818
await connection.get("foobar")
19-
capture_message("hi")
19+
sentry_sdk.capture_message("hi")
2020

2121
(event,) = events
2222
(crumb,) = event["breadcrumbs"]["values"]
@@ -54,7 +54,7 @@ async def test_async_redis_pipeline(
5454
events = capture_events()
5555

5656
connection = FakeRedis()
57-
with start_transaction():
57+
with sentry_sdk.start_span():
5858
pipeline = connection.pipeline(transaction=is_transaction)
5959
pipeline.get("foo")
6060
pipeline.set("bar", 1)
@@ -92,7 +92,7 @@ async def test_async_span_origin(sentry_init, capture_events):
9292
events = capture_events()
9393

9494
connection = FakeRedis()
95-
with start_transaction(name="custom_transaction"):
95+
with sentry_sdk.start_span(name="custom_transaction"):
9696
# default case
9797
await connection.set("somekey", "somevalue")
9898

tests/integrations/redis/cluster/test_redis_cluster.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import pytest
2-
from sentry_sdk import capture_message
2+
3+
import sentry_sdk
34
from sentry_sdk.consts import SPANDATA
4-
from sentry_sdk.api import start_transaction
55
from sentry_sdk.integrations.redis import RedisIntegration
66
from tests.conftest import ApproxDict
77

@@ -27,7 +27,7 @@ def test_rediscluster_breadcrumb(sentry_init, capture_events):
2727

2828
rc = redis.RedisCluster(host="localhost", port=6379)
2929
rc.get("foobar")
30-
capture_message("hi")
30+
sentry_sdk.capture_message("hi")
3131

3232
(event,) = events
3333
crumbs = event["breadcrumbs"]["values"]
@@ -68,7 +68,7 @@ def test_rediscluster_basic(sentry_init, capture_events, send_default_pii, descr
6868
)
6969
events = capture_events()
7070

71-
with start_transaction():
71+
with sentry_sdk.start_span():
7272
rc = redis.RedisCluster(host="localhost", port=6379)
7373
rc.set("bar", 1)
7474

@@ -117,7 +117,7 @@ def test_rediscluster_pipeline(
117117
events = capture_events()
118118

119119
rc = redis.RedisCluster(host="localhost", port=6379)
120-
with start_transaction():
120+
with sentry_sdk.start_span():
121121
pipeline = rc.pipeline()
122122
pipeline.get("foo")
123123
pipeline.set("bar", 1)
@@ -152,7 +152,7 @@ def test_rediscluster_span_origin(sentry_init, capture_events):
152152
events = capture_events()
153153

154154
rc = redis.RedisCluster(host="localhost", port=6379)
155-
with start_transaction(name="custom_transaction"):
155+
with sentry_sdk.start_span(name="custom_transaction"):
156156
# default case
157157
rc.set("somekey", "somevalue")
158158

tests/integrations/redis/cluster_asyncio/test_redis_cluster_asyncio.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import pytest
22

3-
from sentry_sdk import capture_message, start_transaction
3+
import sentry_sdk
44
from sentry_sdk.consts import SPANDATA
55
from sentry_sdk.integrations.redis import RedisIntegration
66
from tests.conftest import ApproxDict
@@ -40,7 +40,7 @@ async def test_async_breadcrumb(sentry_init, capture_events):
4040
connection = cluster.RedisCluster(host="localhost", port=6379)
4141

4242
await connection.get("foobar")
43-
capture_message("hi")
43+
sentry_sdk.capture_message("hi")
4444

4545
(event,) = events
4646
(crumb,) = event["breadcrumbs"]["values"]
@@ -78,7 +78,7 @@ async def test_async_basic(sentry_init, capture_events, send_default_pii, descri
7878
events = capture_events()
7979

8080
connection = cluster.RedisCluster(host="localhost", port=6379)
81-
with start_transaction():
81+
with sentry_sdk.start_span():
8282
await connection.set("bar", 1)
8383

8484
(event,) = events
@@ -120,7 +120,7 @@ async def test_async_redis_pipeline(
120120
events = capture_events()
121121

122122
connection = cluster.RedisCluster(host="localhost", port=6379)
123-
with start_transaction():
123+
with sentry_sdk.start_span():
124124
pipeline = connection.pipeline()
125125
pipeline.get("foo")
126126
pipeline.set("bar", 1)
@@ -156,7 +156,7 @@ async def test_async_span_origin(sentry_init, capture_events):
156156
events = capture_events()
157157

158158
connection = cluster.RedisCluster(host="localhost", port=6379)
159-
with start_transaction(name="custom_transaction"):
159+
with sentry_sdk.start_span(name="custom_transaction"):
160160
# default case
161161
await connection.set("somekey", "somevalue")
162162

tests/integrations/redis/test_redis.py

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import pytest
44
from fakeredis import FakeStrictRedis
55

6-
from sentry_sdk import capture_message, start_transaction
6+
import sentry_sdk
77
from sentry_sdk.consts import SPANDATA
88
from sentry_sdk.integrations.redis import RedisIntegration
99

@@ -23,7 +23,7 @@ def test_basic(sentry_init, capture_events):
2323
connection = FakeStrictRedis()
2424

2525
connection.get("foobar")
26-
capture_message("hi")
26+
sentry_sdk.capture_message("hi")
2727

2828
(event,) = events
2929
(crumb,) = event["breadcrumbs"]["values"]
@@ -60,7 +60,7 @@ def test_redis_pipeline(
6060
events = capture_events()
6161

6262
connection = FakeStrictRedis()
63-
with start_transaction():
63+
with sentry_sdk.start_span():
6464
pipeline = connection.pipeline(transaction=is_transaction)
6565
pipeline.get("foo")
6666
pipeline.set("bar", 1)
@@ -94,7 +94,7 @@ def test_sensitive_data(sentry_init, capture_events, render_span_tree):
9494
events = capture_events()
9595

9696
connection = FakeStrictRedis()
97-
with start_transaction():
97+
with sentry_sdk.start_span():
9898
connection.get(
9999
"this is super secret"
100100
) # because fakeredis does not support AUTH we use GET instead
@@ -103,7 +103,7 @@ def test_sensitive_data(sentry_init, capture_events, render_span_tree):
103103
assert (
104104
render_span_tree(event)
105105
== """\
106-
- op="": description=null
106+
- op="<unlabeled span>": description=null
107107
- op="db.redis": description="GET [Filtered]"\
108108
"""
109109
)
@@ -117,7 +117,7 @@ def test_pii_data_redacted(sentry_init, capture_events, render_span_tree):
117117
events = capture_events()
118118

119119
connection = FakeStrictRedis()
120-
with start_transaction():
120+
with sentry_sdk.start_span():
121121
connection.set("somekey1", "my secret string1")
122122
connection.set("somekey2", "my secret string2")
123123
connection.get("somekey2")
@@ -127,7 +127,7 @@ def test_pii_data_redacted(sentry_init, capture_events, render_span_tree):
127127
assert (
128128
render_span_tree(event)
129129
== """\
130-
- op="": description=null
130+
- op="<unlabeled span>": description=null
131131
- op="db.redis": description="SET 'somekey1' [Filtered]"
132132
- op="db.redis": description="SET 'somekey2' [Filtered]"
133133
- op="db.redis": description="GET 'somekey2'"
@@ -145,7 +145,7 @@ def test_pii_data_sent(sentry_init, capture_events, render_span_tree):
145145
events = capture_events()
146146

147147
connection = FakeStrictRedis()
148-
with start_transaction():
148+
with sentry_sdk.start_span():
149149
connection.set("somekey1", "my secret string1")
150150
connection.set("somekey2", "my secret string2")
151151
connection.get("somekey2")
@@ -155,7 +155,7 @@ def test_pii_data_sent(sentry_init, capture_events, render_span_tree):
155155
assert (
156156
render_span_tree(event)
157157
== """\
158-
- op="": description=null
158+
- op="<unlabeled span>": description=null
159159
- op="db.redis": description="SET 'somekey1' 'my secret string1'"
160160
- op="db.redis": description="SET 'somekey2' 'my secret string2'"
161161
- op="db.redis": description="GET 'somekey2'"
@@ -173,7 +173,7 @@ def test_data_truncation(sentry_init, capture_events, render_span_tree):
173173
events = capture_events()
174174

175175
connection = FakeStrictRedis()
176-
with start_transaction():
176+
with sentry_sdk.start_span():
177177
long_string = "a" * 100000
178178
connection.set("somekey1", long_string)
179179
short_string = "b" * 10
@@ -183,7 +183,7 @@ def test_data_truncation(sentry_init, capture_events, render_span_tree):
183183
assert (
184184
render_span_tree(event)
185185
== f"""\
186-
- op="": description=null
186+
- op="<unlabeled span>": description=null
187187
- op="db.redis": description="SET 'somekey1' '{long_string[: 1024 - len("...") - len("SET 'somekey1' '")]}..."
188188
- op="db.redis": description="SET 'somekey2' 'bbbbbbbbbb'"\
189189
""" # noqa: E221
@@ -199,7 +199,7 @@ def test_data_truncation_custom(sentry_init, capture_events, render_span_tree):
199199
events = capture_events()
200200

201201
connection = FakeStrictRedis()
202-
with start_transaction():
202+
with sentry_sdk.start_span():
203203
long_string = "a" * 100000
204204
connection.set("somekey1", long_string)
205205
short_string = "b" * 10
@@ -209,7 +209,7 @@ def test_data_truncation_custom(sentry_init, capture_events, render_span_tree):
209209
assert (
210210
render_span_tree(event)
211211
== f"""\
212-
- op="": description=null
212+
- op="<unlabeled span>": description=null
213213
- op="db.redis": description="SET 'somekey1' '{long_string[: 30 - len("...") - len("SET 'somekey1' '")]}..."
214214
- op="db.redis": description="SET 'somekey2' '{short_string}'"\
215215
""" # noqa: E221
@@ -230,7 +230,7 @@ def test_breadcrumbs(sentry_init, capture_events):
230230
short_string = "b" * 10
231231
connection.set("somekey2", short_string)
232232

233-
capture_message("hi")
233+
sentry_sdk.capture_message("hi")
234234

235235
(event,) = events
236236
crumbs = event["breadcrumbs"]["values"]
@@ -268,7 +268,7 @@ def test_db_connection_attributes_client(sentry_init, capture_events):
268268
)
269269
events = capture_events()
270270

271-
with start_transaction():
271+
with sentry_sdk.start_span():
272272
connection = FakeStrictRedis(connection_pool=MOCK_CONNECTION_POOL)
273273
connection.get("foobar")
274274

@@ -290,7 +290,7 @@ def test_db_connection_attributes_pipeline(sentry_init, capture_events):
290290
)
291291
events = capture_events()
292292

293-
with start_transaction():
293+
with sentry_sdk.start_span():
294294
connection = FakeStrictRedis(connection_pool=MOCK_CONNECTION_POOL)
295295
pipeline = connection.pipeline(transaction=False)
296296
pipeline.get("foo")
@@ -317,7 +317,7 @@ def test_span_origin(sentry_init, capture_events):
317317
events = capture_events()
318318

319319
connection = FakeStrictRedis()
320-
with start_transaction(name="custom_transaction"):
320+
with sentry_sdk.start_span(name="custom_transaction"):
321321
# default case
322322
connection.set("somekey", "somevalue")
323323

tests/integrations/redis/test_redis_cache_module.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ def test_no_cache_basic(sentry_init, capture_events, render_span_tree):
3131
assert (
3232
render_span_tree(event)
3333
== """\
34-
- op="": description=null
34+
- op="<unlabeled span>": description=null
3535
- op="db.redis": description="GET 'mycachekey'"\
3636
"""
3737
)
@@ -61,7 +61,7 @@ def test_cache_basic(sentry_init, capture_events, render_span_tree):
6161
assert (
6262
render_span_tree(event)
6363
== """\
64-
- op="": description=null
64+
- op="<unlabeled span>": description=null
6565
- op="db.redis": description="HGET 'mycachekey' [Filtered]"
6666
- op="cache.get": description="mycachekey"
6767
- op="db.redis": description="GET 'mycachekey'"
@@ -97,7 +97,7 @@ def test_cache_keys(sentry_init, capture_events, render_span_tree):
9797
assert (
9898
render_span_tree(event)
9999
== """\
100-
- op="": description=null
100+
- op="<unlabeled span>": description=null
101101
- op="db.redis": description="GET 'somethingelse'"
102102
- op="cache.get": description="blub"
103103
- op="db.redis": description="GET 'blub'"

tests/integrations/redis/test_redis_cache_module_async.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -31,14 +31,14 @@ async def test_no_cache_basic(sentry_init, capture_events, render_span_tree):
3131
events = capture_events()
3232

3333
connection = FakeRedisAsync()
34-
with sentry_sdk.start_transaction():
34+
with sentry_sdk.start_span():
3535
await connection.get("myasynccachekey")
3636

3737
(event,) = events
3838
assert (
3939
render_span_tree(event)
4040
== """\
41-
- op="": description=null
41+
- op="<unlabeled span>": description=null
4242
- op="db.redis": description="GET 'myasynccachekey'"\
4343
"""
4444
)
@@ -57,14 +57,14 @@ async def test_cache_basic(sentry_init, capture_events, render_span_tree):
5757
events = capture_events()
5858

5959
connection = FakeRedisAsync()
60-
with sentry_sdk.start_transaction():
60+
with sentry_sdk.start_span():
6161
await connection.get("myasynccachekey")
6262

6363
(event,) = events
6464
assert (
6565
render_span_tree(event)
6666
== """\
67-
- op="": description=null
67+
- op="<unlabeled span>": description=null
6868
- op="cache.get": description="myasynccachekey"
6969
- op="db.redis": description="GET 'myasynccachekey'"\
7070
"""
@@ -84,7 +84,7 @@ async def test_cache_keys(sentry_init, capture_events, render_span_tree):
8484
events = capture_events()
8585

8686
connection = FakeRedisAsync()
87-
with sentry_sdk.start_transaction():
87+
with sentry_sdk.start_span():
8888
await connection.get("asomethingelse")
8989
await connection.get("ablub")
9090
await connection.get("ablubkeything")
@@ -94,7 +94,7 @@ async def test_cache_keys(sentry_init, capture_events, render_span_tree):
9494
assert (
9595
render_span_tree(event)
9696
== """\
97-
- op="": description=null
97+
- op="<unlabeled span>": description=null
9898
- op="db.redis": description="GET 'asomethingelse'"
9999
- op="cache.get": description="ablub"
100100
- op="db.redis": description="GET 'ablub'"
@@ -118,7 +118,7 @@ async def test_cache_data(sentry_init, capture_events):
118118
events = capture_events()
119119

120120
connection = FakeRedisAsync(host="mycacheserver.io", port=6378)
121-
with sentry_sdk.start_transaction():
121+
with sentry_sdk.start_span():
122122
await connection.get("myasynccachekey")
123123
await connection.set("myasynccachekey", "事实胜于雄辩")
124124
await connection.get("myasynccachekey")

tox.ini

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -403,7 +403,7 @@ deps =
403403

404404
# Django
405405
django: psycopg2-binary
406-
django-v{1.11,2.0,2.1,2.2,3.0,3.1,3.2}: djangorestframework>=3.0.0,<4.0.0
406+
django-v{1.11,2.0,2.2,3.0,3.1,3.2}: djangorestframework>=3.0.0,<4.0.0
407407
django-v{2.0,2.2,3.0,3.2,4.0,4.1,4.2,5.0,5.1}: channels[daphne]
408408
django-v{2.2,3.0}: six
409409
django-v{1.11,2.0,2.2,3.0,3.2}: Werkzeug<2.1.0

0 commit comments

Comments
 (0)