Skip to content

Fix NoSuchMethodError in Log4j 2.7 log correlation - #4600

Open
Anshuman-Prakash wants to merge 1 commit into
elastic:mainfrom
Anshuman-Prakash:fix-log4j-2.7-correlation
Open

Anshuman-Prakash wants to merge 1 commit into
elastic:mainfrom
Anshuman-Prakash:fix-log4j-2.7-correlation

Conversation

@Anshuman-Prakash

Copy link
Copy Markdown

What does this PR do?

Fixes #4593

Log4J2_7PlusLogCorrelationInstrumentation is selected for log4j-core >= 2.7, but its helper cleaned up the correlation IDs with ThreadContext.removeAll(Iterable), which Log4j only added in 2.8. On 2.7 every logging event during a transaction therefore threw NoSuchMethodError from the advice. Because the advice suppresses it, the correlation keys were never removed, so a stale error.id leaked onto later log lines.

  • Log4j2_7PlusLogCorrelationHelper.removeFromMdc() now removes the keys one by one with ThreadContext.remove(String), as the 2.6 helper already does. putAll(Map) (available since 2.7) is still used to add them.
  • Log4j2InstrumentationTest now also runs the instrumentation and error-capture definitions against Log4j 2.7, next to 2.6, 2.17.1 and 2.24.0.

Tests

Run in maven:3.9-eclipse-temurin-17 (Linux):

mvn -pl apm-agent-plugins/apm-logging-plugin/apm-log4j2-plugin-tests -am install -DskipTests
mvn -pl apm-agent-plugins/apm-logging-plugin/apm-log4j2-plugin-tests test -Dtest=Log4j2InstrumentationTest
  • Without the fix (test change only): Tests run: 8, Failures: 1. testInstrumentation[2] (2.7) fails in verifyErrorCaptureAndCorrelation with expected: null but was: "16e9e1523a…" (a leaked error.id), and the log shows the NoSuchMethodError: ThreadContext.removeAll(java.lang.Iterable) from the issue.
  • With the fix: Tests run: 8, Failures: 0, Errors: 0, and there is no NoSuchMethodError in the output.

Checklist

  • This is an enhancement of existing features, or a new feature in existing plugins
  • This is a bugfix
  • This is a new plugin
  • This is something else

🤖 Generated with Claude Code

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 elastic#4593

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@Anshuman-Prakash
Anshuman-Prakash requested a review from a team as a code owner October 18, 2026 17:45
@github-actions

Copy link
Copy Markdown

🤖 GitHub comments

Just comment with:

  • run docs-build : Re-trigger the docs validation. (use unformatted text in the comment!)

@github-actions github-actions Bot added agent-java community Issues and PRs created by the community triage labels Oct 18, 2026
@LikeTheSalad
LikeTheSalad requested a balanced review from Copilot October 27, 2026 09:47

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 Approval recommended

The narrow compatibility fix preserves cleanup behavior and adds regression coverage for the affected Log4j version, with no unresolved findings.

No open issues 🎉

What changed in this PR

Fixes #4593 by making log-correlation cleanup compatible with Log4j 2.7, preventing NoSuchMethodError and stale correlation IDs.

Changes:

  • Replaces bulk key removal with compatible per-key removal while retaining bulk insertion.
  • Adds Log4j 2.7 to the instrumentation and error-capture test matrices.
  • Documents the compatibility fix in the changelog.
File Description
CHANGELOG.next-release.md Records the Log4j 2.7 compatibility fix.
apm-agent-plugins/​apm-logging-plugin/​apm-log4j2-plugin/​src/​main/​java/​co/​elastic/​apm/​agent/​log4j2/​correlation/​Log4j2_7PlusLogCorrelationHelper.java Uses compatible per-key cleanup and explains the API constraint.
apm-agent-plugins/​apm-logging-plugin/​apm-log4j2-plugin-tests/​src/​test/​java/​co/​elastic/​apm/​agent/​log4j2/​Log4j2InstrumentationTest.java Adds Log4j 2.7 coverage to both test matrices.

Review effort: Balanced

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

agent-java community Issues and PRs created by the community triage

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Log4j 2.7 log correlation calls ThreadContext.removeAll, which was introduced in 2.8

2 participants