More refactoring for ScopeManager (DataDog/dd-trace-java#1467)
MODIFY
instrumentation/slf4j-mdc/src/main/java/io/opentelemetry/auto/instrumentation/slf4j/mdc/MDCInjectionInstrumentation.java
instrumentation/slf4j-mdc/src/main/java/io/opentelemetry/auto/instrumentation/slf4j/mdc/MDCInjectionInstrumentation.java
@@ -15,6 +15,7 @@ import java.util.Map;
import net.bytebuddy.asm.Advice;
import net.bytebuddy.description.method.MethodDescription;
import net.bytebuddy.description.type.TypeDescription;
+import net.bytebuddy.matcher.BooleanMatcher;
import net.bytebuddy.matcher.ElementMatcher;
import net.bytebuddy.utility.JavaModule;
@@ -26,6 +27,8 @@ public class MDCInjectionInstrumentation extends Instrumenter.Default {
// mdcClassName = org.slf4j.MDC
private static final String mdcClassName = "org.TMP.MDC".replaceFirst("TMP", "slf4j");
+ private boolean initialized = false;
+
public MDCInjectionInstrumentation() {
super(MDC_INSTRUMENTATION_NAME);
}
@@ -47,15 +50,22 @@ public class MDCInjectionInstrumentation extends Instrumenter.Default {
final JavaModule module,
final Class<?> classBeingRedefined,
final ProtectionDomain protectionDomain) {
- if (classBeingRedefined != null) {
+ if (classBeingRedefined != null && !initialized) {
MDCAdvice.mdcClassInitialized(classBeingRedefined);
}
+ initialized = true;
}
@Override
public Map<? extends ElementMatcher<? super MethodDescription>, String> transformers() {
return singletonMap(
- isTypeInitializer(), MDCInjectionInstrumentation.class.getName() + "$MDCAdvice");
+ new BooleanMatcher<MethodDescription>(false) {
+ @Override
+ public boolean matches(final MethodDescription target) {
+ return initialized;
+ }
+ }.and(isTypeInitializer()),
+ MDCInjectionInstrumentation.class.getName() + "$MDCAdvice");
}
@Override

More refactoring for ScopeManager (DataDog/dd-trace-java#1467)