Skip to content

Commit f4207df

Browse files
committed
Using render_span_tree
1 parent 9bfb8bc commit f4207df

File tree

3 files changed

+115
-105
lines changed

3 files changed

+115
-105
lines changed

tests/integrations/redis/test_redis.py

Lines changed: 46 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ def test_redis_pipeline(
8080
}
8181

8282

83-
def test_sensitive_data(sentry_init, capture_events):
83+
def test_sensitive_data(sentry_init, capture_events, render_span_tree):
8484
# fakeredis does not support the AUTH command, so we need to mock it
8585
with mock.patch(
8686
"sentry_sdk.integrations.redis.utils._COMMANDS_INCLUDING_SENSITIVE_DATA",
@@ -100,12 +100,16 @@ def test_sensitive_data(sentry_init, capture_events):
100100
) # because fakeredis does not support AUTH we use GET instead
101101

102102
(event,) = events
103-
spans = event["spans"]
104-
assert spans[0]["op"] == "db.redis"
105-
assert spans[0]["description"] == "GET [Filtered]"
103+
assert (
104+
render_span_tree(event)
105+
== """\
106+
- op="": description=null
107+
- op="db.redis": description="GET [Filtered]"\
108+
"""
109+
)
106110

107111

108-
def test_pii_data_redacted(sentry_init, capture_events):
112+
def test_pii_data_redacted(sentry_init, capture_events, render_span_tree):
109113
sentry_init(
110114
integrations=[RedisIntegration()],
111115
traces_sample_rate=1.0,
@@ -120,15 +124,19 @@ def test_pii_data_redacted(sentry_init, capture_events):
120124
connection.delete("somekey1", "somekey2")
121125

122126
(event,) = events
123-
spans = event["spans"]
124-
assert spans[0]["op"] == "db.redis"
125-
assert spans[0]["description"] == "SET 'somekey1' [Filtered]"
126-
assert spans[1]["description"] == "SET 'somekey2' [Filtered]"
127-
assert spans[2]["description"] == "GET 'somekey2'"
128-
assert spans[3]["description"] == "DEL 'somekey1' [Filtered]"
127+
assert (
128+
render_span_tree(event)
129+
== """\
130+
- op="": description=null
131+
- op="db.redis": description="SET 'somekey1' [Filtered]"
132+
- op="db.redis": description="SET 'somekey2' [Filtered]"
133+
- op="db.redis": description="GET 'somekey2'"
134+
- op="db.redis": description="DEL 'somekey1' [Filtered]"\
135+
"""
136+
)
129137

130138

131-
def test_pii_data_sent(sentry_init, capture_events):
139+
def test_pii_data_sent(sentry_init, capture_events, render_span_tree):
132140
sentry_init(
133141
integrations=[RedisIntegration()],
134142
traces_sample_rate=1.0,
@@ -144,15 +152,19 @@ def test_pii_data_sent(sentry_init, capture_events):
144152
connection.delete("somekey1", "somekey2")
145153

146154
(event,) = events
147-
spans = event["spans"]
148-
assert spans[0]["op"] == "db.redis"
149-
assert spans[0]["description"] == "SET 'somekey1' 'my secret string1'"
150-
assert spans[1]["description"] == "SET 'somekey2' 'my secret string2'"
151-
assert spans[2]["description"] == "GET 'somekey2'"
152-
assert spans[3]["description"] == "DEL 'somekey1' 'somekey2'"
155+
assert (
156+
render_span_tree(event)
157+
== """\
158+
- op="": description=null
159+
- op="db.redis": description="SET 'somekey1' 'my secret string1'"
160+
- op="db.redis": description="SET 'somekey2' 'my secret string2'"
161+
- op="db.redis": description="GET 'somekey2'"
162+
- op="db.redis": description="DEL 'somekey1' 'somekey2'"\
163+
"""
164+
)
153165

154166

155-
def test_data_truncation(sentry_init, capture_events):
167+
def test_data_truncation(sentry_init, capture_events, render_span_tree):
156168
sentry_init(
157169
integrations=[RedisIntegration()],
158170
traces_sample_rate=1.0,
@@ -168,15 +180,17 @@ def test_data_truncation(sentry_init, capture_events):
168180
connection.set("somekey2", short_string)
169181

170182
(event,) = events
171-
spans = event["spans"]
172-
assert spans[0]["op"] == "db.redis"
173-
assert spans[0]["description"] == "SET 'somekey1' '%s..." % (
174-
long_string[: 1024 - len("...") - len("SET 'somekey1' '")],
183+
assert (
184+
render_span_tree(event)
185+
== f"""\
186+
- op="": description=null
187+
- op="db.redis": description="SET 'somekey1' '{long_string[: 1024 - len("...") - len("SET 'somekey1' '")]}..."
188+
- op="db.redis": description="SET 'somekey2' 'bbbbbbbbbb'"\
189+
"""
175190
)
176-
assert spans[1]["description"] == "SET 'somekey2' '%s'" % (short_string,)
177191

178192

179-
def test_data_truncation_custom(sentry_init, capture_events):
193+
def test_data_truncation_custom(sentry_init, capture_events, render_span_tree):
180194
sentry_init(
181195
integrations=[RedisIntegration(max_data_size=30)],
182196
traces_sample_rate=1.0,
@@ -192,12 +206,14 @@ def test_data_truncation_custom(sentry_init, capture_events):
192206
connection.set("somekey2", short_string)
193207

194208
(event,) = events
195-
spans = event["spans"]
196-
assert spans[0]["op"] == "db.redis"
197-
assert spans[0]["description"] == "SET 'somekey1' '%s..." % (
198-
long_string[: 30 - len("...") - len("SET 'somekey1' '")],
209+
assert (
210+
render_span_tree(event)
211+
== f"""\
212+
- op="": description=null
213+
- op="db.redis": description="SET 'somekey1' '{long_string[: 30 - len("...") - len("SET 'somekey1' '")]}..."
214+
- op="db.redis": description="SET 'somekey2' '{short_string}'"\
215+
"""
199216
)
200-
assert spans[1]["description"] == "SET 'somekey2' '%s'" % (short_string,)
201217

202218

203219
def test_breadcrumbs(sentry_init, capture_events):

tests/integrations/redis/test_redis_cache_module.py

Lines changed: 38 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
FAKEREDIS_VERSION = parse_version(fakeredis.__version__)
1515

1616

17-
def test_no_cache_basic(sentry_init, capture_events):
17+
def test_no_cache_basic(sentry_init, capture_events, render_span_tree):
1818
sentry_init(
1919
integrations=[
2020
RedisIntegration(),
@@ -28,12 +28,16 @@ def test_no_cache_basic(sentry_init, capture_events):
2828
connection.get("mycachekey")
2929

3030
(event,) = events
31-
spans = sorted(event["spans"], key=lambda x: x["start_timestamp"])
32-
assert len(spans) == 1
33-
assert spans[0]["op"] == "db.redis"
31+
assert (
32+
render_span_tree(event)
33+
== """\
34+
- op="": description=null
35+
- op="db.redis": description="GET 'mycachekey'"\
36+
"""
37+
)
3438

3539

36-
def test_cache_basic(sentry_init, capture_events):
40+
def test_cache_basic(sentry_init, capture_events, render_span_tree):
3741
sentry_init(
3842
integrations=[
3943
RedisIntegration(
@@ -53,31 +57,25 @@ def test_cache_basic(sentry_init, capture_events):
5357
connection.mget("mycachekey1", "mycachekey2")
5458

5559
(event,) = events
56-
spans = sorted(event["spans"], key=lambda x: x["start_timestamp"])
57-
assert len(spans) == 9
58-
59-
# no cache support for hget command
60-
assert spans[0]["op"] == "db.redis"
61-
assert spans[0]["tags"]["redis.command"] == "HGET"
62-
63-
assert spans[1]["op"] == "cache.get"
64-
assert spans[2]["op"] == "db.redis"
65-
assert spans[2]["tags"]["redis.command"] == "GET"
66-
67-
assert spans[3]["op"] == "cache.put"
68-
assert spans[4]["op"] == "db.redis"
69-
assert spans[4]["tags"]["redis.command"] == "SET"
70-
71-
assert spans[5]["op"] == "cache.put"
72-
assert spans[6]["op"] == "db.redis"
73-
assert spans[6]["tags"]["redis.command"] == "SETEX"
74-
75-
assert spans[7]["op"] == "cache.get"
76-
assert spans[8]["op"] == "db.redis"
77-
assert spans[8]["tags"]["redis.command"] == "MGET"
60+
# no cache support for HGET command
61+
assert (
62+
render_span_tree(event)
63+
== """\
64+
- op="": description=null
65+
- op="db.redis": description="HGET 'mycachekey' [Filtered]"
66+
- op="cache.get": description="mycachekey"
67+
- op="db.redis": description="GET 'mycachekey'"
68+
- op="cache.put": description="mycachekey1"
69+
- op="db.redis": description="SET 'mycachekey1' [Filtered]"
70+
- op="cache.put": description="mycachekey2"
71+
- op="db.redis": description="SETEX 'mycachekey2' [Filtered] [Filtered]"
72+
- op="cache.get": description="mycachekey1, mycachekey2"
73+
- op="db.redis": description="MGET 'mycachekey1' [Filtered]"\
74+
"""
75+
)
7876

7977

80-
def test_cache_keys(sentry_init, capture_events):
78+
def test_cache_keys(sentry_init, capture_events, render_span_tree):
8179
sentry_init(
8280
integrations=[
8381
RedisIntegration(
@@ -96,24 +94,18 @@ def test_cache_keys(sentry_init, capture_events):
9694
connection.get("bl")
9795

9896
(event,) = events
99-
spans = sorted(event["spans"], key=lambda x: x["start_timestamp"])
100-
101-
assert len(spans) == 6
102-
assert spans[0]["op"] == "db.redis"
103-
assert spans[0]["description"] == "GET 'somethingelse'"
104-
105-
assert spans[1]["op"] == "cache.get"
106-
assert spans[1]["description"] == "blub"
107-
assert spans[2]["op"] == "db.redis"
108-
assert spans[2]["description"] == "GET 'blub'"
109-
110-
assert spans[3]["op"] == "cache.get"
111-
assert spans[3]["description"] == "blubkeything"
112-
assert spans[4]["op"] == "db.redis"
113-
assert spans[4]["description"] == "GET 'blubkeything'"
114-
115-
assert spans[5]["op"] == "db.redis"
116-
assert spans[5]["description"] == "GET 'bl'"
97+
assert (
98+
render_span_tree(event)
99+
== """\
100+
- op="": description=null
101+
- op="db.redis": description="GET 'somethingelse'"
102+
- op="cache.get": description="blub"
103+
- op="db.redis": description="GET 'blub'"
104+
- op="cache.get": description="blubkeything"
105+
- op="db.redis": description="GET 'blubkeything'"
106+
- op="db.redis": description="GET 'bl'"\
107+
"""
108+
)
117109

118110

119111
def test_cache_data(sentry_init, capture_events):

tests/integrations/redis/test_redis_cache_module_async.py

Lines changed: 31 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121

2222

2323
@pytest.mark.asyncio
24-
async def test_no_cache_basic(sentry_init, capture_events):
24+
async def test_no_cache_basic(sentry_init, capture_events, render_span_tree):
2525
sentry_init(
2626
integrations=[
2727
RedisIntegration(),
@@ -35,13 +35,17 @@ async def test_no_cache_basic(sentry_init, capture_events):
3535
await connection.get("myasynccachekey")
3636

3737
(event,) = events
38-
spans = event["spans"]
39-
assert len(spans) == 1
40-
assert spans[0]["op"] == "db.redis"
38+
assert (
39+
render_span_tree(event)
40+
== """\
41+
- op="": description=null
42+
- op="db.redis": description="GET 'myasynccachekey'"\
43+
"""
44+
)
4145

4246

4347
@pytest.mark.asyncio
44-
async def test_cache_basic(sentry_init, capture_events):
48+
async def test_cache_basic(sentry_init, capture_events, render_span_tree):
4549
sentry_init(
4650
integrations=[
4751
RedisIntegration(
@@ -57,15 +61,18 @@ async def test_cache_basic(sentry_init, capture_events):
5761
await connection.get("myasynccachekey")
5862

5963
(event,) = events
60-
spans = event["spans"]
61-
assert len(spans) == 2
62-
63-
assert spans[0]["op"] == "cache.get"
64-
assert spans[1]["op"] == "db.redis"
64+
assert (
65+
render_span_tree(event)
66+
== """\
67+
- op="": description=null
68+
- op="cache.get": description="myasynccachekey"
69+
- op="db.redis": description="GET 'myasynccachekey'"\
70+
"""
71+
)
6572

6673

6774
@pytest.mark.asyncio
68-
async def test_cache_keys(sentry_init, capture_events):
75+
async def test_cache_keys(sentry_init, capture_events, render_span_tree):
6976
sentry_init(
7077
integrations=[
7178
RedisIntegration(
@@ -84,23 +91,18 @@ async def test_cache_keys(sentry_init, capture_events):
8491
await connection.get("abl")
8592

8693
(event,) = events
87-
spans = event["spans"]
88-
assert len(spans) == 6
89-
assert spans[0]["op"] == "db.redis"
90-
assert spans[0]["description"] == "GET 'asomethingelse'"
91-
92-
assert spans[1]["op"] == "cache.get"
93-
assert spans[1]["description"] == "ablub"
94-
assert spans[2]["op"] == "db.redis"
95-
assert spans[2]["description"] == "GET 'ablub'"
96-
97-
assert spans[3]["op"] == "cache.get"
98-
assert spans[3]["description"] == "ablubkeything"
99-
assert spans[4]["op"] == "db.redis"
100-
assert spans[4]["description"] == "GET 'ablubkeything'"
101-
102-
assert spans[5]["op"] == "db.redis"
103-
assert spans[5]["description"] == "GET 'abl'"
94+
assert (
95+
render_span_tree(event)
96+
== """\
97+
- op="": description=null
98+
- op="db.redis": description="GET 'asomethingelse'"
99+
- op="cache.get": description="ablub"
100+
- op="db.redis": description="GET 'ablub'"
101+
- op="cache.get": description="ablubkeything"
102+
- op="db.redis": description="GET 'ablubkeything'"
103+
- op="db.redis": description="GET 'abl'"\
104+
"""
105+
)
104106

105107

106108
@pytest.mark.asyncio
@@ -122,7 +124,7 @@ async def test_cache_data(sentry_init, capture_events):
122124
await connection.get("myasynccachekey")
123125

124126
(event,) = events
125-
spans = event["spans"]
127+
spans = sorted(event["spans"], key=lambda x: x["start_timestamp"])
126128

127129
assert len(spans) == 6
128130

0 commit comments

Comments
 (0)