-
Notifications
You must be signed in to change notification settings - Fork 612
feat(asyncpg): Add cursor span support via BaseCursor method patching #6252
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+221
−15
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
6406ae7
feat(asyncpg): Add cursor span support via BaseCursor method patching
ericapisani b81ac97
Merge branch 'master' into py-2408-asyncpg-cursor-span-support
ericapisani 65c07eb
Merge branch 'master' into py-2408-asyncpg-cursor-span-support
ericapisani 72c0455
Update tests/integrations/asyncpg/test_asyncpg.py
ericapisani File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -342,7 +342,7 @@ async def test_cursor(sentry_init, capture_events) -> None: | |
| {"category": "query", "data": {}, "message": "BEGIN;", "type": "default"}, | ||
| { | ||
| "category": "query", | ||
| "data": {}, | ||
| "data": {"db.cursor": mock.ANY}, | ||
| "message": "SELECT * FROM users WHERE dob > $1", | ||
| "type": "default", | ||
| }, | ||
|
|
@@ -400,7 +400,19 @@ async def test_cursor_manual(sentry_init, capture_events) -> None: | |
| {"category": "query", "data": {}, "message": "BEGIN;", "type": "default"}, | ||
| { | ||
| "category": "query", | ||
| "data": {}, | ||
| "data": {"db.cursor": mock.ANY}, | ||
| "message": "SELECT * FROM users WHERE dob > $1", | ||
| "type": "default", | ||
| }, | ||
| { | ||
| "category": "query", | ||
| "data": {"db.cursor": mock.ANY}, | ||
| "message": "SELECT * FROM users WHERE dob > $1", | ||
| "type": "default", | ||
| }, | ||
| { | ||
| "category": "query", | ||
| "data": {"db.cursor": mock.ANY}, | ||
| "message": "SELECT * FROM users WHERE dob > $1", | ||
| "type": "default", | ||
| }, | ||
|
|
@@ -1102,3 +1114,188 @@ def before_send_transaction(event, hint): | |
|
|
||
| assert len(spans) == 1 | ||
| assert spans[0]["description"] == "filtered" | ||
|
|
||
|
|
||
| @pytest.mark.asyncio | ||
| @pytest.mark.parametrize("span_streaming", [True, False]) | ||
| async def test_cursor__bind_exec_creates_spans( | ||
| sentry_init, capture_events, capture_items, span_streaming | ||
| ) -> None: | ||
| """ | ||
| Exercises the bind_exec patch through the iterator that's created in asyncpg when "for record in conn.cursor" is called. | ||
| See https://github.com/MagicStack/asyncpg/blob/db8ecc2a38e16fb0c090aef6f5506547c2831c24/asyncpg/cursor.py#L234 | ||
| """ | ||
| sentry_init( | ||
| integrations=[AsyncPGIntegration()], | ||
| traces_sample_rate=1.0, | ||
| _experiments={ | ||
| "trace_lifecycle": "stream" if span_streaming else "static", | ||
| }, | ||
| ) | ||
|
|
||
| if span_streaming: | ||
| items = capture_items("span") | ||
| with sentry_sdk.traces.start_span(name="test_transaction"): | ||
| conn: Connection = await connect(PG_CONNECTION_URI) | ||
|
|
||
| await conn.executemany( | ||
| "INSERT INTO users(name, password, dob) VALUES($1, $2, $3)", | ||
| [ | ||
| ("Bob", "secret_pw", datetime.date(1984, 3, 1)), | ||
| ("Alice", "pw", datetime.date(1990, 12, 25)), | ||
| ], | ||
| ) | ||
|
|
||
| async with conn.transaction(): | ||
| async for record in conn.cursor( | ||
| "SELECT * FROM users WHERE dob > $1", | ||
| datetime.date(1970, 1, 1), | ||
| ): | ||
| pass | ||
|
|
||
| await conn.close() | ||
| sentry_sdk.flush() | ||
|
|
||
| spans = [item.payload for item in items] | ||
|
|
||
| assert len(spans) == 6 | ||
|
|
||
| connect_span = spans[0] | ||
| executemany_span = spans[1] | ||
| begin_span = spans[2] | ||
| bind_exec_span = spans[3] | ||
| commit_span = spans[4] | ||
| segment = spans[5] | ||
|
|
||
| assert connect_span["name"] == "connect" | ||
| assert ( | ||
| executemany_span["name"] | ||
| == "INSERT INTO users(name, password, dob) VALUES($1, $2, $3)" | ||
| ) | ||
| assert begin_span["name"] == "BEGIN;" | ||
| assert bind_exec_span["name"] == "SELECT * FROM users WHERE dob > $1" | ||
| assert commit_span["name"] == "COMMIT;" | ||
| assert segment["name"] == "test_transaction" | ||
|
|
||
| assert bind_exec_span["attributes"]["sentry.origin"] == "auto.db.asyncpg" | ||
| assert bind_exec_span["attributes"]["sentry.op"] == "db" | ||
| assert bind_exec_span["attributes"]["db.system"] == "postgresql" | ||
| assert bind_exec_span["attributes"]["db.driver.name"] == "asyncpg" | ||
| assert bind_exec_span["attributes"]["server.address"] == PG_HOST | ||
| assert bind_exec_span["attributes"]["server.port"] == PG_PORT | ||
| assert bind_exec_span["attributes"]["db.name"] == PG_NAME | ||
| assert bind_exec_span["attributes"]["db.user"] == PG_USER | ||
| else: | ||
| events = capture_events() | ||
|
|
||
| with start_transaction(name="test_transaction", sampled=True): | ||
| conn: Connection = await connect(PG_CONNECTION_URI) | ||
|
|
||
| await conn.executemany( | ||
| "INSERT INTO users(name, password, dob) VALUES($1, $2, $3)", | ||
| [ | ||
| ("Bob", "secret_pw", datetime.date(1984, 3, 1)), | ||
| ("Alice", "pw", datetime.date(1990, 12, 25)), | ||
| ], | ||
| ) | ||
|
|
||
| async with conn.transaction(): | ||
| async for record in conn.cursor( | ||
| "SELECT * FROM users WHERE dob > $1", | ||
| datetime.date(1970, 1, 1), | ||
| ): | ||
| pass | ||
|
|
||
| await conn.close() | ||
|
|
||
| (event,) = events | ||
|
|
||
| assert len(event["spans"]) == 5 | ||
|
|
||
| connect_span = event["spans"][0] | ||
| executemany_span = event["spans"][1] | ||
| begin_span = event["spans"][2] | ||
| bind_exec_span = event["spans"][3] | ||
| commit_span = event["spans"][4] | ||
|
|
||
| assert connect_span["description"] == "connect" | ||
| assert ( | ||
| executemany_span["description"] | ||
| == "INSERT INTO users(name, password, dob) VALUES($1, $2, $3)" | ||
| ) | ||
| assert begin_span["description"] == "BEGIN;" | ||
| assert bind_exec_span["description"] == "SELECT * FROM users WHERE dob > $1" | ||
| assert commit_span["description"] == "COMMIT;" | ||
|
|
||
| assert bind_exec_span["origin"] == "auto.db.asyncpg" | ||
| assert bind_exec_span["data"]["db.system"] == "postgresql" | ||
| assert bind_exec_span["data"]["db.driver.name"] == "asyncpg" | ||
| assert bind_exec_span["data"]["server.address"] == PG_HOST | ||
| assert bind_exec_span["data"]["server.port"] == PG_PORT | ||
| assert bind_exec_span["data"]["db.name"] == PG_NAME | ||
| assert bind_exec_span["data"]["db.user"] == PG_USER | ||
|
|
||
|
|
||
| @pytest.mark.asyncio | ||
| async def test_cursor__bind_and__exec_methods_create_spans( | ||
|
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I did my best to create some distinction between this and This test tests the |
||
| sentry_init, capture_events | ||
| ) -> None: | ||
| sentry_init( | ||
| integrations=[AsyncPGIntegration()], | ||
| traces_sample_rate=1.0, | ||
| ) | ||
| events = capture_events() | ||
|
|
||
| with start_transaction(name="test_transaction"): | ||
| conn: Connection = await connect(PG_CONNECTION_URI) | ||
|
|
||
| await conn.executemany( | ||
| "INSERT INTO users(name, password, dob) VALUES($1, $2, $3)", | ||
| [ | ||
| ("Bob", "secret_pw", datetime.date(1984, 3, 1)), | ||
| ("Alice", "pw", datetime.date(1990, 12, 25)), | ||
| ], | ||
| ) | ||
|
|
||
| async with conn.transaction(): | ||
| # This exercises the `_bind` patch and the `cursor` patch | ||
| cur = await conn.cursor( | ||
| "SELECT * FROM users WHERE dob > $1", datetime.date(1970, 1, 1) | ||
| ) | ||
| # These exercise the `_exec` patch | ||
| await cur.fetchrow() | ||
| await cur.fetchrow() | ||
|
|
||
| await conn.close() | ||
|
|
||
| (event,) = events | ||
|
|
||
| assert len(event["spans"]) == 7 | ||
|
|
||
| connect_span = event["spans"][0] | ||
| executemany_span = event["spans"][1] | ||
| begin_span = event["spans"][2] | ||
| cursor_creation_and_bind_span = event["spans"][3] | ||
| fetchrow_span_1 = event["spans"][4] | ||
| fetchrow_span_2 = event["spans"][5] | ||
| commit_span = event["spans"][6] | ||
|
|
||
| assert connect_span["description"] == "connect" | ||
| assert ( | ||
| executemany_span["description"] | ||
| == "INSERT INTO users(name, password, dob) VALUES($1, $2, $3)" | ||
| ) | ||
| assert begin_span["description"] == "BEGIN;" | ||
| assert fetchrow_span_1["description"] == "SELECT * FROM users WHERE dob > $1" | ||
| assert ( | ||
| cursor_creation_and_bind_span["description"] | ||
| == "SELECT * FROM users WHERE dob > $1" | ||
| ) | ||
| assert fetchrow_span_2["description"] == "SELECT * FROM users WHERE dob > $1" | ||
| assert commit_span["description"] == "COMMIT;" | ||
|
|
||
| for span in (cursor_creation_and_bind_span, fetchrow_span_1, fetchrow_span_2): | ||
| assert span["data"]["db.cursor"] is not None | ||
| assert span["data"]["db.system"] == "postgresql" | ||
| assert span["data"]["db.driver.name"] == "asyncpg" | ||
| assert span["origin"] == "auto.db.asyncpg" | ||
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is safe to remove because this patches the function that led to the creation/return of a
CursorFactory. When that cursor isawaited, theCursorFactorycalls_bindthrough the underlying cursor'sinitmethod, 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)