From 93c2fff100727fa75f5baf7e34a5d6e0f7adf6d0 Mon Sep 17 00:00:00 2001 From: Anshuman-Prakash <199287261+Anshuman-Prakash@users.noreply.github.com> Date: Sun, 18 Oct 2026 23:14:39 +0530 Subject: [PATCH] Fix NoSuchMethodError in Log4j 2.7 log correlation ThreadContext.removeAll(Iterable) is only available since Log4j 2.8, but Log4j2_7PlusLogCorrelationHelper is selected for log4j-core >= 2.7. Remove the correlation keys one by one with ThreadContext.remove(String) instead, and run the log4j2 instrumentation tests against 2.7 as well. Fixes #4593 Co-Authored-By: Claude Opus 5.5 --- CHANGELOG.next-release.md | 1 + .../apm/agent/log4j2/Log4j2InstrumentationTest.java | 4 ++-- .../correlation/Log4j2_7PlusLogCorrelationHelper.java | 7 +++++-- 3 files changed, 8 insertions(+), 4 deletions(-) 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); + } } }