Skip to content

Commit b3b57e1

Browse files
committed
Add test/note about how nested Connection's transaction works for us.
1 parent 78eb21e commit b3b57e1

2 files changed

Lines changed: 38 additions & 2 deletions

File tree

README.rst

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,13 +55,25 @@ It is possible to trace all statements being executed under a connection's trans
5555
with conn.begin() as trans:
5656
sqlalchemy_opentracing.set_parent_span(conn, parent_span)
5757
58-
# these three statements will be traced as child of
58+
# these three statements will be traced as children of
5959
# parent_span
6060
conn.execute(users.insert().values(name='John'))
6161
conn.execute(users.insert().values(name='Jason'))
6262
conn.execute(users.insert().values(name='Jackie'))
6363
64-
Either a commit or a rollback on a connection's transaction will finish its tracing. If the same Connection object is used afterwards, no tracing will be done for it (unless registered for tracing again).
64+
Either a commit or a rollback on a connection's transaction will finish its tracing. If the same Connection object is used afterwards, no tracing will be done for it (unless registered for tracing again). When using (emulated) nested transactions, the tracing needs to be marked at top-level transaction time, and tracing will happen for all statements under the nested transactions:
65+
66+
.. code-block:: python
67+
68+
with conn.begin() as trans:
69+
sqlalchemy_opentracing.set_parent_span(conn, parent_span)
70+
conn.execute(users.insert().values(name='John'))
71+
72+
with conn.begin() as nested_trans:
73+
# This statement will also be traced as
74+
# child of parent_span
75+
conn.execute(users.insert().values(name='Jason'))
76+
6577
6678
Tracing under a Session (ORM)
6779
=============================

tests/test_core.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,30 @@ def test_traced_transaction(self):
137137
self.assertEqual(['create_table', 'insert', 'select'],
138138
map(lambda x: x.operation_name, tracer.spans))
139139

140+
def test_traced_transaction_nested(self):
141+
tracer = DummyTracer()
142+
creat = CreateTable(self.users_table)
143+
ins = self.users_table.insert().values(name='John Doe')
144+
sel = select([self.users_table])
145+
146+
sqlalchemy_opentracing.init_tracing(tracer)
147+
parent_span = DummySpan('parent span')
148+
conn = self.engine.connect()
149+
150+
with conn.begin() as trans:
151+
sqlalchemy_opentracing.set_parent_span(conn, parent_span)
152+
conn.execute(creat)
153+
154+
with conn.begin() as trans2:
155+
conn.execute(ins)
156+
conn.execute(sel)
157+
158+
self.assertEqual(3, len(tracer.spans))
159+
self.assertEqual(True, all(map(lambda x: x.is_finished, tracer.spans)))
160+
self.assertEqual(True, all(map(lambda x: x.child_of == parent_span, tracer.spans)))
161+
self.assertEqual(['create_table', 'insert', 'select'],
162+
map(lambda x: x.operation_name, tracer.spans))
163+
140164
def test_traced_rollback(self):
141165
tracer = DummyTracer()
142166
creat = CreateTable(self.users_table)

0 commit comments

Comments
 (0)