Skip to content

Commit

Permalink
feat: introduce java.time methods (#1729)
Browse files Browse the repository at this point in the history
* 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 5a5abd4.

* Revert "use add-opens to allow serialization of java.time.Duration"

This reverts commit d1feef2.
  • Loading branch information
diegomarquezp authored Nov 26, 2024
1 parent 30963e1 commit 323eb33
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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() {}

Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -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()));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -612,6 +613,14 @@ public JsonElement serialize(
}
}

static final class DurationSerializer implements JsonSerializer<Duration> {
@Override
public JsonElement serialize(
Duration src, java.lang.reflect.Type typeOfSrc, JsonSerializationContext context) {
return new JsonPrimitive(src.toString());
}
}

static final class SourceLocationSerializer implements JsonSerializer<SourceLocation> {
@Override
public JsonElement serialize(
Expand Down Expand Up @@ -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())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 323eb33

Please sign in to comment.