Skip to content

fix(redis): Support multiple keys with cache_prefixes #3136

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jun 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading