Skip to content

Commit c758f7f

Browse files
committed
set untruncated redis commands as the query text attribute. remove cache key attribute for now
1 parent c5cfd87 commit c758f7f

4 files changed

Lines changed: 53 additions & 15 deletions

File tree

sentry_sdk/integrations/redis/_async_common.py

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
)
1010
from sentry_sdk.integrations.redis.modules.queries import _compile_db_span_properties
1111
from sentry_sdk.integrations.redis.utils import (
12+
_get_safe_command,
1213
_set_client_data,
1314
_set_pipeline_data,
1415
)
@@ -109,6 +110,12 @@ async def _sentry_execute_command(
109110
integration,
110111
)
111112

113+
additional_cache_span_attributes = {}
114+
with capture_internal_exceptions():
115+
additional_cache_span_attributes[SPANDATA.DB_QUERY_TEXT] = (
116+
_get_safe_command(name, args)
117+
)
118+
112119
cache_span: "Optional[Union[Span, StreamedSpan]]" = None
113120
if cache_properties["is_cache_key"] and cache_properties["op"] is not None:
114121
if span_streaming:
@@ -117,7 +124,7 @@ async def _sentry_execute_command(
117124
attributes={
118125
"sentry.op": cache_properties["op"],
119126
"sentry.origin": SPAN_ORIGIN,
120-
SPANDATA.CACHE_KEY: cache_properties["description"],
127+
**additional_cache_span_attributes,
121128
},
122129
)
123130
else:
@@ -130,14 +137,20 @@ async def _sentry_execute_command(
130137

131138
db_properties = _compile_db_span_properties(integration, name, args)
132139

140+
additional_db_span_attributes = {}
141+
with capture_internal_exceptions():
142+
additional_db_span_attributes[SPANDATA.DB_QUERY_TEXT] = _get_safe_command(
143+
name, args
144+
)
145+
133146
db_span: "Union[Span, StreamedSpan]"
134147
if span_streaming:
135148
db_span = sentry_sdk.traces.start_span(
136149
name=db_properties["description"],
137150
attributes={
138151
"sentry.op": db_properties["op"],
139152
"sentry.origin": SPAN_ORIGIN,
140-
SPANDATA.DB_QUERY_TEXT: db_properties["description"],
153+
**additional_db_span_attributes,
141154
},
142155
)
143156
else:

sentry_sdk/integrations/redis/_sync_common.py

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
)
1010
from sentry_sdk.integrations.redis.modules.queries import _compile_db_span_properties
1111
from sentry_sdk.integrations.redis.utils import (
12+
_get_safe_command,
1213
_set_client_data,
1314
_set_pipeline_data,
1415
)
@@ -108,6 +109,12 @@ def sentry_patched_execute_command(
108109
integration,
109110
)
110111

