Skip to content

Commit

Permalink
fix(redis): Support multiple keys with cache_prefixes (#3136)
Browse files Browse the repository at this point in the history
  • Loading branch information
sentrivana authored Jun 5, 2024
1 parent 8f80dfe commit ac4d657
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 2 deletions.
8 changes: 6 additions & 2 deletions sentry_sdk/integrations/redis/modules/caches.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,15 @@ def _compile_cache_span_properties(redis_command, args, kwargs, integration):
# type: (str, tuple[Any, ...], dict[str, Any], RedisIntegration) -> dict[str, Any]
key = _get_safe_key(redis_command, args, kwargs)
key_as_string = _key_as_string(key)
keys_as_string = key_as_string.split(", ")

is_cache_key = False
for prefix in integration.cache_prefixes:
if key_as_string.startswith(prefix):
is_cache_key = True
for kee in keys_as_string:
if kee.startswith(prefix):
is_cache_key = True
break
if is_cache_key:
break

value = None
Expand Down
37 changes: 37 additions & 0 deletions tests/integrations/redis/test_redis_cache_module.py
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,43 @@ def test_cache_data(sentry_init, capture_events):
assert spans[5]["op"] == "db.redis" # we ignore db spans in this test.


def test_cache_prefixes(sentry_init, capture_events):
sentry_init(
integrations=[
RedisIntegration(
cache_prefixes=["yes"],
),
],
traces_sample_rate=1.0,
)
events = capture_events()

connection = FakeStrictRedis()
with sentry_sdk.start_transaction():
connection.mget("yes", "no")
connection.mget("no", 1, "yes")
connection.mget("no", "yes.1", "yes.2")
connection.mget("no.1", "no.2", "no.3")
connection.mget("no.1", "no.2", "no.actually.yes")
connection.mget(b"no.3", b"yes.5")
connection.mget(uuid.uuid4().bytes)
connection.mget(uuid.uuid4().bytes, "yes")

(event,) = events

spans = event["spans"]
assert len(spans) == 13 # 8 db spans + 5 cache spans

cache_spans = [span for span in spans if span["op"] == "cache.get"]
assert len(cache_spans) == 5

assert cache_spans[0]["description"] == "yes, no"
assert cache_spans[1]["description"] == "no, 1, yes"
assert cache_spans[2]["description"] == "no, yes.1, yes.2"
assert cache_spans[3]["description"] == "no.3, yes.5"
assert cache_spans[4]["description"] == ", yes"


@pytest.mark.parametrize(
"method_name,args,kwargs,expected_key",
[
Expand Down

0 comments on commit ac4d657

Please sign in to comment.