Skip to content

Commit 1c37cf4

Browse files
committed
request_id
1 parent 6762b9e commit 1c37cf4

File tree

6 files changed

+67
-6
lines changed

6 files changed

+67
-6
lines changed

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616

1717
package com.google.cloud.spanner;
1818

19+
import static com.google.cloud.spanner.XGoogSpannerRequestId.REQUEST_ID;
20+
1921
import com.google.api.core.InternalApi;
2022
import com.google.api.gax.tracing.OpenTelemetryMetricsRecorder;
2123
import com.google.common.collect.ImmutableList;
@@ -26,7 +28,9 @@
2628
import io.opentelemetry.sdk.metrics.InstrumentSelector;
2729
import io.opentelemetry.sdk.metrics.InstrumentType;
2830
import io.opentelemetry.sdk.metrics.View;
31+
import java.util.Arrays;
2932
import java.util.Collection;
33+
import java.util.HashSet;
3034
import java.util.List;
3135
import java.util.Map;
3236
import java.util.Set;
@@ -94,6 +98,10 @@ public class BuiltInMetricsConstant {
9498
AttributeKey.stringKey("directpath_enabled");
9599
public static final AttributeKey<String> DIRECT_PATH_USED_KEY =
96100
AttributeKey.stringKey("directpath_used");
101+
public static final AttributeKey<String> REQUEST_ID_KEY =
102+
AttributeKey.stringKey(REQUEST_ID);
103+
public static Set<String> ALLOWED_EXEMPLARS_ATTRIBUTES = new HashSet<>(
104+
Arrays.asList(REQUEST_ID));
97105

98106
// IP address prefixes allocated for DirectPath backends.
99107
public static final String DP_IPV6_PREFIX = "2001:4860:8040";

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ class BuiltInMetricsTracer extends MetricsTracer implements ApiTracer {
6464
// filtered_attributes.
6565
// Below testmetric attribute will be available in exemplar as we have added a attributefilter
6666
// for our metric views.
67-
this.attributes.put("request_id", "test");
67+
// this.attributes.put("request_id", "test");
6868
this.traceWrapper = traceWrapper;
6969
this.currentSpan = currentSpan;
7070
}

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import static com.google.api.MetricDescriptor.ValueType.DISTRIBUTION;
2323
import static com.google.api.MetricDescriptor.ValueType.DOUBLE;
2424
import static com.google.api.MetricDescriptor.ValueType.INT64;
25+
import static com.google.cloud.spanner.BuiltInMetricsConstant.ALLOWED_EXEMPLARS_ATTRIBUTES;
2526
import static com.google.cloud.spanner.BuiltInMetricsConstant.GAX_METER_NAME;
2627
import static com.google.cloud.spanner.BuiltInMetricsConstant.GRPC_METER_NAME;
2728
import static com.google.cloud.spanner.BuiltInMetricsConstant.PROJECT_ID_KEY;
@@ -294,6 +295,12 @@ private static String makeSpanName(String projectId, String traceId, String span
294295
private static DroppedLabels mapFilteredAttributes(Attributes attributes) {
295296
DroppedLabels.Builder labels = DroppedLabels.newBuilder();
296297
attributes.forEach((k, v) -> labels.putLabel(cleanAttributeKey(k.getKey()), v.toString()));
298+
attributes.forEach((k, v) -> {
299+
String key = cleanAttributeKey(k.getKey());
300+
if (ALLOWED_EXEMPLARS_ATTRIBUTES.contains(key)) {
301+
labels.putLabel(key, v.toString());
302+
}
303+
});
297304
return labels.build();
298305
}
299306

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,9 @@ public class XGoogSpannerRequestId {
3535
@VisibleForTesting
3636
static final String RAND_PROCESS_ID = XGoogSpannerRequestId.generateRandProcessId();
3737

38+
static String REQUEST_ID = "x-goog-spanner-request-id";
3839
public static final Metadata.Key<String> REQUEST_HEADER_KEY =
39-
Metadata.Key.of("x-goog-spanner-request-id", Metadata.ASCII_STRING_MARSHALLER);
40+
Metadata.Key.of(REQUEST_ID, Metadata.ASCII_STRING_MARSHALLER);
4041

4142
@VisibleForTesting
4243
static final long VERSION = 1; // The version of the specification being implemented.

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,7 @@ public void onHeaders(Metadata metadata) {
128128
Boolean isDirectPathUsed =
129129
isDirectPathUsed(getAttributes().get(Grpc.TRANSPORT_ATTR_REMOTE_ADDR));
130130
addDirectPathUsedAttribute(compositeTracer, isDirectPathUsed);
131+
addRequestIdAttribute(compositeTracer, "test");
131132
processHeader(
132133
metadata, tagContext, attributes, span, compositeTracer, isDirectPathUsed);
133134
super.onHeaders(metadata);
@@ -308,6 +309,14 @@ private void addDirectPathUsedAttribute(
308309
}
309310
}
310311

312+
private void addRequestIdAttribute(
313+
CompositeTracer compositeTracer, String requestId) {
314+
if (compositeTracer != null) {
315+
compositeTracer.addAttributes(
316+
BuiltInMetricsConstant.REQUEST_ID_KEY.getKey(), requestId);
317+
}
318+
}
319+
311320
private Boolean isDirectPathUsed(SocketAddress remoteAddr) {
312321
if (remoteAddr instanceof InetSocketAddress) {
313322
InetAddress inetAddress = ((InetSocketAddress) remoteAddr).getAddress();

google-cloud-spanner/src/test/java/com/google/cloud/spanner/it/ITBuiltInMetricsTest.java

Lines changed: 40 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,13 @@
3535
import java.io.IOException;
3636
import java.time.Duration;
3737
import java.time.Instant;
38+
import java.util.ArrayList;
39+
import java.util.List;
40+
import java.util.concurrent.Callable;
41+
import java.util.concurrent.ExecutionException;
42+
import java.util.concurrent.ExecutorService;
43+
import java.util.concurrent.Executors;
44+
import java.util.concurrent.Future;
3845
import java.util.concurrent.TimeUnit;
3946
import org.junit.After;
4047
import org.junit.BeforeClass;
@@ -89,12 +96,41 @@ public void testBuiltinMetricsWithDefaultOTEL() throws Exception {
8996
.setEndTime(Timestamps.fromMillis(end.toEpochMilli()))
9097
.build();
9198

92-
for (int i = 0; i < 100; i++) {
93-
client
94-
.readWriteTransaction()
95-
.run(transaction -> transaction.executeQuery(Statement.of("Select 1")));
99+
int totalQueries = 1;
100+
int batchSize = 1;
101+
ExecutorService executor = Executors.newFixedThreadPool(100);
102+
103+
for (int batch = 0; batch < totalQueries / batchSize; batch++) {
104+
List<Callable<Void>> tasks = new ArrayList<>();
105+
for (int i = 0; i < batchSize; i++) {
106+
tasks.add(
107+
() -> {
108+
client
109+
.readWriteTransaction()
110+
.run(
111+
transaction -> {
112+
transaction.executeQuery(Statement.of("SELECT 1"));
113+
return null;
114+
});
115+
return null;
116+
});
117+
}
118+
119+
List<Future<Void>> futures = executor.invokeAll(tasks);
120+
for (Future<Void> future : futures) {
121+
try {
122+
future.get(); // propagate exceptions
123+
} catch (ExecutionException e) {
124+
System.err.println("Error in query: " + e.getCause());
125+
}
126+
}
127+
128+
System.out.println("Completed batch " + (batch + 1));
96129
}
97130

131+
executor.shutdown();
132+
executor.awaitTermination(1, TimeUnit.HOURS);
133+
98134
for (String metric : METRICS) {
99135
String metricFilter =
100136
String.format(

0 commit comments

Comments
 (0)