Skip to content

Commit eecca2e

Browse files
committed
More upates for test span results
1 parent cfe27c8 commit eecca2e

File tree

7 files changed

+78
-34
lines changed

7 files changed

+78
-34
lines changed

google/cloud/spanner_v1/batch.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ def __init__(self, session):
5151
super(_BatchBase, self).__init__(session)
5252
self._mutations = []
5353
self.__base_discard_span = trace_call_end_lazily(
54-
f"CloudSpanner.{type(self).__name__}",
54+
f"CloudSpanner.a{type(self).__name__}",
5555
self._session,
5656
None,
5757
getattr(self._session._database, "observability_options", None),

google/cloud/spanner_v1/database.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -910,7 +910,6 @@ def run_in_transaction(self, func, *args, **kw):
910910
observability_options = getattr(self, "observability_options", None)
911911
with trace_call(
912912
"CloudSpanner.Database.run_in_transaction",
913-
None,
914913
observability_options=observability_options,
915914
):
916915
with SessionCheckout(self._pool) as session:
@@ -1566,7 +1565,6 @@ def process_read_batch(
15661565
observability_options = self.observability_options or {}
15671566
with trace_call(
15681567
f"CloudSpanner.{type(self).__name__}.process_read_batch",
1569-
session,
15701568
observability_options=observability_options,
15711569
):
15721570
kwargs = copy.deepcopy(batch["read"])

google/cloud/spanner_v1/transaction.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ def begin(self):
157157
)
158158
observability_options = getattr(database, "observability_options", None)
159159
with trace_call(
160-
"CloudSpanner.BeginTransaction",
160+
f"CloudSpanner.{type(self).__name__}.begin",
161161
self._session,
162162
observability_options=observability_options,
163163
):

tests/_helpers.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,6 @@ def tearDown(self):
7878
def assertNoSpans(self):
7979
if HAS_OPENTELEMETRY_INSTALLED:
8080
span_list = self.get_finished_spans()
81-
print("got_span_list", [span.name for span in span_list])
8281
self.assertEqual(len(span_list), 0)
8382

8483
def assertSpanAttributes(
@@ -92,8 +91,6 @@ def assertSpanAttributes(
9291

9392
self.assertEqual(span.name, name)
9493
self.assertEqual(span.status.status_code, status)
95-
print("got_span_attributes ", dict(span.attributes))
96-
print("want_span_attributes", attributes)
9794
self.assertEqual(dict(span.attributes), attributes)
9895

9996
def get_finished_spans(self):
@@ -104,3 +101,6 @@ def get_finished_spans(self):
104101
# A span with name=None is the result from invoking trace_call without
105102
# intention to trace, hence these have to be filtered out.
106103
return list(filter(lambda span: span.name, spans))
104+
105+
def reset(self):
106+
self.tearDown()

tests/system/test_session_api.py

Lines changed: 51 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -658,7 +658,7 @@ def test_transaction_read_and_insert_then_rollback(
658658
)
659659
assert_span_attributes(
660660
ot_exporter,
661-
"CloudSpanner.BeginTransaction",
661+
"CloudSpanner.Transaction.begin",
662662
attributes=_make_attributes(db_name),
663663
span=span_list[5],
664664
)
@@ -1370,38 +1370,67 @@ def unit_of_work(transaction):
13701370
with tracer.start_as_current_span("Test Span"):
13711371
session.run_in_transaction(unit_of_work)
13721372

1373-
span_list = ot_exporter.get_finished_spans()
1373+
span_list = []
1374+
for span in ot_exporter.get_finished_spans():
1375+
if span and span.name:
1376+
span_list.append(span)
1377+
1378+
span_list = sorted(span_list, key=lambda v1: v1.start_time)
1379+
13741380
expected_span_names = [
13751381
"CloudSpanner.CreateSession",
1376-
"CloudSpanner.Batch.commit",
13771382
"CloudSpanner.Batch",
13781383
"CloudSpanner.Batch",
1384+
"CloudSpanner.Batch.commit",
1385+
"Test Span",
1386+
"CloudSpanner.ReadWriteTransaction",
1387+
"CloudSpanner.Transaction",
13791388
"CloudSpanner.DMLTransaction",
13801389
"CloudSpanner.Transaction.commit",
1381-
"CloudSpanner.Transaction",
1382-
"CloudSpanner.ReadWriteTransaction",
1383-
"Test Span",
13841390
]
1391+
13851392
got_span_names = [span.name for span in span_list]
13861393
assert got_span_names == expected_span_names
13871394

1395+
# We expect:
1396+
# |------CloudSpanner.CreateSession--------
1397+
#
1398+
# |---Test Span----------------------------|
1399+
# |>--ReadWriteTransaction-----------------
1400+
# |>-Transaction-------------------------
1401+
# |--------------DMLTransaction--------
1402+
#
1403+
# |>---Batch-------------------------------
1404+
#
1405+
# |>----------Batch-------------------------
1406+
# |>------------Batch.commit---------------
1407+
1408+
# CreateSession should have a trace of its own, with no children
1409+
# nor being a child of any other span.
1410+
session_span = span_list[0]
1411+
test_span = span_list[4]
1412+
# assert session_span.context.trace_id != test_span.context.trace_id
1413+
for span in span_list[1:]:
1414+
if span.parent:
1415+
assert span.parent.span_id != session_span.context.span_id
1416+
1417+
def assert_parent_and_children(parent_span, children):
1418+
for span in children:
1419+
assert span.context.trace_id == parent_span.context.trace_id
1420+
assert span.parent.span_id == parent_span.context.span_id
1421+
13881422
# [CreateSession --> Batch] should have their own trace.
1389-
session_parent_span = span_list[0]
1390-
session_creation_child_spans = span_list[1:1]
1391-
for span in session_creation_child_spans:
1392-
assert span.context.trace_id == session_parent_span.context.trace_id
1393-
assert span.parent.span_id == session_parent_span.context.span_id
1394-
1395-
# [Test Span --> DMLTransaction] should have their own trace.
1396-
overall_userland_span = span_list[-1]
1397-
current_context_spans = span_list[3:-1]
1398-
for span in current_context_spans:
1399-
assert span.context.trace_id == overall_userland_span.context.trace_id
1400-
assert span.parent.span_id == overall_userland_span.context.span_id
1401-
1402-
assert (
1403-
session_parent_span.context.trace_id != overall_userland_span.context.trace_id
1404-
)
1423+
rw_txn_span = span_list[5]
1424+
children_of_test_span = [rw_txn_span]
1425+
assert_parent_and_children(test_span, children_of_test_span)
1426+
1427+
children_of_rw_txn_span = [span_list[6]]
1428+
assert_parent_and_children(rw_txn_span, children_of_rw_txn_span)
1429+
1430+
# Batch_first should have no parent, should be in its own trace.
1431+
batch_0_span = span_list[2]
1432+
children_of_batch_0 = [span_list[1]]
1433+
assert_parent_and_children(rw_txn_span, children_of_rw_txn_span)
14051434

14061435

14071436
def test_execute_partitioned_dml(

tests/unit/test_snapshot.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1701,8 +1701,13 @@ def test_begin_w_other_error(self):
17011701
with self.assertRaises(RuntimeError):
17021702
snapshot.begin()
17031703

1704+
span_list = self.get_finished_spans()
1705+
got_span_names = [span.name for span in span_list]
1706+
want_span_names = ["CloudSpanner.Snapshot.begin"]
1707+
assert got_span_names == want_span_names
1708+
17041709
self.assertSpanAttributes(
1705-
"CloudSpanner.BeginTransaction",
1710+
"CloudSpanner.Snapshot.begin",
17061711
status=StatusCode.ERROR,
17071712
attributes=BASE_ATTRIBUTES,
17081713
)
@@ -1760,7 +1765,7 @@ def test_begin_ok_exact_staleness(self):
17601765
)
17611766

17621767
self.assertSpanAttributes(
1763-
"CloudSpanner.BeginTransaction",
1768+
"CloudSpanner.Snapshot.begin",
17641769
status=StatusCode.OK,
17651770
attributes=BASE_ATTRIBUTES,
17661771
)
@@ -1796,7 +1801,7 @@ def test_begin_ok_exact_strong(self):
17961801
)
17971802

17981803
self.assertSpanAttributes(
1799-
"CloudSpanner.BeginTransaction",
1804+
"CloudSpanner.Snapshot.begin",
18001805
status=StatusCode.OK,
18011806
attributes=BASE_ATTRIBUTES,
18021807
)

tests/unit/test_transaction.py

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,7 @@ def test__make_txn_selector(self):
125125
self.assertEqual(selector.id, self.TRANSACTION_ID)
126126

127127
def test_begin_already_begun(self):
128+
self.reset()
128129
session = _Session()
129130
transaction = self._make_one(session)
130131
transaction._transaction_id = self.TRANSACTION_ID
@@ -134,6 +135,7 @@ def test_begin_already_begun(self):
134135
self.assertNoSpans()
135136

136137
def test_begin_already_rolled_back(self):
138+
self.reset()
137139
session = _Session()
138140
transaction = self._make_one(session)
139141
transaction.rolled_back = True
@@ -143,6 +145,7 @@ def test_begin_already_rolled_back(self):
143145
self.assertNoSpans()
144146

145147
def test_begin_already_committed(self):
148+
self.reset()
146149
session = _Session()
147150
transaction = self._make_one(session)
148151
transaction.committed = object()
@@ -152,6 +155,7 @@ def test_begin_already_committed(self):
152155
self.assertNoSpans()
153156

154157
def test_begin_w_other_error(self):
158+
self.reset()
155159
database = _Database()
156160
database.spanner_api = self._make_spanner_api()
157161
database.spanner_api.begin_transaction.side_effect = RuntimeError()
@@ -161,13 +165,20 @@ def test_begin_w_other_error(self):
161165
with self.assertRaises(RuntimeError):
162166
transaction.begin()
163167

168+
span_list = self.get_finished_spans()
169+
got_span_names = [span.name for span in span_list]
170+
want_span_names = ["CloudSpanner.Transaction.begin"]
171+
print(got_span_names)
172+
assert got_span_names == want_span_names
173+
164174
self.assertSpanAttributes(
165-
"CloudSpanner.BeginTransaction",
175+
"CloudSpanner.Transaction.begin",
166176
status=StatusCode.ERROR,
167177
attributes=TestTransaction.BASE_ATTRIBUTES,
168178
)
169179

170180
def test_begin_ok(self):
181+
self.reset()
171182
from google.cloud.spanner_v1 import Transaction as TransactionPB
172183

173184
transaction_pb = TransactionPB(id=self.TRANSACTION_ID)
@@ -195,10 +206,11 @@ def test_begin_ok(self):
195206
)
196207

197208
self.assertSpanAttributes(
198-
"CloudSpanner.BeginTransaction", attributes=TestTransaction.BASE_ATTRIBUTES
209+
"CloudSpanner.Transaction.begin", attributes=TestTransaction.BASE_ATTRIBUTES
199210
)
200211

201212
def test_begin_w_retry(self):
213+
self.reset()
202214
from google.cloud.spanner_v1 import (
203215
Transaction as TransactionPB,
204216
)

0 commit comments

Comments
 (0)