-
Notifications
You must be signed in to change notification settings - Fork 286
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2321 from DataDog/mcculls/osgiInstrumentation
Add BundleWiring instrumentation to improve tracer behaviour on OSGi
- Loading branch information
Showing
13 changed files
with
502 additions
and
23 deletions.
There are no files selected for viewing
34 changes: 34 additions & 0 deletions
34
dd-java-agent/agent-bootstrap/src/main/java/datadog/trace/bootstrap/AgentClassLoading.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package datadog.trace.bootstrap; | ||
|
||
/** | ||
* Helps distinguish agent class-loading requests from application requests. | ||
* | ||
* <p>Should be used in a short try..finally block around the class-loader call. | ||
*/ | ||
public enum AgentClassLoading { | ||
/** Lightweight class-loader probing to select instrumentations. */ | ||
PROBING_CLASSLOADER, | ||
/** Locating class resources for Byte-Buddy. */ | ||
LOCATING_CLASS, | ||
/** Injecting helper classes into class-loaders. */ | ||
INJECTING_HELPERS; | ||
|
||
private static final ThreadLocal<AgentClassLoading> REQUEST = new ThreadLocal<>(); | ||
|
||
/** | ||
* Gets the current agent class-loading request type; {@code null} if there's no agent request. | ||
*/ | ||
public static AgentClassLoading type() { | ||
return REQUEST.get(); | ||
} | ||
|
||
/** Records that this agent class-loading request has begun. */ | ||
public final void begin() { | ||
REQUEST.set(this); | ||
} | ||
|
||
/** Records that this agent class-loading request has ended. */ | ||
public final void end() { | ||
REQUEST.remove(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
62 changes: 62 additions & 0 deletions
62
...agent-tooling/src/main/java/datadog/trace/agent/tooling/bytebuddy/DDClassFileLocator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
package datadog.trace.agent.tooling.bytebuddy; | ||
|
||
import static datadog.trace.bootstrap.AgentClassLoading.LOCATING_CLASS; | ||
|
||
import datadog.trace.agent.tooling.Utils; | ||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.lang.ref.WeakReference; | ||
import net.bytebuddy.dynamic.ClassFileLocator; | ||
import net.bytebuddy.utility.StreamDrainer; | ||
|
||
/** | ||
* Locate resources with the loading classloader. Because of a quirk with the way classes appended | ||
* to the bootstrap classpath work, we first check our bootstrap proxy. If the loading classloader | ||
* cannot find the desired resource, check up the classloader hierarchy until a resource is found. | ||
*/ | ||
public final class DDClassFileLocator extends WeakReference<ClassLoader> | ||
implements ClassFileLocator { | ||
|
||
public DDClassFileLocator(final ClassLoader classLoader) { | ||
super(classLoader); | ||
} | ||
|
||
@Override | ||
public Resolution locate(final String className) throws IOException { | ||
String resourceName = className.replace('.', '/') + ".class"; | ||
|
||
// try bootstrap first | ||
Resolution resolution = loadClassResource(Utils.getBootstrapProxy(), resourceName); | ||
ClassLoader cl = get(); | ||
|
||
// now go up the classloader hierarchy | ||
if (null == resolution && null != cl) { | ||
LOCATING_CLASS.begin(); | ||
try { | ||
do { | ||
resolution = loadClassResource(cl, resourceName); | ||
cl = cl.getParent(); | ||
} while (null == resolution && null != cl); | ||
} finally { | ||
LOCATING_CLASS.end(); | ||
} | ||
} | ||
|
||
return resolution != null ? resolution : new Resolution.Illegal(className); | ||
} | ||
|
||
@Override | ||
public void close() { | ||
// nothing to close | ||
} | ||
|
||
private static Resolution loadClassResource( | ||
final ClassLoader classLoader, final String resourceName) throws IOException { | ||
try (InputStream in = classLoader.getResourceAsStream(resourceName)) { | ||
if (null != in) { | ||
return new Resolution.Explicit(StreamDrainer.DEFAULT.drain(in)); | ||
} | ||
return null; | ||
} | ||
} | ||
} |
22 changes: 5 additions & 17 deletions
22
...agent-tooling/src/main/java/datadog/trace/agent/tooling/bytebuddy/DDLocationStrategy.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,30 +1,18 @@ | ||
package datadog.trace.agent.tooling.bytebuddy; | ||
|
||
import datadog.trace.agent.tooling.Utils; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
import net.bytebuddy.agent.builder.AgentBuilder; | ||
import net.bytebuddy.dynamic.ClassFileLocator; | ||
import net.bytebuddy.utility.JavaModule; | ||
|
||
/** | ||
* Locate resources with the loading classloader. Because of a quirk with the way classes appended | ||
* to the bootstrap classpath work, we first check our bootstrap proxy. If the loading classloader | ||
* cannot find the desired resource, check up the classloader hierarchy until a resource is found. | ||
*/ | ||
public class DDLocationStrategy implements AgentBuilder.LocationStrategy { | ||
/** Strategy that uses {@link DDClassFileLocator} to locate class files. */ | ||
public final class DDLocationStrategy implements AgentBuilder.LocationStrategy { | ||
public ClassFileLocator classFileLocator(final ClassLoader classLoader) { | ||
return classFileLocator(classLoader, null); | ||
} | ||
|
||
@Override | ||
public ClassFileLocator classFileLocator(ClassLoader classLoader, final JavaModule javaModule) { | ||
final List<ClassFileLocator> locators = new ArrayList<>(); | ||
locators.add(ClassFileLocator.ForClassLoader.of(Utils.getBootstrapProxy())); | ||
while (classLoader != null) { | ||
locators.add(ClassFileLocator.ForClassLoader.WeaklyReferenced.of(classLoader)); | ||
classLoader = classLoader.getParent(); | ||
} | ||
return new ClassFileLocator.Compound(locators.toArray(new ClassFileLocator[0])); | ||
public ClassFileLocator classFileLocator( | ||
final ClassLoader classLoader, final JavaModule javaModule) { | ||
return new DDClassFileLocator(classLoader); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
muzzle { | ||
// old coordinates | ||
pass { | ||
group = 'org.osgi' | ||
module = 'org.osgi.core' | ||
versions = '[4.3,]' | ||
} | ||
|
||
// new coordinates | ||
pass { | ||
group = 'org.osgi' | ||
module = 'osgi.core' | ||
versions = '[4.3.1,]' | ||
} | ||
} | ||
|
||
apply from: "$rootDir/gradle/java.gradle" | ||
|
||
dependencies { | ||
compileOnly group: 'org.osgi', name: 'org.osgi.core', version: '4.3.0' | ||
} |
Oops, something went wrong.