Skip to content

feat(weekly-reports): Add data layer for "Top Issues" section - #119174

Closed
amy-chen23 wants to merge 9 commits into
masterfrom
amyc/worktree-surface-top-issues
Closed

feat(weekly-reports): Add data layer for "Top Issues" section#119174
amy-chen23 wants to merge 9 commits into
masterfrom
amyc/worktree-surface-top-issues

Conversation

@amy-chen23

@amy-chen23 amy-chen23 commented Jul 8, 2026

Copy link
Copy Markdown
Contributor

Resolves backend of ID-1645

Changes

  • Replace project_key_errors() and project_key_performance_issues() Snuba queries with a single search.backend.query() call per organization
  • query top 5 issues (instead of top 3)
  • Gated behind organizations:weekly-report-recommended-sort and organizations:weekly-report-top-issues FF

@github-actions github-actions Bot added the Scope: Backend Automatically applied to PRs that change backend components label Jul 8, 2026
@amy-chen23
amy-chen23 marked this pull request as ready for review July 8, 2026 15:57
@amy-chen23
amy-chen23 requested review from a team as code owners July 8, 2026 15:57
Comment thread src/sentry/tasks/summaries/utils.py Outdated
Comment thread src/sentry/tasks/summaries/utils.py Outdated
Comment thread src/sentry/tasks/summaries/utils.py Outdated
Comment thread src/sentry/tasks/summaries/utils.py Outdated
@amy-chen23 amy-chen23 changed the title feat(weekly-reports): Add "Top Issues" section feat(weekly-reports): Add data layer for "Top Issues" section Jul 8, 2026
@amy-chen23
amy-chen23 marked this pull request as draft July 8, 2026 16:44
@amy-chen23
amy-chen23 marked this pull request as ready for review July 8, 2026 17:25
@amy-chen23
amy-chen23 marked this pull request as draft July 9, 2026 22:12
@linear-code

linear-code Bot commented Jul 9, 2026

Copy link
Copy Markdown

ID-1669

@amy-chen23
amy-chen23 force-pushed the amyc/worktree-surface-top-issues branch from 0b9c556 to f7f7a57 Compare July 10, 2026 22:08
Comment thread src/sentry/tasks/summaries/organization_report_context_factory.py Outdated
@amy-chen23
amy-chen23 marked this pull request as ready for review July 13, 2026 17:52
Comment thread src/sentry/features/temporary.py Outdated
# Use batched Snuba queries for weekly report key errors instead of per-project queries
manager.add("organizations:weekly-report-batched-key-errors", OrganizationFeature, FeatureHandlerStrategy.FLAGPOLE, api_expose=False)
# Use recommended sort instead of frequency sort for weekly report top issues
manager.add("organizations:weekly-report-recommended-sort", OrganizationFeature, FeatureHandlerStrategy.FLAGPOLE, api_expose=False)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

i thought we were sticking with frequency for now and moving to recommended sort in the future?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

was thinking about using recommended sort for sentry, but i can remove this flag and use frequency for all

if features.has("organizations:weekly-report-top-issues", organization):
self._append_project_key_issues_new(ctx)
else:
self._append_project_key_issues_legacy(ctx)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

tbh it might be fine to just remove the legacy version entirely and roll out the new version to everyone

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

yea, the only concern was about performance, but it seems like using search.backend.query() would actually be better for performance

per_project_key_errors = project_key_errors(
ctx, project, referrer=Referrer.REPORTS_KEY_ERRORS.value
try:
errors_by_project, perf_by_project = org_top_issues(

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

would recommend renaming this here and in the org_top_issues function so it's clear these are issues, not errors, i.e. error_issues_by_project and perf_issues_by_project might be better

f"Expected a ProjectContext, received {type(project_ctx)}"
)
if project.id in errors_by_project:
project_ctx.key_errors_by_id = errors_by_project[project.id]

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

as a follow-up can we also rename this in the context and bring it into line with the performance issues name, e.g. ctx.key_error_issues and ctx.key_performance_issues?

@amy-chen23 amy-chen23 Jul 13, 2026

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

yea i will put renaming into a separate follow-up PR

Comment thread src/sentry/tasks/summaries/utils.py Outdated
op = "weekly_reports.org_top_issues"
with start_span(op=op, name=op):
limit = min(3 * len(projects), 1000)
result = search.backend.query(

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

i don't think it should be necessary to query Postgres first to get the groups themselves, i think we can just issue the below Snuba queries below and group on the group ID

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

agreed. purpose of postgres first was to use rec sort, but since we're using sort_by=freq for all, i can just use snuba queries

@amy-chen23
amy-chen23 force-pushed the amyc/worktree-surface-top-issues branch from 77f61e9 to 1efa0b2 Compare July 13, 2026 23:57

@cursor cursor Bot left a comment

Copy link
Copy Markdown
Contributor

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.

Want reviews to match your repository better? Bugbot Learning can learn team-specific rules from PR activity. A team admin can enable Learning in the Cursor dashboard.

Reviewed by Cursor Bugbot for commit 1efa0b2. Configure here.

Comment thread src/sentry/tasks/summaries/organization_report_context_factory.py
@amy-chen23
amy-chen23 marked this pull request as draft July 14, 2026 00:10
Comment thread src/sentry/tasks/summaries/utils.py Outdated
"""
op = "weekly_reports.org_top_issues"
with start_span(op=op, name=op):
limit = min(3 * len(projects), 1000)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Bug: The org_top_issues function fetches a globally sorted list of top issues instead of enforcing a per-project limit. High-volume projects can exhaust the global limit, preventing other projects from getting their top issues.
Severity: HIGH

Suggested Fix

Reintroduce a mechanism to enforce a per-project limit within the search query, similar to the previous implementation's use of a LimitBy clause. This will ensure that the top issues are fetched for each project individually, guaranteeing that each project receives its top 5 issues as intended by the function's contract.

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/tasks/summaries/utils.py#L149

Potential issue: The `org_top_issues` function is intended to fetch the top 5 issues for
each project. However, it queries for a global list of top issues across all projects,
sorted by frequency, with a limit calculated as `min(3 * len(projects), 1000)`. It then
iterates this list to collect up to 5 issues per project. If one project has a high
volume of frequent issues, it can dominate this global list. This can cause the function
to exhaust the result set before finding the top issues for lower-volume projects,
leading to them receiving fewer than 5 issues or none at all. This breaks the function's
documented behavior, as the previous implementation's `LimitBy` clause, which enforced
per-project limits, was removed.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

setting to min(5 * len(projects), 1000) instead

@amy-chen23
amy-chen23 force-pushed the amyc/worktree-surface-top-issues branch from fe5fc6f to 263b62d Compare July 14, 2026 17:31
Comment thread src/sentry/tasks/summaries/organization_report_context_factory.py
Comment thread src/sentry/tasks/summaries/utils.py
@amy-chen23

Copy link
Copy Markdown
Contributor Author

closing b/c the file diff is too big

@amy-chen23 amy-chen23 closed this Jul 14, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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