Skip to content
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

add support for psycopg3 #396

Merged
merged 6 commits into from
Jan 10, 2025
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions aws_xray_sdk/core/patcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
'pymongo',
'pymysql',
'psycopg2',
'psycopg',
'pg8000',
'sqlalchemy_core',
'httpx',
Expand All @@ -38,6 +39,7 @@
'pymongo',
'pymysql',
'psycopg2',
'psycopg',
'pg8000',
'sqlalchemy_core',
'httpx',
Expand Down
4 changes: 4 additions & 0 deletions aws_xray_sdk/ext/psycopg/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
from .patch import patch


__all__ = ['patch']
37 changes: 37 additions & 0 deletions aws_xray_sdk/ext/psycopg/patch.py
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
wangzlei marked this conversation as resolved.
Show resolved Hide resolved
)


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)
1 change: 1 addition & 0 deletions docs/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ Currently supported web frameworks and libraries:
* mysql-connector
* pg8000
* psycopg2
* psycopg (psycopg3)
* pymongo
* pymysql
* pynamodb
Expand Down
Empty file added tests/ext/psycopg/__init__.py
Empty file.
136 changes: 136 additions & 0 deletions tests/ext/psycopg/test_psycopg.py
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())
8 changes: 8 additions & 0 deletions tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ envlist =

py{37,38,39,310,311}-ext-psycopg2

py{37,38,39,310,311}-ext-psycopg

py{37,38,39,310,311}-ext-pymysql

py{37,38,39,310,311}-ext-pynamodb
Expand Down Expand Up @@ -94,6 +96,10 @@ deps =
ext-psycopg2: psycopg2
ext-psycopg2: testing.postgresql

ext-psycopg: psycopg
ext-psycopg: psycopg[pool]
ext-psycopg: testing.postgresql

ext-pg8000: pg8000 <= 1.20.0
ext-pg8000: testing.postgresql

Expand Down Expand Up @@ -130,6 +136,8 @@ commands =
ext-pg8000: coverage run --append --source aws_xray_sdk -m pytest tests/ext/pg8000 {posargs}

ext-psycopg2: coverage run --append --source aws_xray_sdk -m pytest tests/ext/psycopg2 {posargs}

ext-psycopg: coverage run --append --source aws_xray_sdk -m pytest tests/ext/psycopg {posargs}

ext-pymysql: coverage run --append --source aws_xray_sdk -m pytest tests/ext/pymysql {posargs}

Expand Down