Refactor log4jX instrumentations

This commit is contained in:
Luca Abbati 2019-06-20 17:36:10 -04:00
parent 34d589e536
commit ecdf6664ef
No known key found for this signature in database
GPG Key ID: 74DBB952D9BA17F2
8 changed files with 11 additions and 31 deletions

View File

@ -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 * 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 @Slf4j
public class LogContextScopeListener implements ScopeListener { public class LogContextScopeListener implements ScopeListener {

View File

@ -15,7 +15,7 @@ muzzle {
configurations { configurations {
// In order to test the real log4j library we need to remove the log4j transitive dependency // 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 // 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' testCompile.exclude group: 'org.slf4j', module: 'log4j-over-slf4j'
} }

View File

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

View File

@ -1,18 +0,0 @@
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............");
}
}

View File

@ -9,7 +9,9 @@ import java.util.concurrent.atomic.AtomicReference
class Log4j1MDCTest extends AgentTestRunner { class Log4j1MDCTest extends AgentTestRunner {
static { static {
ConfigUtils.updateConfig {
System.setProperty("dd.logs.injection", "true") System.setProperty("dd.logs.injection", "true")
}
} }
def "MDC shows trace and span ids for active scope"() { def "MDC shows trace and span ids for active scope"() {

View File

@ -7,7 +7,7 @@ ext {
configurations { configurations {
// In order to test the real log4j library we need to remove the log4j transitive dependency // 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 // 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' testCompile.exclude group: 'org.slf4j', module: 'log4j-over-slf4j'
} }

View File

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

View File

@ -10,7 +10,9 @@ import java.util.concurrent.atomic.AtomicReference
class Log4jThreadContextTest extends AgentTestRunner { class Log4jThreadContextTest extends AgentTestRunner {
static { static {
ConfigUtils.updateConfig {
System.setProperty("dd.logs.injection", "true") System.setProperty("dd.logs.injection", "true")
}
} }
def "ThreadContext shows trace and span ids for active scope"() { def "ThreadContext shows trace and span ids for active scope"() {