Skip to content

ref(redis): Use new scopes API #2854

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
Mar 20, 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
21 changes: 7 additions & 14 deletions sentry_sdk/integrations/redis/__init__.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
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
from sentry_sdk._types import TYPE_CHECKING
from sentry_sdk.utils import (
SENSITIVE_DATA_SUBSTITUTE,
capture_internal_exceptions,
ensure_integration_enabled,
logger,
)

Expand Down Expand Up @@ -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():
Expand All @@ -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 = (
Expand All @@ -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)

Expand Down
27 changes: 11 additions & 16 deletions sentry_sdk/integrations/redis/asyncio.py
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -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
Expand All @@ -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():
Expand All @@ -45,26 +44,22 @@ 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) # type: ignore
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)

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