Skip to content

Commit 27d5ee1

Browse files
authored
ref(redis): Use new scopes API (getsentry#2854)
1 parent 37d07f0 commit 27d5ee1

2 files changed

Lines changed: 18 additions & 30 deletions

File tree

sentry_sdk/integrations/redis/__init__.py

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
1-
from sentry_sdk import Hub
1+
import sentry_sdk
22
from sentry_sdk.consts import OP, SPANDATA
33
from sentry_sdk.hub import _should_send_default_pii
44
from sentry_sdk.integrations import Integration, DidNotEnable
55
from sentry_sdk._types import TYPE_CHECKING
66
from sentry_sdk.utils import (
77
SENSITIVE_DATA_SUBSTITUTE,
88
capture_internal_exceptions,
9+
ensure_integration_enabled,
910
logger,
1011
)
1112

@@ -176,14 +177,10 @@ def patch_redis_pipeline(pipeline_cls, is_cluster, get_command_args_fn, set_db_d
176177
# type: (Any, bool, Any, Callable[[Span, Any], None]) -> None
177178
old_execute = pipeline_cls.execute
178179

180+
@ensure_integration_enabled(RedisIntegration, old_execute)
179181
def sentry_patched_execute(self, *args, **kwargs):
180182
# type: (Any, *Any, **Any) -> Any
181-
hub = Hub.current
182-
183-
if hub.get_integration(RedisIntegration) is None:
184-
return old_execute(self, *args, **kwargs)
185-
186-
with hub.start_span(
183+
with sentry_sdk.start_span(
187184
op=OP.DB_REDIS, description="redis.pipeline.execute"
188185
) as span:
189186
with capture_internal_exceptions():
@@ -209,14 +206,10 @@ def patch_redis_client(cls, is_cluster, set_db_data_fn):
209206
"""
210207
old_execute_command = cls.execute_command
211208

209+
@ensure_integration_enabled(RedisIntegration, old_execute_command)
212210
def sentry_patched_execute_command(self, name, *args, **kwargs):
213211
# type: (Any, str, *Any, **Any) -> Any
214-
hub = Hub.current
215-
integration = hub.get_integration(RedisIntegration)
216-
217-
if integration is None:
218-
return old_execute_command(self, name, *args, **kwargs)
219-
212+
integration = sentry_sdk.get_client().get_integration(RedisIntegration)
220213
description = _get_span_description(name, *args)
221214

222215
data_should_be_truncated = (
@@ -225,7 +218,7 @@ def sentry_patched_execute_command(self, name, *args, **kwargs):
225218
if data_should_be_truncated:
226219
description = description[: integration.max_data_size - len("...")] + "..."
227220

228-
with hub.start_span(op=OP.DB_REDIS, description=description) as span:
221+
with sentry_sdk.start_span(op=OP.DB_REDIS, description=description) as span:
229222
set_db_data_fn(span, self)
230223
_set_client_data(span, is_cluster, name, *args)
231224

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from sentry_sdk import Hub
1+
import sentry_sdk
22
from sentry_sdk.consts import OP
33
from sentry_sdk.integrations.redis import (
44
RedisIntegration,
@@ -8,7 +8,10 @@
88
)
99
from sentry_sdk._types import TYPE_CHECKING
1010
from sentry_sdk.tracing import Span
11-
from sentry_sdk.utils import capture_internal_exceptions
11+
from sentry_sdk.utils import (
12+
capture_internal_exceptions,
13+
ensure_integration_enabled_async,
14+
)
1215

1316
if TYPE_CHECKING:
1417
from collections.abc import Callable
@@ -23,14 +26,10 @@ def patch_redis_async_pipeline(
2326
# type: (Union[type[Pipeline[Any]], type[ClusterPipeline[Any]]], bool, Any, Callable[[Span, Any], None]) -> None
2427
old_execute = pipeline_cls.execute
2528

29+
@ensure_integration_enabled_async(RedisIntegration, old_execute)
2630
async def _sentry_execute(self, *args, **kwargs):
2731
# type: (Any, *Any, **Any) -> Any
28-
hub = Hub.current
29-
30-
if hub.get_integration(RedisIntegration) is None:
31-
return await old_execute(self, *args, **kwargs)
32-
33-
with hub.start_span(
32+
with sentry_sdk.start_span(
3433
op=OP.DB_REDIS, description="redis.pipeline.execute"
3534
) as span:
3635
with capture_internal_exceptions():
@@ -45,26 +44,22 @@ async def _sentry_execute(self, *args, **kwargs):
4544

4645
return await old_execute(self, *args, **kwargs)
4746

48-
pipeline_cls.execute = _sentry_execute # type: ignore[method-assign]
47+
pipeline_cls.execute = _sentry_execute # type: ignore
4948

5049

5150
def patch_redis_async_client(cls, is_cluster, set_db_data_fn):
5251
# type: (Union[type[StrictRedis[Any]], type[RedisCluster[Any]]], bool, Callable[[Span, Any], None]) -> None
5352
old_execute_command = cls.execute_command
5453

54+
@ensure_integration_enabled_async(RedisIntegration, old_execute_command) # type: ignore
5555
async def _sentry_execute_command(self, name, *args, **kwargs):
5656
# type: (Any, str, *Any, **Any) -> Any
57-
hub = Hub.current
58-
59-
if hub.get_integration(RedisIntegration) is None:
60-
return await old_execute_command(self, name, *args, **kwargs)
61-
6257
description = _get_span_description(name, *args)
6358

64-
with hub.start_span(op=OP.DB_REDIS, description=description) as span:
59+
with sentry_sdk.start_span(op=OP.DB_REDIS, description=description) as span:
6560
set_db_data_fn(span, self)
6661
_set_client_data(span, is_cluster, name, *args)
6762

6863
return await old_execute_command(self, name, *args, **kwargs)
6964

70-
cls.execute_command = _sentry_execute_command # type: ignore[method-assign]
65+
cls.execute_command = _sentry_execute_command # type: ignore

0 commit comments

Comments
 (0)