Skip to content

Commit 1b74118

Browse files
authored
chore: add transaction.retried attribute to traces (#3148)
Adds a 'transaction.retried' attribute to Connection API read/write transaction spans if it was retried. This makes it easier to search for transactions that were retried.
1 parent 71f56da commit 1b74118

File tree

2 files changed

+7
-0
lines changed

2 files changed

+7
-0
lines changed

google-cloud-spanner/src/main/java/com/google/cloud/spanner/connection/ReadWriteTransaction.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@
5757
import com.google.common.collect.Iterables;
5858
import com.google.common.util.concurrent.MoreExecutors;
5959
import com.google.spanner.v1.SpannerGrpc;
60+
import io.opentelemetry.api.common.AttributeKey;
6061
import io.opentelemetry.context.Scope;
6162
import java.time.Duration;
6263
import java.util.ArrayList;
@@ -80,6 +81,8 @@
8081
* exact same results as the original transaction.
8182
*/
8283
class ReadWriteTransaction extends AbstractMultiUseTransaction {
84+
private static final AttributeKey<Boolean> TRANSACTION_RETRIED =
85+
AttributeKey.booleanKey("transaction.retried");
8386
private static final Logger logger = Logger.getLogger(ReadWriteTransaction.class.getName());
8487
private static final AtomicLong ID_GENERATOR = new AtomicLong();
8588
private static final String MAX_INTERNAL_RETRIES_EXCEEDED =
@@ -990,6 +993,7 @@ private void handleAborted(AbortedException aborted) {
990993
long delay = aborted.getRetryDelayInMillis();
991994
span.addEvent(
992995
"Transaction aborted. Backing off for " + delay + " milliseconds and retrying.");
996+
span.setAttribute(TRANSACTION_RETRIED, true);
993997
try {
994998
if (delay > 0L) {
995999
//noinspection BusyWait

google-cloud-spanner/src/test/java/com/google/cloud/spanner/connection/OpenTelemetryTracingTest.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -447,6 +447,9 @@ public void testMultiUseReadWriteAborted() {
447447
.filter(span -> span.getName().equals("CloudSpannerJdbc.ReadWriteTransaction"))
448448
.findFirst()
449449
.orElseThrow(IllegalStateException::new);
450+
assertEquals(
451+
Boolean.TRUE,
452+
transactionSpan.getAttributes().get(AttributeKey.booleanKey("transaction.retried")));
450453
assertEquals(1, transactionSpan.getTotalRecordedEvents());
451454
EventData event = transactionSpan.getEvents().get(0);
452455
assertEquals(

0 commit comments

Comments
 (0)