Shadow the bootstrap jar into the main agent jar
This commit is contained in:
parent
a1f8cad2e8
commit
c275143eae
|
@ -13,6 +13,9 @@ description = 'dd-java-agent'
|
||||||
apply from: "${rootDir}/gradle/java.gradle"
|
apply from: "${rootDir}/gradle/java.gradle"
|
||||||
apply from: "${rootDir}/gradle/publish.gradle"
|
apply from: "${rootDir}/gradle/publish.gradle"
|
||||||
|
|
||||||
|
configurations {
|
||||||
|
shadowInclude
|
||||||
|
}
|
||||||
/*
|
/*
|
||||||
* Include subproject's shadowJar in the dd-java-agent jar.
|
* Include subproject's shadowJar in the dd-java-agent jar.
|
||||||
* Note jarname must end in .zip, or its classes will be on the classpath of
|
* Note jarname must end in .zip, or its classes will be on the classpath of
|
||||||
|
@ -56,7 +59,6 @@ def includeShadowJar(subproject, jarname) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
includeShadowJar(project(':dd-java-agent:agent-bootstrap'), 'agent-bootstrap.jar.zip')
|
|
||||||
includeShadowJar(project(':dd-java-agent:instrumentation'), 'agent-tooling-and-instrumentation.jar.zip')
|
includeShadowJar(project(':dd-java-agent:instrumentation'), 'agent-tooling-and-instrumentation.jar.zip')
|
||||||
includeShadowJar(project(':dd-java-agent:agent-jmxfetch'), 'agent-jmxfetch.jar.zip')
|
includeShadowJar(project(':dd-java-agent:agent-jmxfetch'), 'agent-jmxfetch.jar.zip')
|
||||||
|
|
||||||
|
@ -75,13 +77,28 @@ jar {
|
||||||
}
|
}
|
||||||
|
|
||||||
shadowJar {
|
shadowJar {
|
||||||
|
configurations = [project.configurations.shadowInclude]
|
||||||
|
|
||||||
classifier null
|
classifier null
|
||||||
|
|
||||||
mergeServiceFiles()
|
mergeServiceFiles()
|
||||||
|
|
||||||
|
exclude '**/module-info.class'
|
||||||
|
|
||||||
dependencies {
|
dependencies {
|
||||||
exclude(dependency("org.projectlombok:lombok:$versions.lombok"))
|
exclude(dependency("org.projectlombok:lombok:$versions.lombok"))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 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.bootstrap.PatchLogger'
|
||||||
|
|
||||||
|
if (!project.hasProperty("disableShadowRelocate") || !disableShadowRelocate) {
|
||||||
|
// shadow OT impl to prevent casts to implementation
|
||||||
|
relocate 'datadog.trace.common', 'datadog.trace.agent.common'
|
||||||
|
relocate 'datadog.opentracing', 'datadog.trace.agent.ot'
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// We don't want bundled dependencies to show up in the pom.
|
// We don't want bundled dependencies to show up in the pom.
|
||||||
|
@ -97,6 +114,8 @@ dependencies {
|
||||||
testCompile deps.opentracingMock
|
testCompile deps.opentracingMock
|
||||||
testCompile deps.testLogging
|
testCompile deps.testLogging
|
||||||
testCompile deps.guava
|
testCompile deps.guava
|
||||||
|
|
||||||
|
shadowInclude project(':dd-java-agent:agent-bootstrap')
|
||||||
}
|
}
|
||||||
|
|
||||||
tasks.withType(Test).configureEach {
|
tasks.withType(Test).configureEach {
|
||||||
|
|
|
@ -2,11 +2,8 @@ package datadog.trace.agent;
|
||||||
|
|
||||||
import java.io.BufferedReader;
|
import java.io.BufferedReader;
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.io.FileOutputStream;
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.InputStream;
|
|
||||||
import java.io.InputStreamReader;
|
import java.io.InputStreamReader;
|
||||||
import java.io.OutputStream;
|
|
||||||
import java.lang.instrument.Instrumentation;
|
import java.lang.instrument.Instrumentation;
|
||||||
import java.lang.reflect.Constructor;
|
import java.lang.reflect.Constructor;
|
||||||
import java.lang.reflect.InvocationTargetException;
|
import java.lang.reflect.InvocationTargetException;
|
||||||
|
@ -30,7 +27,7 @@ public class TracingAgent {
|
||||||
// fields must be managed under class lock
|
// fields must be managed under class lock
|
||||||
private static ClassLoader AGENT_CLASSLOADER = null;
|
private static ClassLoader AGENT_CLASSLOADER = null;
|
||||||
private static ClassLoader JMXFETCH_CLASSLOADER = null;
|
private static ClassLoader JMXFETCH_CLASSLOADER = null;
|
||||||
private static File bootstrapJar = null;
|
private static URL BOOTSTRAP_URL = null;
|
||||||
|
|
||||||
public static void premain(final String agentArgs, final Instrumentation inst) throws Exception {
|
public static void premain(final String agentArgs, final Instrumentation inst) throws Exception {
|
||||||
agentmain(agentArgs, inst);
|
agentmain(agentArgs, inst);
|
||||||
|
@ -39,7 +36,7 @@ public class TracingAgent {
|
||||||
public static void agentmain(final String agentArgs, final Instrumentation inst)
|
public static void agentmain(final String agentArgs, final Instrumentation inst)
|
||||||
throws Exception {
|
throws Exception {
|
||||||
configureLogger();
|
configureLogger();
|
||||||
startDatadogAgent(agentArgs, inst);
|
startDatadogAgent(inst);
|
||||||
if (isAppUsingCustomLogManager()) {
|
if (isAppUsingCustomLogManager()) {
|
||||||
System.out.println("Custom logger detected. Delaying JMXFetch initialization.");
|
System.out.println("Custom logger detected. Delaying JMXFetch initialization.");
|
||||||
/*
|
/*
|
||||||
|
@ -62,25 +59,22 @@ public class TracingAgent {
|
||||||
@Override
|
@Override
|
||||||
public void run() {
|
public void run() {
|
||||||
try {
|
try {
|
||||||
startJmxFetch();
|
startJmxFetch(inst);
|
||||||
} catch (final Exception e) {
|
} catch (final Exception e) {
|
||||||
throw new RuntimeException(e);
|
throw new RuntimeException(e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
} else {
|
} else {
|
||||||
startJmxFetch();
|
startJmxFetch(inst);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public static synchronized void startDatadogAgent(
|
public static synchronized void startDatadogAgent(final Instrumentation inst) throws Exception {
|
||||||
final String agentArgs, final Instrumentation inst) throws Exception {
|
installBootstrapJar(inst);
|
||||||
initializeJars();
|
|
||||||
if (AGENT_CLASSLOADER == null) {
|
if (AGENT_CLASSLOADER == null) {
|
||||||
// bootstrap jar must be appended before agent classloader is created.
|
|
||||||
inst.appendToBootstrapClassLoaderSearch(new JarFile(bootstrapJar));
|
|
||||||
final ClassLoader agentClassLoader =
|
final ClassLoader agentClassLoader =
|
||||||
createDatadogClassLoader(bootstrapJar, "agent-tooling-and-instrumentation.jar.zip");
|
createDatadogClassLoader("agent-tooling-and-instrumentation.jar.zip");
|
||||||
final ClassLoader contextLoader = Thread.currentThread().getContextClassLoader();
|
final ClassLoader contextLoader = Thread.currentThread().getContextClassLoader();
|
||||||
try {
|
try {
|
||||||
Thread.currentThread().setContextClassLoader(agentClassLoader);
|
Thread.currentThread().setContextClassLoader(agentClassLoader);
|
||||||
|
@ -107,11 +101,10 @@ public class TracingAgent {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public static synchronized void startJmxFetch() throws Exception {
|
public static synchronized void startJmxFetch(final Instrumentation inst) throws Exception {
|
||||||
initializeJars();
|
installBootstrapJar(inst);
|
||||||
if (JMXFETCH_CLASSLOADER == null) {
|
if (JMXFETCH_CLASSLOADER == null) {
|
||||||
final ClassLoader jmxFetchClassLoader =
|
final ClassLoader jmxFetchClassLoader = createDatadogClassLoader("agent-jmxfetch.jar.zip");
|
||||||
createDatadogClassLoader(bootstrapJar, "agent-jmxfetch.jar.zip");
|
|
||||||
final ClassLoader contextLoader = Thread.currentThread().getContextClassLoader();
|
final ClassLoader contextLoader = Thread.currentThread().getContextClassLoader();
|
||||||
try {
|
try {
|
||||||
Thread.currentThread().setContextClassLoader(jmxFetchClassLoader);
|
Thread.currentThread().setContextClassLoader(jmxFetchClassLoader);
|
||||||
|
@ -138,18 +131,13 @@ public class TracingAgent {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
private static synchronized void installBootstrapJar(final Instrumentation inst)
|
||||||
* Extract embeded jars out of the dd-java-agent to a temporary location.
|
throws Exception {
|
||||||
*
|
if (BOOTSTRAP_URL == null) {
|
||||||
* <p>Has no effect if jars are already extracted.
|
BOOTSTRAP_URL = TracingAgent.class.getProtectionDomain().getCodeSource().getLocation();
|
||||||
*/
|
|
||||||
private static synchronized void initializeJars() throws Exception {
|
// bootstrap jar must be appended before agent classloader is created.
|
||||||
if (bootstrapJar == null) {
|
inst.appendToBootstrapClassLoaderSearch(new JarFile(new File(BOOTSTRAP_URL.toURI())));
|
||||||
bootstrapJar =
|
|
||||||
extractToTmpFile(
|
|
||||||
TracingAgent.class.getClassLoader(),
|
|
||||||
"agent-bootstrap.jar.zip",
|
|
||||||
"agent-bootstrap.jar");
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -157,13 +145,12 @@ public class TracingAgent {
|
||||||
* Create the datadog classloader. This must be called after the bootstrap jar has been appened to
|
* Create the datadog classloader. This must be called after the bootstrap jar has been appened to
|
||||||
* the bootstrap classpath.
|
* the bootstrap classpath.
|
||||||
*
|
*
|
||||||
* @param bootstrapJar datadog bootstrap jar which has been appended to the bootstrap loader
|
|
||||||
* @param innerJarFilename Filename of internal jar to use for the classpath of the datadog
|
* @param innerJarFilename Filename of internal jar to use for the classpath of the datadog
|
||||||
* classloader
|
* classloader
|
||||||
* @return Datadog Classloader
|
* @return Datadog Classloader
|
||||||
*/
|
*/
|
||||||
private static ClassLoader createDatadogClassLoader(
|
private static ClassLoader createDatadogClassLoader(final String innerJarFilename)
|
||||||
final File bootstrapJar, final String innerJarFilename) throws Exception {
|
throws Exception {
|
||||||
final ClassLoader agentParent;
|
final ClassLoader agentParent;
|
||||||
final String javaVersion = System.getProperty("java.version");
|
final String javaVersion = System.getProperty("java.version");
|
||||||
if (javaVersion.startsWith("1.7") || javaVersion.startsWith("1.8")) {
|
if (javaVersion.startsWith("1.7") || javaVersion.startsWith("1.8")) {
|
||||||
|
@ -180,53 +167,7 @@ public class TracingAgent {
|
||||||
URL.class, String.class, ClassLoader.class, ClassLoader.class);
|
URL.class, String.class, ClassLoader.class, ClassLoader.class);
|
||||||
return (ClassLoader)
|
return (ClassLoader)
|
||||||
constructor.newInstance(
|
constructor.newInstance(
|
||||||
bootstrapJar.toURI().toURL(),
|
BOOTSTRAP_URL, innerJarFilename, TracingAgent.class.getClassLoader(), agentParent);
|
||||||
innerJarFilename,
|
|
||||||
TracingAgent.class.getClassLoader(),
|
|
||||||
agentParent);
|
|
||||||
}
|
|
||||||
|
|
||||||
/** Extract sourcePath out of loader to a temporary file named destName. */
|
|
||||||
private static File extractToTmpFile(
|
|
||||||
final ClassLoader loader, final String sourcePath, final String destName) throws Exception {
|
|
||||||
final String destPrefix;
|
|
||||||
final String destSuffix;
|
|
||||||
{
|
|
||||||
final int i = destName.lastIndexOf('.');
|
|
||||||
if (i > 0) {
|
|
||||||
destPrefix = destName.substring(0, i);
|
|
||||||
destSuffix = destName.substring(i);
|
|
||||||
} else {
|
|
||||||
destPrefix = destName;
|
|
||||||
destSuffix = "";
|
|
||||||
}
|
|
||||||
}
|
|
||||||
InputStream inputStream = null;
|
|
||||||
OutputStream outputStream = null;
|
|
||||||
try {
|
|
||||||
inputStream = loader.getResourceAsStream(sourcePath);
|
|
||||||
if (inputStream == null) {
|
|
||||||
throw new RuntimeException(sourcePath + ": Not found by loader: " + loader);
|
|
||||||
}
|
|
||||||
|
|
||||||
int readBytes;
|
|
||||||
final byte[] buffer = new byte[4096];
|
|
||||||
final File tmpFile = File.createTempFile(destPrefix, destSuffix);
|
|
||||||
tmpFile.deleteOnExit();
|
|
||||||
outputStream = new FileOutputStream(tmpFile);
|
|
||||||
while ((readBytes = inputStream.read(buffer)) > 0) {
|
|
||||||
outputStream.write(buffer, 0, readBytes);
|
|
||||||
}
|
|
||||||
|
|
||||||
return tmpFile;
|
|
||||||
} finally {
|
|
||||||
if (null != inputStream) {
|
|
||||||
inputStream.close();
|
|
||||||
}
|
|
||||||
if (null != outputStream) {
|
|
||||||
outputStream.close();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private static ClassLoader getPlatformClassLoader()
|
private static ClassLoader getPlatformClassLoader()
|
||||||
|
|
Loading…
Reference in New Issue