Refactor log4jX instrumentations
MODIFY
dd-java-agent/agent-tooling/src/main/java/datadog/trace/agent/tooling/log/LogContextScopeListener.java
dd-java-agent/agent-tooling/src/main/java/datadog/trace/agent/tooling/log/LogContextScopeListener.java
@@ -7,7 +7,7 @@ import lombok.extern.slf4j.Slf4j;
/**
* A scope listener that receives the MDC/ThreadContext put and receive methods and update the trace
- * and span reference anytime a new scope is activated.
+ * and span reference anytime a new scope is activated or closed.
*/
@Slf4j
public class LogContextScopeListener implements ScopeListener {

MODIFY
dd-java-agent/instrumentation/log4j1/log4j1.gradle
dd-java-agent/instrumentation/log4j1/log4j1.gradle
@@ -15,7 +15,7 @@ muzzle {
configurations {
// In order to test the real log4j library we need to remove the log4j transitive dependency
// dependency brought in by :dd-java-agent:testing over 'log4j-over-slf4j' which would shadow
- // the log4j module under test.
+ // the log4j module under test using a proxy to slf4j instead.
testCompile.exclude group: 'org.slf4j', module: 'log4j-over-slf4j'
}

MODIFY
dd-java-agent/instrumentation/log4j1/src/main/java/datadog/trace/instrumentation/log4j1/Log4j1MDCInstrumentation.java
dd-java-agent/instrumentation/log4j1/src/main/java/datadog/trace/instrumentation/log4j1/Log4j1MDCInstrumentation.java
@@ -20,21 +20,18 @@ import net.bytebuddy.matcher.ElementMatcher;
public class Log4j1MDCInstrumentation extends Instrumenter.Default {
public static final String MDC_INSTRUMENTATION_NAME = "log4j-mdc";
- private static final String mdcClassName = "org.apache.log4j.MDC";
-
public Log4j1MDCInstrumentation() {
super(MDC_INSTRUMENTATION_NAME);
}
@Override
protected boolean defaultEnabled() {
- return Config.getBooleanSettingFromEnvironment(
- Config.LOGS_INJECTION_ENABLED, Config.DEFAULT_LOGS_INJECTION_ENABLED);
+ return Config.get().isLogsInjectionEnabled();
}
@Override
public ElementMatcher<? super TypeDescription> typeMatcher() {
- return named(mdcClassName);
+ return named("org.apache.log4j.MDC");
}
@Override

DELETE
dd-java-agent/instrumentation/log4j1/src/main/java/datadoggggg/trace/instrumentation/log4j1/something/SomeClass.java
-package datadoggggg.trace.instrumentation.log4j1.something;
-
-public class SomeClass {
-
- private static SomeClass instance = new SomeClass();
-
- public SomeClass() {
- System.out.println("SomeClass Constructor.......!!!!!!!");
- }
-
- public static void put() {
- instance.doSomething();
- }
-
- public void doSomething() {
- System.out.println("SomeClass Doing something............");
- }
-}

MODIFY
dd-java-agent/instrumentation/log4j1/src/test/groovy/Log4j1MDCTest.groovy
dd-java-agent/instrumentation/log4j1/src/test/groovy/Log4j1MDCTest.groovy
@@ -9,8 +9,10 @@ import java.util.concurrent.atomic.AtomicReference
class Log4j1MDCTest extends AgentTestRunner {
static {
+ ConfigUtils.updateConfig {
System.setProperty("dd.logs.injection", "true")
}
+ }
def "MDC shows trace and span ids for active scope"() {

MODIFY
dd-java-agent/instrumentation/log4j2/log4j2.gradle
dd-java-agent/instrumentation/log4j2/log4j2.gradle
@@ -7,7 +7,7 @@ ext {
configurations {
// In order to test the real log4j library we need to remove the log4j transitive dependency
// dependency brought in by :dd-java-agent:testing over 'log4j-over-slf4j' which would shadow
- // the log4j module under test.
+ // the log4j module under test using a proxy to slf4j instead.
testCompile.exclude group: 'org.slf4j', module: 'log4j-over-slf4j'
}

MODIFY
dd-java-agent/instrumentation/log4j2/src/main/java/datadog/trace/instrumentation/log4j2/ThreadContextInstrumentation.java
dd-java-agent/instrumentation/log4j2/src/main/java/datadog/trace/instrumentation/log4j2/ThreadContextInstrumentation.java
@@ -22,21 +22,18 @@ import net.bytebuddy.utility.JavaModule;
public class ThreadContextInstrumentation extends Instrumenter.Default {
public static final String MDC_INSTRUMENTATION_NAME = "log4j-thread-context";
- private static final String mdcClassName = "org.apache.logging.log4j.ThreadContext";
-
public ThreadContextInstrumentation() {
super(MDC_INSTRUMENTATION_NAME);
}
@Override
protected boolean defaultEnabled() {
- return Config.getBooleanSettingFromEnvironment(
- Config.LOGS_INJECTION_ENABLED, Config.DEFAULT_LOGS_INJECTION_ENABLED);
+ return Config.get().isLogsInjectionEnabled();
}
@Override
public ElementMatcher<? super TypeDescription> typeMatcher() {
- return named(mdcClassName);
+ return named("org.apache.logging.log4j.ThreadContext");
}
@Override

MODIFY
dd-java-agent/instrumentation/log4j2/src/test/groovy/Log4jThreadContextTest.groovy
dd-java-agent/instrumentation/log4j2/src/test/groovy/Log4jThreadContextTest.groovy
@@ -10,8 +10,10 @@ import java.util.concurrent.atomic.AtomicReference
class Log4jThreadContextTest extends AgentTestRunner {
static {
+ ConfigUtils.updateConfig {
System.setProperty("dd.logs.injection", "true")
}
+ }
def "ThreadContext shows trace and span ids for active scope"() {
when:

Refactor log4jX instrumentations