@@ -424,9 +424,11 @@ def run_in_transaction(self, func, *args, **kw):
424
424
observability_options = getattr (self ._database , "observability_options" , None )
425
425
with trace_call (
426
426
"CloudSpanner.ReadWriteTransaction" , self , observability_options
427
- ):
427
+ ) as span :
428
428
while True :
429
429
if self ._transaction is None :
430
+ if span :
431
+ span .add_event ("Creating Transaction" )
430
432
txn = self .transaction ()
431
433
txn .transaction_tag = transaction_tag
432
434
txn .exclude_txn_from_change_streams = (
@@ -435,17 +437,38 @@ def run_in_transaction(self, func, *args, **kw):
435
437
else :
436
438
txn = self ._transaction
437
439
440
+ attempts += 1
441
+
442
+ span_attributes = {"transaction.id" : txn .id , "attempt" : attempts }
443
+ if span :
444
+ span .add_event ("Using Transaction" , span_attributes )
445
+
438
446
try :
439
- attempts += 1
440
447
return_value = func (txn , * args , ** kw )
441
448
except Aborted as exc :
442
449
del self ._transaction
450
+ if span :
451
+ delay_seconds = _get_retry_delay (exc , attempts )
452
+ attributes = dict (delay_seconds = delay_seconds )
453
+ attributes .update (span_attributes )
454
+ span .add_event ("Transaction was aborted, retrying" , attributes )
455
+
443
456
_delay_until_retry (exc , deadline , attempts )
444
457
continue
445
458
except GoogleAPICallError :
446
459
del self ._transaction
460
+ if span :
461
+ span .add_event (
462
+ "Transaction.commit failed due to GoogleAPICallError, not retrying" ,
463
+ span_attributes ,
464
+ )
447
465
raise
448
466
except Exception :
467
+ if span :
468
+ span .add_event (
469
+ "Invoking Transaction.rollback(), not retrying" ,
470
+ span_attributes ,
471
+ )
449
472
txn .rollback ()
450
473
raise
451
474
@@ -457,9 +480,23 @@ def run_in_transaction(self, func, *args, **kw):
457
480
)
458
481
except Aborted as exc :
459
482
del self ._transaction
483
+ if span :
484
+ delay_seconds = _get_retry_delay (exc , attempts )
485
+ attributes = dict (delay_seconds = delay_seconds )
486
+ attributes .update (span_attributes )
487
+ span .add_event (
488
+ "Transaction.commit was aborted, retrying afresh" ,
489
+ attributes ,
490
+ )
491
+
460
492
_delay_until_retry (exc , deadline , attempts )
461
493
except GoogleAPICallError :
462
494
del self ._transaction
495
+ if span :
496
+ span .add_event (
497
+ "Transaction.commit failed due to GoogleAPICallError, not retrying" ,
498
+ span_attributes ,
499
+ )
463
500
raise
464
501
else :
465
502
if self ._database .log_commit_stats and txn .commit_stats :
0 commit comments