Skip to content

fix(seer): Use count(value) in tracemetrics query for metric metadata#113616

Merged
isaacwang-sentry merged 1 commit into
masterfrom
isaac/fix/seer-metric-metadata-count-aggregate
Apr 22, 2026
Merged

fix(seer): Use count(value) in tracemetrics query for metric metadata#113616
isaacwang-sentry merged 1 commit into
masterfrom
isaac/fix/seer-metric-metadata-count-aggregate

Conversation

@isaacwang-sentry

@isaacwang-sentry isaacwang-sentry commented Apr 21, 2026

Copy link
Copy Markdown
Member

Fix the get_metric_metadata RPC handler to use count(value) instead of zero-arg count() in its tracemetrics query.

The tracemetrics dataset parser requires count() to take an attribute argument. Zero-arg count() parse-fails at the events layer with status=400 body={'detail': 'Invalid number of arguments for count, was expecting 1 arguments', 'code': 'parse_error'}. Every call to this handler since it was merged in #113462 has been returning events_query_failed — the Seer assisted-query metrics agent has never once received real candidates from this tool and has been silently falling back to get_field_values probes instead.

The bug wasn't caught because the existing tests mocked sentry.api.client wholesale and never exercised the real parser. This PR also adds TestGetMetricMetadataIntegration, which persists real TraceItems and hits the handler end-to-end via TraceMetricsTestCase — this is the shape of test that would have caught the original bug, and will catch future regressions in the aggregate-function shape.

While here, the ApiError catch block now attaches status_code and a 500-char body prefix to its logger.exception extras so future flakes are debuggable from logs without a new deploy cycle.

Reproduction (before this PR, on prod):

POST /api/0/organizations/1/seer-rpc/get_metric_metadata/
{"args": {"org_id": 1, "project_ids": [1], "name_substrings": ["storage.put.size"], "stats_period": "7d", "limit": 20}}

returns {"candidates": [], "has_more": false, "error": "events_query_failed"} on every substring, 25/25 tries. After this PR the same request returns populated candidates.

Refs #113462

The get_metric_metadata RPC handler passed zero-arg count() as an aggregate
field on the tracemetrics dataset. The tracemetrics parser requires count()
to take an attribute argument, so every call since the handler merged has
returned events_query_failed from the events-layer parser. The bug wasn't
caught in CI because the existing tests mocked sentry.api.client wholesale
and never exercised the real parser.

Changes:
- Replace count() with count(value) in the field list, sort, and row lookup.
- Extend logger.exception extras with ApiError.status_code and a truncated
  body prefix so future flakes are debuggable from logs without a new deploy.
- Add TestGetMetricMetadataIntegration that persists real TraceItems and
  hits the handler end-to-end; this is the shape of test that would have
  caught the original bug.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
@github-actions github-actions Bot added the Scope: Backend Automatically applied to PRs that change backend components label Apr 21, 2026
@isaacwang-sentry
isaacwang-sentry marked this pull request as ready for review April 21, 2026 22:46
@isaacwang-sentry
isaacwang-sentry requested a review from a team as a code owner April 21, 2026 22:46
@isaacwang-sentry
isaacwang-sentry merged commit 65e775c into master Apr 22, 2026
59 checks passed
@isaacwang-sentry
isaacwang-sentry deleted the isaac/fix/seer-metric-metadata-count-aggregate branch April 22, 2026 16:49
@github-actions github-actions Bot locked and limited conversation to collaborators May 8, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

Scope: Backend Automatically applied to PRs that change backend components

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants