From e61c325ba37d883e7a91f41c8f18bf0bdd698d21 Mon Sep 17 00:00:00 2001 From: diegomarquezp Date: Tue, 19 Nov 2024 16:32:35 +0000 Subject: [PATCH 1/6] feat: introduce `java.time` methods --- .../com/google/cloud/logging/HttpRequest.java | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 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..6b66203d3 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,6 +16,8 @@ package com.google.cloud.logging; +import static com.google.api.gax.util.TimeConversionUtils.toJavaTimeDuration; + import com.google.api.core.ApiFunction; import com.google.cloud.StringEnumType; import com.google.cloud.StringEnumValue; @@ -23,7 +25,6 @@ import com.google.errorprone.annotations.CanIgnoreReturnValue; import java.io.Serializable; 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 +52,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 +113,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 +259,19 @@ public Builder setCacheFillBytes(long cacheFillBytes) { return this; } + /** + * This method + */ + 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; } From b521713c224b3be9fc167dfd4b22cdee953a4de2 Mon Sep 17 00:00:00 2001 From: diegomarquezp Date: Tue, 19 Nov 2024 16:32:35 +0000 Subject: [PATCH 2/6] feat: introduce `java.time` methods --- .../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) From d1feef27525d2a52d80f3073c919d8cfa8711de6 Mon Sep 17 00:00:00 2001 From: Diego Alonso Marquez Palacios Date: Thu, 21 Nov 2024 20:32:58 -0500 Subject: [PATCH 3/6] use add-opens to allow serialization of java.time.Duration --- .../com/google/cloud/logging/LogEntry.java | 10 ------ pom.xml | 32 +++++++++++++++++++ 2 files changed, 32 insertions(+), 10 deletions(-) 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 6f3a5fd7c..2fd872f09 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,7 +34,6 @@ 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; @@ -613,14 +612,6 @@ 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( @@ -658,7 +649,6 @@ 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/pom.xml b/pom.xml index 8ae30ca0b..377e9b31a 100644 --- a/pom.xml +++ b/pom.xml @@ -197,6 +197,38 @@ org.objenesis:objenesis + + org.apache.maven.plugins + maven-jar-plugin + + + default-jar + package + + jar + + + + + + java.base/java.time=ALL-UNNAMED + + + + + + + + org.apache.maven.plugins + maven-surefire-plugin + + + --add-opens java.base/java.time=ALL-UNNAMED + + From 5a5abd436102c1fc8d89ef09ea829739cea310d5 Mon Sep 17 00:00:00 2001 From: Diego Alonso Marquez Palacios Date: Thu, 21 Nov 2024 20:38:01 -0500 Subject: [PATCH 4/6] use add-opens in surefire only when running in jdk9+ --- pom.xml | 30 +++++++++++++++++++++--------- 1 file changed, 21 insertions(+), 9 deletions(-) diff --git a/pom.xml b/pom.xml index 377e9b31a..3f3a87609 100644 --- a/pom.xml +++ b/pom.xml @@ -220,15 +220,6 @@ - - org.apache.maven.plugins - maven-surefire-plugin - - - --add-opens java.base/java.time=ALL-UNNAMED - - @@ -308,4 +299,25 @@ + + + java9 + + [9,) + + + + + org.apache.maven.plugins + maven-surefire-plugin + + + --add-opens java.base/java.time=ALL-UNNAMED + + + + + + From 2b09a03a24a8625a045903ab4845fe13032a4d6e Mon Sep 17 00:00:00 2001 From: Diego Alonso Marquez Palacios Date: Thu, 21 Nov 2024 21:34:32 -0500 Subject: [PATCH 5/6] Revert "use add-opens in surefire only when running in jdk9+" This reverts commit 5a5abd436102c1fc8d89ef09ea829739cea310d5. --- pom.xml | 30 +++++++++--------------------- 1 file changed, 9 insertions(+), 21 deletions(-) diff --git a/pom.xml b/pom.xml index 3f3a87609..377e9b31a 100644 --- a/pom.xml +++ b/pom.xml @@ -220,6 +220,15 @@ + + org.apache.maven.plugins + maven-surefire-plugin + + + --add-opens java.base/java.time=ALL-UNNAMED + + @@ -299,25 +308,4 @@ - - - java9 - - [9,) - - - - - org.apache.maven.plugins - maven-surefire-plugin - - - --add-opens java.base/java.time=ALL-UNNAMED - - - - - - From 46b9172781d884af0dd63f54f8a652186d3af543 Mon Sep 17 00:00:00 2001 From: Diego Alonso Marquez Palacios Date: Thu, 21 Nov 2024 21:34:39 -0500 Subject: [PATCH 6/6] Revert "use add-opens to allow serialization of java.time.Duration" This reverts commit d1feef27525d2a52d80f3073c919d8cfa8711de6. --- .../com/google/cloud/logging/LogEntry.java | 10 ++++++ pom.xml | 32 ------------------- 2 files changed, 10 insertions(+), 32 deletions(-) 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/pom.xml b/pom.xml index 377e9b31a..8ae30ca0b 100644 --- a/pom.xml +++ b/pom.xml @@ -197,38 +197,6 @@ org.objenesis:objenesis - - org.apache.maven.plugins - maven-jar-plugin - - - default-jar - package - - jar - - - - - - java.base/java.time=ALL-UNNAMED - - - - - - - - org.apache.maven.plugins - maven-surefire-plugin - - - --add-opens java.base/java.time=ALL-UNNAMED - -