Skip to content

Commit fdc9550

Browse files
committed
afe_env and unit tests
1 parent 21dddef commit fdc9550

File tree

10 files changed

+158
-42
lines changed

10 files changed

+158
-42
lines changed

google-cloud-spanner/clirr-ignored-differences.xml

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -751,6 +751,13 @@
751751
<method>boolean isEnableBuiltInMetrics()</method>
752752
</difference>
753753

754+
<!-- Added AFE Server Timing option -->
755+
<difference>
756+
<differenceType>7012</differenceType>
757+
<className>com/google/cloud/spanner/SpannerOptions$SpannerEnvironment</className>
758+
<method>boolean isEnableAFEServerTiming()</method>
759+
</difference>
760+
754761
<!-- Added Monitoring host option -->
755762
<difference>
756763
<differenceType>7012</differenceType>
@@ -807,7 +814,7 @@
807814
<className>com/google/cloud/spanner/connection/Connection</className>
808815
<method>boolean isKeepTransactionAlive()</method>
809816
</difference>
810-
817+
811818
<!-- Automatic DML batching -->
812819
<difference>
813820
<differenceType>7012</differenceType>
@@ -839,7 +846,7 @@
839846
<className>com/google/cloud/spanner/connection/Connection</className>
840847
<method>boolean isAutoBatchDmlUpdateCountVerification()</method>
841848
</difference>
842-
849+
843850
<!-- Retry DML as Partitioned DML -->
844851
<difference>
845852
<differenceType>7012</differenceType>
@@ -863,7 +870,7 @@
863870
<className>com/google/cloud/spanner/connection/Connection</className>
864871
<method>java.lang.Object runTransaction(com.google.cloud.spanner.connection.Connection$TransactionCallable)</method>
865872
</difference>
866-
873+
867874
<!-- Added experimental host option -->
868875
<difference>
869876
<differenceType>7012</differenceType>
@@ -892,7 +899,7 @@
892899
<className>com/google/cloud/spanner/connection/Connection</className>
893900
<method>java.lang.String getDefaultSequenceKind()</method>
894901
</difference>
895-
902+
896903
<!-- Default isolation level -->
897904
<difference>
898905
<differenceType>7012</differenceType>
@@ -927,5 +934,5 @@
927934
<className>com/google/cloud/spanner/connection/ConnectionOptions</className>
928935
<field>VALID_PROPERTIES</field>
929936
</difference>
930-
937+
931938
</differences>

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

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -95,15 +95,20 @@ class BuiltInMetricsRecorder extends OpenTelemetryMetricsRecorder {
9595
* @param attributes Map of the attributes to store
9696
*/
9797
void recordServerTimingHeaderMetrics(
98-
double gfeLatency,
99-
double afeLatency,
98+
Long gfeLatency,
99+
Long afeLatency,
100100
Long gfeHeaderMissingCount,
101101
Long afeHeaderMissingCount,
102102
Map<String, String> attributes) {
103103
io.opentelemetry.api.common.Attributes otelAttributes = toOtelAttributes(attributes);
104-
gfeLatencyRecorder.record(gfeLatency, otelAttributes);
104+
if (gfeLatency != null) {
105+
gfeLatencyRecorder.record(gfeLatency, otelAttributes);
106+
}
105107
gfeHeaderMissingCountRecorder.add(gfeHeaderMissingCount, otelAttributes);
106-
afeLatencyRecorder.record(afeLatency, otelAttributes);
108+
109+
if (afeLatency != null) {
110+
afeLatencyRecorder.record(afeLatency, otelAttributes);
111+
}
107112
afeHeaderMissingCountRecorder.add(afeHeaderMissingCount, otelAttributes);
108113
}
109114

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

Lines changed: 15 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -60,11 +60,9 @@ class BuiltInMetricsTracer extends MetricsTracer implements ApiTracer {
6060
@Override
6161
public void attemptSucceeded() {
6262
super.attemptSucceeded();
63-
if (gfeLatency != null) {
64-
attributes.put(STATUS_ATTRIBUTE, StatusCode.Code.OK.toString());
65-
builtInOpenTelemetryMetricsRecorder.recordServerTimingHeaderMetrics(
66-
gfeLatency, afeLatency, gfeHeaderMissingCount, afeHeaderMissingCount, attributes);
67-
}
63+
attributes.put(STATUS_ATTRIBUTE, StatusCode.Code.OK.toString());
64+
builtInOpenTelemetryMetricsRecorder.recordServerTimingHeaderMetrics(
65+
gfeLatency, afeLatency, gfeHeaderMissingCount, afeHeaderMissingCount, attributes);
6866
}
6967

7068
/**
@@ -74,11 +72,9 @@ public void attemptSucceeded() {
7472
@Override
7573
public void attemptCancelled() {
7674
super.attemptCancelled();
77-
if (gfeLatency != null) {
78-
attributes.put(STATUS_ATTRIBUTE, StatusCode.Code.CANCELLED.toString());
79-
builtInOpenTelemetryMetricsRecorder.recordServerTimingHeaderMetrics(
80-
gfeLatency, afeLatency, gfeHeaderMissingCount, afeHeaderMissingCount, attributes);
81-
}
75+
attributes.put(STATUS_ATTRIBUTE, StatusCode.Code.CANCELLED.toString());
76+
builtInOpenTelemetryMetricsRecorder.recordServerTimingHeaderMetrics(
77+
gfeLatency, afeLatency, gfeHeaderMissingCount, afeHeaderMissingCount, attributes);
8278
}
8379

8480
/**
@@ -92,11 +88,9 @@ public void attemptCancelled() {
9288
@Override
9389
public void attemptFailedDuration(Throwable error, java.time.Duration delay) {
9490
super.attemptFailedDuration(error, delay);
95-
if (gfeLatency != null) {
96-
attributes.put(STATUS_ATTRIBUTE, extractStatus(error));
97-
builtInOpenTelemetryMetricsRecorder.recordServerTimingHeaderMetrics(
98-
gfeLatency, afeLatency, gfeHeaderMissingCount, afeHeaderMissingCount, attributes);
99-
}
91+
attributes.put(STATUS_ATTRIBUTE, extractStatus(error));
92+
builtInOpenTelemetryMetricsRecorder.recordServerTimingHeaderMetrics(
93+
gfeLatency, afeLatency, gfeHeaderMissingCount, afeHeaderMissingCount, attributes);
10094
}
10195

10296
/**
@@ -109,11 +103,9 @@ public void attemptFailedDuration(Throwable error, java.time.Duration delay) {
109103
@Override
110104
public void attemptFailedRetriesExhausted(Throwable error) {
111105
super.attemptFailedRetriesExhausted(error);
112-
if (gfeLatency != null) {
113-
attributes.put(STATUS_ATTRIBUTE, extractStatus(error));
114-
builtInOpenTelemetryMetricsRecorder.recordServerTimingHeaderMetrics(
115-
gfeLatency, afeLatency, gfeHeaderMissingCount, afeHeaderMissingCount, attributes);
116-
}
106+
attributes.put(STATUS_ATTRIBUTE, extractStatus(error));
107+
builtInOpenTelemetryMetricsRecorder.recordServerTimingHeaderMetrics(
108+
gfeLatency, afeLatency, gfeHeaderMissingCount, afeHeaderMissingCount, attributes);
117109
}
118110

119111
/**
@@ -126,11 +118,9 @@ public void attemptFailedRetriesExhausted(Throwable error) {
126118
@Override
127119
public void attemptPermanentFailure(Throwable error) {
128120
super.attemptPermanentFailure(error);
129-
if (gfeLatency != null) {
130-
attributes.put(STATUS_ATTRIBUTE, extractStatus(error));
131-
builtInOpenTelemetryMetricsRecorder.recordServerTimingHeaderMetrics(
132-
gfeLatency, afeLatency, gfeHeaderMissingCount, afeHeaderMissingCount, attributes);
133-
}
121+
attributes.put(STATUS_ATTRIBUTE, extractStatus(error));
122+
builtInOpenTelemetryMetricsRecorder.recordServerTimingHeaderMetrics(
123+
gfeLatency, afeLatency, gfeHeaderMissingCount, afeHeaderMissingCount, attributes);
134124
}
135125

136126
void recordGFELatency(Long gfeLatency) {

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -848,6 +848,10 @@ default boolean isEnableBuiltInMetrics() {
848848
return true;
849849
}
850850

851+
default boolean isEnableAFEServerTiming() {
852+
return true;
853+
}
854+
851855
default boolean isEnableEndToEndTracing() {
852856
return false;
853857
}
@@ -878,6 +882,8 @@ private static class SpannerEnvironmentImpl implements SpannerEnvironment {
878882
private static final String SPANNER_ENABLE_END_TO_END_TRACING =
879883
"SPANNER_ENABLE_END_TO_END_TRACING";
880884
private static final String SPANNER_DISABLE_BUILTIN_METRICS = "SPANNER_DISABLE_BUILTIN_METRICS";
885+
private static final String SPANNER_DISABLE_AFE_SERVER_TIMING =
886+
"SPANNER_DISABLE_AFE_SERVER_TIMING";
881887
private static final String SPANNER_MONITORING_HOST = "SPANNER_MONITORING_HOST";
882888

883889
private SpannerEnvironmentImpl() {}
@@ -910,6 +916,11 @@ public boolean isEnableBuiltInMetrics() {
910916
return !Boolean.parseBoolean(System.getenv(SPANNER_DISABLE_BUILTIN_METRICS));
911917
}
912918

919+
@Override
920+
public boolean isEnableAFEServerTiming() {
921+
return !Boolean.parseBoolean(System.getenv(SPANNER_DISABLE_AFE_SERVER_TIMING));
922+
}
923+
913924
@Override
914925
public boolean isEnableEndToEndTracing() {
915926
return Boolean.parseBoolean(System.getenv(SPANNER_ENABLE_END_TO_END_TRACING));
@@ -2075,6 +2086,10 @@ public boolean isEndToEndTracingEnabled() {
20752086
return enableEndToEndTracing;
20762087
}
20772088

2089+
public boolean isEnableAFEServerTiming() {
2090+
return SpannerOptions.environment.isEnableAFEServerTiming();
2091+
}
2092+
20782093
/** Returns the default query options to use for the specific database. */
20792094
public QueryOptions getDefaultQueryOptions(DatabaseId databaseId) {
20802095
// Use the specific query options for the database if any have been specified. These have

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -275,6 +275,8 @@ public class GapicSpannerRpc implements SpannerRpc {
275275
new ConcurrentHashMap<>();
276276
private final boolean leaderAwareRoutingEnabled;
277277
private final boolean endToEndTracingEnabled;
278+
279+
private final boolean afeServerTimingEnabled;
278280
private final int numChannels;
279281
private final boolean isGrpcGcpExtensionEnabled;
280282

@@ -331,6 +333,7 @@ public GapicSpannerRpc(final SpannerOptions options) {
331333
this.compressorName = options.getCompressorName();
332334
this.leaderAwareRoutingEnabled = options.isLeaderAwareRoutingEnabled();
333335
this.endToEndTracingEnabled = options.isEndToEndTracingEnabled();
336+
this.afeServerTimingEnabled = options.isEnableAFEServerTiming();
334337
this.numChannels = options.getNumChannels();
335338
this.isGrpcGcpExtensionEnabled = options.isGrpcGcpExtensionEnabled();
336339

@@ -2030,6 +2033,9 @@ <ReqT, RespT> GrpcCallContext newCallContext(
20302033
if (endToEndTracingEnabled) {
20312034
context = context.withExtraHeaders(metadataProvider.newEndToEndTracingHeader());
20322035
}
2036+
if (afeServerTimingEnabled) {
2037+
context = context.withExtraHeaders(metadataProvider.newAfeServerTimingHeader());
2038+
}
20332039
if (callCredentialsProvider != null) {
20342040
CallCredentials callCredentials = callCredentialsProvider.getCallCredentials();
20352041
if (callCredentials != null) {

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

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -186,8 +186,6 @@ private void processHeader(
186186
}
187187
}
188188

189-
// Record AFE latency
190-
// TODO: Add condition to check if AFE is enabled
191189
if (compositeTracer != null) {
192190
if (serverTimingMetrics.containsKey(AFE_TIMING_HEADER)) {
193191
long afeLatency = serverTimingMetrics.get(AFE_TIMING_HEADER);

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ class SpannerMetadataProvider {
3838
private final String resourceHeaderKey;
3939
private static final String ROUTE_TO_LEADER_HEADER_KEY = "x-goog-spanner-route-to-leader";
4040
private static final String END_TO_END_TRACING_HEADER_KEY = "x-goog-spanner-end-to-end-tracing";
41+
private static final String AFE_SERVER_TIMING_HEADER_KEY =
42+
"x-goog-spanner-enable-afe-server-timing";
4143
private static final Pattern[] RESOURCE_TOKEN_PATTERNS = {
4244
Pattern.compile("^(?<headerValue>projects/[^/]*/instances/[^/]*/databases/[^/]*)(.*)?"),
4345
Pattern.compile("^(?<headerValue>projects/[^/]*/instances/[^/]*)(.*)?")
@@ -47,6 +49,8 @@ class SpannerMetadataProvider {
4749
ImmutableMap.of(ROUTE_TO_LEADER_HEADER_KEY, Collections.singletonList("true"));
4850
private static final Map<String, List<String>> END_TO_END_TRACING_HEADER_MAP =
4951
ImmutableMap.of(END_TO_END_TRACING_HEADER_KEY, Collections.singletonList("true"));
52+
private static final Map<String, List<String>> AFE_SERVER_TIMING_HEADER_MAP =
53+
ImmutableMap.of(AFE_SERVER_TIMING_HEADER_KEY, Collections.singletonList("true"));
5054

5155
private SpannerMetadataProvider(Map<String, String> headers, String resourceHeaderKey) {
5256
this.resourceHeaderKey = resourceHeaderKey;
@@ -96,6 +100,10 @@ Map<String, List<String>> newEndToEndTracingHeader() {
96100
return END_TO_END_TRACING_HEADER_MAP;
97101
}
98102

103+
Map<String, List<String>> newAfeServerTimingHeader() {
104+
return AFE_SERVER_TIMING_HEADER_MAP;
105+
}
106+
99107
private Map<Metadata.Key<String>, String> constructHeadersAsMetadata(
100108
Map<String, String> headers) {
101109
ImmutableMap.Builder<Metadata.Key<String>, String> headersAsMetadataBuilder =

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,9 @@ abstract class AbstractNettyMockServerTest {
4747
protected static AtomicInteger fakeServerTiming =
4848
new AtomicInteger(new Random().nextInt(1000) + 1);
4949

50+
protected static AtomicInteger fakeAFEServerTiming =
51+
new AtomicInteger(new Random().nextInt(500) + 1);
52+
5053
protected Spanner spanner;
5154

5255
@BeforeClass
@@ -72,7 +75,9 @@ public <ReqT, RespT> ServerCall.Listener<ReqT> interceptCall(
7275
public void sendHeaders(Metadata headers) {
7376
headers.put(
7477
Metadata.Key.of("server-timing", Metadata.ASCII_STRING_MARSHALLER),
75-
String.format("gfet4t7; dur=%d", fakeServerTiming.get()));
78+
String.format(
79+
"afet4t7; dur=%d, gfet4t7; dur=%d",
80+
fakeAFEServerTiming.get(), fakeServerTiming.get()));
7681
super.sendHeaders(headers);
7782
}
7883
},

0 commit comments

Comments
 (0)