MDC ThreadLocal initValue should be modifiable for SLF4j's copy-on-write thread context map (DataDog/dd-trace-java#1645)
MODIFY
agent-tooling/src/test/groovy/io/opentelemetry/auto/test/ClassLoaderMatcherTest.groovy
agent-tooling/src/test/groovy/io/opentelemetry/auto/test/ClassLoaderMatcherTest.groovy
@@ -19,6 +19,7 @@ package io.opentelemetry.auto.test
import io.opentelemetry.auto.bootstrap.AgentClassLoader
import io.opentelemetry.auto.tooling.ClassLoaderMatcher
import io.opentelemetry.auto.tooling.ExporterClassLoader
+import io.opentelemetry.auto.tooling.log.LogContextScopeListener
import io.opentelemetry.auto.util.test.AgentSpecification
class ClassLoaderMatcherTest extends AgentSpecification {
@@ -60,4 +61,9 @@ class ClassLoaderMatcherTest extends AgentSpecification {
expect:
ExporterClassLoader.name == "io.opentelemetry.auto.tooling.ExporterClassLoader"
}
+
+ def "helper class names are hardcoded in Log Instrumentations"() {
+ expect:
+ LogContextScopeListener.name == "io.opentelemetry.auto.tooling.log.LogContextScopeListener"
+ }
}

MODIFY
instrumentation/log4j/log4j-1.1/src/main/java/io/opentelemetry/auto/instrumentation/log4j/v1_1/Log4jMDCInstrumentation.java
instrumentation/log4j/log4j-1.1/src/main/java/io/opentelemetry/auto/instrumentation/log4j/v1_1/Log4jMDCInstrumentation.java
@@ -22,7 +22,6 @@ import static net.bytebuddy.matcher.ElementMatchers.named;
import io.opentelemetry.auto.config.Config;
import io.opentelemetry.auto.tooling.Instrumenter;
-import io.opentelemetry.auto.tooling.log.LogContextScopeListener;
import java.lang.reflect.Method;
import java.util.Map;
import net.bytebuddy.asm.Advice;
@@ -55,7 +54,7 @@ public class Log4jMDCInstrumentation extends Instrumenter.Default {
@Override
public String[] helperClassNames() {
- return new String[] {LogContextScopeListener.class.getName()};
+ return new String[] {"io.opentelemetry.auto.tooling.log.LogContextScopeListener"};
}
public static class MDCContextAdvice {

MODIFY
instrumentation/log4j/log4j-1.1/src/test/groovy/Log4jMDCTest.groovy
instrumentation/log4j/log4j-1.1/src/test/groovy/Log4jMDCTest.groovy
@@ -38,4 +38,15 @@ class Log4jMDCTest extends LogContextInjectionTestBase {
def get(String key) {
return MDC.get(key)
}
+
+ @Override
+ def remove(String key) {
+ MDC.context
+ return MDC.remove(key)
+ }
+
+ @Override
+ def clear() {
+ return MDC.clear()
+ }
}

MODIFY
instrumentation/log4j/log4j-2.0/src/test/groovy/Log4jThreadContextTest.groovy
instrumentation/log4j/log4j-2.0/src/test/groovy/Log4jThreadContextTest.groovy
@@ -31,4 +31,14 @@ class Log4jThreadContextTest extends LogContextInjectionTestBase {
def get(String key) {
return ThreadContext.get(key)
}
+
+ @Override
+ def remove(String key) {
+ return ThreadContext.remove(key)
+ }
+
+ @Override
+ def clear() {
+ return ThreadContext.clearAll()
+ }
}

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
@@ -70,7 +70,7 @@ public class MDCInjectionInstrumentation extends Instrumenter.Default {
@Override
public String[] helperClassNames() {
- return new String[] {LogContextScopeListener.class.getName()};
+ return new String[] {"io.opentelemetry.auto.tooling.log.LogContextScopeListener"};
}
public static class MDCAdvice {

MODIFY
instrumentation/slf4j-mdc/src/test/groovy/Slf4jMDCTest.groovy
instrumentation/slf4j-mdc/src/test/groovy/Slf4jMDCTest.groovy
@@ -12,4 +12,14 @@ class Slf4jMDCTest extends LogContextInjectionTestBase {
def get(String key) {
return MDC.get(key)
}
+
+ @Override
+ def remove(String key) {
+ return MDC.remove(key)
+ }
+
+ @Override
+ def clear() {
+ return MDC.clear()
+ }
}

MODIFY
testing-common/src/main/groovy/io/opentelemetry/auto/test/log/injection/LogContextInjectionTestBase.groovy
testing-common/src/main/groovy/io/opentelemetry/auto/test/log/injection/LogContextInjectionTestBase.groovy
@@ -44,6 +44,13 @@ abstract class LogContextInjectionTestBase extends AgentTestRunner {
*/
abstract get(String key)
+ /**
+ * Remove from the framework-specific context the value at the given key
+ */
+ abstract remove(String key)
+
+ abstract clear()
+
static {
ConfigUtils.updateConfig {
System.setProperty("ota.logs.injection", "true")
@@ -139,4 +146,21 @@ abstract class LogContextInjectionTestBase extends AgentTestRunner {
mainSpan?.end()
mainScope?.close()
}
+
+ def "modify thread context after clear of context map at the beginning of new thread"() {
+ def t1A
+ final Thread thread1 = new Thread() {
+ @Override
+ void run() {
+ clear()
+ put("a", "a thread1")
+ t1A = get("a")
+ }
+ }
+ thread1.start()
+ thread1.join()
+
+ expect:
+ t1A == "a thread1"
+ }
}

MDC ThreadLocal initValue should be modifiable for SLF4j's copy-on-write thread context map (DataDog/dd-trace-java#1645)