Skip to content

Commit 2e49067

Browse files
committed
Address updates from running transaction aborting
1 parent 5fec9fd commit 2e49067

File tree

2 files changed

+16
-3
lines changed

2 files changed

+16
-3
lines changed

google/cloud/spanner_v1/_opentelemetry_tracing.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,3 +200,9 @@ def add_event_on_current_span(event_name, attributes=None, span=None):
200200

201201
if span:
202202
span.add_event(event_name, attributes)
203+
204+
205+
def record_span_exception_and_status(span, exc):
206+
if span:
207+
span.set_status(Status(StatusCode.ERROR, "foo"))
208+
span.record_exception(exc)

google/cloud/spanner_v1/session.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
)
3333
from google.cloud.spanner_v1._opentelemetry_tracing import (
3434
add_event_on_current_span,
35+
record_span_exception_and_status,
3536
trace_call,
3637
)
3738
from google.cloud.spanner_v1.batch import Batch
@@ -431,7 +432,9 @@ def run_in_transaction(self, func, *args, **kw):
431432

432433
observability_options = getattr(self._database, "observability_options", None)
433434
with trace_call(
434-
"CloudSpanner.ReadWriteTransaction", self, observability_options
435+
"CloudSpanner.ReadWriteTransaction",
436+
self,
437+
observability_options=observability_options,
435438
) as span:
436439
while True:
437440
if self._transaction is None:
@@ -446,8 +449,11 @@ def run_in_transaction(self, func, *args, **kw):
446449

447450
attempts += 1
448451

449-
txn_id = getattr(txn, "_transaction_id", None) or ""
450-
span_attributes = {"transaction.id": txn_id, "attempt": attempts}
452+
txn_id = getattr(txn, "_transaction_id", "") or ""
453+
span_attributes = {"attempt": attempts}
454+
if txn_id:
455+
span_attributes["transaction.id"] = txn_id
456+
451457
add_event_on_current_span("Using Transaction", span_attributes, span)
452458

453459
try:
@@ -458,6 +464,7 @@ def run_in_transaction(self, func, *args, **kw):
458464
delay_seconds = _get_retry_delay(exc.errors[0], attempts)
459465
attributes = dict(delay_seconds=delay_seconds)
460466
attributes.update(span_attributes)
467+
record_span_exception_and_status(span, exc)
461468
add_event_on_current_span(
462469
"Transaction was aborted, retrying", attributes, span
463470
)

0 commit comments

Comments
 (0)