Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions .readme-partials.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,38 @@ custom_content: |
> may lead to unexpected results such as absense of expected log entries or abnormal program execution.
> To avoid these unexpected results, it is recommended to use synchronous mode.

#### Controlling the batching settings
As mentioned before, in the asynchronous mode the call(s) to Logging API takes place asynchronously and few calls to `write()`
method may be batched together to compose a single call to Logging API. In order to control the batching settings, the `LoggingOptions`
is enhanced with `BatchingSettings` which can be set as shown in example below:

```java
import com.google.api.gax.batching.BatchingSettings;
import com.google.api.gax.batching.FlowControlSettings;
import com.google.api.gax.batching.FlowController;

LoggingOptions actual =
LoggingOptions.newBuilder()
.setBatchingSettings(
BatchingSettings.newBuilder()
.setIsEnabled(true)
.setElementCountThreshold(1000L)
.setRequestByteThreshold(1048576L)
.setDelayThreshold(Duration.ofMillis(50L))
.setFlowControlSettings(
FlowControlSettings.newBuilder()
.setMaxOutstandingElementCount(100000L)
.setMaxOutstandingRequestBytes(10485760L)
.setLimitExceededBehavior(
FlowController.LimitExceededBehavior.ThrowException)
.build())
.build())
.setProjectId('Your project ID')
.build();
```

