fix: change server timing duration attribute to float as per w3c (#3851) · googleapis/java-spanner@b3d53e8 · GitHub | Latest TMZ Celebrity News & Gossip | Watch TMZ Live
Skip to content

Commit b3d53e8

Browse files
fix: change server timing duration attribute to float as per w3c (#3851)
* fix: change server timing duration attribute to float as per w3c * fix clirr * remove deprecated warning --------- Co-authored-by: rahul yadav <rahulyadavsep92@gmail.com> Co-authored-by: rahul2393 <irahul@google.com>
1 parent ffda3b1 commit b3d53e8

File tree

6 files changed

+55
-39
lines changed

6 files changed

+55
-39
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,8 +97,8 @@ class BuiltInMetricsRecorder extends OpenTelemetryMetricsRecorder {
9797
* @param attributes Map of the attributes to store
9898
*/
9999
void recordServerTimingHeaderMetrics(
100-
Long gfeLatency,
101-
Long afeLatency,
100+
Float gfeLatency,
101+
Float afeLatency,
102102
Long gfeHeaderMissingCount,
103103
Long afeHeaderMissingCount,
104104
Map<String, String> attributes) {

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,8 @@ class BuiltInMetricsTracer extends MetricsTracer implements ApiTracer {
3737
private final BuiltInMetricsRecorder builtInOpenTelemetryMetricsRecorder;
3838
// These are RPC specific attributes and pertain to a specific API Trace
3939
private final Map<String, String> attributes = new HashMap<>();
40-
private Long gfeLatency = null;
41-
private Long afeLatency = null;
40+
private Float gfeLatency = null;
41+
private Float afeLatency = null;
4242
private long gfeHeaderMissingCount = 0;
4343
private long afeHeaderMissingCount = 0;
4444

@@ -119,11 +119,11 @@ public void attemptPermanentFailure(Throwable error) {
119119
gfeLatency, afeLatency, gfeHeaderMissingCount, afeHeaderMissingCount, attributes);
120120
}
121121

122-
void recordGFELatency(Long gfeLatency) {
122+
void recordGFELatency(Float gfeLatency) {
123123
this.gfeLatency = gfeLatency;
124124
}
125125

126-
void recordAFELatency(Long afeLatency) {
126+
void recordAFELatency(Float afeLatency) {
127127
this.afeLatency = afeLatency;
128128
}
129129

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

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@ public void addAttributes(Map<String, String> attributes) {
194194
public void recordGFELatency(Long gfeLatency) {
195195
for (ApiTracer child : children) {
196196
if (child instanceof BuiltInMetricsTracer) {
197-
((BuiltInMetricsTracer) child).recordGFELatency(gfeLatency);
197+
((BuiltInMetricsTracer) child).recordGFELatency(Float.valueOf(gfeLatency));
198198
}
199199
}
200200
}
@@ -210,7 +210,7 @@ public void recordGfeHeaderMissingCount(Long value) {
210210
public void recordAFELatency(Long afeLatency) {
211211
for (ApiTracer child : children) {
212212
if (child instanceof BuiltInMetricsTracer) {
213-
((BuiltInMetricsTracer) child).recordAFELatency(afeLatency);
213+
((BuiltInMetricsTracer) child).recordAFELatency(Float.valueOf(afeLatency));
214214
}
215215
}
216216
}
@@ -222,4 +222,20 @@ public void recordAfeHeaderMissingCount(Long value) {
222222
}
223223
}
224224
}
225+
226+
public void recordGFELatency(Float gfeLatency) {
227+
for (ApiTracer child : children) {
228+
if (child instanceof BuiltInMetricsTracer) {
229+
((BuiltInMetricsTracer) child).recordGFELatency(gfeLatency);
230+
}
231+
}
232+
}
233+
234+
public void recordAFELatency(Float afeLatency) {
235+
for (ApiTracer child : children) {
236+
if (child instanceof BuiltInMetricsTracer) {
237+
((BuiltInMetricsTracer) child).recordAFELatency(afeLatency);
238+
}
239+
}
240+
}
225241
}

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

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ class HeaderInterceptor implements ClientInterceptor {
7676
private static final Metadata.Key<String> GOOGLE_CLOUD_RESOURCE_PREFIX_KEY =
7777
Metadata.Key.of("google-cloud-resource-prefix", Metadata.ASCII_STRING_MARSHALLER);
7878
private static final Pattern SERVER_TIMING_PATTERN =
79-
Pattern.compile("(?<metricName>[a-zA-Z0-9_-]+);\\s*dur=(?<duration>\\d+)");
79+
Pattern.compile("(?<metricName>[a-zA-Z0-9_-]+);\\s*dur=(?<duration>\\d+(\\.\\d+)?)");
8080
private static final Pattern GOOGLE_CLOUD_RESOURCE_PREFIX_PATTERN =
8181
Pattern.compile(
8282
".*projects/(?<project>\\p{ASCII}[^/]*)(/instances/(?<instance>\\p{ASCII}[^/]*))?(/databases/(?<database>\\p{ASCII}[^/]*))?");
@@ -164,15 +164,15 @@ private void processHeader(
164164
// would fail to parse it correctly. To make the parsing more robust, the logic has been
165165
// updated to handle multiple metrics gracefully.
166166

167-
Map<String, Long> serverTimingMetrics = parseServerTimingHeader(serverTiming);
167+
Map<String, Float> serverTimingMetrics = parseServerTimingHeader(serverTiming);
168168
if (serverTimingMetrics.containsKey(GFE_TIMING_HEADER)) {
169-
long gfeLatency = serverTimingMetrics.get(GFE_TIMING_HEADER);
169+
float gfeLatency = serverTimingMetrics.get(GFE_TIMING_HEADER);
170170

171-
measureMap.put(SPANNER_GFE_LATENCY, gfeLatency);
171+
measureMap.put(SPANNER_GFE_LATENCY, (long) gfeLatency);
172172
measureMap.put(SPANNER_GFE_HEADER_MISSING_COUNT, 0L);
173173
measureMap.record(tagContext);
174174

175-
spannerRpcMetrics.recordGfeLatency(gfeLatency, attributes);
175+
spannerRpcMetrics.recordGfeLatency((long) gfeLatency, attributes);
176176
spannerRpcMetrics.recordGfeHeaderMissingCount(0L, attributes);
177177
if (compositeTracer != null && !isDirectPathUsed) {
178178
compositeTracer.recordGFELatency(gfeLatency);
@@ -191,7 +191,7 @@ private void processHeader(
191191
// Record AFE metrics
192192
if (compositeTracer != null && GapicSpannerRpc.isEnableAFEServerTiming()) {
193193
if (serverTimingMetrics.containsKey(AFE_TIMING_HEADER)) {
194-
long afeLatency = serverTimingMetrics.get(AFE_TIMING_HEADER);
194+
float afeLatency = serverTimingMetrics.get(AFE_TIMING_HEADER);
195195
compositeTracer.recordAFELatency(afeLatency);
196196
} else {
197197
compositeTracer.recordAfeHeaderMissingCount(1L);
@@ -202,16 +202,16 @@ private void processHeader(
202202
}
203203
}
204204

205-
private Map<String, Long> parseServerTimingHeader(String serverTiming) {
206-
Map<String, Long> serverTimingMetrics = new HashMap<>();
205+
private Map<String, Float> parseServerTimingHeader(String serverTiming) {
206+
Map<String, Float> serverTimingMetrics = new HashMap<>();
207207
if (serverTiming != null) {
208208
Matcher matcher = SERVER_TIMING_PATTERN.matcher(serverTiming);
209209
while (matcher.find()) {
210210
String metricName = matcher.group("metricName");
211211
String durationStr = matcher.group("duration");
212212

213213
if (metricName != null && durationStr != null) {
214-
serverTimingMetrics.put(metricName, Long.valueOf(durationStr));
214+
serverTimingMetrics.put(metricName, Float.valueOf(durationStr));
215215
}
216216
}
217217
}

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

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
import java.util.Random;
3232
import java.util.concurrent.ExecutorService;
3333
import java.util.concurrent.Executors;
34-
import java.util.concurrent.atomic.AtomicInteger;
34+
import java.util.concurrent.atomic.AtomicReference;
3535
import org.junit.After;
3636
import org.junit.AfterClass;
3737
import org.junit.Before;
@@ -44,11 +44,10 @@ abstract class AbstractNettyMockServerTest {
4444
protected static InetSocketAddress address;
4545
static ExecutorService executor;
4646
protected static LocalChannelProvider channelProvider;
47-
protected static AtomicInteger fakeServerTiming =
48-
new AtomicInteger(new Random().nextInt(1000) + 1);
49-
50-
protected static AtomicInteger fakeAFEServerTiming =
51-
new AtomicInteger(new Random().nextInt(500) + 1);
47+
protected static final AtomicReference<Float> fakeServerTiming =
48+
new AtomicReference<>((float) (new Random().nextDouble() * 1000) + 1);
49+
protected static final AtomicReference<Float> fakeAFEServerTiming =
50+
new AtomicReference<>((float) new Random().nextInt(500) + 1);
5251

5352
protected Spanner spanner;
5453

@@ -76,7 +75,7 @@ public void sendHeaders(Metadata headers) {
7675
headers.put(
7776
Metadata.Key.of("server-timing", Metadata.ASCII_STRING_MARSHALLER),
7877
String.format(
79-
"afe; dur=%d, gfet4t7; dur=%d",
78+
"afe; dur=%f, gfet4t7; dur=%f",
8079
fakeAFEServerTiming.get(), fakeServerTiming.get()));
8180
super.sendHeaders(headers);
8281
}

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

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ public class OpenTelemetryBuiltInMetricsTracerTest extends AbstractNettyMockServ
8585
Attributes.builder().put(BuiltInMetricsConstant.DIRECT_PATH_USED_KEY, "false").build();
8686
;
8787

88-
private static final long MIN_LATENCY = 0;
88+
private static final double MIN_LATENCY = 0;
8989

9090
private DatabaseClient client;
9191

@@ -160,7 +160,7 @@ public void testMetricsSingleUseQuery() {
160160
assertFalse(resultSet.next());
161161
}
162162

163-
long elapsed = stopwatch.elapsed(TimeUnit.MILLISECONDS);
163+
double elapsed = stopwatch.elapsed(TimeUnit.MILLISECONDS);
164164
Attributes expectedAttributes =
165165
expectedCommonBaseAttributes.toBuilder()
166166
.putAll(expectedCommonRequestAttributes)
@@ -171,13 +171,14 @@ public void testMetricsSingleUseQuery() {
171171
MetricData operationLatencyMetricData =
172172
getMetricData(metricReader, BuiltInMetricsConstant.OPERATION_LATENCIES_NAME);
173173
assertNotNull(operationLatencyMetricData);
174-
long operationLatencyValue = getAggregatedValue(operationLatencyMetricData, expectedAttributes);
174+
double operationLatencyValue =
175+
getAggregatedValue(operationLatencyMetricData, expectedAttributes);
175176
assertThat(operationLatencyValue).isIn(Range.closed(MIN_LATENCY, elapsed));
176177

177178
MetricData attemptLatencyMetricData =
178179
getMetricData(metricReader, BuiltInMetricsConstant.ATTEMPT_LATENCIES_NAME);
179180
assertNotNull(attemptLatencyMetricData);
180-
long attemptLatencyValue = getAggregatedValue(attemptLatencyMetricData, expectedAttributes);
181+
double attemptLatencyValue = getAggregatedValue(attemptLatencyMetricData, expectedAttributes);
181182
assertThat(attemptLatencyValue).isIn(Range.closed(MIN_LATENCY, elapsed));
182183

183184
MetricData operationCountMetricData =
@@ -198,12 +199,12 @@ public void testMetricsSingleUseQuery() {
198199
// AFE metrics are enabled for DirectPath.
199200
MetricData afeLatencyMetricData =
200201
getMetricData(metricReader, BuiltInMetricsConstant.AFE_LATENCIES_NAME);
201-
long afeLatencyValue = getAggregatedValue(afeLatencyMetricData, expectedAttributes);
202+
double afeLatencyValue = getAggregatedValue(afeLatencyMetricData, expectedAttributes);
202203
assertEquals(fakeAFEServerTiming.get(), afeLatencyValue, 0);
203204
} else {
204205
MetricData gfeLatencyMetricData =
205206
getMetricData(metricReader, BuiltInMetricsConstant.GFE_LATENCIES_NAME);
206-
long gfeLatencyValue = getAggregatedValue(gfeLatencyMetricData, expectedAttributes);
207+
double gfeLatencyValue = getAggregatedValue(gfeLatencyMetricData, expectedAttributes);
207208
assertEquals(fakeServerTiming.get(), gfeLatencyValue, 0);
208209
assertFalse(checkIfMetricExists(metricReader, BuiltInMetricsConstant.AFE_LATENCIES_NAME));
209210
}
@@ -237,7 +238,7 @@ public void testMetricsSingleUseQueryWithAfeEnabled() throws Exception {
237238
assertFalse(resultSet.next());
238239
}
239240

240-
long elapsed = stopwatch.elapsed(TimeUnit.MILLISECONDS);
241+
double elapsed = stopwatch.elapsed(TimeUnit.MILLISECONDS);
241242
Attributes expectedAttributes =
242243
expectedCommonBaseAttributes.toBuilder()
243244
.putAll(expectedCommonRequestAttributes)
@@ -248,14 +249,14 @@ public void testMetricsSingleUseQueryWithAfeEnabled() throws Exception {
248249
MetricData operationLatencyMetricData =
249250
getMetricData(metricReader, BuiltInMetricsConstant.OPERATION_LATENCIES_NAME);
250251
assertNotNull(operationLatencyMetricData);
251-
long operationLatencyValue =
252+
double operationLatencyValue =
252253
getAggregatedValue(operationLatencyMetricData, expectedAttributes);
253254
assertThat(operationLatencyValue).isIn(Range.closed(MIN_LATENCY, elapsed));
254255

255256
MetricData attemptLatencyMetricData =
256257
getMetricData(metricReader, BuiltInMetricsConstant.ATTEMPT_LATENCIES_NAME);
257258
assertNotNull(attemptLatencyMetricData);
258-
long attemptLatencyValue = getAggregatedValue(attemptLatencyMetricData, expectedAttributes);
259+
double attemptLatencyValue = getAggregatedValue(attemptLatencyMetricData, expectedAttributes);
259260
assertThat(attemptLatencyValue).isIn(Range.closed(MIN_LATENCY, elapsed));
260261

261262
MetricData operationCountMetricData =
@@ -274,12 +275,12 @@ public void testMetricsSingleUseQueryWithAfeEnabled() throws Exception {
274275
checkIfMetricExists(metricReader, BuiltInMetricsConstant.AFE_CONNECTIVITY_ERROR_NAME));
275276
MetricData afeLatencyMetricData =
276277
getMetricData(metricReader, BuiltInMetricsConstant.AFE_LATENCIES_NAME);
277-
long afeLatencyValue = getAggregatedValue(afeLatencyMetricData, expectedAttributes);
278+
double afeLatencyValue = getAggregatedValue(afeLatencyMetricData, expectedAttributes);
278279
assertEquals(fakeAFEServerTiming.get(), afeLatencyValue, 0);
279280

280281
MetricData gfeLatencyMetricData =
281282
getMetricData(metricReader, BuiltInMetricsConstant.GFE_LATENCIES_NAME);
282-
long gfeLatencyValue = getAggregatedValue(gfeLatencyMetricData, expectedAttributes);
283+
double gfeLatencyValue = getAggregatedValue(gfeLatencyMetricData, expectedAttributes);
283284
assertEquals(fakeServerTiming.get(), gfeLatencyValue, 0);
284285
} finally {
285286
writeableEnvironmentVariables.remove("GOOGLE_CLOUD_SPANNER_MULTIPLEXED_SESSIONS");
@@ -409,7 +410,7 @@ public void testNoNetworkConnection() {
409410

410411
// Attempt count should have a failed metric point for CreateSession.
411412
assertEquals(
412-
1, getAggregatedValue(attemptCountMetricData, expectedAttributesCreateSessionFailed));
413+
1, getAggregatedValue(attemptCountMetricData, expectedAttributesCreateSessionFailed), 0);
413414
}
414415

415416
@Test
@@ -523,14 +524,14 @@ private boolean checkIfMetricExists(InMemoryMetricReader reader, String metricNa
523524
return false;
524525
}
525526

526-
private long getAggregatedValue(MetricData metricData, Attributes attributes) {
527+
private float getAggregatedValue(MetricData metricData, Attributes attributes) {
527528
switch (metricData.getType()) {
528529
case HISTOGRAM:
529530
return metricData.getHistogramData().getPoints().stream()
530531
.filter(pd -> pd.getAttributes().equals(attributes))
531-
.map(data -> (long) data.getSum() / data.getCount())
532+
.map(data -> (float) data.getSum() / data.getCount())
532533
.findFirst()
533-
.orElse(0L);
534+
.orElse(0F);
534535
case LONG_SUM:
535536
return metricData.getLongSumData().getPoints().stream()
536537
.filter(pd -> pd.getAttributes().equals(attributes))

0 commit comments

Comments
 (0)

TMZ Celebrity News – Breaking Stories, Videos & Gossip

Looking for the latest TMZ celebrity news? You've come to the right place. From shocking Hollywood scandals to exclusive videos, TMZ delivers it all in real time.

Whether it’s a red carpet slip-up, a viral paparazzi moment, or a legal drama involving your favorite stars, TMZ news is always first to break the story. Stay in the loop with daily updates, insider tips, and jaw-dropping photos.

🎥 Watch TMZ Live

TMZ Live brings you daily celebrity news and interviews straight from the TMZ newsroom. Don’t miss a beat—watch now and see what’s trending in Hollywood.