From a39081c17524ed250331b3213675125a460c26a8 Mon Sep 17 00:00:00 2001 From: Ivana Kellyerova Date: Tue, 19 Mar 2024 15:12:11 +0100 Subject: [PATCH 1/3] ref(redis): Update to use new API --- sentry_sdk/integrations/redis/__init__.py | 21 +++++++-------------- 1 file changed, 7 insertions(+), 14 deletions(-) diff --git a/sentry_sdk/integrations/redis/__init__.py b/sentry_sdk/integrations/redis/__init__.py index d1178525b7..45f8653e29 100644 --- a/sentry_sdk/integrations/redis/__init__.py +++ b/sentry_sdk/integrations/redis/__init__.py @@ -1,4 +1,4 @@ -from sentry_sdk import Hub +import sentry_sdk from sentry_sdk.consts import OP, SPANDATA from sentry_sdk.hub import _should_send_default_pii from sentry_sdk.integrations import Integration, DidNotEnable @@ -6,6 +6,7 @@ from sentry_sdk.utils import ( SENSITIVE_DATA_SUBSTITUTE, capture_internal_exceptions, + ensure_integration_enabled, logger, ) @@ -176,14 +177,10 @@ def patch_redis_pipeline(pipeline_cls, is_cluster, get_command_args_fn, set_db_d # type: (Any, bool, Any, Callable[[Span, Any], None]) -> None old_execute = pipeline_cls.execute + @ensure_integration_enabled(RedisIntegration, old_execute) def sentry_patched_execute(self, *args, **kwargs): # type: (Any, *Any, **Any) -> Any - hub = Hub.current - - if hub.get_integration(RedisIntegration) is None: - return old_execute(self, *args, **kwargs) - - with hub.start_span( + with sentry_sdk.start_span( op=OP.DB_REDIS, description="redis.pipeline.execute" ) as span: with capture_internal_exceptions(): @@ -209,14 +206,10 @@ def patch_redis_client(cls, is_cluster, set_db_data_fn): """ old_execute_command = cls.execute_command + @ensure_integration_enabled(RedisIntegration, old_execute_command) def sentry_patched_execute_command(self, name, *args, **kwargs): # type: (Any, str, *Any, **Any) -> Any - hub = Hub.current - integration = hub.get_integration(RedisIntegration) - - if integration is None: - return old_execute_command(self, name, *args, **kwargs) - + integration = sentry_sdk.get_client().get_integration(RedisIntegration) description = _get_span_description(name, *args) data_should_be_truncated = ( @@ -225,7 +218,7 @@ def sentry_patched_execute_command(self, name, *args, **kwargs): if data_should_be_truncated: description = description[: integration.max_data_size - len("...")] + "..." - with hub.start_span(op=OP.DB_REDIS, description=description) as span: + with sentry_sdk.start_span(op=OP.DB_REDIS, description=description) as span: set_db_data_fn(span, self) _set_client_data(span, is_cluster, name, *args) From 1787c305d3b857484283282fa539afa5e343c80b Mon Sep 17 00:00:00 2001 From: Ivana Kellyerova Date: Tue, 19 Mar 2024 15:16:14 +0100 Subject: [PATCH 2/3] update asyncio redis --- sentry_sdk/integrations/redis/asyncio.py | 23 +++++++++-------------- 1 file changed, 9 insertions(+), 14 deletions(-) diff --git a/sentry_sdk/integrations/redis/asyncio.py b/sentry_sdk/integrations/redis/asyncio.py index 9a9083dda0..777f5a2b9e 100644 --- a/sentry_sdk/integrations/redis/asyncio.py +++ b/sentry_sdk/integrations/redis/asyncio.py @@ -1,4 +1,4 @@ -from sentry_sdk import Hub +import sentry_sdk from sentry_sdk.consts import OP from sentry_sdk.integrations.redis import ( RedisIntegration, @@ -8,7 +8,10 @@ ) from sentry_sdk._types import TYPE_CHECKING from sentry_sdk.tracing import Span -from sentry_sdk.utils import capture_internal_exceptions +from sentry_sdk.utils import ( + capture_internal_exceptions, + ensure_integration_enabled_async, +) if TYPE_CHECKING: from collections.abc import Callable @@ -23,14 +26,10 @@ def patch_redis_async_pipeline( # type: (Union[type[Pipeline[Any]], type[ClusterPipeline[Any]]], bool, Any, Callable[[Span, Any], None]) -> None old_execute = pipeline_cls.execute + @ensure_integration_enabled_async(RedisIntegration, old_execute) async def _sentry_execute(self, *args, **kwargs): # type: (Any, *Any, **Any) -> Any - hub = Hub.current - - if hub.get_integration(RedisIntegration) is None: - return await old_execute(self, *args, **kwargs) - - with hub.start_span( + with sentry_sdk.start_span( op=OP.DB_REDIS, description="redis.pipeline.execute" ) as span: with capture_internal_exceptions(): @@ -52,16 +51,12 @@ def patch_redis_async_client(cls, is_cluster, set_db_data_fn): # type: (Union[type[StrictRedis[Any]], type[RedisCluster[Any]]], bool, Callable[[Span, Any], None]) -> None old_execute_command = cls.execute_command + @ensure_integration_enabled_async(RedisIntegration, old_execute_command) async def _sentry_execute_command(self, name, *args, **kwargs): # type: (Any, str, *Any, **Any) -> Any - hub = Hub.current - - if hub.get_integration(RedisIntegration) is None: - return await old_execute_command(self, name, *args, **kwargs) - description = _get_span_description(name, *args) - with hub.start_span(op=OP.DB_REDIS, description=description) as span: + with sentry_sdk.start_span(op=OP.DB_REDIS, description=description) as span: set_db_data_fn(span, self) _set_client_data(span, is_cluster, name, *args) From 2df30d97a2447966fce155428ea85d2aa865fc2e Mon Sep 17 00:00:00 2001 From: Ivana Kellyerova Date: Tue, 19 Mar 2024 15:22:35 +0100 Subject: [PATCH 3/3] mypy --- sentry_sdk/integrations/redis/asyncio.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/sentry_sdk/integrations/redis/asyncio.py b/sentry_sdk/integrations/redis/asyncio.py index 777f5a2b9e..227e3fa85c 100644 --- a/sentry_sdk/integrations/redis/asyncio.py +++ b/sentry_sdk/integrations/redis/asyncio.py @@ -44,14 +44,14 @@ async def _sentry_execute(self, *args, **kwargs): return await old_execute(self, *args, **kwargs) - pipeline_cls.execute = _sentry_execute # type: ignore[method-assign] + pipeline_cls.execute = _sentry_execute # type: ignore def patch_redis_async_client(cls, is_cluster, set_db_data_fn): # type: (Union[type[StrictRedis[Any]], type[RedisCluster[Any]]], bool, Callable[[Span, Any], None]) -> None old_execute_command = cls.execute_command - @ensure_integration_enabled_async(RedisIntegration, old_execute_command) + @ensure_integration_enabled_async(RedisIntegration, old_execute_command) # type: ignore async def _sentry_execute_command(self, name, *args, **kwargs): # type: (Any, str, *Any, **Any) -> Any description = _get_span_description(name, *args) @@ -62,4 +62,4 @@ async def _sentry_execute_command(self, name, *args, **kwargs): return await old_execute_command(self, name, *args, **kwargs) - cls.execute_command = _sentry_execute_command # type: ignore[method-assign] + cls.execute_command = _sentry_execute_command # type: ignore