fix(performance): exclude db.cursor spans from N+1 DB detection#118031
Open
ericapisani wants to merge 3 commits into
Open
fix(performance): exclude db.cursor spans from N+1 DB detection#118031ericapisani wants to merge 3 commits into
ericapisani wants to merge 3 commits into
Conversation
Cursor iteration (e.g. asyncpg BaseCursor._exec on each cursor.fetch()) produces N spans with the original SQL as their description. These are sequential FETCH calls draining a pre-existing server-side cursor, not N independently issued queries — equivalent to how db.redis and db.connection are already excluded. Paired SDK change: sentry-python asyncpg integration will tag cursor fetch spans with op='db.cursor.fetch' instead of the generic 'db' op. Co-Authored-By: sentry-junior[bot] <264270552+sentry-junior[bot]@users.noreply.github.com>
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.
Summary
Cursor iteration in asyncpg (and other DB drivers that support server-side cursors) produces N spans that share the original SQL as their description. Each
cursor.fetch()andcursor.__anext__calls trigger aBaseCursor._execinvocation, which the SDK wraps as adbspan — so the detector sees a source span followed by N identical repeating spans and fires a false-positive N+1 alert.These are sequential calls draining a pre-existing server-side cursor result set, not N independently issued queries. They should be excluded from N+1 detection the same way
db.redisanddb.connectionalready are.Changes
n_plus_one_db_span_detector.py: addnot op.startswith("db.cursor")to_is_db_op, consistent with existingdb.redisanddb.connectionexclusions.test_n_plus_one_db_span_detector.py: adds tests for the 2 new operation names introduced that start withdb.cursorfollowing the pattern of thepg-pool.connectand cached-query tests.Paired SDK change
The sentry-python asyncpg integration needs to tag cursor fetch spans with
op='db.cursor.fetch'instead of the generic'db'op. That change will be tracked in getsentry/sentry-python.See getsentry/sentry-python#6609
Context
cursor-n+1-reproin ep-starlette project on sentry-sdks orgView Session in Sentry