Skip to content

Commit 54027b2

Browse files
committed
Add excluding Logback MDC attributes
This change adds a setting to the Logback appender to exclude MDC attributes when all attributes are being copied.
1 parent c48dfea commit 54027b2

File tree

2 files changed

+38
-8
lines changed

2 files changed

+38
-8
lines changed

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

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@
55

66
package io.opentelemetry.instrumentation.logback.appender.v1_0;
77

8-
import static java.util.Collections.emptyList;
9-
108
import ch.qos.logback.classic.LoggerContext;
119
import ch.qos.logback.classic.spi.ILoggingEvent;
1210
import ch.qos.logback.core.Appender;
@@ -37,7 +35,8 @@ public class OpenTelemetryAppender extends UnsynchronizedAppenderBase<ILoggingEv
3735
private boolean captureLoggerContext = false;
3836
private boolean captureArguments = false;
3937
private boolean captureLogstashAttributes = false;
40-
private List<String> captureMdcAttributes = emptyList();
38+
private List<String> captureMdcAttributes = List.of();
39+
private Set<String> excludeMdcAttributes = Set.of();
4140
private boolean captureEventName = false;
4241

4342
private volatile OpenTelemetry openTelemetry;
@@ -83,6 +82,7 @@ public void start() {
8382
LoggingEventMapper.builder()
8483
.setCaptureExperimentalAttributes(captureExperimentalAttributes)
8584
.setCaptureMdcAttributes(captureMdcAttributes)
85+
.setExcludeMdcAttributes(excludeMdcAttributes)
8686
.setCaptureCodeAttributes(captureCodeAttributes)
8787
.setCaptureMarkerAttribute(captureMarkerAttribute)
8888
.setCaptureKeyValuePairAttributes(captureKeyValuePairAttributes)
@@ -193,12 +193,31 @@ public void setCaptureLogstashAttributes(boolean captureLogstashAttributes) {
193193
this.captureLogstashAttributes = captureLogstashAttributes;
194194
}
195195

196-
/** Configures the {@link MDC} attributes that will be copied to logs. */
196+
/**
197+
* Configures the {@link MDC} attributes that will be copied to logs. A single wildcard * will
198+
* copy all attributes.
199+
*
200+
* @param attributes The attributes to copy
201+
*/
197202
public void setCaptureMdcAttributes(String attributes) {
198203
if (attributes != null) {
199204
captureMdcAttributes = filterBlanksAndNulls(attributes.split(","));
200205
} else {
201-
captureMdcAttributes = emptyList();
206+
captureMdcAttributes = List.of();
207+
}
208+
}
209+
210+
/**
211+
* Configures the {@link MDC} attributes that will be excluded from being copied to logs when
212+
* all attributes are being copied.
213+
*
214+
* @param attributes The attributes to exclude from being copied
215+
*/
216+
public void setExcludeMdcAttributes(String attributes) {
217+
if (attributes != null) {
218+
excludeMdcAttributes = Set.copyOf(filterBlanksAndNulls(attributes.split(",")));
219+
} else {
220+
excludeMdcAttributes = Set.of();
202221
}
203222
}
204223

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

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
import static io.opentelemetry.semconv.CodeAttributes.CODE_FILE_PATH;
99
import static io.opentelemetry.semconv.CodeAttributes.CODE_FUNCTION_NAME;
1010
import static io.opentelemetry.semconv.CodeAttributes.CODE_LINE_NUMBER;
11-
import static java.util.Collections.emptyList;
1211

1312
import ch.qos.logback.classic.Level;
1413
import ch.qos.logback.classic.spi.ILoggingEvent;
@@ -35,6 +34,7 @@
3534
import java.util.Iterator;
3635
import java.util.List;
3736
import java.util.Map;
37+
import java.util.Set;
3838
import java.util.concurrent.TimeUnit;
3939
import java.util.function.Function;
4040
import java.util.stream.Collectors;
@@ -77,6 +77,7 @@ public final class LoggingEventMapper {
7777

7878
private final boolean captureExperimentalAttributes;
7979
private final List<String> captureMdcAttributes;
80+
private final Set<String> excludeMdcAttributes;
8081
private final boolean captureAllMdcAttributes;
8182
private final boolean captureCodeAttributes;
8283
private final boolean captureMarkerAttribute;
@@ -90,6 +91,7 @@ private LoggingEventMapper(Builder builder) {
9091
this.captureExperimentalAttributes = builder.captureExperimentalAttributes;
9192
this.captureCodeAttributes = builder.captureCodeAttributes;
9293
this.captureMdcAttributes = builder.captureMdcAttributes;
94+
this.excludeMdcAttributes = builder.excludeMdcAttributes;
9395
this.captureMarkerAttribute = builder.captureMarkerAttribute;
9496
this.captureKeyValuePairAttributes = builder.captureKeyValuePairAttributes;
9597
this.captureLoggerContext = builder.captureLoggerContext;
@@ -243,7 +245,9 @@ private static void setTimestampFromInstant(
243245
void captureMdcAttributes(LogRecordBuilder builder, Map<String, String> mdcProperties) {
244246
if (captureAllMdcAttributes) {
245247
for (Map.Entry<String, String> entry : mdcProperties.entrySet()) {
246-
setAttributeOrEventName(builder, getAttributeKey(entry.getKey()), entry.getValue());
248+
if (!excludeMdcAttributes.contains(entry.getKey())) {
249+
setAttributeOrEventName(builder, getAttributeKey(entry.getKey()), entry.getValue());
250+
}
247251
}
248252
return;
249253
}
@@ -636,7 +640,8 @@ protected FieldReader computeValue(Class<?> type) {
636640
*/
637641
public static final class Builder {
638642
private boolean captureExperimentalAttributes;
639-
private List<String> captureMdcAttributes = emptyList();
643+
private List<String> captureMdcAttributes = List.of();
644+
private Set<String> excludeMdcAttributes = Set.of();
640645
private boolean captureCodeAttributes;
641646
private boolean captureMarkerAttribute;
642647
private boolean captureKeyValuePairAttributes;
@@ -659,6 +664,12 @@ public Builder setCaptureMdcAttributes(List<String> captureMdcAttributes) {
659664
return this;
660665
}
661666

667+
@CanIgnoreReturnValue
668+
public Builder setExcludeMdcAttributes(Set<String> excludeMdcAttributes) {
669+
this.excludeMdcAttributes = excludeMdcAttributes;
670+
return this;
671+
}
672+
662673
@CanIgnoreReturnValue
663674
public Builder setCaptureCodeAttributes(boolean captureCodeAttributes) {
664675
this.captureCodeAttributes = captureCodeAttributes;

0 commit comments

Comments
 (0)