Remove helpers from agent
MODIFY
dd-java-agent-ittests/src/test/java/com/datadoghq/agent/ClassLoaderTest.java
dd-java-agent-ittests/src/test/java/com/datadoghq/agent/ClassLoaderTest.java
@@ -3,7 +3,6 @@ package com.datadoghq.agent;
import static dd.test.TestUtils.createJarWithClasses;
import com.datadoghq.trace.Trace;
-import java.lang.reflect.Method;
import java.net.URL;
import java.net.URLClassLoader;
import org.junit.Assert;
@@ -28,14 +27,6 @@ public class ClassLoaderTest {
Assert.assertEquals(
"Class must be loaded by loader.", loader, instrumentedClass.getClassLoader());
- final Class<?> rulesManagerClass =
- Class.forName(
- "com.datadoghq.agent.InstrumentationRulesManager",
- true,
- ClassLoader.getSystemClassLoader());
- Method isRegisteredMethod = rulesManagerClass.getMethod("isRegistered", Object.class);
- Assert.assertTrue(
- "Agent did not initialized loader.", (boolean) isRegisteredMethod.invoke(null, loader));
loader.close();
}

MODIFY
dd-java-agent-ittests/src/test/java/com/datadoghq/agent/instrumentation/annotation/TraceAnnotationsTest.java
dd-java-agent-ittests/src/test/java/com/datadoghq/agent/instrumentation/annotation/TraceAnnotationsTest.java
@@ -20,9 +20,6 @@ public class TraceAnnotationsTest {
@Before
public void beforeTest() throws Exception {
- Class.forName("com.datadoghq.agent.InstrumentationRulesManager")
- .getMethod("registerClassLoad")
- .invoke(null);
TestUtils.registerOrReplaceGlobalTracer(tracer);
writer.start();

MODIFY
dd-java-agent/benchmark/src/jmh/java/com/datadoghq/benchmark/ClassRetransformingBenchmark.java
dd-java-agent/benchmark/src/jmh/java/com/datadoghq/benchmark/ClassRetransformingBenchmark.java
@@ -31,12 +31,8 @@ public class ClassRetransformingBenchmark {
@Setup
public void initializeInstrumentation() {
- try {
- final Class<?> manager = Class.forName("com.datadoghq.agent.InstrumentationRulesManager");
- final Method registerClassLoad = manager.getMethod("registerClassLoad");
- registerClassLoad.invoke(null);
- } catch (final Exception e) {
- }
+ // loading TracedClass will initialize helper injection
+ TracedClass.class.getName();
}
@TearDown

MODIFY
dd-java-agent/dd-java-agent.gradle
dd-java-agent/dd-java-agent.gradle
@@ -44,18 +44,6 @@ dependencies {
testCompile deps.opentracingMock
}
-project(':dd-java-agent:integrations:helpers').afterEvaluate { helperProject ->
- project.processResources {
- from(helperProject.tasks.shadowJar)
- rename {
- it.startsWith("helpers") && it.endsWith(".jar") ?
- "helpers.jar.zip" :
- it
- }
- }
- project.processResources.dependsOn helperProject.tasks.shadowJar
-}
-
jar {
classifier = 'unbundled'

DELETE
dd-java-agent/integrations/helpers/helpers.gradle
-apply plugin: "com.github.johnrengelman.shadow"
-apply from: "${rootDir}/gradle/java.gradle"
-
-// We want to keep this jar as lean as possible. Only helpers and OT contrib classes.
-configurations.compile {
- transitive = false
-}
-
-dependencies {
- compileOnly project(':dd-trace')
- compileOnly project(':dd-trace-annotations')
- compileOnly deps.slf4j
-
- compileOnly group: 'org.mongodb', name: 'mongo-java-driver', version: '3.4.2'
- compileOnly group: 'org.mongodb', name: 'mongodb-driver-async', version: '3.4.2'
- compileOnly group: 'com.squareup.okhttp3', name: 'okhttp', version: '3.6.0'
- compileOnly group: 'javax.jms', name: 'javax.jms-api', version: '2.0.1'
- compileOnly group: 'com.datastax.cassandra', name: 'cassandra-driver-core', version: '3.2.0'
- compileOnly group: 'org.apache.httpcomponents', name: 'httpclient', version: '4.3'
-}
-
-jar {
- classifier = 'unbundled'
-}
-
-shadowJar {
- classifier null
- configurations = [project.configurations.compile]
-
- relocate 'org.slf4j', 'dd.slf4j' // Prevents conflict with other SLF4J instances. Important for premain.
-
- if (!project.hasProperty("disableShadowRelocate") || !disableShadowRelocate) {
- // This is needed to align with the relocation of TraceResolver in dd-java-agent.
- relocate 'io.opentracing.contrib', 'dd.opentracing.contrib'
- }
-
- dependencies {
- exclude(dependency('org.projectlombok:lombok:1.16.18'))
- }
-}

MODIFY
dd-java-agent/src/main/java/com/datadoghq/agent/InstrumentationRulesManager.java
dd-java-agent/src/main/java/com/datadoghq/agent/InstrumentationRulesManager.java
package com.datadoghq.agent;
-import com.google.common.collect.Maps;
import io.opentracing.Tracer;
import io.opentracing.contrib.tracerresolver.TracerResolver;
import io.opentracing.util.GlobalTracer;
-import java.io.ByteArrayOutputStream;
-import java.io.IOException;
-import java.io.InputStream;
-import java.util.Collections;
-import java.util.Map;
-import java.util.Set;
-import java.util.WeakHashMap;
-import java.util.zip.ZipEntry;
-import java.util.zip.ZipInputStream;
import lombok.extern.slf4j.Slf4j;
/**
@@ -21,106 +11,10 @@ import lombok.extern.slf4j.Slf4j;
*/
@Slf4j
public class InstrumentationRulesManager {
-
- private static final String INTEGRATION_RULES = "integration-rules.btm";
- private static final String HELPERS_NAME = "/helpers.jar.zip";
-
private static final Object SYNC = new Object();
- private final TracingAgentConfig config;
- private final AgentRulesManager agentRulesManager;
- private final ClassLoaderIntegrationInjector injector;
- private final InstrumentationChecker checker = new InstrumentationChecker();
-
- private final Set<ClassLoader> initializedClassloaders =
- Collections.newSetFromMap(new WeakHashMap<ClassLoader, Boolean>());
-
public InstrumentationRulesManager(
- final TracingAgentConfig config, final AgentRulesManager agentRulesManager) {
- this.config = config;
- this.agentRulesManager = agentRulesManager;
- final InputStream helpersStream = this.getClass().getResourceAsStream(HELPERS_NAME);
- final ZipInputStream stream = new ZipInputStream(helpersStream);
- final Map<ZipEntry, byte[]> helperEntries = Maps.newHashMap();
- try {
- ZipEntry entry = stream.getNextEntry();
- while (entry != null) {
- if (entry.isDirectory()) {
- entry = stream.getNextEntry();
- continue;
- }
- // this is a buffer, so the long->int truncation is not an issue.
- final ByteArrayOutputStream os = new ByteArrayOutputStream((int) entry.getSize());
-
- int n;
- final byte[] buf = new byte[1024];
- while ((n = stream.read(buf, 0, 1024)) > -1) {
- os.write(buf, 0, n);
- }
- helperEntries.put(entry, os.toByteArray());
- entry = stream.getNextEntry();
- }
- } catch (final IOException e) {
- log.error("Error extracting helpers", e);
- }
- injector = new ClassLoaderIntegrationInjector(helperEntries);
- }
-
- public static void registerClassLoad() {
- log.debug("Register called by class initializer.");
- registerClassLoad(Thread.currentThread().getContextClassLoader());
- }
-
- public static void registerClassLoad(final Object obj) {
- if (AgentRulesManager.INSTANCE == null) {
- return;
- }
- final ClassLoader cl;
- if (obj instanceof ClassLoader) {
- cl = (ClassLoader) obj;
- log.debug("Calling initialize with {}", cl);
- } else {
- cl = obj.getClass().getClassLoader();
- log.debug("Calling initialize with {} and classloader {}", obj, cl);
- }
-
- AgentRulesManager.INSTANCE.instrumentationRulesManager.initialize(cl);
- }
-
- /** True if this object's classload has been registered. */
- public static boolean isRegistered(final Object obj) {
- if (AgentRulesManager.INSTANCE == null) {
- return false;
- }
- final ClassLoader cl;
- if (obj instanceof ClassLoader) {
- cl = (ClassLoader) obj;
- } else {
- cl = obj.getClass().getClassLoader();
- }
- synchronized (cl) {
- return AgentRulesManager.INSTANCE.instrumentationRulesManager.initializedClassloaders
- .contains(cl);
- }
- }
-
- /**
- * This method is separated out from initialize to allow Spring Boot's LaunchedURLClassLoader to
- * call it once it is loaded.
- *
- * @param classLoader
- */
- public void initialize(final ClassLoader classLoader) {
- synchronized (classLoader) {
- if (initializedClassloaders.contains(classLoader)) {
- return;
- }
- initializedClassloaders.add(classLoader);
- }
- log.info("Initializing on classloader {}", classLoader);
-
- injector.inject(classLoader);
- }
+ final TracingAgentConfig config, final AgentRulesManager agentRulesManager) {}
void initTracer() {
synchronized (SYNC) {

MODIFY
dd-java-agent/src/main/java/com/datadoghq/agent/TracingAgent.java
dd-java-agent/src/main/java/com/datadoghq/agent/TracingAgent.java
@@ -26,10 +26,7 @@ import static net.bytebuddy.matcher.ElementMatchers.nameStartsWith;
import dd.trace.Instrumenter;
import java.lang.instrument.Instrumentation;
-import java.util.Collections;
import java.util.ServiceLoader;
-import java.util.Set;
-import java.util.WeakHashMap;
import lombok.extern.slf4j.Slf4j;
import net.bytebuddy.agent.builder.AgentBuilder;
import net.bytebuddy.description.type.TypeDescription;
@@ -97,9 +94,6 @@ public class TracingAgent {
@Slf4j
static class Listener implements AgentBuilder.Listener {
- private final Set<ClassLoader> initializedClassloaders =
- Collections.newSetFromMap(new WeakHashMap<ClassLoader, Boolean>());
-
@Override
public void onError(
final String typeName,
@@ -118,22 +112,6 @@ public class TracingAgent {
final boolean loaded,
final DynamicType dynamicType) {
log.debug("Transformed {} -- {}", typeDescription, classLoader);
-
- if (classLoader == null) {
- return;
- }
- synchronized (classLoader) {
- if (initializedClassloaders.contains(classLoader)) {
- return;
- }
- initializedClassloaders.add(classLoader);
-
- try {
- InstrumentationRulesManager.registerClassLoad(classLoader);
- } catch (final Throwable e) {
- log.error("Failed ClassLoad Registration for target " + classLoader, e);
- }
- }
}
@Override

MODIFY
dd-java-agent/tooling/src/main/java/dd/trace/HelperInjector.java
dd-java-agent/tooling/src/main/java/dd/trace/HelperInjector.java
@@ -48,7 +48,7 @@ public class HelperInjector implements Transformer {
TypeDescription typeDescription,
ClassLoader classLoader,
JavaModule module) {
- if (helperClassNames.size() > 0) {
+ if (helperClassNames.size() > 0 && classLoader != null) {
try {
new ClassInjector.UsingReflection(classLoader).inject(getHelperMap());
} catch (ClassNotFoundException cnfe) {

MODIFY
dd-java-agent/tooling/src/test/groovy/dd/test/TestUtils.groovy
dd-java-agent/tooling/src/test/groovy/dd/test/TestUtils.groovy
@@ -35,12 +35,6 @@ class TestUtils {
}
static registerOrReplaceGlobalTracer(Tracer tracer) {
- try {
- Class.forName("com.datadoghq.agent.InstrumentationRulesManager")
- .getMethod("registerClassLoad")
- .invoke(null)
- } catch (ClassNotFoundException e) {
- }
try {
GlobalTracer.register(tracer)
} catch (final Exception e) {

MODIFY
settings.gradle
settings.gradle
@@ -13,7 +13,6 @@ include ':dd-trace-examples'
include ':dd-trace-annotations'
// integrations:
-include ':dd-java-agent:integrations:helpers'
include ':dd-java-agent:integrations:apache-httpclient-4.3'
include ':dd-java-agent:integrations:aws-sdk'
include ':dd-java-agent:integrations:datastax-cassandra-3.2'

Remove helpers from agent