An engineer used a coding agent connected to Sentry MCP to inspect Sentry’s own traces and find backend performance problems. The agent sorted spans by execution frequency and duration, then returned candidate fixes with a file and line, estimated weekly volume, and an example trace. Django query spans included SQL as their description, while query-source attributes supplied file and line and the user_agent attribute exposed a sentry-cli set-commits workload paging through roughly 16k repositories every 40 seconds. For SQL changes, the agent ran EXPLAIN ANALYZE before and after on a read-only database mirror and compared results. Fixes included reusing a loaded project to remove an estimated 143M weekly SELECTs, rewriting a GroupHistory lookup to probe each status separately, and eager-loading Sentry app relations; the GroupHistory query fell from 935ms to 0.4ms with a warm cache.
Context
Sentry’s backend is largely Django, and its Django SDK instrumentation emits query spans whose descriptions contain SQL. Traces also include cache calls, cross-silo RPCs, tasks, and API routes. The engineer connected Claude with Opus 5.5 to Sentry MCP and pointed it at Sentry’s own organization and project, starting from a small performance PR and then asking broad questions about critical paths and database-heavy issues. The agent’s candidate list included file and line, estimated weekly volume, and an example trace, and every PR linked a trace showing the extra query or call.
Approach / What changed
The agent was connected to Sentry MCP and pointed at Sentry’s own organization and project. It sorted spans by how often they ran and how long they took, grouped spans by description, and used query-source attributes to identify file and line. For each candidate it supplied an estimated weekly volume and an example trace. The engineer then had it read the code and git history, assess risk, and validate SQL changes with EXPLAIN ANALYZE before and after on a read-only database mirror. If the fix held up, the agent opened a draft PR, with the example trace linked for reviewers.
Takeaways
1
Rewriting the GroupHistory lookup from one ORDER BY … LIMIT query across several statuses to one probe per status with UNION ALL and taking the max in Python cut the busiest group on the mirror from 935ms to 0.4ms with a warm cache.
2
Reusing the already loaded project in two hot paths removed an estimated 143M extra SELECTs a week; the two occurrence-ingestion fixes moved save_issue_occurrence p50 from about 35ms to 30ms and p95 from about 95ms to 61ms versus the same hours the previous day.
3
Eager-loading the application and token for Sentry app requests reduced two extra selects from about 1.9% to 0.16% of query volume on the control database, roughly 29M fewer queries a day.
FAQ
How did the agent validate SQL query changes before opening a PR?
It ran EXPLAIN ANALYZE before and after on a read-only database mirror and compared the results. The engineer also checked request latency and database load in Sentry to judge how much a removed query mattered.
How did the agent turn trace data into a ranked list of expensive work?
It sorted spans by how often they ran and how long they took, and grouped spans by description. Django query spans carried SQL as their description, and query-source attributes supplied the file and line that ran a query.
What did the GroupHistory query change do?
The original query asked for the newest row across several statuses with ORDER BY date_added DESC LIMIT 1, so Postgres read and sorted every matching row. The replacement ran one probe per status, took the max in Python, and combined the results with UNION ALL; on the busiest group it went from 935ms to 0.4ms with a warm cache.
Agent assist is AI that feeds a human agent suggestions, knowledge, and wrap-up work during a live conversation. This diagram shows the loop that decides what runs, when, and who stays in control.
A five-checkpoint loop for chip-design teams: pin reference artifacts, trace multistage agent runs, refine tool descriptions, and re-run a composite benchmark so outputs are verified, not merely plausible.
Keep reading
Related reading
The Good Stuff
Paid picks we use ourselves. Links carry a referral code — a subscription through one earns us a commission at no extra cost to you.