Hacktoberfest 2026: le issue che i maintainer hanno segnato per ottobre, aperte e adatte ai principianti. Sfoglia le issue Hacktoberfest

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

Aperta
#4,593 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

I maintainer di solito rispondono entro 1 giorno

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
3/5
Tempo stimato
1-2 giorni
Idoneità per principianti
55/100
Tipo di issue
Bug
Chiarezza
Abbastanza chiara
Stato di attività
Attiva
Stack tecnologico
java
Ambito
observability

Direzione di ricerca

Start with Log4j2_7PlusLogCorrelationHelper.removeFromMdc in the apm-log4j2-plugin, which calls ThreadContext.removeAll, and with the version check in Log4j2LogCorrelationInstrumentation that selects log4j-core 2.7 and above. Decide whether to replace the call with per-key ThreadContext.remove for 2.7 or gate the instrumentation, then check that the reproduction no longer throws NoSuchMethodError. Done means a regression test passes for both 2.7 and 2.8+, including cleanup after logging and thread reuse.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

agent-java community triage

Describe the bug

Elastic APM Java agent 1.57.0 emits repeated NoSuchMethodError messages when log correlation is used with Log4j 2.7:

ERROR co.elastic.apm.agent.bci.IndyBootstrap - Advice threw an exception, this should never happen!
java.lang.NoSuchMethodError: org.apache.logging.log4j.ThreadContext.removeAll(Ljava/lang/Iterable;)V
    at co.elastic.apm.agent.log4j2.correlation.Log4j2_7PlusLogCorrelationHelper.removeFromMdc(Log4j2_7PlusLogCorrelationHelper.java:47)
    at co.elastic.apm.agent.loginstr.correlation.AbstractLogCorrelationHelper.afterLoggingEvent(AbstractLogCorrelationHelper.java:54)

The application continues running, but ordinary logging during traced requests repeatedly triggers the error. This is not limited to startup.

Relevant versions: Java 8; log4j-api and log4j-core both 2.7; Elastic APM Java agent 1.57.0.

Steps to reproduce

Suggested minimal reproduction, derived from the observed error and source analysis (not yet independently executed as a standalone test):

  1. Use matching org.apache.logging.log4j:log4j-api:2.7 and org.apache.logging.log4j:log4j-core:2.7 dependencies.
  2. Start with agent 1.57.0, leaving log-correlation instrumentation enabled.
  3. Emit an enabled log event while an APM transaction is active, for example:
Transaction transaction = ElasticApm.startTransaction();
try (Scope scope = transaction.activate()) {
    LogManager.getLogger("example").info("correlation test");
} finally {
    transaction.end();
}

Here ElasticApm, Transaction, and Scope are from co.elastic.apm.api, and LogManager is from org.apache.logging.log4j.

Expected behavior

Log correlation should avoid invoking an API absent from the selected Log4j version. If Log4j 2.7 is supported, correlation IDs should be added and cleaned up without an exception. Otherwise the incompatible instrumentation should not be selected for 2.7.

Source analysis

The same version/API mismatch is present in main at commit 5cabf9ceeaea99f7a811b896064bf7aa8b131e5d (source inspected; that commit was not runtime-tested):

A possible fix is a compatible per-key ThreadContext.remove(String) cleanup for 2.7, or a separate version/capability-gated implementation. A regression test should cover 2.7 specifically, as well as 2.8+, including cleanup after logging and thread reuse.

This differs from #3745 / #3764: those address version detection in shaded JARs; here the application actually uses Log4j 2.7 and lacks the invoked API.

Workaround

Upgrade the application's matching Log4j dependencies to a compatible maintained version, or disable log-correlation temporarily, accepting the loss of automatic logging correlation.

Lingua principale
Java
Stelle
593
Fork
338
Merge medio
1g 9h
PR unite (30g)
13

Preparare l'ambiente

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di elastic/apm-agent-java

Tutte le issue di elastic/apm-agent-java

Issue simili

Altre issue su Java

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.