Browse Source

Add builder for logback LoggingEventMapper (#10000)

Lauri Tulmin 1 year ago
parent
commit
2eb5974ecd

+ 8 - 7
instrumentation/logback/logback-appender-1.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/logback/appender/v1_0/LogbackSingletons.java

@@ -41,13 +41,14 @@ public final class LogbackSingletons {
             emptyList());
 
     mapper =
-        new LoggingEventMapper(
-            captureExperimentalAttributes,
-            captureMdcAttributes,
-            captureCodeAttributes,
-            captureMarkerAttribute,
-            captureKeyValuePairAttributes,
-            captureLoggerContext);
+        LoggingEventMapper.builder()
+            .setCaptureExperimentalAttributes(captureExperimentalAttributes)
+            .setCaptureMdcAttributes(captureMdcAttributes)
+            .setCaptureCodeAttributes(captureCodeAttributes)
+            .setCaptureMarkerAttribute(captureMarkerAttribute)
+            .setCaptureKeyValuePairAttributes(captureKeyValuePairAttributes)
+            .setCaptureLoggerContext(captureLoggerContext)
+            .build();
   }
 
   public static LoggingEventMapper mapper() {

+ 8 - 7
instrumentation/logback/logback-appender-1.0/library/src/main/java/io/opentelemetry/instrumentation/logback/appender/v1_0/OpenTelemetryAppender.java

@@ -72,13 +72,14 @@ public class OpenTelemetryAppender extends UnsynchronizedAppenderBase<ILoggingEv
   @Override
   public void start() {
     mapper =
-        new LoggingEventMapper(
-            captureExperimentalAttributes,
-            captureMdcAttributes,
-            captureCodeAttributes,
-            captureMarkerAttribute,
-            captureKeyValuePairAttributes,
-            captureLoggerContext);
+        LoggingEventMapper.builder()
+            .setCaptureExperimentalAttributes(captureExperimentalAttributes)
+            .setCaptureMdcAttributes(captureMdcAttributes)
+            .setCaptureCodeAttributes(captureCodeAttributes)
+            .setCaptureMarkerAttribute(captureMarkerAttribute)
+            .setCaptureKeyValuePairAttributes(captureKeyValuePairAttributes)
+            .setCaptureLoggerContext(captureLoggerContext)
+            .build();
     eventsToReplay = new ArrayBlockingQueue<>(numLogsCapturedBeforeOtelInstall);
     super.start();
   }

+ 70 - 14
instrumentation/logback/logback-appender-1.0/library/src/main/java/io/opentelemetry/instrumentation/logback/appender/v1_0/internal/LoggingEventMapper.java

@@ -5,9 +5,12 @@
 
 package io.opentelemetry.instrumentation.logback.appender.v1_0.internal;
 
+import static java.util.Collections.emptyList;
+
 import ch.qos.logback.classic.Level;
 import ch.qos.logback.classic.spi.ILoggingEvent;
 import ch.qos.logback.classic.spi.ThrowableProxy;
+import com.google.errorprone.annotations.CanIgnoreReturnValue;
 import io.opentelemetry.api.common.AttributeKey;
 import io.opentelemetry.api.common.Attributes;
 import io.opentelemetry.api.common.AttributesBuilder;
@@ -49,21 +52,19 @@ public final class LoggingEventMapper {
   private final boolean captureKeyValuePairAttributes;
   private final boolean captureLoggerContext;
 
-  public LoggingEventMapper(
-      boolean captureExperimentalAttributes,
-      List<String> captureMdcAttributes,
-      boolean captureCodeAttributes,
-      boolean captureMarkerAttribute,
-      boolean captureKeyValuePairAttributes,
-      boolean captureLoggerContext) {
-    this.captureExperimentalAttributes = captureExperimentalAttributes;
-    this.captureCodeAttributes = captureCodeAttributes;
-    this.captureMdcAttributes = captureMdcAttributes;
-    this.captureMarkerAttribute = captureMarkerAttribute;
-    this.captureKeyValuePairAttributes = captureKeyValuePairAttributes;
-    this.captureLoggerContext = captureLoggerContext;
+  private LoggingEventMapper(Builder builder) {
+    this.captureExperimentalAttributes = builder.captureExperimentalAttributes;
+    this.captureCodeAttributes = builder.captureCodeAttributes;
+    this.captureMdcAttributes = builder.captureMdcAttributes;
+    this.captureMarkerAttribute = builder.captureMarkerAttribute;
+    this.captureKeyValuePairAttributes = builder.captureKeyValuePairAttributes;
+    this.captureLoggerContext = builder.captureLoggerContext;
     this.captureAllMdcAttributes =
-        captureMdcAttributes.size() == 1 && captureMdcAttributes.get(0).equals("*");
+        builder.captureMdcAttributes.size() == 1 && builder.captureMdcAttributes.get(0).equals("*");
+  }
+
+  public static Builder builder() {
+    return new Builder();
   }
 
   public void emit(LoggerProvider loggerProvider, ILoggingEvent event, long threadId) {
@@ -296,4 +297,59 @@ public final class LoggingEventMapper {
 
     return true;
   }
+
+  /**
+   * This class is internal and is hence not for public use. Its APIs are unstable and can change at
+   * any time.
+   */
+  public static final class Builder {
+    private boolean captureExperimentalAttributes;
+    private List<String> captureMdcAttributes = emptyList();
+    private boolean captureCodeAttributes;
+    private boolean captureMarkerAttribute;
+    private boolean captureKeyValuePairAttributes;
+    private boolean captureLoggerContext;
+
+    Builder() {}
+
+    @CanIgnoreReturnValue
+    public Builder setCaptureExperimentalAttributes(boolean captureExperimentalAttributes) {
+      this.captureExperimentalAttributes = captureExperimentalAttributes;
+      return this;
+    }
+
+    @CanIgnoreReturnValue
+    public Builder setCaptureMdcAttributes(List<String> captureMdcAttributes) {
+      this.captureMdcAttributes = captureMdcAttributes;
+      return this;
+    }
+
+    @CanIgnoreReturnValue
+    public Builder setCaptureCodeAttributes(boolean captureCodeAttributes) {
+      this.captureCodeAttributes = captureCodeAttributes;
+      return this;
+    }
+
+    @CanIgnoreReturnValue
+    public Builder setCaptureMarkerAttribute(boolean captureMarkerAttribute) {
+      this.captureMarkerAttribute = captureMarkerAttribute;
+      return this;
+    }
+
+    @CanIgnoreReturnValue
+    public Builder setCaptureKeyValuePairAttributes(boolean captureKeyValuePairAttributes) {
+      this.captureKeyValuePairAttributes = captureKeyValuePairAttributes;
+      return this;
+    }
+
+    @CanIgnoreReturnValue
+    public Builder setCaptureLoggerContext(boolean captureLoggerContext) {
+      this.captureLoggerContext = captureLoggerContext;
+      return this;
+    }
+
+    public LoggingEventMapper build() {
+      return new LoggingEventMapper(this);
+    }
+  }
 }

+ 3 - 5
instrumentation/logback/logback-appender-1.0/library/src/test/java/io/opentelemetry/instrumentation/logback/appender/v1_0/internal/LoggingEventMapperTest.java

@@ -6,7 +6,6 @@
 package io.opentelemetry.instrumentation.logback.appender.v1_0.internal;
 
 import static io.opentelemetry.sdk.testing.assertj.OpenTelemetryAssertions.assertThat;
-import static java.util.Collections.emptyList;
 import static java.util.Collections.singletonList;
 import static org.assertj.core.api.Assertions.entry;
 
@@ -22,8 +21,7 @@ class LoggingEventMapperTest {
   @Test
   void testDefault() {
     // given
-    LoggingEventMapper mapper =
-        new LoggingEventMapper(false, emptyList(), false, false, false, false);
+    LoggingEventMapper mapper = LoggingEventMapper.builder().build();
     Map<String, String> contextData = new HashMap<>();
     contextData.put("key1", "value1");
     contextData.put("key2", "value2");
@@ -40,7 +38,7 @@ class LoggingEventMapperTest {
   void testSome() {
     // given
     LoggingEventMapper mapper =
-        new LoggingEventMapper(false, singletonList("key2"), false, false, false, false);
+        LoggingEventMapper.builder().setCaptureMdcAttributes(singletonList("key2")).build();
     Map<String, String> contextData = new HashMap<>();
     contextData.put("key1", "value1");
     contextData.put("key2", "value2");
@@ -58,7 +56,7 @@ class LoggingEventMapperTest {
   void testAll() {
     // given
     LoggingEventMapper mapper =
-        new LoggingEventMapper(false, singletonList("*"), false, false, false, false);
+        LoggingEventMapper.builder().setCaptureMdcAttributes(singletonList("*")).build();
     Map<String, String> contextData = new HashMap<>();
     contextData.put("key1", "value1");
     contextData.put("key2", "value2");