Skip to content

Commit 2bee953

Browse files
committed
caches
1 parent 263859c commit 2bee953

1 file changed

Lines changed: 15 additions & 9 deletions

File tree

  • sentry_sdk/integrations/redis/modules

sentry_sdk/integrations/redis/modules/caches.py

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
from sentry_sdk.consts import OP, SPANDATA
66
from sentry_sdk.integrations.redis.utils import _get_safe_key, _key_as_string
7+
from sentry_sdk.traces import StreamedSpan
78
from sentry_sdk.utils import capture_internal_exceptions
89

910
GET_COMMANDS = ("get", "mget")
@@ -14,7 +15,7 @@
1415
if TYPE_CHECKING:
1516
from sentry_sdk.integrations.redis import RedisIntegration
1617
from sentry_sdk.tracing import Span
17-
from typing import Any, Optional
18+
from typing import Any, Optional, Union
1819

1920

2021
def _get_op(name: str) -> "Optional[str]":
@@ -80,25 +81,30 @@ def _get_cache_span_description(
8081

8182

8283
def _set_cache_data(
83-
span: "Span",
84+
span: "Union[Span, StreamedSpan]",
8485
redis_client: "Any",
8586
properties: "dict[str, Any]",
8687
return_value: "Optional[Any]",
8788
) -> None:
89+
if isinstance(span, StreamedSpan):
90+
set_on_span = span.set_attribute
91+
else:
92+
set_on_span = span.set_data
93+
8894
with capture_internal_exceptions():
89-
span.set_data(SPANDATA.CACHE_KEY, properties["key"])
95+
set_on_span(SPANDATA.CACHE_KEY, properties["key"])
9096

9197
if properties["redis_command"] in GET_COMMANDS:
9298
if return_value is not None:
93-
span.set_data(SPANDATA.CACHE_HIT, True)
99+
set_on_span(SPANDATA.CACHE_HIT, True)
94100
size = (
95101
len(str(return_value).encode("utf-8"))
96102
if not isinstance(return_value, bytes)
97103
else len(return_value)
98104
)
99-
span.set_data(SPANDATA.CACHE_ITEM_SIZE, size)
105+
set_on_span(SPANDATA.CACHE_ITEM_SIZE, size)
100106
else:
101-
span.set_data(SPANDATA.CACHE_HIT, False)
107+
set_on_span(SPANDATA.CACHE_HIT, False)
102108

103109
elif properties["redis_command"] in SET_COMMANDS:
104110
if properties["value"] is not None:
@@ -107,7 +113,7 @@ def _set_cache_data(
107113
if not isinstance(properties["value"], bytes)
108114
else len(properties["value"])
109115
)
110-
span.set_data(SPANDATA.CACHE_ITEM_SIZE, size)
116+
set_on_span(SPANDATA.CACHE_ITEM_SIZE, size)
111117

112118
try:
113119
connection_params = redis_client.connection_pool.connection_kwargs
@@ -122,8 +128,8 @@ def _set_cache_data(
122128

123129
host = connection_params.get("host")
124130
if host is not None:
125-
span.set_data(SPANDATA.NETWORK_PEER_ADDRESS, host)
131+
set_on_span(SPANDATA.NETWORK_PEER_ADDRESS, host)
126132

127133
port = connection_params.get("port")
128134
if port is not None:
129-
span.set_data(SPANDATA.NETWORK_PEER_PORT, port)
135+
set_on_span(SPANDATA.NETWORK_PEER_PORT, port)

0 commit comments

Comments
 (0)