fix(asyncpg): Use distinct span ops for cursor iteration and fetch to prevent N+1 false positives#6609
Open
ericapisani wants to merge 3 commits into
Open
Conversation
… prevent N+1 false positives When asyncpg iterates a server-side cursor (via CursorIterator.__anext__) or fetches rows in batches (via Cursor.fetch), each batch triggers BaseCursor._bind_exec or BaseCursor._exec, producing a burst of identical spans. Sentry's N+1 detector incorrectly flags these as N+1 queries. Set `sentry.op` to `db.cursor.iter` for CursorIterator-driven spans and `db.cursor.fetch` for Cursor.fetch-driven spans so the backend can exclude them from N+1 detection. Adds span_op_override_value parameter to record_sql_queries to propagate the override without changing existing callers. This will require changes on the backend in order to not trigger n+1 alerts on these ops. Fixes #6576 Fixes PY-2534
Contributor
Codecov Results 📊✅ 92048 passed | ⏭️ 6140 skipped | Total: 98188 | Pass Rate: 93.75% | Execution Time: 322m 24s 📊 Comparison with Base Branch
All tests are passing successfully. ✅ Patch coverage is 93.33%. Project has 2414 uncovered lines. Files with missing lines (1)
Coverage diff@@ Coverage Diff @@
## main #PR +/-##
==========================================
+ Coverage 89.85% 89.85% —%
==========================================
Files 192 192 —
Lines 23745 23774 +29
Branches 8198 8206 +8
==========================================
+ Hits 21334 21360 +26
- Misses 2411 2414 +3
- Partials 1344 1348 +4Generated by Codecov Action |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
When asyncpg iterates a server-side cursor (via CursorIterator.anext) or fetches rows in batches (via Cursor.fetch), each batch triggers BaseCursor._bind_exec or BaseCursor._exec, producing a burst of identical spans. Sentry's N+1 detector incorrectly flags these as N+1 queries.
Set
sentry.optodb.cursor.iterfor CursorIterator-driven spans anddb.cursor.fetchfor Cursor.fetch-driven spans so the backend can exclude them from N+1 detection.Adds span_op_override_value parameter to record_sql_queries to propagate the override without changing existing callers.
This will require changes on the backend in order to not trigger n+1 alerts on these ops.
Fixes #6576
Fixes PY-2534