-
Notifications
You must be signed in to change notification settings - Fork 144
ISSUE-80: Add patch support for psycopg2 #83
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 4 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
7598218
ISSUE-80: Basic psycopg2 patcher and test
Tankanow ca90406
ISSUE-80: Add pool test
Tankanow 96a3a62
ISSUE-80: Update dbname; Add host and user
Tankanow d3fc58e
ISSUE-80: Add testing resources to tox.ini
Tankanow d8f4930
Merge branch 'master' into ISSUE-80
haotianw465 c1952fd
ISSUE-80: Update meta keys to match supported keys
Tankanow 553d03e
Merge branch 'ISSUE-80' of github.com:Cloudzero/aws-xray-sdk-python i…
Tankanow 609c0f0
ISSUE-80: Fix failing tests; simply assert database_version rather th…
Tankanow 7bbdc79
ISSUE-80: Add psycopg2 to NO_DOUBLE_PATCH
Tankanow 971139b
ISSUE-80: Remove debug print statements
Tankanow 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,6 +12,7 @@ | |
'mysql', | ||
'httplib', | ||
'pymongo', | ||
'psycopg2' | ||
) | ||
|
||
_PATCHED_MODULES = set() | ||
|
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,26 @@ | ||
import re | ||
import wrapt | ||
|
||
from aws_xray_sdk.ext.dbapi2 import XRayTracedConn | ||
|
||
|
||
def patch(): | ||
|
||
wrapt.wrap_function_wrapper( | ||
'psycopg2', | ||
'connect', | ||
_xray_traced_connect | ||
) | ||
|
||
|
||
def _xray_traced_connect(wrapped, instance, args, kwargs): | ||
|
||
conn = wrapped(*args, **kwargs) | ||
meta = { | ||
'database_type': 'postgresql', | ||
'database_host': kwargs['host'] if 'host' in kwargs else re.search(r'host=(\S+)\b', args[0]).groups()[0], | ||
'database_name': kwargs['dbname'] if 'dbname' in kwargs else re.search(r'dbname=(\S+)\b', args[0]).groups()[0], | ||
'database_user': kwargs['user'] if 'user' in kwargs else re.search(r'user=(\S+)\b', args[0]).groups()[0], | ||
} | ||
|
||
return XRayTracedConn(conn, meta) |
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,117 @@ | ||
import psycopg2 | ||
import psycopg2.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(('psycopg2',)) | ||
|
||
|
||
@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: | ||
dsn = postgresql.dsn() | ||
conn = psycopg2.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['database_host'] == dsn['host'] | ||
assert sql['database_name'] == dsn['database'] | ||
assert sql['database_user'] == dsn['user'] | ||
|
||
|
||
def test_execute_dsn_string(): | ||
q = 'SELECT 1' | ||
with testing.postgresql.Postgresql() as postgresql: | ||
dsn = postgresql.dsn() | ||
conn = psycopg2.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['database_host'] == dsn['host'] | ||
assert sql['database_name'] == dsn['database'] | ||
assert sql['database_user'] == dsn['user'] | ||
|
||
|
||
|
||
def test_execute_in_pool(): | ||
q = 'SELECT 1' | ||
with testing.postgresql.Postgresql() as postgresql: | ||
dsn = postgresql.dsn() | ||
pool = psycopg2.pool.SimpleConnectionPool(1, 1, | ||
dbname=dsn['database'], | ||
user=dsn['user'], | ||
password='', | ||
host=dsn['host'], | ||
port=dsn['port']) | ||
cur = pool.getconn(key=dsn['user']).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['database_host'] == dsn['host'] | ||
assert sql['database_name'] == dsn['database'] | ||
assert sql['database_user'] == dsn['user'] | ||
|
||
|
||
def test_execute_bad_query(): | ||
q = 'SELECT blarg' | ||
with testing.postgresql.Postgresql() as postgresql: | ||
dsn = postgresql.dsn() | ||
conn = psycopg2.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['database_host'] == dsn['host'] | ||
assert sql['database_name'] == dsn['database'] | ||
assert sql['database_user'] == dsn['user'] | ||
|
||
exception = subsegment.cause['exceptions'][0] | ||
assert exception.type == 'ProgrammingError' |
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.
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.
@haotianw465, how do I see the custom metadata in XRay? I system tested this code by deploying it along with one of my Lambda functions; I did see the correct
database_type
, however I did not seedatabase_host
,database_name
, ordatabase_user
in the Raw Trace Data in the AWS XRay Console.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.
The service will drop unknown keys. Here is a sample sql document the service support.