Skip to content

Commit

Permalink
Address updates from running transaction aborting
Browse files Browse the repository at this point in the history
  • Loading branch information
odeke-em committed Dec 3, 2024
1 parent 5fec9fd commit 2e49067
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 3 deletions.
6 changes: 6 additions & 0 deletions google/cloud/spanner_v1/_opentelemetry_tracing.py
Original file line number Diff line number Diff line change
Expand Up @@ -200,3 +200,9 @@ def add_event_on_current_span(event_name, attributes=None, span=None):

if span:
span.add_event(event_name, attributes)


def record_span_exception_and_status(span, exc):
if span:
span.set_status(Status(StatusCode.ERROR, "foo"))
span.record_exception(exc)
13 changes: 10 additions & 3 deletions google/cloud/spanner_v1/session.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
)
from google.cloud.spanner_v1._opentelemetry_tracing import (
add_event_on_current_span,
record_span_exception_and_status,
trace_call,
)
from google.cloud.spanner_v1.batch import Batch
Expand Down Expand Up @@ -431,7 +432,9 @@ def run_in_transaction(self, func, *args, **kw):

observability_options = getattr(self._database, "observability_options", None)
with trace_call(
"CloudSpanner.ReadWriteTransaction", self, observability_options
"CloudSpanner.ReadWriteTransaction",
self,
observability_options=observability_options,
) as span:
while True:
if self._transaction is None:
Expand All @@ -446,8 +449,11 @@ def run_in_transaction(self, func, *args, **kw):

attempts += 1

txn_id = getattr(txn, "_transaction_id", None) or ""
span_attributes = {"transaction.id": txn_id, "attempt": attempts}
txn_id = getattr(txn, "_transaction_id", "") or ""
span_attributes = {"attempt": attempts}
if txn_id:
span_attributes["transaction.id"] = txn_id

add_event_on_current_span("Using Transaction", span_attributes, span)

try:
Expand All @@ -458,6 +464,7 @@ def run_in_transaction(self, func, *args, **kw):
delay_seconds = _get_retry_delay(exc.errors[0], attempts)
attributes = dict(delay_seconds=delay_seconds)
attributes.update(span_attributes)
record_span_exception_and_status(span, exc)
add_event_on_current_span(
"Transaction was aborted, retrying", attributes, span
)
Expand Down

0 comments on commit 2e49067

Please sign in to comment.