Fix double log capture
MODIFY
instrumentation/log4j/log4j-2.0/src/main/java/io/opentelemetry/auto/instrumentation/log4j/v2_0/Log4jSpansInstrumentation.java
instrumentation/log4j/log4j-2.0/src/main/java/io/opentelemetry/auto/instrumentation/log4j/v2_0/Log4jSpansInstrumentation.java
@@ -85,13 +85,26 @@ public class Log4jSpansInstrumentation extends Instrumenter.Default {
public static class LogMessageAdvice {
@Advice.OnMethodEnter(suppress = Throwable.class)
- public static void methodEnter(
+ public static boolean methodEnter(
@Advice.This final Logger logger,
@Advice.Argument(1) final Level level,
@Advice.Argument(3) final Message message,
@Advice.Argument(4) final Throwable t) {
+ // need to track call depth across all loggers in order to avoid double capture when one
+ // logging framework delegates to another
+ final boolean topLevel = CallDepthThreadLocalMap.incrementCallDepth("logger") == 0;
+ if (topLevel) {
Log4jSpans.capture(logger, level, message, t);
}
+ return topLevel;
+ }
+
+ @Advice.OnMethodExit(onThrowable = Throwable.class, suppress = Throwable.class)
+ public static void methodExit(@Advice.Enter final boolean topLevel) {
+ if (topLevel) {
+ CallDepthThreadLocalMap.reset("logger");
+ }
+ }
}
public static class LogAdvice {

Fix double log capture (#299)