32
32
)
33
33
from google .cloud .spanner_v1 ._opentelemetry_tracing import (
34
34
add_event_on_current_span ,
35
+ record_span_exception_and_status ,
35
36
trace_call ,
36
37
)
37
38
from google .cloud .spanner_v1 .batch import Batch
@@ -431,7 +432,9 @@ def run_in_transaction(self, func, *args, **kw):
431
432
432
433
observability_options = getattr (self ._database , "observability_options" , None )
433
434
with trace_call (
434
- "CloudSpanner.ReadWriteTransaction" , self , observability_options
435
+ "CloudSpanner.ReadWriteTransaction" ,
436
+ self ,
437
+ observability_options = observability_options ,
435
438
) as span :
436
439
while True :
437
440
if self ._transaction is None :
@@ -446,8 +449,11 @@ def run_in_transaction(self, func, *args, **kw):
446
449
447
450
attempts += 1
448
451
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
+
451
457
add_event_on_current_span ("Using Transaction" , span_attributes , span )
452
458
453
459
try :
@@ -458,6 +464,7 @@ def run_in_transaction(self, func, *args, **kw):
458
464
delay_seconds = _get_retry_delay (exc .errors [0 ], attempts )
459
465
attributes = dict (delay_seconds = delay_seconds )
460
466
attributes .update (span_attributes )
467
+ record_span_exception_and_status (span , exc )
461
468
add_event_on_current_span (
462
469
"Transaction was aborted, retrying" , attributes , span
463
470
)
0 commit comments