feat(asyncpg): Add cursor span support via BaseCursor method patching#6252
feat(asyncpg): Add cursor span support via BaseCursor method patching#6252ericapisani wants to merge 3 commits into
Conversation
Patch BaseCursor._bind_exec, _bind, and _exec instead of wrapping Connection.cursor creation. This creates spans for each cursor operation (iteration via _bind_exec, and explicit cursor usage via _bind/_exec), and supports both static and streaming span lifecycles. Fixes PY-2408 Fixes #6240
Codecov Results 📊✅ 13 passed | Total: 13 | Pass Rate: 100% | Execution Time: 16.55s 📊 Comparison with Base Branch
✨ No test changes detected All tests are passing successfully. ❌ Patch coverage is 0.00%. Project has 15158 uncovered lines. Files with missing lines (1)
Coverage diff@@ Coverage Diff @@
## main #PR +/-##
==========================================
- Coverage 31.28% 31.27% -0.01%
==========================================
Files 190 190 —
Lines 22052 22055 +3
Branches 7408 7410 +2
==========================================
+ Hits 6897 6897 —
- Misses 15155 15158 +3
- Partials 583 583 —Generated by Codecov Action |
|
bugbot run |
|
@sentry review |
There was a problem hiding this comment.
✅ Bugbot reviewed your changes and found no new issues!
Comment @cursor review or bugbot run to trigger another review on this PR
Reviewed by Cursor Bugbot for commit b81ac97. Configure here.
| asyncpg.Connection._executemany = _wrap_connection_method( | ||
| asyncpg.Connection._executemany, executemany=True | ||
| ) | ||
| asyncpg.Connection.cursor = _wrap_cursor_creation(asyncpg.Connection.cursor) |
There was a problem hiding this comment.
This is safe to remove because this patches the function that led to the creation/return of a CursorFactory. When that cursor is awaited, the CursorFactory calls _bind through the underlying cursor's init method, essentially capturing the creation and invocation of the cursor (as opposed to just capturing the creation of the factory which is what was happening previously)
|
|
||
|
|
||
| @pytest.mark.asyncio | ||
| async def test_cursor__bind_and__exec_methods_create_spans( |
There was a problem hiding this comment.
I did my best to create some distinction between this and test_cursor__bind_exec_creates_spans but in case this naming causes confusion:
This test tests the _bind and _exec patches, whereas the other test above checks the _bind_exec patch.
Patch
BaseCursor._bind_exec,_bind, and_execinstead of wrappingConnection.cursorcreation. The old approach only captured cursor creation, not actual query execution. The new approach creates spans for each cursor operation (iteration via_bind_exec, explicit cursor usage via_bind/_exec) and supports both static and streaming span lifecycles.Builds on #6215 which added streaming span support to the asyncpg integration.
Removes the patch on
asyncpg.Connection.cursorbecause the patches onBaseCursorintroduced in this pull request will create the correct span when the cursor is used.Fixes PY-2408
Fixes #6240