Add builder for logback LoggingEventMapper (#10000)
This commit is contained in:
parent
5fbed13539
commit
2eb5974ecd
|
@ -41,13 +41,14 @@ public final class LogbackSingletons {
|
||||||
emptyList());
|
emptyList());
|
||||||
|
|
||||||
mapper =
|
mapper =
|
||||||
new LoggingEventMapper(
|
LoggingEventMapper.builder()
|
||||||
captureExperimentalAttributes,
|
.setCaptureExperimentalAttributes(captureExperimentalAttributes)
|
||||||
captureMdcAttributes,
|
.setCaptureMdcAttributes(captureMdcAttributes)
|
||||||
captureCodeAttributes,
|
.setCaptureCodeAttributes(captureCodeAttributes)
|
||||||
captureMarkerAttribute,
|
.setCaptureMarkerAttribute(captureMarkerAttribute)
|
||||||
captureKeyValuePairAttributes,
|
.setCaptureKeyValuePairAttributes(captureKeyValuePairAttributes)
|
||||||
captureLoggerContext);
|
.setCaptureLoggerContext(captureLoggerContext)
|
||||||
|
.build();
|
||||||
}
|
}
|
||||||
|
|
||||||
public static LoggingEventMapper mapper() {
|
public static LoggingEventMapper mapper() {
|
||||||
|
|
|
@ -72,13 +72,14 @@ public class OpenTelemetryAppender extends UnsynchronizedAppenderBase<ILoggingEv
|
||||||
@Override
|
@Override
|
||||||
public void start() {
|
public void start() {
|
||||||
mapper =
|
mapper =
|
||||||
new LoggingEventMapper(
|
LoggingEventMapper.builder()
|
||||||
captureExperimentalAttributes,
|
.setCaptureExperimentalAttributes(captureExperimentalAttributes)
|
||||||
captureMdcAttributes,
|
.setCaptureMdcAttributes(captureMdcAttributes)
|
||||||
captureCodeAttributes,
|
.setCaptureCodeAttributes(captureCodeAttributes)
|
||||||
captureMarkerAttribute,
|
.setCaptureMarkerAttribute(captureMarkerAttribute)
|
||||||
captureKeyValuePairAttributes,
|
.setCaptureKeyValuePairAttributes(captureKeyValuePairAttributes)
|
||||||
captureLoggerContext);
|
.setCaptureLoggerContext(captureLoggerContext)
|
||||||
|
.build();
|
||||||
eventsToReplay = new ArrayBlockingQueue<>(numLogsCapturedBeforeOtelInstall);
|
eventsToReplay = new ArrayBlockingQueue<>(numLogsCapturedBeforeOtelInstall);
|
||||||
super.start();
|
super.start();
|
||||||
}
|
}
|
||||||
|
|
|
@ -5,9 +5,12 @@
|
||||||
|
|
||||||
package io.opentelemetry.instrumentation.logback.appender.v1_0.internal;
|
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.Level;
|
||||||
import ch.qos.logback.classic.spi.ILoggingEvent;
|
import ch.qos.logback.classic.spi.ILoggingEvent;
|
||||||
import ch.qos.logback.classic.spi.ThrowableProxy;
|
import ch.qos.logback.classic.spi.ThrowableProxy;
|
||||||
|
import com.google.errorprone.annotations.CanIgnoreReturnValue;
|
||||||
import io.opentelemetry.api.common.AttributeKey;
|
import io.opentelemetry.api.common.AttributeKey;
|
||||||
import io.opentelemetry.api.common.Attributes;
|
import io.opentelemetry.api.common.Attributes;
|
||||||
import io.opentelemetry.api.common.AttributesBuilder;
|
import io.opentelemetry.api.common.AttributesBuilder;
|
||||||
|
@ -49,21 +52,19 @@ public final class LoggingEventMapper {
|
||||||
private final boolean captureKeyValuePairAttributes;
|
private final boolean captureKeyValuePairAttributes;
|
||||||
private final boolean captureLoggerContext;
|
private final boolean captureLoggerContext;
|
||||||
|
|
||||||
public LoggingEventMapper(
|
private LoggingEventMapper(Builder builder) {
|
||||||
boolean captureExperimentalAttributes,
|
this.captureExperimentalAttributes = builder.captureExperimentalAttributes;
|
||||||
List<String> captureMdcAttributes,
|
this.captureCodeAttributes = builder.captureCodeAttributes;
|
||||||
boolean captureCodeAttributes,
|
this.captureMdcAttributes = builder.captureMdcAttributes;
|
||||||
boolean captureMarkerAttribute,
|
this.captureMarkerAttribute = builder.captureMarkerAttribute;
|
||||||
boolean captureKeyValuePairAttributes,
|
this.captureKeyValuePairAttributes = builder.captureKeyValuePairAttributes;
|
||||||
boolean captureLoggerContext) {
|
this.captureLoggerContext = builder.captureLoggerContext;
|
||||||
this.captureExperimentalAttributes = captureExperimentalAttributes;
|
|
||||||
this.captureCodeAttributes = captureCodeAttributes;
|
|
||||||
this.captureMdcAttributes = captureMdcAttributes;
|
|
||||||
this.captureMarkerAttribute = captureMarkerAttribute;
|
|
||||||
this.captureKeyValuePairAttributes = captureKeyValuePairAttributes;
|
|
||||||
this.captureLoggerContext = captureLoggerContext;
|
|
||||||
this.captureAllMdcAttributes =
|
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) {
|
public void emit(LoggerProvider loggerProvider, ILoggingEvent event, long threadId) {
|
||||||
|
@ -296,4 +297,59 @@ public final class LoggingEventMapper {
|
||||||
|
|
||||||
return true;
|
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);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -6,7 +6,6 @@
|
||||||
package io.opentelemetry.instrumentation.logback.appender.v1_0.internal;
|
package io.opentelemetry.instrumentation.logback.appender.v1_0.internal;
|
||||||
|
|
||||||
import static io.opentelemetry.sdk.testing.assertj.OpenTelemetryAssertions.assertThat;
|
import static io.opentelemetry.sdk.testing.assertj.OpenTelemetryAssertions.assertThat;
|
||||||
import static java.util.Collections.emptyList;
|
|
||||||
import static java.util.Collections.singletonList;
|
import static java.util.Collections.singletonList;
|
||||||
import static org.assertj.core.api.Assertions.entry;
|
import static org.assertj.core.api.Assertions.entry;
|
||||||
|
|
||||||
|
@ -22,8 +21,7 @@ class LoggingEventMapperTest {
|
||||||
@Test
|
@Test
|
||||||
void testDefault() {
|
void testDefault() {
|
||||||
// given
|
// given
|
||||||
LoggingEventMapper mapper =
|
LoggingEventMapper mapper = LoggingEventMapper.builder().build();
|
||||||
new LoggingEventMapper(false, emptyList(), false, false, false, false);
|
|
||||||
Map<String, String> contextData = new HashMap<>();
|
Map<String, String> contextData = new HashMap<>();
|
||||||
contextData.put("key1", "value1");
|
contextData.put("key1", "value1");
|
||||||
contextData.put("key2", "value2");
|
contextData.put("key2", "value2");
|
||||||
|
@ -40,7 +38,7 @@ class LoggingEventMapperTest {
|
||||||
void testSome() {
|
void testSome() {
|
||||||
// given
|
// given
|
||||||
LoggingEventMapper mapper =
|
LoggingEventMapper mapper =
|
||||||
new LoggingEventMapper(false, singletonList("key2"), false, false, false, false);
|
LoggingEventMapper.builder().setCaptureMdcAttributes(singletonList("key2")).build();
|
||||||
Map<String, String> contextData = new HashMap<>();
|
Map<String, String> contextData = new HashMap<>();
|
||||||
contextData.put("key1", "value1");
|
contextData.put("key1", "value1");
|
||||||
contextData.put("key2", "value2");
|
contextData.put("key2", "value2");
|
||||||
|
@ -58,7 +56,7 @@ class LoggingEventMapperTest {
|
||||||
void testAll() {
|
void testAll() {
|
||||||
// given
|
// given
|
||||||
LoggingEventMapper mapper =
|
LoggingEventMapper mapper =
|
||||||
new LoggingEventMapper(false, singletonList("*"), false, false, false, false);
|
LoggingEventMapper.builder().setCaptureMdcAttributes(singletonList("*")).build();
|
||||||
Map<String, String> contextData = new HashMap<>();
|
Map<String, String> contextData = new HashMap<>();
|
||||||
contextData.put("key1", "value1");
|
contextData.put("key1", "value1");
|
||||||
contextData.put("key2", "value2");
|
contextData.put("key2", "value2");
|
||||||
|
|
Loading…
Reference in New Issue