Skip to content

Commit 436cf2e

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 1cbc00b commit 436cf2e

File tree

3 files changed

+26
-1
lines changed

3 files changed

+26
-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
@@ -198,4 +198,8 @@ XGoogSpannerRequestId withNthClientId(long replacementClientId) {
198198
return XGoogSpannerRequestId.of(
199199
replacementClientId, this.nthChannelId, this.nthRequest, this.attempt);
200200
}
201+
202+
public static String getRequestIdFromMetadata(Metadata md) {
203+
return md.get(REQUEST_HEADER_KEY);
204+
}
201205
}

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: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
import static org.junit.Assert.assertEquals;
2020
import static org.junit.Assert.assertFalse;
21+
import static org.junit.Assert.assertNotNull;
2122
import static org.junit.Assert.assertThrows;
2223
import static org.junit.Assert.assertTrue;
2324

@@ -748,6 +749,17 @@ public void testTransactionRunnerWithRetryOnBeginTransaction() {
748749
beginTransactionSpan.toString(),
749750
beginTransactionSpan.getEvents().stream()
750751
.anyMatch(event -> event.getName().equals("Starting RPC retry 1")));
752+
verifyAtLeast1SpanHasXGoogSpannerRequestIdAttribute(finishedSpans);
753+
}
754+
755+
private void verifyAtLeast1SpanHasXGoogSpannerRequestIdAttribute(List<SpanData> finishedSpans) {
756+
AttributeKey<String> attributeKey = AttributeKey.stringKey("x_goog_spanner_request_id");
757+
SpanData matchedSpan =
758+
finishedSpans.stream()
759+
.filter(span -> !span.getAttributes().get(attributeKey).isEmpty())
760+
.findAny()
761+
.orElseThrow(IllegalStateException::new);
762+
assertNotNull(matchedSpan);
751763
}
752764

753765
@Test
@@ -798,6 +810,7 @@ public void testSingleUseRetryOnExecuteStreamingSql() {
798810
executeStreamingQuery.toString(),
799811
executeStreamingQuery.getEvents().stream()
800812
.anyMatch(event -> event.getName().contains("Stream broken. Safe to retry")));
813+
verifyAtLeast1SpanHasXGoogSpannerRequestIdAttribute(finishedSpans);
801814
}
802815

803816
@Test
@@ -845,6 +858,7 @@ public void testRetryOnExecuteSql() {
845858
executeSqlSpan.toString(),
846859
executeSqlSpan.getEvents().stream()
847860
.anyMatch(event -> event.getName().equals("Starting RPC retry 1")));
861+
verifyAtLeast1SpanHasXGoogSpannerRequestIdAttribute(finishedSpans);
848862
}
849863

850864
@Test
@@ -866,12 +880,14 @@ public void testTableAttributes() {
866880
}
867881
return null;
868882
});
883+
List<SpanData> finishedSpans = spanExporter.getFinishedSpanItems();
869884
SpanData spanData =
870-
spanExporter.getFinishedSpanItems().stream()
885+
finishedSpans.stream()
871886
.filter(x -> x.getName().equals("CloudSpannerOperation.ExecuteStreamingRead"))
872887
.findFirst()
873888
.get();
874889
verifyTableAttributes(spanData);
890+
verifyAtLeast1SpanHasXGoogSpannerRequestIdAttribute(finishedSpans);
875891
}
876892

877893
private void waitForFinishedSpans(int numExpectedSpans) {

0 commit comments

Comments
 (0)