You can find more information about batching parameters see [BatchingSettings](https://cloud.google.com/java/docs/reference/gax/latest/com.google.api.gax.batching.BatchingSettings).

#### Listing log entries

With Logging you can also list log entries that have been previously written. Add the following
Expand Down
36 changes: 34 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,13 +58,13 @@ implementation 'com.google.cloud:google-cloud-logging'
If you are using Gradle without BOM, add this to your dependencies:

```Groovy
implementation 'com.google.cloud:google-cloud-logging:3.11.9'
implementation 'com.google.cloud:google-cloud-logging:3.11.10'
```

If you are using SBT, add this to your dependencies:

```Scala
libraryDependencies += "com.google.cloud" % "google-cloud-logging" % "3.11.9"
libraryDependencies += "com.google.cloud" % "google-cloud-logging" % "3.11.10"
```

## Authentication
Expand Down Expand Up @@ -191,6 +191,38 @@ NOTE:
> may lead to unexpected results such as absense of expected log entries or abnormal program execution.
> To avoid these unexpected results, it is recommended to use synchronous mode.

#### Controlling the batching settings
As mentioned before, in the asynchronous mode the call(s) to Logging API takes place asynchronously and few calls to `write()`
method may be batched together to compose a single call to Logging API. In order to control the batching settings, the `LoggingOptions`
is enhanced with `BatchingSettings` which can be set as shown in example below:

```java
import com.google.api.gax.batching.BatchingSettings;
import com.google.api.gax.batching.FlowControlSettings;
import com.google.api.gax.batching.FlowController;

LoggingOptions actual =
LoggingOptions.newBuilder()
.setBatchingSettings(
BatchingSettings.newBuilder()
.setIsEnabled(true)
.setElementCountThreshold(1000L)
.setRequestByteThreshold(1048576L)
.setDelayThreshold(Duration.ofMillis(50L))
.setFlowControlSettings(
FlowControlSettings.newBuilder()
.setMaxOutstandingElementCount(100000L)
.setMaxOutstandingRequestBytes(10485760L)
.setLimitExceededBehavior(
FlowController.LimitExceededBehavior.ThrowException)
.build())
.build())
.setProjectId('Your project ID')
.build();
```

You can find more information about batching parameters see [BatchingSettings](https://cloud.google.com/java/docs/reference/gax/latest/com.google.api.gax.batching.BatchingSettings).

#### Listing log entries

With Logging you can also list log entries that have been previously written. Add the following
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package com.google.cloud.logging;

import com.google.api.core.InternalApi;
import com.google.api.gax.batching.BatchingSettings;
import com.google.cloud.ServiceDefaults;
import com.google.cloud.ServiceOptions;
import com.google.cloud.ServiceRpc;
Expand All @@ -40,6 +41,7 @@ public class LoggingOptions extends ServiceOptions<Logging, LoggingOptions> {
private static final long serialVersionUID = 5753499510627426717L;

private Boolean autoPopulateMetadataOnWrite = null;
private BatchingSettings batchingSettings = null;

public static class DefaultLoggingFactory implements LoggingFactory {
private static final LoggingFactory INSTANCE = new DefaultLoggingFactory();
Expand Down Expand Up @@ -76,6 +78,7 @@ protected String getDefaultHost() {
public static class Builder extends ServiceOptions.Builder<Logging, LoggingOptions, Builder> {

private Boolean autoPopulateMetadataOnWrite = true;
private BatchingSettings batchingSettings = null;

private Builder() {}

Expand All @@ -98,6 +101,13 @@ public Builder setAutoPopulateMetadata(boolean autoPopulateMetadataOnWrite) {
return this;
}

@CanIgnoreReturnValue
public Builder setBatchingSettings(BatchingSettings batchingSettings) {
this.batchingSettings =
batchingSettings == null ? null : batchingSettings.toBuilder().build();
return this;
}

@Override
public LoggingOptions build() {
return new LoggingOptions(this);
Expand All @@ -108,6 +118,8 @@ public LoggingOptions build() {
protected LoggingOptions(Builder builder) {
super(LoggingFactory.class, LoggingRpcFactory.class, builder, new LoggingDefaults());
this.autoPopulateMetadataOnWrite = builder.autoPopulateMetadataOnWrite;
this.batchingSettings =
builder.batchingSettings == null ? null : builder.batchingSettings.toBuilder().build();
}

@SuppressWarnings("serial")
Expand Down Expand Up @@ -146,6 +158,10 @@ public Boolean getAutoPopulateMetadata() {
return this.autoPopulateMetadataOnWrite;
}

public BatchingSettings getBatchingSettings() {
return this.batchingSettings;
}

@Override
public boolean equals(Object obj) {
return obj instanceof LoggingOptions && baseEquals((LoggingOptions) obj);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -169,18 +169,25 @@ public Void apply(UnaryCallSettings.Builder<?, ?> builder) {
// released.
BatchingSettings oldBatchSettings =
logBuilder.writeLogEntriesSettings().getBatchingSettings();

// The BatchingSettings from LoggingOptions should override
// ones provided in oldBatchSettings
BatchingSettings batchingSettings = options.getBatchingSettings();

logBuilder
.writeLogEntriesSettings()
.setBatchingSettings(
oldBatchSettings
.toBuilder()
.setFlowControlSettings(
oldBatchSettings
.getFlowControlSettings()
.toBuilder()
.setLimitExceededBehavior(LimitExceededBehavior.Block)
.build())
.build());
batchingSettings != null
? batchingSettings
: oldBatchSettings
.toBuilder()
.setFlowControlSettings(
oldBatchSettings
.getFlowControlSettings()
.toBuilder()
.setLimitExceededBehavior(LimitExceededBehavior.Block)
.build())
.build());

configClient = ConfigClient.create(confBuilder.build());
loggingClient = LoggingClient.create(logBuilder.build());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,29 @@

import static org.easymock.EasyMock.createMock;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertThrows;

import com.google.api.gax.batching.BatchingSettings;
import com.google.api.gax.batching.FlowControlSettings;
import com.google.api.gax.batching.FlowController;
import com.google.cloud.NoCredentials;
import com.google.cloud.TransportOptions;
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 {
private static final Boolean DONT_AUTO_POPULATE_METADATA = false;
private static final String PROJECT_ID = "fake-project-id";
private static final Long ELEMENTS_TRESHOLD_COUNT = 100L;
private static final Long REQUEST_BYTE_TRESHOLD_COUNT = 10485760L;
private static final long DURATION = 501L;
private static final Long MAX_OUTSTANDING_ELEMENTS_COUNT = 1000001L;
private static final Long MAX_OUTSTANDING_REQUEST_BYTES_COUNT = 104857601L;

@Test
public void testNonGrpcTransportOptions() {
Expand All @@ -53,4 +65,59 @@ public void testAutoPopulateMetadataDefaultOption() {
LoggingOptions actual = LoggingOptions.newBuilder().setProjectId(PROJECT_ID).build();
assertEquals(Boolean.TRUE, actual.getAutoPopulateMetadata());
}

@Test
public void testBatchingSettingsDefaultOption() {
LoggingOptions actual = LoggingOptions.newBuilder().setProjectId(PROJECT_ID).build();
assertNull("Batching settings should be null by default!", actual.getBatchingSettings());
}

@Test
public void testBatchingSettingsOption() {
verifyBatchingSettings(generateLoggingOptions().getBatchingSettings());
}

@Test
public void testBatchingSettingsOptionWithGrpc() {
verifyBatchingSettings(
generateLoggingOptions().getService().getOptions().getBatchingSettings());
}

private static LoggingOptions generateLoggingOptions() {
return LoggingOptions.newBuilder()
.setBatchingSettings(
BatchingSettings.newBuilder()
.setIsEnabled(true)
.setElementCountThreshold(ELEMENTS_TRESHOLD_COUNT)
.setRequestByteThreshold(REQUEST_BYTE_TRESHOLD_COUNT)
.setDelayThreshold(Duration.ofMillis(DURATION))
.setFlowControlSettings(
FlowControlSettings.newBuilder()
.setMaxOutstandingElementCount(MAX_OUTSTANDING_ELEMENTS_COUNT)
.setMaxOutstandingRequestBytes(MAX_OUTSTANDING_REQUEST_BYTES_COUNT)
.setLimitExceededBehavior(
FlowController.LimitExceededBehavior.ThrowException)
.build())
.build())
.setProjectId(PROJECT_ID)
.setCredentials(NoCredentials.getInstance())
.build();
}

private static void verifyBatchingSettings(BatchingSettings settings) {
assertEquals(true, settings.getIsEnabled());
assertEquals(ELEMENTS_TRESHOLD_COUNT, settings.getElementCountThreshold());
assertEquals(REQUEST_BYTE_TRESHOLD_COUNT, settings.getRequestByteThreshold());
assertNotNull(settings.getDelayThreshold());
assertEquals(DURATION, settings.getDelayThreshold().toMillis());
assertEquals(
MAX_OUTSTANDING_ELEMENTS_COUNT,
settings.getFlowControlSettings().getMaxOutstandingElementCount());
assertEquals(
MAX_OUTSTANDING_REQUEST_BYTES_COUNT,
settings.getFlowControlSettings().getMaxOutstandingRequestBytes());
assertEquals(
FlowController.LimitExceededBehavior.ThrowException,
settings.getFlowControlSettings().getLimitExceededBehavior());
}
}