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

Support binding connection in sqlalchemy as well as engine #78

Merged
merged 1 commit into from
Aug 22, 2018
Merged
Show file tree
Hide file tree
Changes from all 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
7 changes: 6 additions & 1 deletion aws_xray_sdk/ext/sqlalchemy/util/decorators.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from future.standard_library import install_aliases
install_aliases()
from urllib.parse import urlparse, uses_netloc
from sqlalchemy.engine.base import Connection


def decorate_all_functions(function_decorator):
Expand Down Expand Up @@ -86,7 +87,11 @@ def wrapper(*args, **kw):
# }
def parse_bind(bind):
"""Parses a connection string and creates SQL trace metadata"""
m = re.match(r"Engine\((.*?)\)", str(bind))
if isinstance(bind, Connection):
engine = bind.engine
else:
engine = bind
m = re.match(r"Engine\((.*?)\)", str(engine))
if m is not None:
u = urlparse(m.group(1))
# Add Scheme to uses_netloc or // will be missing from url.
Expand Down
30 changes: 29 additions & 1 deletion tests/ext/sqlalchemy/test_query.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,12 @@ class User(Base):


@pytest.fixture()
def session():
def engine():
return create_engine('sqlite:///:memory:')


@pytest.fixture()
def session(engine):
"""Test Fixture to Create DataBase Tables and start a trace segment"""
engine = create_engine('sqlite:///:memory:')
xray_recorder.configure(service='test', sampling=False, context=Context())
Expand All @@ -35,6 +40,21 @@ def session():
xray_recorder.clear_trace_entities()


@pytest.fixture()
def connection(engine):
conn = engine.connect()
xray_recorder.configure(service='test', sampling=False, context=Context())
xray_recorder.clear_trace_entities()
xray_recorder.begin_segment('SQLAlchemyTest')
Session = XRaySessionMaker(bind=conn)
Base.metadata.create_all(engine)
session = Session()
yield session
xray_recorder.end_segment()
xray_recorder.clear_trace_entities()



def test_all(capsys, session):
""" Test calling all() on get all records.
Verify we run the query and return the SQL as metdata"""
Expand All @@ -46,6 +66,14 @@ def test_all(capsys, session):
assert subsegment['sql']['url']


def test_supports_connection(capsys, connection):
""" Test that XRaySessionMaker supports connection as well as engine"""
connection.query(User).all()
subsegment = find_subsegment_by_annotation(xray_recorder.current_segment(), 'sqlalchemy',
'sqlalchemy.orm.query.all')
assert subsegment['annotations']['sqlalchemy'] == 'sqlalchemy.orm.query.all'


def test_add(capsys, session):
""" Test calling add() on insert a row.
Verify we that we capture trace for the add"""
Expand Down