-
Notifications
You must be signed in to change notification settings - Fork 144
Add patch support for pymysql (pure Python driver) #215
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
10 commits
Select commit
Hold shift + click to select a range
9383591
Add pymysql patch module
73cbfef
export patch method
bd87499
fix attribute for server_version
d8e3236
Add unpatch for pymysql
8bd9fca
Add tests for pymysql patch
81341e1
Fix dsn params
279439a
Fix tests
4914d91
Fix typo
15cb69b
Assert driver_version as PyMySQL
98087bd
Fix driver version string
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, unpatch | ||
|
||
|
||
__all__ = ['patch', 'unpatch'] |
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,53 @@ | ||
import pymysql | ||
import wrapt | ||
|
||
from aws_xray_sdk.ext.dbapi2 import XRayTracedConn | ||
from aws_xray_sdk.core.patcher import _PATCHED_MODULES | ||
from aws_xray_sdk.ext.util import unwrap | ||
|
||
|
||
def patch(): | ||
|
||
wrapt.wrap_function_wrapper( | ||
'pymysql', | ||
'connect', | ||
_xray_traced_connect | ||
) | ||
|
||
# patch alias | ||
if hasattr(pymysql, 'Connect'): | ||
pymysql.Connect = pymysql.connect | ||
|
||
|
||
def _xray_traced_connect(wrapped, instance, args, kwargs): | ||
|
||
conn = wrapped(*args, **kwargs) | ||
meta = { | ||
'database_type': 'MySQL', | ||
'user': conn.user.decode('utf-8'), | ||
'driver_version': 'PyMySQL' | ||
} | ||
|
||
if hasattr(conn, 'server_version'): | ||
version = sanitize_db_ver(getattr(conn, 'server_version')) | ||
if version: | ||
meta['database_version'] = version | ||
|
||
return XRayTracedConn(conn, meta) | ||
|
||
|
||
def sanitize_db_ver(raw): | ||
|
||
if not raw or not isinstance(raw, tuple): | ||
return raw | ||
|
||
return '.'.join(str(num) for num in raw) | ||
|
||
|
||
def unpatch(): | ||
""" | ||
Unpatch any previously patched modules. | ||
This operation is idempotent. | ||
""" | ||
_PATCHED_MODULES.discard('pymysql') | ||
unwrap(pymysql, 'connect') |
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,79 @@ | ||
import pymysql | ||
|
||
import pytest | ||
import testing.mysqld | ||
|
||
from aws_xray_sdk.core import patch | ||
from aws_xray_sdk.core import xray_recorder | ||
from aws_xray_sdk.core.context import Context | ||
from aws_xray_sdk.ext.pymysql import unpatch | ||
|
||
|
||
@pytest.fixture(scope='module', autouse=True) | ||
def patch_module(): | ||
patch(('pymysql',)) | ||
yield | ||
unpatch() | ||
|
||
|
||
@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.mysqld.Mysqld() as mysqld: | ||
dsn = mysqld.dsn() | ||
conn = pymysql.connect(database=dsn['db'], | ||
user=dsn['user'], | ||
password='', | ||
host=dsn['host'], | ||
port=dsn['port']) | ||
cur = conn.cursor() | ||
cur.execute(q) | ||
|
||
subsegment = xray_recorder.current_segment().subsegments[-1] | ||
assert subsegment.name == 'execute' | ||
sql = subsegment.sql | ||
assert sql['database_type'] == 'MySQL' | ||
assert sql['user'] == dsn['user'] | ||
assert sql['driver_version'] == 'PyMySQL' | ||
assert sql['database_version'] | ||
|
||
|
||
def test_execute_bad_query(): | ||
q = "SELECT blarg" | ||
with testing.mysqld.Mysqld() as mysqld: | ||
dsn = mysqld.dsn() | ||
conn = pymysql.connect(database=dsn['db'], | ||
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[-1] | ||
assert subsegment.name == "execute" | ||
sql = subsegment.sql | ||
assert sql['database_type'] == 'MySQL' | ||
assert sql['user'] == dsn['user'] | ||
assert sql['driver_version'] == 'PyMySQL' | ||
assert sql['database_version'] | ||
aniljaiswal marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
exception = subsegment.cause['exceptions'][0] | ||
assert exception.type == 'InternalError' |
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.