diff --git a/google-cloud-logging/src/main/java/com/google/cloud/logging/Logging.java b/google-cloud-logging/src/main/java/com/google/cloud/logging/Logging.java index b665d96fd..f029602b2 100644 --- a/google-cloud-logging/src/main/java/com/google/cloud/logging/Logging.java +++ b/google-cloud-logging/src/main/java/com/google/cloud/logging/Logging.java @@ -70,7 +70,8 @@ enum OptionType implements Option.OptionType { LOG_NAME, RESOURCE, LABELS, - LOG_DESTINATION; + LOG_DESTINATION, + AUTO_POPULATE_METADATA; @SuppressWarnings("unchecked") T get(Map options) { @@ -114,6 +115,14 @@ public static WriteOption labels(Map 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. */ diff --git a/google-cloud-logging/src/main/java/com/google/cloud/logging/LoggingConfig.java b/google-cloud-logging/src/main/java/com/google/cloud/logging/LoggingConfig.java index d2a001fb0..9d0fd94b2 100644 --- a/google-cloud-logging/src/main/java/com/google/cloud/logging/LoggingConfig.java +++ b/google-cloud-logging/src/main/java/com/google/cloud/logging/LoggingConfig.java @@ -88,10 +88,11 @@ List getEnhancers() { if (list != null) { String[] items = list.split(","); for (String e_name : items) { - Class clz = - (Class) - ClassLoader.getSystemClassLoader().loadClass(e_name); - enhancers.add(clz.getDeclaredConstructor().newInstance()); + Class clazz = + ClassLoader.getSystemClassLoader() + .loadClass(e_name) + .asSubclass(LoggingEnhancer.class); + enhancers.add(clazz.getDeclaredConstructor().newInstance()); } } return enhancers; diff --git a/google-cloud-logging/src/main/java/com/google/cloud/logging/LoggingOptions.java b/google-cloud-logging/src/main/java/com/google/cloud/logging/LoggingOptions.java index 20c1eb29c..d1d6d1d6b 100644 --- a/google-cloud-logging/src/main/java/com/google/cloud/logging/LoggingOptions.java +++ b/google-cloud-logging/src/main/java/com/google/cloud/logging/LoggingOptions.java @@ -38,6 +38,8 @@ public class LoggingOptions extends ServiceOptions { 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(); @@ -72,6 +74,8 @@ protected String getDefaultHost() { public static class Builder extends ServiceOptions.Builder { + private Boolean autoPopulateMetadataOnWrite = true; + private Builder() {} private Builder(LoggingOptions options) { @@ -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); @@ -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") @@ -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); 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 c3795e3d3..092a5ff17 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 @@ -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.createMock(TransportOptions.class)); - Assert.fail(); - } catch (IllegalArgumentException expected) { - Assert.assertNotNull(expected.getMessage()); - } + public void testAutoPopulateMetadataDefaultOption() { + LoggingOptions actual = LoggingOptions.getDefaultInstance(); + assertEquals(Boolean.TRUE, actual.getAutoPopulateMetadata()); } } diff --git a/google-cloud-logging/src/test/java/com/google/cloud/logging/LoggingTest.java b/google-cloud-logging/src/test/java/com/google/cloud/logging/LoggingTest.java index 2b7dc0f0e..fab23f972 100644 --- a/google-cloud-logging/src/test/java/com/google/cloud/logging/LoggingTest.java +++ b/google-cloud-logging/src/test/java/com/google/cloud/logging/LoggingTest.java @@ -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() { @@ -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