Skip to content

Commit

Permalink
fix: typing for redis integration
Browse files Browse the repository at this point in the history
  • Loading branch information
md384 committed Dec 4, 2023
1 parent 3e6e9b6 commit c868892
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 10 deletions.
15 changes: 10 additions & 5 deletions sentry_sdk/integrations/redis/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -141,15 +141,15 @@ def _set_db_data_on_span(span, connection_params):


def _set_db_data(span, redis_instance):
# type: (Span, Redis) -> None
# type: (Span, Redis[Any]) -> None
try:
_set_db_data_on_span(span, redis_instance.connection_pool.connection_kwargs)
except AttributeError:
pass # connections_kwargs may be missing in some cases

Check warning on line 148 in sentry_sdk/integrations/redis/__init__.py

View check run for this annotation

Codecov / codecov/patch

sentry_sdk/integrations/redis/__init__.py#L147-L148

Added lines #L147 - L148 were not covered by tests


def _set_cluster_db_data(span, redis_cluster_instance):
# type: (Span, RedisCluster) -> None
# type: (Span, RedisCluster[Any]) -> None
default_node = redis_cluster_instance.get_default_node()
if default_node is not None:
_set_db_data_on_span(
Expand All @@ -158,15 +158,20 @@ def _set_cluster_db_data(span, redis_cluster_instance):


def _set_async_cluster_db_data(span, async_redis_cluster_instance):
# type: (Span, AsyncRedisCluster) -> None
# type: (Span, AsyncRedisCluster[Any]) -> None
default_node = async_redis_cluster_instance.get_default_node()
if default_node is not None and default_node.connection_kwargs is not None:
_set_db_data_on_span(span, default_node.connection_kwargs)


def _set_async_cluster_pipeline_db_data(span, async_redis_cluster_pipeline_instance):
# type: (Span, AsyncClusterPipeline) -> None
_set_async_cluster_db_data(span, async_redis_cluster_pipeline_instance._client)
# type: (Span, AsyncClusterPipeline[Any]) -> None
_set_async_cluster_db_data(
span,
# the AsyncClusterPipeline has always had a `_client` attr but it is private so potentially problematic and mypy
# does not recognize it - see https://github.com/redis/redis-py/blame/v5.0.0/redis/asyncio/cluster.py#L1386
async_redis_cluster_pipeline_instance._client, # type: ignore[attr-defined]
)


def patch_redis_pipeline(pipeline_cls, is_cluster, get_command_args_fn, set_db_data_fn):
Expand Down
12 changes: 7 additions & 5 deletions sentry_sdk/integrations/redis/asyncio.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,15 @@

if TYPE_CHECKING:
from collections.abc import Callable
from typing import Any
from typing import Any, Union
from redis.asyncio.client import Pipeline, StrictRedis
from redis.asyncio.cluster import ClusterPipeline, RedisCluster

Check warning on line 19 in sentry_sdk/integrations/redis/asyncio.py

View check run for this annotation

Codecov / codecov/patch

sentry_sdk/integrations/redis/asyncio.py#L16-L19

Added lines #L16 - L19 were not covered by tests


def patch_redis_async_pipeline(
pipeline_cls, is_cluster, get_command_args_fn, set_db_data_fn
):
# type: (type, bool, Any, Callable[[Span, Any], None]) -> None
# type: (Union[type[Pipeline[Any]], type[ClusterPipeline[Any]]], bool, Any, Callable[[Span, Any], None]) -> None
old_execute = pipeline_cls.execute

async def _sentry_execute(self, *args, **kwargs):
Expand All @@ -45,11 +47,11 @@ async def _sentry_execute(self, *args, **kwargs):

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

pipeline_cls.execute = _sentry_execute
pipeline_cls.execute = _sentry_execute # type: ignore[method-assign]


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

async def _sentry_execute_command(self, name, *args, **kwargs):
Expand All @@ -67,4 +69,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
cls.execute_command = _sentry_execute_command # type: ignore[method-assign]

0 comments on commit c868892

Please sign in to comment.