feat: send public alias in stats endpoint#104346
Conversation
src/sentry/snuba/spans_rpc.py
Outdated
| continue | ||
|
|
||
| for bucket in attribute.buckets: | ||
| name = translate_internal_to_public_alias(attribute.attribute_name) |
There was a problem hiding this comment.
Bug: Missing required parameters in translate_internal_to_public_alias call will raise a TypeError.
Severity: CRITICAL | Confidence: High
🔍 Detailed Analysis
The translate_internal_to_public_alias function is invoked at src/sentry/snuba/spans_rpc.py, line 333, with only one argument (attribute.attribute_name). However, its definition requires three parameters: internal_alias, type, and item_type. This mismatch will result in a TypeError at runtime when the run_stats_query method is called with "attributeDistributions" in stats_types, specifically during iteration over attribute.buckets. This unhandled exception will cause the process handling the request to terminate.
💡 Suggested Fix
Provide the missing type and item_type arguments to translate_internal_to_public_alias. Additionally, unpack the three-element tuple returned by the function, for example: name, _, _ = translate_internal_to_public_alias(attribute.attribute_name, "string", SupportedTraceItemType.SPANS).
🤖 Prompt for AI Agent
Review the code at the location below. A potential bug has been identified by an AI
agent.
Verify if this is a real issue. If it is, propose a fix; if not, explain why it's not
valid.
Location: src/sentry/snuba/spans_rpc.py#L333
Potential issue: The `translate_internal_to_public_alias` function is invoked at
`src/sentry/snuba/spans_rpc.py`, line 333, with only one argument
(`attribute.attribute_name`). However, its definition requires three parameters:
`internal_alias`, `type`, and `item_type`. This mismatch will result in a `TypeError` at
runtime when the `run_stats_query` method is called with `"attributeDistributions"` in
`stats_types`, specifically during iteration over `attribute.buckets`. This unhandled
exception will cause the process handling the request to terminate.
Did we get this right? 👍 / 👎 to inform future reviews.
Reference ID: 5308109
src/sentry/snuba/spans_rpc.py
Outdated
| name = translate_internal_to_public_alias( | ||
| attribute.attribute_name, "string", SupportedTraceItemType.SPANS | ||
| ) |
There was a problem hiding this comment.
Bug: The name variable receives a tuple from translate_internal_to_public_alias but is used as a string for the "label" key.
Severity: CRITICAL | Confidence: High
🔍 Detailed Analysis
The name variable is assigned the full tuple returned by translate_internal_to_public_alias, which is tuple[str | None, str | None, AttributeSource]. This tuple is then directly used as the value for the "label" key in a dictionary. Since the API expects a string for "label", this will lead to JSON serialization failures when the response is generated, breaking the API contract and causing runtime errors.
💡 Suggested Fix
Unpack the tuple returned by translate_internal_to_public_alias to extract the desired string value for name. For example, change name = translate_internal_to_public_alias(...) to name, _, _ = translate_internal_to_public_alias(...).
🤖 Prompt for AI Agent
Review the code at the location below. A potential bug has been identified by an AI
agent.
Verify if this is a real issue. If it is, propose a fix; if not, explain why it's not
valid.
Location: src/sentry/snuba/spans_rpc.py#L333-L335
Potential issue: The `name` variable is assigned the full tuple returned by
`translate_internal_to_public_alias`, which is `tuple[str | None, str | None,
AttributeSource]`. This tuple is then directly used as the value for the `"label"` key
in a dictionary. Since the API expects a string for `"label"`, this will lead to JSON
serialization failures when the response is generated, breaking the API contract and
causing runtime errors.
Did we get this right? 👍 / 👎 to inform future reviews.
Reference ID: 5309255
src/sentry/snuba/spans_rpc.py
Outdated
| name = translate_internal_to_public_alias( | ||
| attribute.attribute_name, "string", SupportedTraceItemType.SPANS | ||
| ) | ||
| attrs[name].append({"label": bucket.label, "value": bucket.value}) |
There was a problem hiding this comment.
Bug: The return value of translate_internal_to_public_alias is not unpacked, causing name to be a tuple instead of a string.
Severity: CRITICAL | Confidence: High
🔍 Detailed Analysis
The translate_internal_to_public_alias function returns a tuple, but its return value is not unpacked when assigned to name in src/sentry/snuba/spans_rpc.py:333-336. This causes name to be a tuple, not a string, when used as a dictionary key in attrs[name]. As a result, the attrs dictionary will contain tuple keys instead of string keys. The API response will then include malformed attribute distribution data, as JSON cannot represent Python tuples, leading to failures or corrupted data for downstream consumers.
💡 Suggested Fix
Unpack the tuple returned by translate_internal_to_public_alias by changing the assignment to name, _, _ = translate_internal_to_public_alias(...).
🤖 Prompt for AI Agent
Review the code at the location below. A potential bug has been identified by an AI
agent.
Verify if this is a real issue. If it is, propose a fix; if not, explain why it's not
valid.
Location: src/sentry/snuba/spans_rpc.py#L333-L336
Potential issue: The `translate_internal_to_public_alias` function returns a tuple, but
its return value is not unpacked when assigned to `name` in
`src/sentry/snuba/spans_rpc.py:333-336`. This causes `name` to be a tuple, not a string,
when used as a dictionary key in `attrs[name]`. As a result, the `attrs` dictionary will
contain tuple keys instead of string keys. The API response will then include malformed
attribute distribution data, as JSON cannot represent Python tuples, leading to failures
or corrupted data for downstream consumers.
Did we get this right? 👍 / 👎 to inform future reviews.
Reference ID: 5310058
| attribute.attribute_name, "string", SupportedTraceItemType.SPANS | ||
| ) | ||
| public_alias = public_alias or attribute.attribute_name | ||
| attrs[public_alias].append({"label": bucket.label, "value": bucket.value}) |
There was a problem hiding this comment.
Bug: Translation computed inside inner loop instead of outer
The translate_internal_to_public_alias call is placed inside the for bucket in attribute.buckets loop, but it only depends on attribute.attribute_name which doesn't change between buckets. This causes the same translation to be computed redundantly up to 75 times per attribute (once per bucket). Moving lines 333-336 outside the bucket loop and before it would compute the public_alias once per attribute instead.
No description provided.