-
Notifications
You must be signed in to change notification settings - Fork 635
feat(redis): Set db.query.text 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
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 |
|---|---|---|
|
|
@@ -139,6 +139,7 @@ | |
|
|
||
| assert parent_span["name"] == "custom parent" | ||
| assert redis_span["name"] == "GET [Filtered]" | ||
| assert redis_span["attributes"][SPANDATA.DB_QUERY_TEXT] == "GET [Filtered]" | ||
| assert redis_span["attributes"]["sentry.op"] == "db.redis" | ||
| else: | ||
| events = capture_events() | ||
|
|
@@ -177,8 +178,10 @@ | |
|
|
||
| assert parent["name"] == "custom parent" | ||
| assert set1["name"] == "SET 'somekey1' [Filtered]" | ||
| assert set1["attributes"][SPANDATA.DB_QUERY_TEXT] == "SET 'somekey1' [Filtered]" | ||
| assert set1["attributes"]["sentry.op"] == "db.redis" | ||
| assert set2["name"] == "SET 'somekey2' [Filtered]" | ||
| assert set2["attributes"][SPANDATA.DB_QUERY_TEXT] == "SET 'somekey2' [Filtered]" | ||
| assert get["name"] == "GET 'somekey2'" | ||
| assert delete["name"] == "DEL 'somekey1' [Filtered]" | ||
| else: | ||
|
|
@@ -223,8 +226,16 @@ | |
|
|
||
| assert parent["name"] == "custom parent" | ||
| assert set1["name"] == "SET 'somekey1' 'my secret string1'" | ||
| assert ( | ||
| set1["attributes"][SPANDATA.DB_QUERY_TEXT] | ||
| == "SET 'somekey1' 'my secret string1'" | ||
| ) | ||
| assert set1["attributes"]["sentry.op"] == "db.redis" | ||
| assert set2["name"] == "SET 'somekey2' 'my secret string2'" | ||
| assert ( | ||
| set2["attributes"][SPANDATA.DB_QUERY_TEXT] | ||
| == "SET 'somekey2' 'my secret string2'" | ||
| ) | ||
| assert get["name"] == "GET 'somekey2'" | ||
| assert delete["name"] == "DEL 'somekey1' 'somekey2'" | ||
| else: | ||
|
|
@@ -271,8 +282,16 @@ | |
|
|
||
| assert parent["name"] == "custom parent" | ||
| assert set1["name"] == f"SET 'somekey1' '{long_string}'" | ||
| assert ( | ||
| set1["attributes"][SPANDATA.DB_QUERY_TEXT] | ||
| == f"SET 'somekey1' '{long_string}'" | ||
| ) | ||
| assert set1["attributes"]["sentry.op"] == "db.redis" | ||
| assert set2["name"] == f"SET 'somekey2' '{short_string}'" | ||
| assert ( | ||
| set2["attributes"][SPANDATA.DB_QUERY_TEXT] | ||
| == f"SET 'somekey2' '{short_string}'" | ||
| ) | ||
| else: | ||
| events = capture_events() | ||
| with start_transaction(): | ||
|
|
@@ -317,8 +336,16 @@ | |
|
|
||
| assert parent["name"] == "custom parent" | ||
| assert set1["name"] == expected_long | ||
| assert ( | ||
| set1["attributes"][SPANDATA.DB_QUERY_TEXT] | ||
| == f"SET 'somekey1' '{long_string}'" | ||
| ) | ||
| assert set1["attributes"]["sentry.op"] == "db.redis" | ||
| assert set2["name"] == expected_short | ||
|
Check warning on line 344 in tests/integrations/redis/test_redis.py
|
||
|
Comment on lines
+339
to
344
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.
In the span-streaming path, Evidence
Identified by Warden code-review · JGL-PYX |
||
| assert ( | ||
| set2["attributes"][SPANDATA.DB_QUERY_TEXT] | ||
| == f"SET 'somekey2' '{short_string}'" | ||
| ) | ||
| else: | ||
| events = capture_events() | ||
| with start_transaction(): | ||
|
|
@@ -401,6 +428,7 @@ | |
| assert redis_span["name"] == "GET 'foobar'" | ||
| attrs = redis_span["attributes"] | ||
| assert attrs["sentry.op"] == "db.redis" | ||
| assert attrs[SPANDATA.DB_QUERY_TEXT] == "GET 'foobar'" | ||
| assert attrs[SPANDATA.DB_SYSTEM_NAME] == "redis" | ||
| assert attrs[SPANDATA.DB_DRIVER_NAME] == "redis-py" | ||
| assert attrs[SPANDATA.DB_NAMESPACE] == "1" | ||
|
|
@@ -508,6 +536,9 @@ | |
| assert parent_span["name"] == "custom parent" | ||
| assert parent_span["attributes"]["sentry.origin"] == "manual" | ||
| assert set_span["attributes"]["sentry.origin"] == "auto.db.redis" | ||
| assert ( | ||
| set_span["attributes"][SPANDATA.DB_QUERY_TEXT] == "SET 'somekey' [Filtered]" | ||
| ) | ||
| assert pipeline_span["attributes"]["sentry.origin"] == "auto.db.redis" | ||
| else: | ||
| events = capture_events() | ||
|
|
||
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.
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.
I think this comment still needs to be addressed @ericapisani
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.
This change was to address your earlier comment re: setting the full redis command without truncation as an attribute.
As part of this, I reversed the earlier change I had made introducing the
cache.keyattribute, so this bot comment is coming up because I forgot to update the title/description of the PRThere 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.
Cool another case of the bots being confused 😞. Approved now!