Support older log4j2 versions in the javaagent (#5979)
This commit is contained in:
parent
2a7700351d
commit
246364d2fc
|
@ -6,7 +6,7 @@ muzzle {
|
|||
pass {
|
||||
group.set("org.apache.logging.log4j")
|
||||
module.set("log4j-core")
|
||||
versions.set("[2.11.0,)")
|
||||
versions.set("[2.0,)")
|
||||
assertInverse.set(true)
|
||||
}
|
||||
}
|
||||
|
|
|
@ -30,7 +30,7 @@ public final class Log4jHelper {
|
|||
LogBuilder builder =
|
||||
AgentLogEmitterProvider.get().logEmitterBuilder(instrumentationName).build().logBuilder();
|
||||
Map<String, String> contextData = ThreadContext.getImmutableContext();
|
||||
mapper.mapLogEvent(builder, message, level, throwable, null, contextData);
|
||||
mapper.mapLogEvent(builder, message, level, throwable, contextData);
|
||||
builder.emit();
|
||||
}
|
||||
|
||||
|
|
|
@ -13,6 +13,7 @@ import io.opentelemetry.instrumentation.log4j.appender.v2_17.internal.LogEventMa
|
|||
import io.opentelemetry.instrumentation.sdk.appender.internal.DelegatingLogEmitterProvider;
|
||||
import io.opentelemetry.sdk.logs.SdkLogEmitterProvider;
|
||||
import java.io.Serializable;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.function.BiConsumer;
|
||||
import javax.annotation.Nullable;
|
||||
import org.apache.logging.log4j.core.Appender;
|
||||
|
@ -24,6 +25,7 @@ import org.apache.logging.log4j.core.appender.AbstractAppender;
|
|||
import org.apache.logging.log4j.core.config.Property;
|
||||
import org.apache.logging.log4j.core.config.plugins.Plugin;
|
||||
import org.apache.logging.log4j.core.config.plugins.PluginBuilderFactory;
|
||||
import org.apache.logging.log4j.core.time.Instant;
|
||||
import org.apache.logging.log4j.util.ReadOnlyStringMap;
|
||||
|
||||
@Plugin(
|
||||
|
@ -74,12 +76,15 @@ public class OpenTelemetryAppender extends AbstractAppender {
|
|||
logEmitterProviderHolder.get().logEmitterBuilder(instrumentationName).build().logBuilder();
|
||||
ReadOnlyStringMap contextData = event.getContextData();
|
||||
mapper.mapLogEvent(
|
||||
builder,
|
||||
event.getMessage(),
|
||||
event.getLevel(),
|
||||
event.getThrown(),
|
||||
event.getInstant(),
|
||||
contextData);
|
||||
builder, event.getMessage(), event.getLevel(), event.getThrown(), contextData);
|
||||
|
||||
Instant timestamp = event.getInstant();
|
||||
if (timestamp != null) {
|
||||
builder.setEpoch(
|
||||
TimeUnit.MILLISECONDS.toNanos(timestamp.getEpochMillisecond())
|
||||
+ timestamp.getNanoOfMillisecond(),
|
||||
TimeUnit.NANOSECONDS);
|
||||
}
|
||||
builder.emit();
|
||||
}
|
||||
|
||||
|
|
|
@ -19,11 +19,9 @@ import io.opentelemetry.semconv.trace.attributes.SemanticAttributes;
|
|||
import java.io.PrintWriter;
|
||||
import java.io.StringWriter;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import javax.annotation.Nullable;
|
||||
import org.apache.logging.log4j.Level;
|
||||
import org.apache.logging.log4j.core.LogEvent;
|
||||
import org.apache.logging.log4j.core.time.Instant;
|
||||
import org.apache.logging.log4j.message.MapMessage;
|
||||
import org.apache.logging.log4j.message.Message;
|
||||
|
||||
|
@ -96,7 +94,6 @@ public final class LogEventMapper<T> {
|
|||
Message message,
|
||||
Level level,
|
||||
@Nullable Throwable throwable,
|
||||
@Nullable Instant timestamp,
|
||||
T contextData) {
|
||||
|
||||
AttributesBuilder attributes = Attributes.builder();
|
||||
|
@ -123,13 +120,6 @@ public final class LogEventMapper<T> {
|
|||
builder.setAttributes(attributes.build());
|
||||
|
||||
builder.setContext(Context.current());
|
||||
|
||||
if (timestamp != null) {
|
||||
builder.setEpoch(
|
||||
TimeUnit.MILLISECONDS.toNanos(timestamp.getEpochMillisecond())
|
||||
+ timestamp.getNanoOfMillisecond(),
|
||||
TimeUnit.NANOSECONDS);
|
||||
}
|
||||
}
|
||||
|
||||
// visible for testing
|
||||
|
@ -155,16 +145,19 @@ public final class LogEventMapper<T> {
|
|||
}
|
||||
|
||||
if (captureMapMessageAttributes) {
|
||||
mapMessage.forEach(
|
||||
(key, value) -> {
|
||||
if (value != null
|
||||
&& (!checkSpecialMapMessageAttribute
|
||||
|| !key.equals(SPECIAL_MAP_MESSAGE_ATTRIBUTE))) {
|
||||
attributes.put(
|
||||
mapMessageAttributeKeyCache.computeIfAbsent(key, AttributeKey::stringKey),
|
||||
value.toString());
|
||||
}
|
||||
});
|
||||
// TODO (trask) this could be optimized in 2.9 and later by calling MapMessage.forEach()
|
||||
mapMessage
|
||||
.getData()
|
||||
.forEach(
|
||||
(key, value) -> {
|
||||
if (value != null
|
||||
&& (!checkSpecialMapMessageAttribute
|
||||
|| !key.equals(SPECIAL_MAP_MESSAGE_ATTRIBUTE))) {
|
||||
attributes.put(
|
||||
mapMessageAttributeKeyCache.computeIfAbsent(key, AttributeKey::stringKey),
|
||||
value.toString());
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue