Skip to content

Commit bc1befe

Browse files
committed
Reduce edit changes
1 parent a1c45aa commit bc1befe

File tree

4 files changed

+25
-25
lines changed

4 files changed

+25
-25
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: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -423,7 +423,7 @@ def run_in_transaction(self, func, *args, **kw):
423423
observability_options = getattr(self._database, "observability_options", None)
424424
attempts = 0
425425

426-
def __run_txn(txn, attempts):
426+
def __run_txn_and_return(txn, attempts):
427427
try:
428428
return_value = func(txn, *args, **kw)
429429
except Aborted as exc:
@@ -457,6 +457,9 @@ def __run_txn(txn, attempts):
457457
)
458458
return return_value, True
459459

460+
# Signal to the caller to continue iterating.
461+
return None, False
462+
460463
while True:
461464
if self._transaction is None:
462465
with trace_call(
@@ -467,12 +470,12 @@ def __run_txn(txn, attempts):
467470
txn.exclude_txn_from_change_streams = (
468471
exclude_txn_from_change_streams
469472
)
470-
return_value, completed = __run_txn(txn, attempts)
473+
return_value, completed = __run_txn_and_return(txn, attempts)
471474
if completed:
472475
return return_value
473476
else:
474477
txn = self._transaction
475-
return_value, completed = __run_txn(txn, attempts)
478+
return_value, completed = __run_txn_and_return(txn, attempts)
476479
if completed:
477480
return return_value
478481

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)