diff --git a/instrumentation/logback/logback-appender-1.0/library/src/main/java/io/opentelemetry/instrumentation/logback/appender/v1_0/OpenTelemetryAppender.java b/instrumentation/logback/logback-appender-1.0/library/src/main/java/io/opentelemetry/instrumentation/logback/appender/v1_0/OpenTelemetryAppender.java index 19dc01bf398a..a39ecbbb3710 100644 --- a/instrumentation/logback/logback-appender-1.0/library/src/main/java/io/opentelemetry/instrumentation/logback/appender/v1_0/OpenTelemetryAppender.java +++ b/instrumentation/logback/logback-appender-1.0/library/src/main/java/io/opentelemetry/instrumentation/logback/appender/v1_0/OpenTelemetryAppender.java @@ -5,8 +5,6 @@ package io.opentelemetry.instrumentation.logback.appender.v1_0; -import static java.util.Collections.emptyList; - import ch.qos.logback.classic.LoggerContext; import ch.qos.logback.classic.spi.ILoggingEvent; import ch.qos.logback.core.Appender; @@ -37,7 +35,8 @@ public class OpenTelemetryAppender extends UnsynchronizedAppenderBase captureMdcAttributes = emptyList(); + private List captureMdcAttributes = List.of(); + private Set excludeMdcAttributes = Set.of(); private boolean captureEventName = false; private volatile OpenTelemetry openTelemetry; @@ -83,6 +82,7 @@ public void start() { LoggingEventMapper.builder() .setCaptureExperimentalAttributes(captureExperimentalAttributes) .setCaptureMdcAttributes(captureMdcAttributes) + .setExcludeMdcAttributes(excludeMdcAttributes) .setCaptureCodeAttributes(captureCodeAttributes) .setCaptureMarkerAttribute(captureMarkerAttribute) .setCaptureKeyValuePairAttributes(captureKeyValuePairAttributes) @@ -193,12 +193,31 @@ public void setCaptureLogstashAttributes(boolean captureLogstashAttributes) { this.captureLogstashAttributes = captureLogstashAttributes; } - /** Configures the {@link MDC} attributes that will be copied to logs. */ + /** + * Configures the {@link MDC} attributes that will be copied to logs. A single wildcard * will + * copy all attributes. + * + * @param attributes The attributes to copy + */ public void setCaptureMdcAttributes(String attributes) { if (attributes != null) { captureMdcAttributes = filterBlanksAndNulls(attributes.split(",")); } else { - captureMdcAttributes = emptyList(); + captureMdcAttributes = List.of(); + } + } + + /** + * Configures the {@link MDC} attributes that will be excluded from being copied to logs when all + * attributes are being copied. + * + * @param attributes The attributes to exclude from being copied + */ + public void setExcludeMdcAttributes(String attributes) { + if (attributes != null) { + excludeMdcAttributes = Set.copyOf(filterBlanksAndNulls(attributes.split(","))); + } else { + excludeMdcAttributes = Set.of(); } } diff --git a/instrumentation/logback/logback-appender-1.0/library/src/main/java/io/opentelemetry/instrumentation/logback/appender/v1_0/internal/LoggingEventMapper.java b/instrumentation/logback/logback-appender-1.0/library/src/main/java/io/opentelemetry/instrumentation/logback/appender/v1_0/internal/LoggingEventMapper.java index fe385550d305..34fc1be6f9e3 100644 --- a/instrumentation/logback/logback-appender-1.0/library/src/main/java/io/opentelemetry/instrumentation/logback/appender/v1_0/internal/LoggingEventMapper.java +++ b/instrumentation/logback/logback-appender-1.0/library/src/main/java/io/opentelemetry/instrumentation/logback/appender/v1_0/internal/LoggingEventMapper.java @@ -8,7 +8,6 @@ import static io.opentelemetry.semconv.CodeAttributes.CODE_FILE_PATH; import static io.opentelemetry.semconv.CodeAttributes.CODE_FUNCTION_NAME; import static io.opentelemetry.semconv.CodeAttributes.CODE_LINE_NUMBER; -import static java.util.Collections.emptyList; import ch.qos.logback.classic.Level; import ch.qos.logback.classic.spi.ILoggingEvent; @@ -35,6 +34,7 @@ import java.util.Iterator; import java.util.List; import java.util.Map; +import java.util.Set; import java.util.concurrent.TimeUnit; import java.util.function.Function; import java.util.stream.Collectors; @@ -77,6 +77,7 @@ public final class LoggingEventMapper { private final boolean captureExperimentalAttributes; private final List captureMdcAttributes; + private final Set excludeMdcAttributes; private final boolean captureAllMdcAttributes; private final boolean captureCodeAttributes; private final boolean captureMarkerAttribute; @@ -90,6 +91,7 @@ private LoggingEventMapper(Builder builder) { this.captureExperimentalAttributes = builder.captureExperimentalAttributes; this.captureCodeAttributes = builder.captureCodeAttributes; this.captureMdcAttributes = builder.captureMdcAttributes; + this.excludeMdcAttributes = builder.excludeMdcAttributes; this.captureMarkerAttribute = builder.captureMarkerAttribute; this.captureKeyValuePairAttributes = builder.captureKeyValuePairAttributes; this.captureLoggerContext = builder.captureLoggerContext; @@ -243,7 +245,9 @@ private static void setTimestampFromInstant( void captureMdcAttributes(LogRecordBuilder builder, Map mdcProperties) { if (captureAllMdcAttributes) { for (Map.Entry entry : mdcProperties.entrySet()) { - setAttributeOrEventName(builder, getAttributeKey(entry.getKey()), entry.getValue()); + if (!excludeMdcAttributes.contains(entry.getKey())) { + setAttributeOrEventName(builder, getAttributeKey(entry.getKey()), entry.getValue()); + } } return; } @@ -636,7 +640,8 @@ protected FieldReader computeValue(Class type) { */ public static final class Builder { private boolean captureExperimentalAttributes; - private List captureMdcAttributes = emptyList(); + private List captureMdcAttributes = List.of(); + private Set excludeMdcAttributes = Set.of(); private boolean captureCodeAttributes; private boolean captureMarkerAttribute; private boolean captureKeyValuePairAttributes; @@ -659,6 +664,12 @@ public Builder setCaptureMdcAttributes(List captureMdcAttributes) { return this; } + @CanIgnoreReturnValue + public Builder setExcludeMdcAttributes(Set excludeMdcAttributes) { + this.excludeMdcAttributes = excludeMdcAttributes; + return this; + } + @CanIgnoreReturnValue public Builder setCaptureCodeAttributes(boolean captureCodeAttributes) { this.captureCodeAttributes = captureCodeAttributes;