Skip to content

Commit

Permalink
feat: allow opt-out auto-population of the log entry metadata on writ…
Browse files Browse the repository at this point in the history
…e() (#798)

Allows to opt-out auto populate log entry metadata on write in the write() API and Logging instance levels.
Tests WriteOption to out-out  auto populating metadata.
Tests LoggingOption to out-out  auto populating metadata.
Tests default LoggingOption for auto populating metadata.
Refactors LoggingOptions tests.
Refactors LoggingOptions auto populate metadata getter method.
Fixes cast compilation warning.
  • Loading branch information
minherz committed Jan 4, 2022
1 parent 013bb50 commit b7612fb
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,8 @@ enum OptionType implements Option.OptionType {
LOG_NAME,
RESOURCE,
LABELS,
LOG_DESTINATION;
LOG_DESTINATION,
AUTO_POPULATE_METADATA;

@SuppressWarnings("unchecked")
<T> T get(Map<Option.OptionType, ?> options) {
Expand Down Expand Up @@ -114,6 +115,14 @@ public static WriteOption labels(Map<String, String> labels) {
public static WriteOption destination(LogDestinationName destination) {
return new WriteOption(OptionType.LOG_DESTINATION, destination);
}

/**
* Returns an option to opt-out automatic population of log entries metadata fields that are not
* set.
*/
public static WriteOption autoPopulateMetadata(boolean autoPopulateMetadata) {
return new WriteOption(OptionType.AUTO_POPULATE_METADATA, autoPopulateMetadata);
}
}

/** Fields according to which log entries can be sorted. */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,10 +88,11 @@ List<LoggingEnhancer> getEnhancers() {
if (list != null) {
String[] items = list.split(",");
for (String e_name : items) {
Class<? extends LoggingEnhancer> clz =
(Class<? extends LoggingEnhancer>)
ClassLoader.getSystemClassLoader().loadClass(e_name);
enhancers.add(clz.getDeclaredConstructor().newInstance());
Class<? extends LoggingEnhancer> clazz =
ClassLoader.getSystemClassLoader()
.loadClass(e_name)
.asSubclass(LoggingEnhancer.class);
enhancers.add(clazz.getDeclaredConstructor().newInstance());
}
}
return enhancers;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ public class LoggingOptions extends ServiceOptions<Logging, LoggingOptions> {
private static final String DEFAULT_HOST = LoggingSettings.getDefaultEndpoint();
private static final long serialVersionUID = 5753499510627426717L;

private Boolean autoPopulateMetadataOnWrite = null;

public static class DefaultLoggingFactory implements LoggingFactory {
private static final LoggingFactory INSTANCE = new DefaultLoggingFactory();

Expand Down Expand Up @@ -72,6 +74,8 @@ protected String getDefaultHost() {

public static class Builder extends ServiceOptions.Builder<Logging, LoggingOptions, Builder> {

private Boolean autoPopulateMetadataOnWrite = true;

private Builder() {}

private Builder(LoggingOptions options) {
Expand All @@ -87,6 +91,11 @@ public Builder setTransportOptions(TransportOptions transportOptions) {
return super.setTransportOptions(transportOptions);
}

public Builder setAutoPopulateMetadata(boolean autoPopulateMetadataOnWrite) {
this.autoPopulateMetadataOnWrite = autoPopulateMetadataOnWrite;
return this;
}

@Override
public LoggingOptions build() {
return new LoggingOptions(this);
Expand All @@ -96,6 +105,7 @@ public LoggingOptions build() {
@InternalApi("This class should only be extended within google-cloud-java")
protected LoggingOptions(Builder builder) {
super(LoggingFactory.class, LoggingRpcFactory.class, builder, new LoggingDefaults());
this.autoPopulateMetadataOnWrite = builder.autoPopulateMetadataOnWrite;
}

@SuppressWarnings("serial")
Expand Down Expand Up @@ -130,6 +140,10 @@ protected LoggingRpc getLoggingRpcV2() {
return (LoggingRpc) getRpc();
}

public Boolean getAutoPopulateMetadata() {
return this.autoPopulateMetadataOnWrite;
}

@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 @@ -16,21 +16,31 @@

package com.google.cloud.logging;

import static org.easymock.EasyMock.createMock;
import static org.junit.Assert.assertEquals;

import com.google.cloud.TransportOptions;
import org.easymock.EasyMock;
import org.junit.Assert;
import org.junit.Test;

public class LoggingOptionsTest {
private static final Boolean DONT_AUTO_POPULATE_METADATA = false;

@Test(expected = IllegalArgumentException.class)
public void testNonGrpcTransportOptions() {
TransportOptions invalidTransport = createMock(TransportOptions.class);
LoggingOptions.newBuilder().setTransportOptions(invalidTransport);
}

@Test
public void testAutoPopulateMetadataOption() {
LoggingOptions actual =
LoggingOptions.newBuilder().setAutoPopulateMetadata(DONT_AUTO_POPULATE_METADATA).build();
assertEquals(DONT_AUTO_POPULATE_METADATA, actual.getAutoPopulateMetadata());
}

@Test
public void testInvalidTransport() {
try {
LoggingOptions.newBuilder()
.setTransportOptions(EasyMock.<TransportOptions>createMock(TransportOptions.class));
Assert.fail();
} catch (IllegalArgumentException expected) {
Assert.assertNotNull(expected.getMessage());
}
public void testAutoPopulateMetadataDefaultOption() {
LoggingOptions actual = LoggingOptions.getDefaultInstance();
assertEquals(Boolean.TRUE, actual.getAutoPopulateMetadata());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ public class LoggingTest {
private static final String FOLDER_NAME = "folder";
private static final String ORGANIZATION_NAME = "organization";
private static final String BILLING_NAME = "billing";
private static final Boolean DONT_AUTO_POPULATE_METADATA = false;

@Test
public void testListOption() {
Expand Down Expand Up @@ -109,6 +110,10 @@ public void testWriteOption() {
writeOption = WriteOption.resource(RESOURCE);
assertEquals(RESOURCE, writeOption.getValue());
assertEquals(WriteOption.OptionType.RESOURCE, writeOption.getOptionType());

writeOption = WriteOption.autoPopulateMetadata(DONT_AUTO_POPULATE_METADATA);
assertEquals(DONT_AUTO_POPULATE_METADATA, writeOption.getValue());
assertEquals(WriteOption.OptionType.AUTO_POPULATE_METADATA, writeOption.getOptionType());
}

@Test
Expand Down

0 comments on commit b7612fb

Please sign in to comment.