Skip to content

Commit 619a95a

Browse files
committed
feat(redis): Patch rediscluster if present
In addition to the redis and rb clients try to patch also the rediscluster library which does not use the already patched clients.
1 parent 7d482b5 commit 619a95a

File tree

1 file changed

+25
-1
lines changed

1 file changed

+25
-1
lines changed

sentry_sdk/integrations/redis.py

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from __future__ import absolute_import
22

33
from sentry_sdk import Hub
4-
from sentry_sdk.utils import capture_internal_exceptions
4+
from sentry_sdk.utils import capture_internal_exceptions, logger
55
from sentry_sdk.integrations import Integration
66

77
from sentry_sdk._types import MYPY
@@ -15,6 +15,25 @@
1515
_MULTI_KEY_COMMANDS = frozenset(["del", "touch", "unlink"])
1616

1717

18+
def _patch_rediscluster():
19+
# type: () -> None
20+
try:
21+
import rediscluster
22+
except ImportError:
23+
return
24+
25+
patch_redis_client(rediscluster.RedisCluster)
26+
27+
# up to v1.3.6, __version__ attribute is a tuple
28+
# from v2.0.0, __version__ is a string and VERSION a tuple
29+
version = getattr(rediscluster, "VERSION", rediscluster.__version__)[0]
30+
31+
# StrictRedisCluster was introduced in v0.2.0 and removed in v2.0.0
32+
# https://github.com/Grokzen/redis-py-cluster/blob/master/docs/release-notes.rst
33+
if (0, 2, 0) < version < (2, 0, 0):
34+
patch_redis_client(rediscluster.StrictRedisCluster)
35+
36+
1837
class RedisIntegration(Integration):
1938
identifier = "redis"
2039

@@ -34,6 +53,11 @@ def setup_once():
3453
patch_redis_client(rb.clients.MappingClient)
3554
patch_redis_client(rb.clients.RoutingClient)
3655

56+
try:
57+
_patch_rediscluster()
58+
except Exception:
59+
logger.exception("Error occured while patching `rediscluster` library")
60+
3761

3862
def patch_redis_client(cls):
3963
# type: (Any) -> None

0 commit comments

Comments
 (0)