diff --git a/google/cloud/spanner_v1/_opentelemetry_tracing.py b/google/cloud/spanner_v1/_opentelemetry_tracing.py index 34a95ea906..94722516e0 100644 --- a/google/cloud/spanner_v1/_opentelemetry_tracing.py +++ b/google/cloud/spanner_v1/_opentelemetry_tracing.py @@ -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) diff --git a/google/cloud/spanner_v1/session.py b/google/cloud/spanner_v1/session.py index 5650329653..cedfe5babf 100644 --- a/google/cloud/spanner_v1/session.py +++ b/google/cloud/spanner_v1/session.py @@ -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 @@ -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: @@ -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: @@ -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 )