Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .circleci/save_artifacts.sh
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ function save_libs () {

save_reports dd-java-agent
save_reports dd-java-agent/tooling
save_reports dd-java-agent/testing
# Save reports for all instrumentation projects
for integration_path in dd-java-agent/instrumentation/*; do
save_reports $integration_path
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,20 +61,47 @@ class ShadowPackageRenamingTest extends Specification {

final ClassPath bootstrapClasspath = ClassPath.from(IntegrationTestUtils.getBootstrapResourceLocator())
final Set<String> bootstrapClasses = new HashSet<>()
final String[] bootstrapPrefixes = IntegrationTestUtils.getBootstrapPackagePrefixes()
final String[] agentPrefixes = IntegrationTestUtils.getAgentPackagePrefixes()
final List<String> badBootstrapPrefixes = []
final List<String> badAgentPrefixes = []
for (ClassPath.ClassInfo info : bootstrapClasspath.getAllClasses()) {
bootstrapClasses.add(info.getName())
// make sure all bootstrap classes can be loaded from system
ClassLoader.getSystemClassLoader().loadClass(info.getName())
boolean goodPrefix = false
for (int i = 0; i < bootstrapPrefixes.length; ++i) {
if (info.getName().startsWith(bootstrapPrefixes[i])) {
goodPrefix = true
break
}
}
if (!goodPrefix) {
badBootstrapPrefixes.add(info.getName())
}
}

final List<ClassPath.ClassInfo> duplicateClassFile = new ArrayList<>()
for (ClassPath.ClassInfo classInfo : agentClasspath.getAllClasses()) {
if (bootstrapClasses.contains(classInfo.getName())) {
duplicateClassFile.add(classInfo)
}
boolean goodPrefix = false
for (int i = 0; i < agentPrefixes.length; ++i) {
if (classInfo.getName().startsWith(agentPrefixes[i])) {
goodPrefix = true
break
}
}
if (!goodPrefix) {
badAgentPrefixes.add(classInfo.getName())
}
}

expect:
duplicateClassFile.size() == 0
duplicateClassFile == []
badBootstrapPrefixes == []
// ListenableFuture is skipped from shadow due to cassandra instrumentation.
badAgentPrefixes == ['com.google.common.util.concurrent.ListenableFuture']
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import java.sql.PreparedStatement
import java.sql.ResultSet
import java.sql.Statement

@Timeout(5)
@Timeout(10)
class JDBCInstrumentationTest extends Specification {

final ListWriter writer = new ListWriter()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -140,4 +140,30 @@ public static void registerOrReplaceGlobalTracer(final Tracer tracer) {
public static String getResourceName(final String className) {
return className.replace('.', '/') + ".class";
}

public static String[] getBootstrapPackagePrefixes() {
try {
Field f =
getAgentClassLoader()
.loadClass("datadog.trace.agent.tooling.Utils")
.getField("BOOTSTRAP_PACKAGE_PREFIXES");
return (String[]) f.get(null);
} catch (Exception e) {
e.printStackTrace();
}
return null;
}

public static String[] getAgentPackagePrefixes() {
try {
Field f =
getAgentClassLoader()
.loadClass("datadog.trace.agent.tooling.Utils")
.getField("AGENT_PACKAGE_PREFIXES");
return (String[]) f.get(null);
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package datadog.trace.agent.bootstrap;
package datadog.trace.bootstrap;

import java.net.URL;
import java.net.URLClassLoader;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package datadog.trace.agent.bootstrap;
package datadog.trace.bootstrap;

/**
* Class used for exception handler logging.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package datadog.trace.agent.bootstrap;
package datadog.trace.bootstrap;

import java.sql.Connection;
import java.sql.PreparedStatement;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package datadog.trace.agent.bootstrap;
package datadog.trace.bootstrap;

import java.util.ResourceBundle;
import java.util.logging.Level;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,14 @@
package datadog.trace.agent.tooling;

import datadog.trace.agent.bootstrap.DatadogClassLoader;
import datadog.trace.agent.bootstrap.PatchLogger;
import datadog.trace.bootstrap.DatadogClassLoader;
import datadog.trace.bootstrap.PatchLogger;
import io.opentracing.util.GlobalTracer;
import java.util.*;
import lombok.extern.slf4j.Slf4j;
import net.bytebuddy.matcher.ElementMatcher;

@Slf4j
public class ClassLoaderMatcher {
/* packages which will be loaded on the bootstrap classloader*/
public static final String[] BOOTSTRAP_PACKAGE_PREFIXES = {
"io.opentracing", "datadog.slf4j", "datadog.trace"
};
public static final ClassLoader BOOTSTRAP_CLASSLOADER = null;

/** A private constructor that must not be invoked. */
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package datadog.trace.agent.tooling;

import datadog.trace.agent.bootstrap.ExceptionLogger;
import datadog.trace.bootstrap.ExceptionLogger;
import net.bytebuddy.implementation.Implementation;
import net.bytebuddy.implementation.bytecode.StackManipulation;
import net.bytebuddy.jar.asm.Label;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,21 @@
import java.lang.reflect.Method;

public class Utils {
/* packages which will be loaded on the bootstrap classloader*/
public static final String[] BOOTSTRAP_PACKAGE_PREFIXES = {
"io.opentracing",

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I assume this will be remapped according to the shadow jar rules, right?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

io.opentracing isn't renamed by our shadow rules. It goes as-is on the bootstrap because we need to pick up OT api calls from the user's code. Everything else is renamed.

"datadog.slf4j",
"datadog.trace.bootstrap",
"datadog.trace.api",
"datadog.trace.context"
};
public static final String[] AGENT_PACKAGE_PREFIXES = {
"datadog.trace.agent",
"datadog.opentracing",
"datadog.trace.common",
"datadog.trace.instrumentation"
};

private static Method findLoadedClassMethod = null;

static {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package datadog.trace.agent.test

import datadog.trace.agent.bootstrap.DatadogClassLoader
import datadog.trace.bootstrap.DatadogClassLoader
import datadog.trace.agent.tooling.ClassLoaderMatcher
import spock.lang.Specification

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package datadog.trace.agent.tooling
package datadog.trace.agent.test

import datadog.trace.agent.tooling.Instrumenter
import net.bytebuddy.agent.builder.AgentBuilder
import org.junit.Rule
import org.junit.contrib.java.lang.system.EnvironmentVariables
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package datadog.trace.agent.test

import datadog.trace.agent.bootstrap.ExceptionLogger
import datadog.trace.bootstrap.ExceptionLogger
import datadog.trace.agent.tooling.ExceptionHandlers
import net.bytebuddy.agent.ByteBuddyAgent
import net.bytebuddy.dynamic.ClassFileLocator
Expand Down
2 changes: 1 addition & 1 deletion dd-java-agent/dd-java-agent.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ def includeShadowJar(subproject, jarname) {
// Prevents conflict with other SLF4J instances. Important for premain.
relocate 'org.slf4j', 'datadog.slf4j'
// rewrite dependencies calling Logger.getLogger
relocate 'java.util.logging.Logger', 'datadog.trace.agent.bootstrap.PatchLogger'
relocate 'java.util.logging.Logger', 'datadog.trace.bootstrap.PatchLogger'

if (!project.hasProperty("disableShadowRelocate") || !disableShadowRelocate) {
relocate 'datadog.trace.common', 'datadog.trace.agent.common'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
import static net.bytebuddy.matcher.ElementMatchers.named;

import com.google.auto.service.AutoService;
import datadog.trace.agent.tooling.ClassLoaderMatcher;
import datadog.trace.agent.tooling.Instrumenter;
import datadog.trace.agent.tooling.Utils;
import net.bytebuddy.agent.builder.AgentBuilder;
import net.bytebuddy.description.type.TypeDescription;
import net.bytebuddy.dynamic.DynamicType;
Expand Down Expand Up @@ -33,11 +33,11 @@ public DynamicType.Builder<?> transform(
// Instead it sets a system prop to tell jboss to delegate
// classloads for datadog bootstrap classes
StringBuilder ddPrefixes = new StringBuilder("");
for (int i = 0; i < ClassLoaderMatcher.BOOTSTRAP_PACKAGE_PREFIXES.length; ++i) {
for (int i = 0; i < Utils.BOOTSTRAP_PACKAGE_PREFIXES.length; ++i) {
if (i > 0) {
ddPrefixes.append(",");
}
ddPrefixes.append(ClassLoaderMatcher.BOOTSTRAP_PACKAGE_PREFIXES[i]);
ddPrefixes.append(Utils.BOOTSTRAP_PACKAGE_PREFIXES[i]);
}
final String existing = System.getProperty("jboss.modules.system.pkgs");
if (null == existing) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@ class JBossClassloadingTest extends AgentTestRunner {
org.jboss.modules.Module.getName()

expect:
System.getProperty("jboss.modules.system.pkgs") == "io.opentracing,datadog.slf4j,datadog.trace"
System.getProperty("jboss.modules.system.pkgs") == "io.opentracing,datadog.slf4j,datadog.trace.bootstrap,datadog.trace.api,datadog.trace.context"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@
import static net.bytebuddy.matcher.ElementMatchers.takesArgument;

import com.google.auto.service.AutoService;
import datadog.trace.agent.bootstrap.JDBCMaps;
import datadog.trace.agent.tooling.DDAdvice;
import datadog.trace.agent.tooling.Instrumenter;
import datadog.trace.bootstrap.JDBCMaps;
import java.sql.Connection;
import java.sql.PreparedStatement;
import net.bytebuddy.agent.builder.AgentBuilder;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@
import static net.bytebuddy.matcher.ElementMatchers.takesArguments;

import com.google.auto.service.AutoService;
import datadog.trace.agent.bootstrap.JDBCMaps;
import datadog.trace.agent.tooling.DDAdvice;
import datadog.trace.agent.tooling.Instrumenter;
import datadog.trace.api.DDTags;
import datadog.trace.bootstrap.JDBCMaps;
import io.opentracing.Scope;
import io.opentracing.Span;
import io.opentracing.noop.NoopScopeManager;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@
import static net.bytebuddy.matcher.ElementMatchers.takesArgument;

import com.google.auto.service.AutoService;
import datadog.trace.agent.bootstrap.JDBCMaps;
import datadog.trace.agent.tooling.DDAdvice;
import datadog.trace.agent.tooling.Instrumenter;
import datadog.trace.api.DDTags;
import datadog.trace.bootstrap.JDBCMaps;
import io.opentracing.Scope;
import io.opentracing.Span;
import io.opentracing.noop.NoopScopeManager;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
import static net.bytebuddy.matcher.ElementMatchers.named;

import com.google.auto.service.AutoService;
import datadog.trace.agent.tooling.ClassLoaderMatcher;
import datadog.trace.agent.tooling.Instrumenter;
import datadog.trace.agent.tooling.Utils;
import net.bytebuddy.agent.builder.AgentBuilder;
import net.bytebuddy.description.type.TypeDescription;
import net.bytebuddy.dynamic.DynamicType;
Expand Down Expand Up @@ -33,13 +33,13 @@ public DynamicType.Builder<?> transform(
// Instead it sets a system prop to tell osgi to delegate
// classloads for datadog bootstrap classes
StringBuilder ddPrefixes = new StringBuilder("");
for (int i = 0; i < ClassLoaderMatcher.BOOTSTRAP_PACKAGE_PREFIXES.length; ++i) {
for (int i = 0; i < Utils.BOOTSTRAP_PACKAGE_PREFIXES.length; ++i) {
if (i > 0) {
// must append twice. Once for exact package and wildcard for child packages
ddPrefixes.append(",");
}
ddPrefixes.append(ClassLoaderMatcher.BOOTSTRAP_PACKAGE_PREFIXES[i]).append(".*,");
ddPrefixes.append(ClassLoaderMatcher.BOOTSTRAP_PACKAGE_PREFIXES[i]);
ddPrefixes.append(Utils.BOOTSTRAP_PACKAGE_PREFIXES[i]).append(".*,");
ddPrefixes.append(Utils.BOOTSTRAP_PACKAGE_PREFIXES[i]);
}
final String existing = System.getProperty("org.osgi.framework.bootdelegation");
if (null == existing) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@ class OSGIClassloadingTest extends AgentTestRunner {
org.osgi.framework.Bundle.getName()

expect:
System.getProperty("org.osgi.framework.bootdelegation") == "io.opentracing.*,io.opentracing,datadog.slf4j.*,datadog.slf4j,datadog.trace.*,datadog.trace"
System.getProperty("org.osgi.framework.bootdelegation") == "io.opentracing.*,io.opentracing,datadog.slf4j.*,datadog.slf4j,datadog.trace.bootstrap.*,datadog.trace.bootstrap,datadog.trace.api.*,datadog.trace.api,datadog.trace.context.*,datadog.trace.context"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -102,8 +102,7 @@ private static ClassLoader createDatadogClassLoader(File bootstrapJar, File tool
agentParent = getPlatformClassLoader();
}
Class<?> loaderClass =
ClassLoader.getSystemClassLoader()
.loadClass("datadog.trace.agent.bootstrap.DatadogClassLoader");
ClassLoader.getSystemClassLoader().loadClass("datadog.trace.bootstrap.DatadogClassLoader");
Constructor constructor =
loaderClass.getDeclaredConstructor(URL.class, URL.class, ClassLoader.class);
return (ClassLoader)
Expand Down
Loading