Skip to content

Commit 072dec3

Browse files
committed
feat: configure opt-out metada auto-population for JUL handler (#807)
Adds metadata auto-population flag to JUL handler. Adds metadata auto-population flag to logging config file. Forwards metadata auto-population flag via WriteOption to write() calls. Refactors JUL handler tests to remove duplication, unused calls and warnings
1 parent f485d70 commit 072dec3

File tree

3 files changed

+98
-75
lines changed

3 files changed

+98
-75
lines changed

google-cloud-logging/src/main/java/com/google/cloud/logging/LoggingConfig.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ class LoggingConfig {
4141
private static final String RESOURCE_TYPE_TAG = "resourceType";
4242
private static final String ENHANCERS_TAG = "enhancers";
4343
private static final String USE_INHERITED_CONTEXT = "useInheritedContext";
44+
private static final String AUTO_POPULATE_METADATA = "autoPopulateMetadata";
4445

4546
public LoggingConfig(String className) {
4647
this.className = className;
@@ -76,6 +77,14 @@ Formatter getFormatter() {
7677
return getFormatterProperty(FORMATTER_TAG, new SimpleFormatter());
7778
}
7879

80+
Boolean getAutoPopulateMetadata() {
81+
String flag = getProperty(AUTO_POPULATE_METADATA);
82+
if (flag != null) {
83+
return Boolean.parseBoolean(flag);
84+
}
85+
return null;
86+
}
87+
7988
MonitoredResource getMonitoredResource(String projectId) {
8089
String resourceType = getProperty(RESOURCE_TYPE_TAG, "");
8190
return MonitoredResourceUtil.getResource(projectId, resourceType);

google-cloud-logging/src/main/java/com/google/cloud/logging/LoggingHandler.java

Lines changed: 44 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
package com.google.cloud.logging;
1818

1919
import static com.google.common.base.MoreObjects.firstNonNull;
20+
import static com.google.common.base.Preconditions.checkNotNull;
2021

2122
import com.google.cloud.MonitoredResource;
2223
import com.google.cloud.logging.Logging.WriteOption;
@@ -25,6 +26,7 @@
2526
import com.google.common.collect.Iterables;
2627
import java.time.Instant;
2728
import java.util.ArrayList;
29+
import java.util.Arrays;
2830
import java.util.Collections;
2931
import java.util.LinkedList;
3032
import java.util.List;
@@ -109,6 +111,9 @@
109111
* else "global").
110112
* <li>{@code com.google.cloud.logging.Synchronicity} the synchronicity of the write method to use
111113
* to write logs to the Cloud Logging service (defaults to {@link Synchronicity#ASYNC}).
114+
* <li>{@code com.google.cloud.logging.LoggingHandler.autoPopulateMetadata} is a boolean flag that
115+
* opts-out the population of the log entries metadata before the logs are sent to Cloud
116+
* Logging (defaults to {@code true}).
112117
* </ul>
113118
*
114119
* <p>To add a {@code LoggingHandler} to an existing {@link Logger} and be sure to avoid infinite
@@ -139,6 +144,8 @@ public class LoggingHandler extends Handler {
139144

140145
private volatile Level flushLevel;
141146

147+
private volatile Boolean autoPopulateMetadata;
148+
142149
private WriteOption[] defaultWriteOptions;
143150

144151
/** Creates an handler that publishes messages to Cloud Logging. */
@@ -196,7 +203,10 @@ public LoggingHandler(
196203
}
197204

198205
/**
199-
* Creates a handler that publishes messages to Cloud Logging.
206+
* Creates a handler that publishes messages to Cloud Logging. Auto-population of the logs
207+
* metadata can be opted-out in {@code options} argument or in the configuration file. At least
208+
* one flag {@link LoggingOptions} or {@link LoggingConfig} has to be explicitly set to {@code
209+
* false} in order to opt-out the metadata auto-population.
200210
*
201211
* @param log the name of the log to which log entries are written
202212
* @param options options for the Cloud Logging service
@@ -222,14 +232,18 @@ public LoggingHandler(
222232
setLevel(level);
223233
baseLevel = level.equals(Level.ALL) ? Level.FINEST : level;
224234
flushLevel = config.getFlushLevel();
235+
Boolean f1 = options.getAutoPopulateMetadata();
236+
Boolean f2 = config.getAutoPopulateMetadata();
237+
autoPopulateMetadata = isTrueOrNull(f1) && isTrueOrNull(f2);
225238
String logName = log != null ? log : config.getLogName();
226-
227239
MonitoredResource resource =
228240
firstNonNull(
229241
monitoredResource, config.getMonitoredResource(loggingOptions.getProjectId()));
230242
List<WriteOption> writeOptions = new ArrayList<WriteOption>();
231243
writeOptions.add(WriteOption.logName(logName));
232-
writeOptions.add(WriteOption.resource(resource));
244+
if (resource != null) {
245+
writeOptions.add(WriteOption.resource(resource));
246+
}
233247
writeOptions.add(
234248
WriteOption.labels(
235249
ImmutableMap.of(
@@ -240,6 +254,7 @@ public LoggingHandler(
240254
if (destination != null) {
241255
writeOptions.add(WriteOption.destination(destination));
242256
}
257+
writeOptions.add(WriteOption.autoPopulateMetadata(autoPopulateMetadata));
243258
defaultWriteOptions = Iterables.toArray(writeOptions, WriteOption.class);
244259

245260
getLogging().setFlushSeverity(severityFor(flushLevel));
@@ -365,6 +380,28 @@ public Synchronicity getSynchronicity() {
365380
return getLogging().getWriteSynchronicity();
366381
}
367382

383+
/**
384+
* Sets the metadata auto population flag.
385+
*/
386+
public void setAutoPopulateMetadata(Boolean value) {
387+
checkNotNull(value);
388+
this.autoPopulateMetadata = value;
389+
List<WriteOption> writeOptions = Arrays.asList(defaultWriteOptions);
390+
for (int i = 0; i < writeOptions.size(); i++) {
391+
if (writeOptions.get(i).getOptionType() == WriteOption.OptionType.AUTO_POPULATE_METADATA) {
392+
writeOptions.remove(i);
393+
break;
394+
}
395+
}
396+
writeOptions.add(WriteOption.autoPopulateMetadata(value));
397+
defaultWriteOptions = Iterables.toArray(writeOptions, WriteOption.class);
398+
}
399+
400+
/** Gets the metadata auto population flag. */
401+
public Boolean getAutoPopulateMetadata() {
402+
return this.autoPopulateMetadata;
403+
}
404+
368405
/**
369406
* Adds the provided {@code LoggingHandler} to {@code logger}. Use this method to register Cloud
370407
* Logging handlers instead of {@link Logger#addHandler(Handler)} to avoid infinite recursion when
@@ -417,4 +454,8 @@ private Logging getLogging() {
417454
}
418455
return logging;
419456
}
457+
458+
private static boolean isTrueOrNull(Boolean b) {
459+
return b == null || b == Boolean.TRUE;
460+
}
420461
}

0 commit comments

Comments
 (0)