Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: allow opt-out auto-population of the log entry metadata on write() #798

Merged
merged 3 commits into from
Dec 16, 2021
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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) {
minherz marked this conversation as resolved.
Show resolved Hide resolved
return new WriteOption(OptionType.AUTO_POPULATE_METADATA, autoPopulateMetadata);
losalex marked this conversation as resolved.
Show resolved Hide resolved
}
}

/** 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;
losalex marked this conversation as resolved.
Show resolved Hide resolved

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 getAutoPopulate() {
return this.autoPopulateMetadataOnWrite;
}

@Override
public boolean equals(Object obj) {
return obj instanceof LoggingOptions && baseEquals((LoggingOptions) obj);
Expand Down