Skip to content
Open
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
8 changes: 8 additions & 0 deletions aws_xray_sdk/ext/dbapi2.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,20 @@ def __init__(self, conn, meta={}):
super().__init__(conn)
self._xray_meta = meta

def __enter__(self):

value = self.__wrapped__.__enter__()
if value is not self.__wrapped__:
return value
return self

def cursor(self, *args, **kwargs):

cursor = self.__wrapped__.cursor(*args, **kwargs)
return XRayTracedCursor(cursor, self._xray_meta)



class XRayTracedCursor(wrapt.ObjectProxy):

_xray_meta = None
Expand Down
13 changes: 13 additions & 0 deletions tests/ext/sqlite3/test_sqlite3.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,3 +54,16 @@ def test_invalid_syntax(db):

exception = subsegment.cause['exceptions'][0]
assert exception.type == 'OperationalError'


def test_connection_context_manager(db):
with db as conn:
q = 'SELECT name FROM sqlite_master'
cur = conn.cursor()
cur.execute(q)

subsegment = xray_recorder.current_segment().subsegments[0]
assert subsegment.name == ':memory:'
sql = subsegment.sql
assert sql['database_type'] == 'sqlite3'
assert sql['database_version']