-
Notifications
You must be signed in to change notification settings - Fork 630
feat(redis): Set db.query.text and cache.key span attributes #6639
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,14 +1,15 @@ | ||
| from typing import TYPE_CHECKING | ||
|
|
||
| import sentry_sdk | ||
| from sentry_sdk.consts import OP | ||
| from sentry_sdk.consts import OP, SPANDATA | ||
| from sentry_sdk.integrations.redis.consts import SPAN_ORIGIN | ||
| from sentry_sdk.integrations.redis.modules.caches import ( | ||
| _compile_cache_span_properties, | ||
| _set_cache_data, | ||
| ) | ||
| from sentry_sdk.integrations.redis.modules.queries import _compile_db_span_properties | ||
| from sentry_sdk.integrations.redis.utils import ( | ||
| _get_safe_command, | ||
| _set_client_data, | ||
| _set_pipeline_data, | ||
| ) | ||
|
|
@@ -108,6 +109,12 @@ def sentry_patched_execute_command( | |
| integration, | ||
| ) | ||
|
|
||
| additional_cache_span_attributes = {} | ||
| with capture_internal_exceptions(): | ||
| additional_cache_span_attributes[SPANDATA.DB_QUERY_TEXT] = ( | ||
| _get_safe_command(name, args) | ||
| ) | ||
|
|
||
| cache_span: "Optional[Union[Span, StreamedSpan]]" = None | ||
| if cache_properties["is_cache_key"] and cache_properties["op"] is not None: | ||
| if span_streaming: | ||
|
|
@@ -116,6 +123,7 @@ def sentry_patched_execute_command( | |
| attributes={ | ||
| "sentry.op": cache_properties["op"], | ||
| "sentry.origin": SPAN_ORIGIN, | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Bug: The cache span is initialized with Suggested FixModify the span creation to use the correct property from the start. Instead of setting Prompt for AI AgentAlso affects:
Did we get this right? 👍 / 👎 to inform future reviews. |
||
| **additional_cache_span_attributes, | ||
| }, | ||
| ) | ||
| else: | ||
|
|
@@ -128,13 +136,20 @@ def sentry_patched_execute_command( | |
|
|
||
| db_properties = _compile_db_span_properties(integration, name, args) | ||
|
|
||
| additional_db_span_attributes = {} | ||
| with capture_internal_exceptions(): | ||
| additional_db_span_attributes[SPANDATA.DB_QUERY_TEXT] = _get_safe_command( | ||
| name, args | ||
| ) | ||
|
|
||
| db_span: "Union[Span, StreamedSpan]" | ||
| if span_streaming: | ||
| db_span = sentry_sdk.traces.start_span( | ||
| name=db_properties["description"], | ||
| attributes={ | ||
| "sentry.op": db_properties["op"], | ||
| "sentry.origin": SPAN_ORIGIN, | ||
| **additional_db_span_attributes, | ||
| }, | ||
| ) | ||
| else: | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -83,21 +83,34 @@ | |
| assert payloads[1]["attributes"]["sentry.op"] == "db.redis" | ||
| assert payloads[1]["attributes"][SPANDATA.DB_OPERATION_NAME] == "GET" | ||
| assert payloads[2]["attributes"]["sentry.op"] == "cache.get" | ||
| assert payloads[2]["attributes"][SPANDATA.DB_QUERY_TEXT] == "GET 'mycachekey'" | ||
|
|
||
| # set: db then cache.put | ||
| assert payloads[3]["attributes"]["sentry.op"] == "db.redis" | ||
| assert payloads[3]["attributes"][SPANDATA.DB_OPERATION_NAME] == "SET" | ||
| assert payloads[4]["attributes"]["sentry.op"] == "cache.put" | ||
| assert ( | ||
| payloads[4]["attributes"][SPANDATA.DB_QUERY_TEXT] | ||
| == "SET 'mycachekey1' [Filtered]" | ||
| ) | ||
|
|
||
| # setex: db then cache.put | ||
| assert payloads[5]["attributes"]["sentry.op"] == "db.redis" | ||
| assert payloads[5]["attributes"][SPANDATA.DB_OPERATION_NAME] == "SETEX" | ||
| assert payloads[6]["attributes"]["sentry.op"] == "cache.put" | ||
| assert ( | ||
| payloads[6]["attributes"][SPANDATA.DB_QUERY_TEXT] | ||
| == "SETEX 'mycachekey2' [Filtered] [Filtered]" | ||
| ) | ||
|
|
||
| # mget: db then cache.get | ||
| assert payloads[7]["attributes"]["sentry.op"] == "db.redis" | ||
| assert payloads[7]["attributes"][SPANDATA.DB_OPERATION_NAME] == "MGET" | ||
| assert payloads[8]["attributes"]["sentry.op"] == "cache.get" | ||
| assert ( | ||
| payloads[8]["attributes"][SPANDATA.DB_QUERY_TEXT] | ||
| == "MGET 'mycachekey1' [Filtered]" | ||
| ) | ||
|
|
||
| assert payloads[9]["name"] == "custom parent" | ||
| else: | ||
|
|
@@ -168,13 +181,15 @@ | |
| assert payloads[1]["attributes"]["sentry.op"] == "db.redis" | ||
| assert payloads[1]["name"] == "GET 'blub'" | ||
| assert payloads[2]["attributes"]["sentry.op"] == "cache.get" | ||
| assert payloads[2]["name"] == "blub" | ||
| assert payloads[2]["attributes"][SPANDATA.DB_QUERY_TEXT] == "GET 'blub'" | ||
|
|
||
| # blubkeything: db then cache.get | ||
| assert payloads[3]["attributes"]["sentry.op"] == "db.redis" | ||
| assert payloads[3]["name"] == "GET 'blubkeything'" | ||
| assert payloads[4]["attributes"]["sentry.op"] == "cache.get" | ||
| assert payloads[4]["name"] == "blubkeything" | ||
|
Check warning on line 191 in tests/integrations/redis/test_redis_cache_module.py
|
||
|
Comment on lines
184
to
191
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. cache.get spans incorrectly assert These test assertions validate the wrong span attribute on Evidence
Also found at 1 additional location
Identified by Warden find-bugs · PTH-4W9 |
||
| assert payloads[4]["attributes"][SPANDATA.DB_QUERY_TEXT] == "GET 'blubkeything'" | ||
|
|
||
| # bl: db only (no prefix match) | ||
| assert payloads[5]["attributes"]["sentry.op"] == "db.redis" | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Cache spans use db.query.text
Medium Severity
For span streaming, cache spans merge
additional_cache_span_attributesusingSPANDATA.DB_QUERY_TEXTand_get_safe_command, but this change was meant to populatecache.keyon cache spans. That mislabelscache.get/cache.putspans with a database query attribute and does not addcache.keyat span creation (only later via_set_cache_data).Additional Locations (1)
sentry_sdk/integrations/redis/_async_common.py#L114-L117Reviewed by Cursor Bugbot for commit c758f7f. Configure here.