Skip to content

Commit 502d0e5

Browse files
committed
Reduce edit changes
1 parent a1c45aa commit 502d0e5

File tree

4 files changed

+26
-27
lines changed

4 files changed

+26
-27
lines changed

google/cloud/spanner_v1/_opentelemetry_tracing.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ def trace_call(name, session, extra_attributes=None, observability_options=None)
8080
attributes = {
8181
"db.type": "spanner",
8282
"db.url": SpannerClient.DEFAULT_ENDPOINT,
83-
"db.instance": session._database.name,
83+
"db.instance": "" if not session._database else session._database.name,
8484
"net.host.name": SpannerClient.DEFAULT_ENDPOINT,
8585
OTEL_SCOPE_NAME: TRACER_NAME,
8686
OTEL_SCOPE_VERSION: TRACER_VERSION,

google/cloud/spanner_v1/database.py

Lines changed: 16 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -890,26 +890,23 @@ def run_in_transaction(self, func, *args, **kw):
890890
# Sanity check: Is there a transaction already running?
891891
# If there is, then raise a red flag. Otherwise, mark that this one
892892
# is running.
893-
with SessionCheckout(self._pool) as session:
894-
observability_options = getattr(self, "observability_options", None)
895-
with trace_call(
896-
"CloudSpanner.Database.run_in_transaction",
897-
session,
898-
observability_options=observability_options,
899-
):
900-
# Sanity check: Is there a transaction already running?
901-
# If there is, then raise a red flag. Otherwise, mark that this one
902-
# is running.
903-
if getattr(self._local, "transaction_running", False):
904-
raise RuntimeError("Spanner does not support nested transactions.")
905-
self._local.transaction_running = True
906-
907-
# Check out a session and run the function in a transaction; once
908-
# done, flip the sanity check bit back.
909-
try:
893+
if getattr(self._local, "transaction_running", False):
894+
raise RuntimeError("Spanner does not support nested transactions.")
895+
self._local.transaction_running = True
896+
897+
# Check out a session and run the function in a transaction; once
898+
# done, flip the sanity check bit back.
899+
try:
900+
with SessionCheckout(self._pool) as session:
901+
observability_options = getattr(self, "observability_options", None)
902+
with trace_call(
903+
"CloudSpanner.Database.run_in_transaction",
904+
session,
905+
observability_options=observability_options,
906+
):
910907
return session.run_in_transaction(func, *args, **kw)
911-
finally:
912-
self._local.transaction_running = False
908+
finally:
909+
self._local.transaction_running = False
913910

914911
def restore(self, source):
915912
"""Restore from a backup to this database.

google/cloud/spanner_v1/session.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -419,11 +419,9 @@ def run_in_transaction(self, func, *args, **kw):
419419
exclude_txn_from_change_streams = kw.pop(
420420
"exclude_txn_from_change_streams", None
421421
)
422-
423-
observability_options = getattr(self._database, "observability_options", None)
424422
attempts = 0
425423

426-
def __run_txn(txn, attempts):
424+
def __run_txn_and_return(txn, attempts):
427425
try:
428426
return_value = func(txn, *args, **kw)
429427
except Aborted as exc:
@@ -457,6 +455,10 @@ def __run_txn(txn, attempts):
457455
)
458456
return return_value, True
459457

458+
# Signal to the caller to continue iterating.
459+
return None, False
460+
461+
observability_options = getattr(self._database, "observability_options", None)
460462
while True:
461463
if self._transaction is None:
462464
with trace_call(
@@ -467,12 +469,12 @@ def __run_txn(txn, attempts):
467469
txn.exclude_txn_from_change_streams = (
468470
exclude_txn_from_change_streams
469471
)
470-
return_value, completed = __run_txn(txn, attempts)
472+
return_value, completed = __run_txn_and_return(txn, attempts)
471473
if completed:
472474
return return_value
473475
else:
474476
txn = self._transaction
475-
return_value, completed = __run_txn(txn, attempts)
477+
return_value, completed = __run_txn_and_return(txn, attempts)
476478
if completed:
477479
return return_value
478480

tests/unit/test_snapshot.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -868,7 +868,7 @@ def test_execute_sql_other_error(self):
868868
self.assertEqual(derived._execute_sql_count, 1)
869869

870870
self.assertSpanAttributes(
871-
"CloudSpanner.ReadWriteTransaction",
871+
"CloudSpanner.execute_sql",
872872
status=StatusCode.ERROR,
873873
attributes=dict(BASE_ATTRIBUTES, **{"db.statement": SQL_QUERY}),
874874
)
@@ -1024,7 +1024,7 @@ def _execute_sql_helper(
10241024
self.assertEqual(derived._execute_sql_count, sql_count + 1)
10251025

10261026
self.assertSpanAttributes(
1027-
"CloudSpanner.ReadWriteTransaction",
1027+
"CloudSpanner.execute_sql",
10281028
status=StatusCode.OK,
10291029
attributes=dict(BASE_ATTRIBUTES, **{"db.statement": SQL_QUERY_WITH_PARAM}),
10301030
)

0 commit comments

Comments
 (0)