Skip to content

feat(eap): Sort trace item attribute names by frequency#8062

Merged
phacops merged 8 commits into
masterfrom
claude/youthful-carson-p024mw
Jun 19, 2026
Merged

feat(eap): Sort trace item attribute names by frequency#8062
phacops merged 8 commits into
masterfrom
claude/youthful-carson-p024mw

Conversation

@phacops

@phacops phacops commented Jun 18, 2026

Copy link
Copy Markdown
Contributor

Adds opt-in frequency sorting to TraceItemAttributeNamesRequest, so callers can request sort:-count() without changing the default order for everyone (EAP-432).

Behavior

  • order_by unset / COLUMN_NAME → the existing distinct, name-ascending result. Unchanged for all current consumers (this is what keeps downstream sentry tests green).
  • order_by.column = COLUMN_COUNT → group by key, count() occurrences, order by count (direction from descending) then name ASC, and populate the new optional Attribute.count field in the response.

This replaces the earlier approach on this branch, which changed the default order and broke order-pinned tests in getsentry/sentry (e.g. test_tags_list_nums, test_pagination).

Blocked on sentry-protos

Depends on sentry-protos 0.31.2 (getsentry/sentry-protos#316), which adds TraceItemAttributeNamesRequest.OrderBy { Column column; bool descending } and TraceItemAttributeNamesResponse.Attribute.count.

Before CI can pass / this can leave draft:

  1. sentry-protos 0.31.2 published.
  2. Bump the pin in pyproject.toml + regenerate uv.lock, and (if needed) rust_snuba/Cargo.toml + Cargo.lock.

Until then this PR is intentionally a draft and its CI will be red (the new proto symbols don't exist in the pinned version yet).

Refs EAP-432

🤖 Generated with Claude Code

https://claude.ai/code/session_0123HwN9no4ce3knzFWh9feV

Return attribute keys from TraceItemAttributeNamesRequest sorted by
frequency (count) descending, with name ascending as a tiebreaker, so
high-cardinality / commonly-used attributes surface first.

The co-occurring attributes query now groups by attr_key and counts
occurrences instead of using distinct(), and orders by count DESC,
attr_key ASC. Synthetic non-stored attributes are assigned an infinite
count so they continue to sort first. This mirrors the pattern used by
the attribute values endpoint.

Refs EAP-432

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_0123HwN9no4ce3knzFWh9feV
@phacops phacops requested review from a team as code owners June 18, 2026 12:23
@linear-code

linear-code Bot commented Jun 18, 2026

Copy link
Copy Markdown

EAP-432

phacops added a commit to getsentry/sentry that referenced this pull request Jun 18, 2026
## Summary

`OrganizationTraceItemAttributesEndpointSpansTest::test_tags_list_nums`
asserted the response with an exact, order-sensitive list comparison
(`assert response.data == [...]`). The number-attributes endpoint does
not guarantee a stable ordering of returned values, so the test should
assert that all expected values are present rather than depending on
their order.

This surfaced via snuba PR getsentry/snuba#8062, which changes the
default sort.

## Change

Switched the assertion to the order-independent pattern already used by
neighboring tests in this file — sorting both sides by `key`:

```python
assert sorted(response.data, key=itemgetter("key")) == sorted(
    [ ...expected values... ],
    key=itemgetter("key"),
)
```

`itemgetter` was already imported and this idiom matches the surrounding
tests (`test_pagination`, etc.).

## Test Plan

- `ruff format --check` and `ruff check` pass on the modified file.
- Assertion now verifies all expected number attributes are present
regardless of order.

https://claude.ai/code/session_01WLKmLXRfRyJpW9u8zEGsy1

---
_Generated by [Claude
Code](https://claude.ai/code/session_01WLKmLXRfRyJpW9u8zEGsy1)_

Co-authored-by: Claude <noreply@anthropic.com>
@phacops phacops marked this pull request as draft June 18, 2026 14:13
claude and others added 4 commits June 18, 2026 14:23
Instead of changing the default ordering of TraceItemAttributeNamesRequest
(which reorders results for every consumer and broke downstream sentry tests),
honor the new opt-in order_by field from sentry-protos:

- order_by unset / COLUMN_NAME: the existing distinct, name-ascending result,
  unchanged for current consumers.
- order_by.column = COLUMN_COUNT: group by key, count occurrences, order by
  count (direction from `descending`) then name ASC, and populate the new
  optional Attribute.count field in the response.

Gated on sentry-protos 0.31.2, which adds TraceItemAttributeNamesRequest.OrderBy
and Attribute.count. The pyproject/uv.lock and rust_snuba Cargo pins must be
bumped to that release before CI can pass; until then this branch stays a draft.

Refs EAP-432
The v1 co-occurring table's count() is a row count (approximate, not the
literal per-item span count), so assert that high-frequency keys outrank
low-frequency ones rather than pinning an exact value. Ordering behavior
is unchanged; exact per-item counts come with the v2 storage follow-up.
@phacops phacops marked this pull request as ready for review June 18, 2026 21:22
Comment thread snuba/web/rpc/v1/endpoint_trace_item_attribute_names.py
Seer review caught that the in-memory sort used reverse=descending with an
infinite-count sentinel, so under ascending count ordering the synthetic
non-stored attribute (sentry.service) sorted last instead of first. Now the
real counted rows are sorted in the requested direction and the non-stored
attributes are prepended unconditionally, which also keeps the sentinel out
of the response. Adds a regression test covering both directions.
…son-p024mw

# Conflicts:
#	snuba/web/rpc/v1/endpoint_trace_item_attribute_names.py

@cursor cursor Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cursor Bugbot has reviewed your changes and found 1 potential issue.

Fix All in Cursor

❌ Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, enable autofix in the Cursor dashboard.

Reviewed by Cursor Bugbot for commit 4b59a9c. Configure here.

Comment thread snuba/web/rpc/v1/endpoint_trace_item_attribute_names.py Outdated
…g results

Cursor Bugbot caught that the in-memory re-sort in
convert_co_occurring_results_to_attributes always sorted by name ascending,
undoing a COLUMN_NAME + descending request that the ClickHouse query had
ordered DESC. Make the re-sort direction-aware via a shared
_order_by_name_descending helper (also used by the query builder) and add a
regression test for descending name ordering.
@phacops phacops merged commit 4fc21de into master Jun 19, 2026
67 checks passed
@phacops phacops deleted the claude/youthful-carson-p024mw branch June 19, 2026 03:52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants