From 323eb332a65f39c5588cf65ecae0a3454903a1c6 Mon Sep 17 00:00:00 2001 From: Diego Marquez Date: Mon, 25 Nov 2024 23:42:00 -0500 Subject: [PATCH] feat: introduce `java.time` methods (#1729) * feat: introduce `java.time` methods * feat: introduce `java.time` methods * use add-opens to allow serialization of java.time.Duration * use add-opens in surefire only when running in jdk9+ * Revert "use add-opens in surefire only when running in jdk9+" This reverts commit 5a5abd436102c1fc8d89ef09ea829739cea310d5. * Revert "use add-opens to allow serialization of java.time.Duration" This reverts commit d1feef27525d2a52d80f3073c919d8cfa8711de6. --- .../com/google/cloud/logging/HttpRequest.java | 28 +++++++++++++++---- .../com/google/cloud/logging/LogEntry.java | 10 +++++++ .../logging/testing/RemoteLoggingHelper.java | 12 ++++---- .../com/google/cloud/logging/ContextTest.java | 4 +-- .../google/cloud/logging/HttpRequestTest.java | 4 +-- .../cloud/logging/LoggingOptionsTest.java | 4 +-- 6 files changed, 44 insertions(+), 18 deletions(-) diff --git a/google-cloud-logging/src/main/java/com/google/cloud/logging/HttpRequest.java b/google-cloud-logging/src/main/java/com/google/cloud/logging/HttpRequest.java index 4bf78e641..73e77b505 100644 --- a/google-cloud-logging/src/main/java/com/google/cloud/logging/HttpRequest.java +++ b/google-cloud-logging/src/main/java/com/google/cloud/logging/HttpRequest.java @@ -16,14 +16,18 @@ package com.google.cloud.logging; +import static com.google.api.gax.util.TimeConversionUtils.toJavaTimeDuration; +import static com.google.api.gax.util.TimeConversionUtils.toThreetenDuration; + import com.google.api.core.ApiFunction; +import com.google.api.core.ObsoleteApi; import com.google.cloud.StringEnumType; import com.google.cloud.StringEnumValue; import com.google.common.base.MoreObjects; import com.google.errorprone.annotations.CanIgnoreReturnValue; import java.io.Serializable; +import java.time.Duration; import java.util.Objects; -import org.threeten.bp.Duration; /** * Objects of this class represent information about the (optional) HTTP request associated with a @@ -51,7 +55,7 @@ public final class HttpRequest implements Serializable { private final boolean cacheHit; private final boolean cacheValidatedWithOriginServer; private final Long cacheFillBytes; - private final Duration latency; + private final java.time.Duration latency; /** The HTTP request method. */ public static final class RequestMethod extends StringEnumValue { @@ -112,7 +116,7 @@ public static final class Builder { private boolean cacheHit; private boolean cacheValidatedWithOriginServer; private Long cacheFillBytes; - private Duration latency; + private java.time.Duration latency; Builder() {} @@ -258,12 +262,18 @@ public Builder setCacheFillBytes(long cacheFillBytes) { return this; } + /** This method is obsolete. Use {@link #setLatencyDuration(java.time.Duration)} instead. */ + @ObsoleteApi("Use setLatencyDuration(java.time.Duration) instead") + public Builder setLatency(org.threeten.bp.Duration latency) { + return setLatencyDuration(toJavaTimeDuration(latency)); + } + /** * Sets the latency on the server, from the time the request was received until the response was * sent. */ @CanIgnoreReturnValue - public Builder setLatency(Duration latency) { + public Builder setLatencyDuration(java.time.Duration latency) { this.latency = latency; return this; } @@ -393,13 +403,19 @@ public Long getCacheFillBytes() { return cacheFillBytes; } + /** This method is obsolete. Use {@link #getLatencyDuration()} instead. */ + @ObsoleteApi("Use getLatencyDuration() instead") + public org.threeten.bp.Duration getLatency() { + return toThreetenDuration(getLatencyDuration()); + } + /** * Returns the processing latency on the server, from the time the request was received until the * response was sent. * * @return the latency, for null if not populated. */ - public Duration getLatency() { + public Duration getLatencyDuration() { return latency; } @@ -561,7 +577,7 @@ static HttpRequest fromPb(com.google.logging.type.HttpRequest requestPb) { } if (requestPb.hasLatency()) { // NOTE(pongad): Don't convert to nano; large durations overflow longs! - builder.setLatency( + builder.setLatencyDuration( Duration.ofSeconds( requestPb.getLatency().getSeconds(), requestPb.getLatency().getNanos())); } diff --git a/google-cloud-logging/src/main/java/com/google/cloud/logging/LogEntry.java b/google-cloud-logging/src/main/java/com/google/cloud/logging/LogEntry.java index 2fd872f09..6f3a5fd7c 100644 --- a/google-cloud-logging/src/main/java/com/google/cloud/logging/LogEntry.java +++ b/google-cloud-logging/src/main/java/com/google/cloud/logging/LogEntry.java @@ -34,6 +34,7 @@ import com.google.logging.v2.LogEntrySourceLocation; import com.google.logging.v2.LogName; import java.io.Serializable; +import java.time.Duration; import java.time.Instant; import java.util.HashMap; import java.util.Map; @@ -612,6 +613,14 @@ public JsonElement serialize( } } + static final class DurationSerializer implements JsonSerializer { + @Override + public JsonElement serialize( + Duration src, java.lang.reflect.Type typeOfSrc, JsonSerializationContext context) { + return new JsonPrimitive(src.toString()); + } + } + static final class SourceLocationSerializer implements JsonSerializer { @Override public JsonElement serialize( @@ -649,6 +658,7 @@ public StructuredLogFormatter(StringBuilder builder) { checkNotNull(builder); this.gson = new GsonBuilder() + .registerTypeAdapter(Duration.class, new DurationSerializer()) .registerTypeAdapter(Instant.class, new InstantSerializer()) .registerTypeAdapter(SourceLocation.class, new SourceLocationSerializer()) .registerTypeAdapter(HttpRequest.RequestMethod.class, new RequestMethodSerializer()) diff --git a/google-cloud-logging/src/main/java/com/google/cloud/logging/testing/RemoteLoggingHelper.java b/google-cloud-logging/src/main/java/com/google/cloud/logging/testing/RemoteLoggingHelper.java index db05fdb1c..d1cc027aa 100644 --- a/google-cloud-logging/src/main/java/com/google/cloud/logging/testing/RemoteLoggingHelper.java +++ b/google-cloud-logging/src/main/java/com/google/cloud/logging/testing/RemoteLoggingHelper.java @@ -22,10 +22,10 @@ import com.google.cloud.logging.LoggingOptions; import java.io.IOException; import java.io.InputStream; +import java.time.Duration; import java.util.UUID; import java.util.logging.Level; import java.util.logging.Logger; -import org.threeten.bp.Duration; /** * Utility to create a remote logging configuration for testing. Logging options can be obtained via @@ -101,13 +101,13 @@ public static String formatForTest(String name) { private static RetrySettings retrySettings() { return RetrySettings.newBuilder() - .setMaxRetryDelay(Duration.ofMillis(30000L)) - .setTotalTimeout(Duration.ofMillis(120000L)) - .setInitialRetryDelay(Duration.ofMillis(250L)) + .setMaxRetryDelayDuration(Duration.ofMillis(30000L)) + .setTotalTimeoutDuration(Duration.ofMillis(120000L)) + .setInitialRetryDelayDuration(Duration.ofMillis(250L)) .setRetryDelayMultiplier(1.0) - .setInitialRpcTimeout(Duration.ofMillis(120000L)) + .setInitialRpcTimeoutDuration(Duration.ofMillis(120000L)) .setRpcTimeoutMultiplier(1.0) - .setMaxRpcTimeout(Duration.ofMillis(120000L)) + .setMaxRpcTimeoutDuration(Duration.ofMillis(120000L)) .build(); } diff --git a/google-cloud-logging/src/test/java/com/google/cloud/logging/ContextTest.java b/google-cloud-logging/src/test/java/com/google/cloud/logging/ContextTest.java index 7ef8f90de..110e31b13 100644 --- a/google-cloud-logging/src/test/java/com/google/cloud/logging/ContextTest.java +++ b/google-cloud-logging/src/test/java/com/google/cloud/logging/ContextTest.java @@ -29,10 +29,10 @@ import io.opentelemetry.sdk.trace.SdkTracerProvider; import io.opentelemetry.sdk.trace.SpanProcessor; import io.opentelemetry.sdk.trace.export.SimpleSpanProcessor; +import java.time.Duration; import org.junit.Test; import org.junit.runner.RunWith; import org.junit.runners.JUnit4; -import org.threeten.bp.Duration; @RunWith(JUnit4.class) public class ContextTest { @@ -62,7 +62,7 @@ public class ContextTest { .setCacheHit(false) .setCacheValidatedWithOriginServer(true) .setCacheFillBytes(303L) - .setLatency(Duration.ofSeconds(123, 456)) + .setLatencyDuration(Duration.ofSeconds(123, 456)) .build(); private static final HttpRequest PARTIAL_REQUEST = HttpRequest.newBuilder() diff --git a/google-cloud-logging/src/test/java/com/google/cloud/logging/HttpRequestTest.java b/google-cloud-logging/src/test/java/com/google/cloud/logging/HttpRequestTest.java index 282937786..2646f95e3 100644 --- a/google-cloud-logging/src/test/java/com/google/cloud/logging/HttpRequestTest.java +++ b/google-cloud-logging/src/test/java/com/google/cloud/logging/HttpRequestTest.java @@ -22,10 +22,10 @@ import static org.junit.Assert.assertTrue; import com.google.cloud.logging.HttpRequest.RequestMethod; +import java.time.Duration; import org.junit.Test; import org.junit.runner.RunWith; import org.junit.runners.JUnit4; -import org.threeten.bp.Duration; @RunWith(JUnit4.class) public class HttpRequestTest { @@ -59,7 +59,7 @@ public class HttpRequestTest { .setCacheHit(CACHE_HIT) .setCacheValidatedWithOriginServer(CACHE_VALIDATED_WITH_ORIGIN_SERVER) .setCacheFillBytes(CACHE_FILL_BYTES) - .setLatency(Duration.ofSeconds(123, 456)) + .setLatencyDuration(Duration.ofSeconds(123, 456)) .build(); @Test diff --git a/google-cloud-logging/src/test/java/com/google/cloud/logging/LoggingOptionsTest.java b/google-cloud-logging/src/test/java/com/google/cloud/logging/LoggingOptionsTest.java index 977390bee..989823719 100644 --- a/google-cloud-logging/src/test/java/com/google/cloud/logging/LoggingOptionsTest.java +++ b/google-cloud-logging/src/test/java/com/google/cloud/logging/LoggingOptionsTest.java @@ -27,10 +27,10 @@ import com.google.api.gax.batching.FlowController; import com.google.cloud.NoCredentials; import com.google.cloud.TransportOptions; +import java.time.Duration; import org.junit.Test; import org.junit.runner.RunWith; import org.junit.runners.JUnit4; -import org.threeten.bp.Duration; @RunWith(JUnit4.class) public class LoggingOptionsTest { @@ -90,7 +90,7 @@ private static LoggingOptions generateLoggingOptions() { .setIsEnabled(true) .setElementCountThreshold(ELEMENTS_TRESHOLD_COUNT) .setRequestByteThreshold(REQUEST_BYTE_TRESHOLD_COUNT) - .setDelayThreshold(Duration.ofMillis(DURATION)) + .setDelayThresholdDuration(Duration.ofMillis(DURATION)) .setFlowControlSettings( FlowControlSettings.newBuilder() .setMaxOutstandingElementCount(MAX_OUTSTANDING_ELEMENTS_COUNT)