Skip to content

Commit 121aa0e

Browse files
antonpirkerszokeasaurusrexsentrivana
authored
Redis Cache Module - 1 - Prepare Code (#3073)
Make the redis integration fit for sending Span data that is eligible for the Caches performance module in Sentry. --------- Co-authored-by: Daniel Szoke <7881302+szokeasaurusrex@users.noreply.github.com> Co-authored-by: Ivana Kellyerova <ivana.kellyerova@sentry.io>
1 parent 30f72a3 commit 121aa0e

23 files changed

+1139
-425
lines changed

.github/workflows/test-integrations-databases.yml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -77,10 +77,10 @@ jobs:
7777
run: |
7878
set -x # print commands that are executed
7979
./scripts/runtox.sh "py${{ matrix.python-version }}-redis-latest" --cov=tests --cov=sentry_sdk --cov-report= --cov-branch
80-
- name: Test rediscluster latest
80+
- name: Test redis_py_cluster_legacy latest
8181
run: |
8282
set -x # print commands that are executed
83-
./scripts/runtox.sh "py${{ matrix.python-version }}-rediscluster-latest" --cov=tests --cov=sentry_sdk --cov-report= --cov-branch
83+
./scripts/runtox.sh "py${{ matrix.python-version }}-redis_py_cluster_legacy-latest" --cov=tests --cov=sentry_sdk --cov-report= --cov-branch
8484
- name: Test sqlalchemy latest
8585
run: |
8686
set -x # print commands that are executed
@@ -152,10 +152,10 @@ jobs:
152152
run: |
153153
set -x # print commands that are executed
154154
./scripts/runtox.sh --exclude-latest "py${{ matrix.python-version }}-redis" --cov=tests --cov=sentry_sdk --cov-report= --cov-branch
155-
- name: Test rediscluster pinned
155+
- name: Test redis_py_cluster_legacy pinned
156156
run: |
157157
set -x # print commands that are executed
158-
./scripts/runtox.sh --exclude-latest "py${{ matrix.python-version }}-rediscluster" --cov=tests --cov=sentry_sdk --cov-report= --cov-branch
158+
./scripts/runtox.sh --exclude-latest "py${{ matrix.python-version }}-redis_py_cluster_legacy" --cov=tests --cov=sentry_sdk --cov-report= --cov-branch
159159
- name: Test sqlalchemy pinned
160160
run: |
161161
set -x # print commands that are executed

scripts/split-tox-gh-actions/split-tox-gh-actions.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@
8282
"clickhouse_driver",
8383
"pymongo",
8484
"redis",
85-
"rediscluster",
85+
"redis_py_cluster_legacy",
8686
"sqlalchemy",
8787
],
8888
"GraphQL": [

sentry_sdk/consts.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -368,7 +368,7 @@ class SPANDATA:
368368
class OP:
369369
ANTHROPIC_MESSAGES_CREATE = "ai.messages.create.anthropic"
370370
CACHE_GET = "cache.get"
371-
CACHE_SET = "cache.set"
371+
CACHE_PUT = "cache.put"
372372
COHERE_CHAT_COMPLETIONS_CREATE = "ai.chat_completions.create.cohere"
373373
COHERE_EMBEDDINGS_CREATE = "ai.embeddings.create.cohere"
374374
DB = "db"

sentry_sdk/integrations/django/caching.py

Lines changed: 6 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import functools
22
from typing import TYPE_CHECKING
3+
from sentry_sdk.integrations.redis.utils import _get_safe_key
34
from urllib3.util import parse_url as urlparse
45

56
from django import VERSION as DJANGO_VERSION
@@ -8,7 +9,6 @@
89
import sentry_sdk
910
from sentry_sdk.consts import OP, SPANDATA
1011
from sentry_sdk.utils import (
11-
SENSITIVE_DATA_SUBSTITUTE,
1212
capture_internal_exceptions,
1313
ensure_integration_enabled,
1414
)
@@ -28,27 +28,9 @@
2828
]
2929

3030

31-
def _get_key(args, kwargs):
32-
# type: (list[Any], dict[str, Any]) -> str
33-
key = ""
34-
35-
if args is not None and len(args) >= 1:
36-
key = args[0]
37-
elif kwargs is not None and "key" in kwargs:
38-
key = kwargs["key"]
39-
40-
if isinstance(key, dict):
41-
# Do not leak sensitive data
42-
# `set_many()` has a dict {"key1": "value1", "key2": "value2"} as first argument.
43-
# Those values could include sensitive data so we replace them with a placeholder
44-
key = {x: SENSITIVE_DATA_SUBSTITUTE for x in key}
45-
46-
return str(key)
47-
48-
4931
def _get_span_description(method_name, args, kwargs):
50-
# type: (str, list[Any], dict[str, Any]) -> str
51-
return _get_key(args, kwargs)
32+
# type: (str, tuple[Any], dict[str, Any]) -> str
33+
return _get_safe_key(method_name, args, kwargs)
5234

5335

5436
def _patch_cache_method(cache, method_name, address, port):
@@ -61,11 +43,11 @@ def _patch_cache_method(cache, method_name, address, port):
6143
def _instrument_call(
6244
cache, method_name, original_method, args, kwargs, address, port
6345
):
64-
# type: (CacheHandler, str, Callable[..., Any], list[Any], dict[str, Any], Optional[str], Optional[int]) -> Any
46+
# type: (CacheHandler, str, Callable[..., Any], tuple[Any, ...], dict[str, Any], Optional[str], Optional[int]) -> Any
6547
is_set_operation = method_name.startswith("set")
6648
is_get_operation = not is_set_operation
6749

68-
op = OP.CACHE_SET if is_set_operation else OP.CACHE_GET
50+
op = OP.CACHE_PUT if is_set_operation else OP.CACHE_GET
6951
description = _get_span_description(method_name, args, kwargs)
7052

7153
with sentry_sdk.start_span(op=op, description=description) as span:
@@ -78,7 +60,7 @@ def _instrument_call(
7860
if port is not None:
7961
span.set_data(SPANDATA.NETWORK_PEER_PORT, port)
8062

81-
key = _get_key(args, kwargs)
63+
key = _get_safe_key(method_name, args, kwargs)
8264
if key != "":
8365
span.set_data(SPANDATA.CACHE_KEY, key)
8466

0 commit comments

Comments
 (0)