88import static io .opentelemetry .semconv .CodeAttributes .CODE_FILE_PATH ;
99import static io .opentelemetry .semconv .CodeAttributes .CODE_FUNCTION_NAME ;
1010import static io .opentelemetry .semconv .CodeAttributes .CODE_LINE_NUMBER ;
11- import static java .util .Collections .emptyList ;
1211
1312import ch .qos .logback .classic .Level ;
1413import ch .qos .logback .classic .spi .ILoggingEvent ;
3534import java .util .Iterator ;
3635import java .util .List ;
3736import java .util .Map ;
37+ import java .util .Set ;
3838import java .util .concurrent .TimeUnit ;
3939import java .util .function .Function ;
4040import 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