Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -37,7 +35,8 @@ public class OpenTelemetryAppender extends UnsynchronizedAppenderBase<ILoggingEv
private boolean captureLoggerContext = false;
private boolean captureArguments = false;
private boolean captureLogstashAttributes = false;
private List<String> captureMdcAttributes = emptyList();
private List<String> captureMdcAttributes = List.of();
private Set<String> excludeMdcAttributes = Set.of();
private boolean captureEventName = false;

private volatile OpenTelemetry openTelemetry;
Expand Down Expand Up @@ -83,6 +82,7 @@ public void start() {
LoggingEventMapper.builder()
.setCaptureExperimentalAttributes(captureExperimentalAttributes)
.setCaptureMdcAttributes(captureMdcAttributes)
.setExcludeMdcAttributes(excludeMdcAttributes)
.setCaptureCodeAttributes(captureCodeAttributes)
.setCaptureMarkerAttribute(captureMarkerAttribute)
.setCaptureKeyValuePairAttributes(captureKeyValuePairAttributes)
Expand Down Expand Up @@ -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();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -77,6 +77,7 @@ public final class LoggingEventMapper {

private final boolean captureExperimentalAttributes;
private final List<String> captureMdcAttributes;
private final Set<String> excludeMdcAttributes;
private final boolean captureAllMdcAttributes;
private final boolean captureCodeAttributes;
private final boolean captureMarkerAttribute;
Expand All @@ -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;
Expand Down Expand Up @@ -243,7 +245,9 @@ private static void setTimestampFromInstant(
void captureMdcAttributes(LogRecordBuilder builder, Map<String, String> mdcProperties) {
if (captureAllMdcAttributes) {
for (Map.Entry<String, String> entry : mdcProperties.entrySet()) {
setAttributeOrEventName(builder, getAttributeKey(entry.getKey()), entry.getValue());
if (!excludeMdcAttributes.contains(entry.getKey())) {
setAttributeOrEventName(builder, getAttributeKey(entry.getKey()), entry.getValue());
}
}
return;
}
Expand Down Expand Up @@ -636,7 +640,8 @@ protected FieldReader computeValue(Class<?> type) {
*/
public static final class Builder {
private boolean captureExperimentalAttributes;
private List<String> captureMdcAttributes = emptyList();
private List<String> captureMdcAttributes = List.of();
private Set<String> excludeMdcAttributes = Set.of();
private boolean captureCodeAttributes;
private boolean captureMarkerAttribute;
private boolean captureKeyValuePairAttributes;
Expand All @@ -659,6 +664,12 @@ public Builder setCaptureMdcAttributes(List<String> captureMdcAttributes) {
return this;
}

@CanIgnoreReturnValue
public Builder setExcludeMdcAttributes(Set<String> excludeMdcAttributes) {
this.excludeMdcAttributes = excludeMdcAttributes;
return this;
}

@CanIgnoreReturnValue
public Builder setCaptureCodeAttributes(boolean captureCodeAttributes) {
this.captureCodeAttributes = captureCodeAttributes;
Expand Down
Loading