From 6e0af7cdeb409e284c9f19a0141793f56aa4ed0f Mon Sep 17 00:00:00 2001 From: Laplie Anderson Date: Wed, 16 Oct 2019 17:10:50 -0400 Subject: [PATCH 1/3] Dont instrument `@Decorator`s or CDI breaks --- .../trace/agent/tooling/AgentInstaller.java | 3 +++ .../instrumentation/cdi-1.2/cdi-1.2.gradle | 22 ++++++++++++++++++ .../src/test/groovy/CDIContainerTest.groovy | 23 +++++++++++++++++++ settings.gradle | 1 + 4 files changed, 49 insertions(+) create mode 100644 dd-java-agent/instrumentation/cdi-1.2/cdi-1.2.gradle create mode 100644 dd-java-agent/instrumentation/cdi-1.2/src/test/groovy/CDIContainerTest.groovy diff --git a/dd-java-agent/agent-tooling/src/main/java/datadog/trace/agent/tooling/AgentInstaller.java b/dd-java-agent/agent-tooling/src/main/java/datadog/trace/agent/tooling/AgentInstaller.java index 26ae102c88c..cb9d8908827 100644 --- a/dd-java-agent/agent-tooling/src/main/java/datadog/trace/agent/tooling/AgentInstaller.java +++ b/dd-java-agent/agent-tooling/src/main/java/datadog/trace/agent/tooling/AgentInstaller.java @@ -1,7 +1,9 @@ package datadog.trace.agent.tooling; import static datadog.trace.agent.tooling.ClassLoaderMatcher.skipClassLoader; +import static net.bytebuddy.matcher.ElementMatchers.annotationType; import static net.bytebuddy.matcher.ElementMatchers.any; +import static net.bytebuddy.matcher.ElementMatchers.hasAnnotation; import static net.bytebuddy.matcher.ElementMatchers.nameContains; import static net.bytebuddy.matcher.ElementMatchers.nameMatches; import static net.bytebuddy.matcher.ElementMatchers.nameStartsWith; @@ -130,6 +132,7 @@ public static ResettableClassFileTransformer installBytebuddyAgent( .or(nameContains("javassist")) .or(nameContains(".asm.")) .or(nameMatches("com\\.mchange\\.v2\\.c3p0\\..*Proxy")) + .or(hasAnnotation(annotationType(named("javax.decorator.Decorator")))) .or(matchesConfiguredExcludes()); for (final AgentBuilder.Listener listener : listeners) { diff --git a/dd-java-agent/instrumentation/cdi-1.2/cdi-1.2.gradle b/dd-java-agent/instrumentation/cdi-1.2/cdi-1.2.gradle new file mode 100644 index 00000000000..89072a7fcea --- /dev/null +++ b/dd-java-agent/instrumentation/cdi-1.2/cdi-1.2.gradle @@ -0,0 +1,22 @@ +apply from: "${rootDir}/gradle/java.gradle" + +apply plugin: 'org.unbroken-dome.test-sets' + +testSets { + latestDepTest { + dirName = 'test' + } +} + +dependencies { + testCompile project(':dd-java-agent:instrumentation:java-concurrent') + + testCompile group: 'org.jboss.weld', name: 'weld-core', version: '2.3.0.Final' + testCompile group: 'org.jboss.weld.se', name: 'weld-se', version: '2.3.0.Final' + testCompile group: 'org.jboss.weld.se', name: 'weld-se-core', version: '2.3.0.Final' + + // Beyond 2.x is CDI 2+ and requires Java 8 + latestDepTestCompile group: 'org.jboss.weld', name: 'weld-core', version: '2.+' + latestDepTestCompile group: 'org.jboss.weld.se', name: 'weld-se', version: '2.+' + latestDepTestCompile group: 'org.jboss.weld.se', name: 'weld-se-core', version: '2.+' +} diff --git a/dd-java-agent/instrumentation/cdi-1.2/src/test/groovy/CDIContainerTest.groovy b/dd-java-agent/instrumentation/cdi-1.2/src/test/groovy/CDIContainerTest.groovy new file mode 100644 index 00000000000..0c6376ab1b2 --- /dev/null +++ b/dd-java-agent/instrumentation/cdi-1.2/src/test/groovy/CDIContainerTest.groovy @@ -0,0 +1,23 @@ +import datadog.trace.agent.test.AgentTestRunner +import org.jboss.weld.environment.se.Weld +import org.jboss.weld.environment.se.WeldContainer +import org.jboss.weld.environment.se.threading.RunnableDecorator + +class CDIContainerTest extends AgentTestRunner { + + def "CDI container starts with agent"() { + given: + Weld builder = new Weld() + .disableDiscovery() + .decorators(RunnableDecorator) + + when: + WeldContainer container = builder.initialize() + + then: + container.isRunning() + + cleanup: + container?.shutdown() + } +} diff --git a/settings.gradle b/settings.gradle index 2d4fba74311..1e6419bf2b9 100644 --- a/settings.gradle +++ b/settings.gradle @@ -28,6 +28,7 @@ include ':dd-java-agent:instrumentation:apache-httpasyncclient-4' include ':dd-java-agent:instrumentation:apache-httpclient-4' include ':dd-java-agent:instrumentation:aws-java-sdk-1.11.0' include ':dd-java-agent:instrumentation:aws-java-sdk-2.2' +include ':dd-java-agent:instrumentation:cdi-1.2' include ':dd-java-agent:instrumentation:couchbase-2.0' include ':dd-java-agent:instrumentation:couchbase-2.6' include ':dd-java-agent:instrumentation:datastax-cassandra-3' From 8971040a4545b43865e2f9f9f78478558291a35c Mon Sep 17 00:00:00 2001 From: Laplie Anderson Date: Thu, 17 Oct 2019 13:42:38 -0400 Subject: [PATCH 2/3] Supertypes are not always available. Only check the current class for decorator --- .../java/datadog/trace/agent/tooling/AgentInstaller.java | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/dd-java-agent/agent-tooling/src/main/java/datadog/trace/agent/tooling/AgentInstaller.java b/dd-java-agent/agent-tooling/src/main/java/datadog/trace/agent/tooling/AgentInstaller.java index cb9d8908827..036f8acc6c4 100644 --- a/dd-java-agent/agent-tooling/src/main/java/datadog/trace/agent/tooling/AgentInstaller.java +++ b/dd-java-agent/agent-tooling/src/main/java/datadog/trace/agent/tooling/AgentInstaller.java @@ -1,9 +1,8 @@ package datadog.trace.agent.tooling; import static datadog.trace.agent.tooling.ClassLoaderMatcher.skipClassLoader; -import static net.bytebuddy.matcher.ElementMatchers.annotationType; import static net.bytebuddy.matcher.ElementMatchers.any; -import static net.bytebuddy.matcher.ElementMatchers.hasAnnotation; +import static net.bytebuddy.matcher.ElementMatchers.isAnnotatedWith; import static net.bytebuddy.matcher.ElementMatchers.nameContains; import static net.bytebuddy.matcher.ElementMatchers.nameMatches; import static net.bytebuddy.matcher.ElementMatchers.nameStartsWith; @@ -132,7 +131,7 @@ public static ResettableClassFileTransformer installBytebuddyAgent( .or(nameContains("javassist")) .or(nameContains(".asm.")) .or(nameMatches("com\\.mchange\\.v2\\.c3p0\\..*Proxy")) - .or(hasAnnotation(annotationType(named("javax.decorator.Decorator")))) + .or(isAnnotatedWith(named("javax.decorator.Decorator"))) .or(matchesConfiguredExcludes()); for (final AgentBuilder.Listener listener : listeners) { From f2c5ae2675066c9cc95d0a0634eb2a280a65967a Mon Sep 17 00:00:00 2001 From: Laplie Anderson Date: Thu, 17 Oct 2019 14:48:51 -0400 Subject: [PATCH 3/3] Latest version requires at least 1 bean --- .../src/test/groovy/CDIContainerTest.groovy | 4 +++- .../datadog/trace/instrumentation/TestBean.java | 14 ++++++++++++++ 2 files changed, 17 insertions(+), 1 deletion(-) create mode 100644 dd-java-agent/instrumentation/cdi-1.2/src/test/java/datadog/trace/instrumentation/TestBean.java diff --git a/dd-java-agent/instrumentation/cdi-1.2/src/test/groovy/CDIContainerTest.groovy b/dd-java-agent/instrumentation/cdi-1.2/src/test/groovy/CDIContainerTest.groovy index 0c6376ab1b2..6acb386213f 100644 --- a/dd-java-agent/instrumentation/cdi-1.2/src/test/groovy/CDIContainerTest.groovy +++ b/dd-java-agent/instrumentation/cdi-1.2/src/test/groovy/CDIContainerTest.groovy @@ -1,4 +1,5 @@ import datadog.trace.agent.test.AgentTestRunner +import datadog.trace.instrumentation.TestBean import org.jboss.weld.environment.se.Weld import org.jboss.weld.environment.se.WeldContainer import org.jboss.weld.environment.se.threading.RunnableDecorator @@ -9,7 +10,8 @@ class CDIContainerTest extends AgentTestRunner { given: Weld builder = new Weld() .disableDiscovery() - .decorators(RunnableDecorator) + .addDecorator(RunnableDecorator) + .addBeanClass(TestBean) when: WeldContainer container = builder.initialize() diff --git a/dd-java-agent/instrumentation/cdi-1.2/src/test/java/datadog/trace/instrumentation/TestBean.java b/dd-java-agent/instrumentation/cdi-1.2/src/test/java/datadog/trace/instrumentation/TestBean.java new file mode 100644 index 00000000000..3a7699aaa62 --- /dev/null +++ b/dd-java-agent/instrumentation/cdi-1.2/src/test/java/datadog/trace/instrumentation/TestBean.java @@ -0,0 +1,14 @@ +package datadog.trace.instrumentation; + +public class TestBean { + + private String someField; + + public String getSomeField() { + return someField; + } + + public void setSomeField(final String someField) { + this.someField = someField; + } +}