Skip to content

Commit 9effc60

Browse files
committed
chore(x-goog-spanner-request-id): set span attribute x_goog_spanner_request_id
This sets the span attribute x_goog_spanner_request_id so as to enable correlation with distributed tracing with OpenTelemetry. Updates #3537
1 parent 0906eac commit 9effc60

File tree

3 files changed

+20
-1
lines changed

3 files changed

+20
-1
lines changed

google-cloud-spanner/src/main/java/com/google/cloud/spanner/XGoogSpannerRequestId.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,4 +159,8 @@ public XGoogSpannerRequestId nextRequestId(long channelId, int attempt) {
159159
return XGoogSpannerRequestId.of(1, 1, 1, 0);
160160
}
161161
}
162+
163+
public static String getRequestIdFromMetadata(Metadata md) {
164+
return md.get(REQUEST_HEADER_KEY);
165+
}
162166
}

google-cloud-spanner/src/main/java/com/google/cloud/spanner/spi/v1/HeaderInterceptor.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import com.google.cloud.spanner.CompositeTracer;
2929
import com.google.cloud.spanner.SpannerExceptionFactory;
3030
import com.google.cloud.spanner.SpannerRpcMetrics;
31+
import com.google.cloud.spanner.XGoogSpannerRequestId;
3132
import com.google.common.cache.Cache;
3233
import com.google.common.cache.CacheBuilder;
3334
import com.google.spanner.admin.database.v1.DatabaseName;
@@ -174,6 +175,10 @@ private void processHeader(
174175
}
175176
if (span != null) {
176177
span.setAttribute("gfe_latency", String.valueOf(gfeLatency));
178+
String reqId = XGoogSpannerRequestId.getRequestIdFromMetadata(metadata);
179+
if (reqId != null) {
180+
span.setAttribute("x_goog_spanner_request_id", reqId);
181+
}
177182
}
178183
} else {
179184
measureMap.put(SPANNER_GFE_HEADER_MISSING_COUNT, 1L).record(tagContext);

google-cloud-spanner/src/test/java/com/google/cloud/spanner/OpenTelemetrySpanTest.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -734,7 +734,7 @@ public void testTransactionRunnerWithRetryOnBeginTransaction() {
734734
assertTrue(
735735
actualSpanNames,
736736
finishedSpanNames.contains("CloudSpannerOperation.BatchCreateSessionsRequest"));
737-
737+
verifyAtLeast1SpanHasXGoogSpannerRequestIdAttribute(finishedSpans);
738738
assertTrue(actualSpanNames, finishedSpanNames.contains("Spanner.BatchCreateSessions"));
739739
assertTrue(actualSpanNames, finishedSpanNames.contains("Spanner.BeginTransaction"));
740740
assertTrue(actualSpanNames, finishedSpanNames.contains("Spanner.Commit"));
@@ -750,6 +750,14 @@ public void testTransactionRunnerWithRetryOnBeginTransaction() {
750750
.anyMatch(event -> event.getName().equals("Starting RPC retry 1")));
751751
}
752752

753+
private void verifyAtLeast1SpanHasXGoogSpannerRequestIdAttribute(List<SpanData> finishedSpans) {
754+
AttributeKey<String> attributeKey = AttributeKey.stringKey("x_goog_spanner_request_id");
755+
SpanData matchingSpan = finishedSpans.stream()
756+
.filter(span -> !span.getAttributes().get(attributeKey).isEmpty())
757+
.findAny()
758+
.orElseThrow(IllegalStateException::new);
759+
}
760+
753761
@Test
754762
public void testSingleUseRetryOnExecuteStreamingSql() {
755763
// First get the client to ensure that the BatchCreateSessions request has been executed.
@@ -798,6 +806,7 @@ public void testSingleUseRetryOnExecuteStreamingSql() {
798806
executeStreamingQuery.toString(),
799807
executeStreamingQuery.getEvents().stream()
800808
.anyMatch(event -> event.getName().contains("Stream broken. Safe to retry")));
809+
verifyAtLeast1SpanHasXGoogSpannerRequestIdAttribute(finishedSpans);
801810
}
802811

803812
@Test
@@ -845,6 +854,7 @@ public void testRetryOnExecuteSql() {
845854
executeSqlSpan.toString(),
846855
executeSqlSpan.getEvents().stream()
847856
.anyMatch(event -> event.getName().equals("Starting RPC retry 1")));
857+
verifyAtLeast1SpanHasXGoogSpannerRequestIdAttribute(finishedSpans);
848858
}
849859

850860
@Test

0 commit comments

Comments
 (0)