-
Notifications
You must be signed in to change notification settings - Fork 144
add support for psycopg3 #396
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
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
4753e3f
add support for psycopg3
Dilski 9a92a55
change patching and remove unrelated tests
csteinle fad82ec
Merge pull request #1 from csteinle/master
Dilski 6092697
add missing psycopg[pool] dependency
csteinle e441b84
Merge pull request #2 from csteinle/master
Dilski bb2cfd7
Merge branch 'master' into master
wangzlei 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 |
---|---|---|
@@ -0,0 +1,4 @@ | ||
from .patch import patch | ||
|
||
|
||
__all__ = ['patch'] |
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 |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import wrapt | ||
from operator import methodcaller | ||
|
||
from aws_xray_sdk.ext.dbapi2 import XRayTracedConn | ||
|
||
|
||
def patch(): | ||
wrapt.wrap_function_wrapper( | ||
'psycopg', | ||
'connect', | ||
_xray_traced_connect | ||
) | ||
|
||
wrapt.wrap_function_wrapper( | ||
'psycopg_pool.pool', | ||
'ConnectionPool._connect', | ||
_xray_traced_connect | ||
) | ||
|
||
|
||
def _xray_traced_connect(wrapped, instance, args, kwargs): | ||
conn = wrapped(*args, **kwargs) | ||
parameterized_dsn = {c[0]: c[-1] for c in map(methodcaller('split', '='), conn.info.dsn.split(' '))} | ||
wangzlei marked this conversation as resolved.
Show resolved
Hide resolved
|
||
meta = { | ||
'database_type': 'PostgreSQL', | ||
'url': 'postgresql://{}@{}:{}/{}'.format( | ||
parameterized_dsn.get('user', 'unknown'), | ||
parameterized_dsn.get('host', 'unknown'), | ||
parameterized_dsn.get('port', 'unknown'), | ||
parameterized_dsn.get('dbname', 'unknown'), | ||
), | ||
'user': parameterized_dsn.get('user', 'unknown'), | ||
'database_version': str(conn.info.server_version), | ||
'driver_version': 'Psycopg 3' | ||
} | ||
|
||
return XRayTracedConn(conn, meta) |
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
Empty file.
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 |
---|---|---|
@@ -0,0 +1,136 @@ | ||
import psycopg | ||
import psycopg.sql | ||
import psycopg_pool | ||
|
||
import pytest | ||
import testing.postgresql | ||
|
||
from aws_xray_sdk.core import patch | ||
from aws_xray_sdk.core import xray_recorder | ||
from aws_xray_sdk.core.context import Context | ||
|
||
patch(('psycopg',)) | ||
|
||
|
||
@pytest.fixture(autouse=True) | ||
def construct_ctx(): | ||
""" | ||
Clean up context storage on each test run and begin a segment | ||
so that later subsegment can be attached. After each test run | ||
it cleans up context storage again. | ||
""" | ||
xray_recorder.configure(service='test', sampling=False, context=Context()) | ||
xray_recorder.clear_trace_entities() | ||
xray_recorder.begin_segment('name') | ||
yield | ||
xray_recorder.clear_trace_entities() | ||
|
||
|
||
def test_execute_dsn_kwargs(): | ||
q = 'SELECT 1' | ||
with testing.postgresql.Postgresql() as postgresql: | ||
url = postgresql.url() | ||
dsn = postgresql.dsn() | ||
conn = psycopg.connect(dbname=dsn['database'], | ||
user=dsn['user'], | ||
password='', | ||
host=dsn['host'], | ||
port=dsn['port']) | ||
cur = conn.cursor() | ||
cur.execute(q) | ||
|
||
subsegment = xray_recorder.current_segment().subsegments[0] | ||
assert subsegment.name == 'execute' | ||
sql = subsegment.sql | ||
assert sql['database_type'] == 'PostgreSQL' | ||
assert sql['user'] == dsn['user'] | ||
assert sql['url'] == url | ||
assert sql['database_version'] | ||
|
||
|
||
def test_execute_dsn_string(): | ||
q = 'SELECT 1' | ||
with testing.postgresql.Postgresql() as postgresql: | ||
url = postgresql.url() | ||
dsn = postgresql.dsn() | ||
conn = psycopg.connect('dbname=' + dsn['database'] + | ||
' password=mypassword' + | ||
' host=' + dsn['host'] + | ||
' port=' + str(dsn['port']) + | ||
' user=' + dsn['user']) | ||
cur = conn.cursor() | ||
cur.execute(q) | ||
|
||
subsegment = xray_recorder.current_segment().subsegments[0] | ||
assert subsegment.name == 'execute' | ||
sql = subsegment.sql | ||
assert sql['database_type'] == 'PostgreSQL' | ||
assert sql['user'] == dsn['user'] | ||
assert sql['url'] == url | ||
assert sql['database_version'] | ||
|
||
|
||
def test_execute_in_pool(): | ||
q = 'SELECT 1' | ||
with testing.postgresql.Postgresql() as postgresql: | ||
url = postgresql.url() | ||
dsn = postgresql.dsn() | ||
pool = psycopg_pool.ConnectionPool('dbname=' + dsn['database'] + | ||
' password=mypassword' + | ||
' host=' + dsn['host'] + | ||
' port=' + str(dsn['port']) + | ||
' user=' + dsn['user'], | ||
min_size=1, | ||
max_size=1) | ||
with pool.connection() as conn: | ||
cur = conn.cursor() | ||
cur.execute(q) | ||
|
||
subsegment = xray_recorder.current_segment().subsegments[0] | ||
assert subsegment.name == 'execute' | ||
sql = subsegment.sql | ||
assert sql['database_type'] == 'PostgreSQL' | ||
assert sql['user'] == dsn['user'] | ||
assert sql['url'] == url | ||
assert sql['database_version'] | ||
|
||
|
||
def test_execute_bad_query(): | ||
q = 'SELECT blarg' | ||
with testing.postgresql.Postgresql() as postgresql: | ||
url = postgresql.url() | ||
dsn = postgresql.dsn() | ||
conn = psycopg.connect(dbname=dsn['database'], | ||
user=dsn['user'], | ||
password='', | ||
host=dsn['host'], | ||
port=dsn['port']) | ||
cur = conn.cursor() | ||
try: | ||
cur.execute(q) | ||
except Exception: | ||
pass | ||
|
||
subsegment = xray_recorder.current_segment().subsegments[0] | ||
assert subsegment.name == 'execute' | ||
sql = subsegment.sql | ||
assert sql['database_type'] == 'PostgreSQL' | ||
assert sql['user'] == dsn['user'] | ||
assert sql['url'] == url | ||
assert sql['database_version'] | ||
|
||
exception = subsegment.cause['exceptions'][0] | ||
assert exception.type == 'UndefinedColumn' | ||
|
||
def test_query_as_string(): | ||
with testing.postgresql.Postgresql() as postgresql: | ||
url = postgresql.url() | ||
dsn = postgresql.dsn() | ||
conn = psycopg.connect('dbname=' + dsn['database'] + | ||
' password=mypassword' + | ||
' host=' + dsn['host'] + | ||
' port=' + str(dsn['port']) + | ||
' user=' + dsn['user']) | ||
test_sql = psycopg.sql.Identifier('test') | ||
assert test_sql.as_string(conn) | ||
assert test_sql.as_string(conn.cursor()) |
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
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.
Uh oh!
There was an error while loading. Please reload this page.