Skip to content

chore(x-goog-spanner-request-id): set span attribute x_goog_spanner_request_id #3905

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -198,4 +198,8 @@ XGoogSpannerRequestId withNthClientId(long replacementClientId) {
return XGoogSpannerRequestId.of(
replacementClientId, this.nthChannelId, this.nthRequest, this.attempt);
}

public static String getRequestIdFromMetadata(Metadata md) {
return md.get(REQUEST_HEADER_KEY);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import com.google.cloud.spanner.CompositeTracer;
import com.google.cloud.spanner.SpannerExceptionFactory;
import com.google.cloud.spanner.SpannerRpcMetrics;
import com.google.cloud.spanner.XGoogSpannerRequestId;
import com.google.common.cache.Cache;
import com.google.common.cache.CacheBuilder;
import com.google.spanner.admin.database.v1.DatabaseName;
Expand Down Expand Up @@ -174,6 +175,10 @@ private void processHeader(
}
if (span != null) {
span.setAttribute("gfe_latency", String.valueOf(gfeLatency));
String reqId = XGoogSpannerRequestId.getRequestIdFromMetadata(metadata);
if (reqId != null) {
span.setAttribute("x_goog_spanner_request_id", reqId);
}
}
} else {
measureMap.put(SPANNER_GFE_HEADER_MISSING_COUNT, 1L).record(tagContext);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -748,6 +748,13 @@ public void testTransactionRunnerWithRetryOnBeginTransaction() {
beginTransactionSpan.toString(),
beginTransactionSpan.getEvents().stream()
.anyMatch(event -> event.getName().equals("Starting RPC retry 1")));
verifyAtLeast1SpanHasXGoogSpannerRequestIdAttribute(finishedSpans);
}

private void verifyAtLeast1SpanHasXGoogSpannerRequestIdAttribute(List<SpanData> finishedSpans) {
AttributeKey<String> attributeKey = AttributeKey.stringKey("x_goog_spanner_request_id");
assertTrue(
finishedSpans.stream().anyMatch(span -> !span.getAttributes().get(attributeKey).isEmpty()));
}

@Test
Expand Down Expand Up @@ -798,6 +805,7 @@ public void testSingleUseRetryOnExecuteStreamingSql() {
executeStreamingQuery.toString(),
executeStreamingQuery.getEvents().stream()
.anyMatch(event -> event.getName().contains("Stream broken. Safe to retry")));
verifyAtLeast1SpanHasXGoogSpannerRequestIdAttribute(finishedSpans);
}

@Test
Expand Down Expand Up @@ -845,6 +853,7 @@ public void testRetryOnExecuteSql() {
executeSqlSpan.toString(),
executeSqlSpan.getEvents().stream()
.anyMatch(event -> event.getName().equals("Starting RPC retry 1")));
verifyAtLeast1SpanHasXGoogSpannerRequestIdAttribute(finishedSpans);
}

@Test
Expand All @@ -866,12 +875,14 @@ public void testTableAttributes() {
}
return null;
});
List<SpanData> finishedSpans = spanExporter.getFinishedSpanItems();
SpanData spanData =
spanExporter.getFinishedSpanItems().stream()
finishedSpans.stream()
.filter(x -> x.getName().equals("CloudSpannerOperation.ExecuteStreamingRead"))
.findFirst()
.get();
verifyTableAttributes(spanData);
verifyAtLeast1SpanHasXGoogSpannerRequestIdAttribute(finishedSpans);
}

private void waitForFinishedSpans(int numExpectedSpans) {
Expand Down
Loading