Support older log4j2 versions in the javaagent
MODIFY
instrumentation/log4j/log4j-appender-2.17/javaagent/build.gradle.kts
instrumentation/log4j/log4j-appender-2.17/javaagent/build.gradle.kts
@@ -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)
}
}

MODIFY
instrumentation/log4j/log4j-appender-2.17/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/log4j/appender/v2_17/Log4jHelper.java
instrumentation/log4j/log4j-appender-2.17/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/log4j/appender/v2_17/Log4jHelper.java
@@ -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();
}

MODIFY
instrumentation/log4j/log4j-appender-2.17/library/src/main/java/io/opentelemetry/instrumentation/log4j/appender/v2_17/OpenTelemetryAppender.java
instrumentation/log4j/log4j-appender-2.17/library/src/main/java/io/opentelemetry/instrumentation/log4j/appender/v2_17/OpenTelemetryAppender.java
@@ -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();
}

MODIFY
instrumentation/log4j/log4j-appender-2.17/library/src/main/java/io/opentelemetry/instrumentation/log4j/appender/v2_17/internal/LogEventMapper.java
instrumentation/log4j/log4j-appender-2.17/library/src/main/java/io/opentelemetry/instrumentation/log4j/appender/v2_17/internal/LogEventMapper.java
@@ -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,7 +145,10 @@ public final class LogEventMapper<T> {
}
if (captureMapMessageAttributes) {
- mapMessage.forEach(
+ // TODO (trask) this could be optimized in 2.9 and later by calling MapMessage.forEach()
+ mapMessage
+ .getData()
+ .forEach(
(key, value) -> {
if (value != null
&& (!checkSpecialMapMessageAttribute

Support older log4j2 versions in the javaagent (#5979)