diff --git a/CHANGELOG.next-release.md b/CHANGELOG.next-release.md index 91ab0d62d0..6292a1852f 100644 --- a/CHANGELOG.next-release.md +++ b/CHANGELOG.next-release.md @@ -9,6 +9,7 @@ This file contains all changes which are not released yet. # Fixes +* Fix `NoSuchMethodError` in Log4j 2.7 log correlation, which called `ThreadContext.removeAll` that is only available since Log4j 2.8 - [#4593](https://github.com/elastic/apm-agent-java/issues/4593) # Features and enhancements diff --git a/apm-agent-plugins/apm-logging-plugin/apm-log4j2-plugin-tests/src/test/java/co/elastic/apm/agent/log4j2/Log4j2InstrumentationTest.java b/apm-agent-plugins/apm-logging-plugin/apm-log4j2-plugin-tests/src/test/java/co/elastic/apm/agent/log4j2/Log4j2InstrumentationTest.java index 3a4c05ce6b..d76aa64524 100644 --- a/apm-agent-plugins/apm-logging-plugin/apm-log4j2-plugin-tests/src/test/java/co/elastic/apm/agent/log4j2/Log4j2InstrumentationTest.java +++ b/apm-agent-plugins/apm-logging-plugin/apm-log4j2-plugin-tests/src/test/java/co/elastic/apm/agent/log4j2/Log4j2InstrumentationTest.java @@ -30,7 +30,7 @@ public class Log4j2InstrumentationTest { @ParameterizedTest - @ValueSource(strings = {"2.6", "2.17.1", "2.24.0"}) + @ValueSource(strings = {"2.6", "2.7", "2.17.1", "2.24.0"}) public void testInstrumentation(String version) throws Exception { List dependencies = List.of( "org.apache.logging.log4j:log4j-core:" + version, @@ -43,7 +43,7 @@ public void testInstrumentation(String version) throws Exception { } @ParameterizedTest - @ValueSource(strings = {"2.6", "2.17.1", "2.24.0"}) + @ValueSource(strings = {"2.6", "2.7", "2.17.1", "2.24.0"}) public void testErrorCapture(String version) throws Exception { List dependencies = List.of( "org.apache.logging.log4j:log4j-core:" + version, diff --git a/apm-agent-plugins/apm-logging-plugin/apm-log4j2-plugin/src/main/java/co/elastic/apm/agent/log4j2/correlation/Log4j2_7PlusLogCorrelationHelper.java b/apm-agent-plugins/apm-logging-plugin/apm-log4j2-plugin/src/main/java/co/elastic/apm/agent/log4j2/correlation/Log4j2_7PlusLogCorrelationHelper.java index fd0b2ac813..54e6e8a346 100644 --- a/apm-agent-plugins/apm-logging-plugin/apm-log4j2-plugin/src/main/java/co/elastic/apm/agent/log4j2/correlation/Log4j2_7PlusLogCorrelationHelper.java +++ b/apm-agent-plugins/apm-logging-plugin/apm-log4j2-plugin/src/main/java/co/elastic/apm/agent/log4j2/correlation/Log4j2_7PlusLogCorrelationHelper.java @@ -27,7 +27,8 @@ import java.util.Map; /** - * Using {@link ThreadContext#putAll(Map)} that is available since 2.7 for improved efficiency + * Using {@link ThreadContext#putAll(Map)} that is available since 2.7 for improved efficiency. + * Keys are removed one by one, as {@link ThreadContext#removeAll(Iterable)} is only available since 2.8. */ public class Log4j2_7PlusLogCorrelationHelper extends AbstractLogCorrelationHelper { @@ -44,6 +45,8 @@ protected boolean addToMdc() { @Override protected void removeFromMdc() { - ThreadContext.removeAll(CorrelationIdMapAdapter.allKeys()); + for (String key : CorrelationIdMapAdapter.allKeys()) { + ThreadContext.remove(key); + } } }