112+
additional_cache_span_attributes = {}
113+
with capture_internal_exceptions():
114+
additional_cache_span_attributes[SPANDATA.DB_QUERY_TEXT] = (
115+
_get_safe_command(name, args)
116+
)
117+
111118
cache_span: "Optional[Union[Span, StreamedSpan]]" = None
112119
if cache_properties["is_cache_key"] and cache_properties["op"] is not None:
113120
if span_streaming:
@@ -116,7 +123,7 @@ def sentry_patched_execute_command(
116123
attributes={
117124
"sentry.op": cache_properties["op"],
118125
"sentry.origin": SPAN_ORIGIN,
119-
SPANDATA.CACHE_KEY: cache_properties["description"],
126+
**additional_cache_span_attributes,
120127
},
121128
)
122129
else:
@@ -129,14 +136,20 @@ def sentry_patched_execute_command(
129136

130137
db_properties = _compile_db_span_properties(integration, name, args)
131138

139+
additional_db_span_attributes = {}
140+
with capture_internal_exceptions():
141+
additional_db_span_attributes[SPANDATA.DB_QUERY_TEXT] = _get_safe_command(
142+
name, args
143+
)
144+
132145
db_span: "Union[Span, StreamedSpan]"
133146
if span_streaming:
134147
db_span = sentry_sdk.traces.start_span(
135148
name=db_properties["description"],
136149
attributes={
137150
"sentry.op": db_properties["op"],
138151
"sentry.origin": SPAN_ORIGIN,
139-
SPANDATA.DB_QUERY_TEXT: db_properties["description"],
152+
**additional_db_span_attributes,
140153
},
141154
)
142155
else:

tests/integrations/redis/test_redis.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -336,10 +336,16 @@ def test_data_truncation_custom(
336336

337337
assert parent["name"] == "custom parent"
338338
assert set1["name"] == expected_long
339-
assert set1["attributes"][SPANDATA.DB_QUERY_TEXT] == expected_long
339+
assert (
340+
set1["attributes"][SPANDATA.DB_QUERY_TEXT]
341+
== f"SET 'somekey1' '{long_string}'"
342+
)
340343
assert set1["attributes"]["sentry.op"] == "db.redis"
341344
assert set2["name"] == expected_short
342-
assert set2["attributes"][SPANDATA.DB_QUERY_TEXT] == expected_short
345+
assert (
346+
set2["attributes"][SPANDATA.DB_QUERY_TEXT]
347+
== f"SET 'somekey2' '{short_string}'"
348+
)
343349
else:
344350
events = capture_events()
345351
with start_transaction():

tests/integrations/redis/test_redis_cache_module.py

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -83,28 +83,34 @@ def test_cache_basic(sentry_init, capture_events, capture_items, span_streaming)
8383
assert payloads[1]["attributes"]["sentry.op"] == "db.redis"
8484
assert payloads[1]["attributes"][SPANDATA.DB_OPERATION_NAME] == "GET"
8585
assert payloads[2]["attributes"]["sentry.op"] == "cache.get"
86-
assert payloads[2]["attributes"][SPANDATA.CACHE_KEY] == ["mycachekey"]
86+
assert payloads[2]["attributes"][SPANDATA.DB_QUERY_TEXT] == "GET 'mycachekey'"
8787

8888
# set: db then cache.put
8989
assert payloads[3]["attributes"]["sentry.op"] == "db.redis"
9090
assert payloads[3]["attributes"][SPANDATA.DB_OPERATION_NAME] == "SET"
9191
assert payloads[4]["attributes"]["sentry.op"] == "cache.put"
92-
assert payloads[4]["attributes"][SPANDATA.CACHE_KEY] == ["mycachekey1"]
92+
assert (
93+
payloads[4]["attributes"][SPANDATA.DB_QUERY_TEXT]
94+
== "SET 'mycachekey1' [Filtered]"
95+
)
9396

9497
# setex: db then cache.put
9598
assert payloads[5]["attributes"]["sentry.op"] == "db.redis"
9699
assert payloads[5]["attributes"][SPANDATA.DB_OPERATION_NAME] == "SETEX"
97100
assert payloads[6]["attributes"]["sentry.op"] == "cache.put"
98-
assert payloads[6]["attributes"][SPANDATA.CACHE_KEY] == ["mycachekey2"]
101+
assert (
102+
payloads[6]["attributes"][SPANDATA.DB_QUERY_TEXT]
103+
== "SETEX 'mycachekey2' [Filtered] [Filtered]"
104+
)
99105

100106
# mget: db then cache.get
101107
assert payloads[7]["attributes"]["sentry.op"] == "db.redis"
102108
assert payloads[7]["attributes"][SPANDATA.DB_OPERATION_NAME] == "MGET"
103109
assert payloads[8]["attributes"]["sentry.op"] == "cache.get"
104-
assert payloads[8]["attributes"][SPANDATA.CACHE_KEY] == [
105-
"mycachekey1",
106-
"mycachekey2",
107-
]
110+
assert (
111+
payloads[8]["attributes"][SPANDATA.DB_QUERY_TEXT]
112+
== "MGET 'mycachekey1' [Filtered]"
113+
)
108114

109115
assert payloads[9]["name"] == "custom parent"
110116
else:
@@ -176,14 +182,14 @@ def test_cache_keys(sentry_init, capture_events, capture_items, span_streaming):
176182
assert payloads[1]["name"] == "GET 'blub'"
177183
assert payloads[2]["attributes"]["sentry.op"] == "cache.get"
178184
assert payloads[2]["name"] == "blub"
179-
assert payloads[2]["attributes"][SPANDATA.CACHE_KEY] == ["blub"]
185+
assert payloads[2]["attributes"][SPANDATA.DB_QUERY_TEXT] == "GET 'blub'"
180186

181187
# blubkeything: db then cache.get
182188
assert payloads[3]["attributes"]["sentry.op"] == "db.redis"
183189
assert payloads[3]["name"] == "GET 'blubkeything'"
184190
assert payloads[4]["attributes"]["sentry.op"] == "cache.get"
185191
assert payloads[4]["name"] == "blubkeything"
186-
assert payloads[4]["attributes"][SPANDATA.CACHE_KEY] == ["blubkeything"]
192+
assert payloads[4]["attributes"][SPANDATA.DB_QUERY_TEXT] == "GET 'blubkeything'"
187193

188194
# bl: db only (no prefix match)
189195
assert payloads[5]["attributes"]["sentry.op"] == "db.redis"

0 commit comments

Comments
 (0)