Skip to content

Commit ac4d657

Browse files
authored
fix(redis): Support multiple keys with cache_prefixes (#3136)
1 parent 8f80dfe commit ac4d657

File tree

2 files changed

+43
-2
lines changed

2 files changed

+43
-2
lines changed

sentry_sdk/integrations/redis/modules/caches.py

+6-2
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,15 @@ def _compile_cache_span_properties(redis_command, args, kwargs, integration):
3131
# type: (str, tuple[Any, ...], dict[str, Any], RedisIntegration) -> dict[str, Any]
3232
key = _get_safe_key(redis_command, args, kwargs)
3333
key_as_string = _key_as_string(key)
34+
keys_as_string = key_as_string.split(", ")
3435

3536
is_cache_key = False
3637
for prefix in integration.cache_prefixes:
37-
if key_as_string.startswith(prefix):
38-
is_cache_key = True
38+
for kee in keys_as_string:
39+
if kee.startswith(prefix):
40+
is_cache_key = True
41+
break
42+
if is_cache_key:
3943
break
4044

4145
value = None

tests/integrations/redis/test_redis_cache_module.py

+37
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,43 @@ def test_cache_data(sentry_init, capture_events):
198198
assert spans[5]["op"] == "db.redis" # we ignore db spans in this test.
199199

200200

201+
def test_cache_prefixes(sentry_init, capture_events):
202+
sentry_init(
203+
integrations=[
204+
RedisIntegration(
205+
cache_prefixes=["yes"],
206+
),
207+
],
208+
traces_sample_rate=1.0,
209+
)
210+
events = capture_events()
211+
212+
connection = FakeStrictRedis()
213+
with sentry_sdk.start_transaction():
214+
connection.mget("yes", "no")
215+
connection.mget("no", 1, "yes")
216+
connection.mget("no", "yes.1", "yes.2")
217+
connection.mget("no.1", "no.2", "no.3")
218+
connection.mget("no.1", "no.2", "no.actually.yes")
219+
connection.mget(b"no.3", b"yes.5")
220+
connection.mget(uuid.uuid4().bytes)
221+
connection.mget(uuid.uuid4().bytes, "yes")
222+
223+
(event,) = events
224+
225+
spans = event["spans"]
226+
assert len(spans) == 13 # 8 db spans + 5 cache spans
227+
228+
cache_spans = [span for span in spans if span["op"] == "cache.get"]
229+
assert len(cache_spans) == 5
230+
231+
assert cache_spans[0]["description"] == "yes, no"
232+
assert cache_spans[1]["description"] == "no, 1, yes"
233+
assert cache_spans[2]["description"] == "no, yes.1, yes.2"
234+
assert cache_spans[3]["description"] == "no.3, yes.5"
235+
assert cache_spans[4]["description"] == ", yes"
236+
237+
201238
@pytest.mark.parametrize(
202239
"method_name,args,kwargs,expected_key",
203240
[

0 commit comments

Comments
 (0)