Add test for ClassNotFound creating a span bug
MODIFY
dd-java-agent/agent-bootstrap/src/main/java/datadog/trace/bootstrap/DatadogClassLoader.java
dd-java-agent/agent-bootstrap/src/main/java/datadog/trace/bootstrap/DatadogClassLoader.java
@@ -33,9 +33,13 @@ public class DatadogClassLoader extends URLClassLoader {
public DatadogClassLoader(
final URL bootstrapJarLocation, final String internalJarFileName, final ClassLoader parent) {
super(new URL[] {}, parent);
- bootstrapProxy = new BootstrapClassLoaderProxy(new URL[] {bootstrapJarLocation});
- if (internalJarFileName != null) { // some tests pass null
+ // some tests pass null
+ bootstrapProxy =
+ bootstrapJarLocation == null
+ ? new BootstrapClassLoaderProxy(new URL[0])
+ : new BootstrapClassLoaderProxy(new URL[] {bootstrapJarLocation});
+
try {
// The fields of the URL are mostly dummy. InternalJarURLHandler is the only important
// field. If extending this class from Classloader instead of URLClassloader required less
@@ -55,7 +59,6 @@ public class DatadogClassLoader extends URLClassLoader {
log.error("URL malformed. Unsupported JDK?", e);
}
}
- }
@Override
public URL getResource(final String resourceName) {

MODIFY
dd-java-agent/agent-bootstrap/src/main/java/datadog/trace/bootstrap/InternalJarURLHandler.java
dd-java-agent/agent-bootstrap/src/main/java/datadog/trace/bootstrap/InternalJarURLHandler.java
@@ -27,7 +27,9 @@ public class InternalJarURLHandler extends URLStreamHandler {
filenameToBytes.put("/", new byte[] {});
final InputStream jarStream =
- classloaderForJarResource.getResourceAsStream(internalJarFileName);
+ internalJarFileName == null
+ ? null
+ : classloaderForJarResource.getResourceAsStream(internalJarFileName);
if (jarStream != null) {
try (final JarInputStream inputStream = new JarInputStream(jarStream)) {

MODIFY
dd-java-agent/instrumentation/http-url-connection/src/test/groovy/UrlConnectionTest.groovy
dd-java-agent/instrumentation/http-url-connection/src/test/groovy/UrlConnectionTest.groovy
import datadog.trace.agent.test.AgentTestRunner
import datadog.trace.api.Config
import datadog.trace.api.DDSpanTypes
+import datadog.trace.bootstrap.DatadogClassLoader
import datadog.trace.instrumentation.http_url_connection.UrlInstrumentation
import io.opentracing.tag.Tags
import io.opentracing.util.GlobalTracer
@@ -117,4 +118,33 @@ class UrlConnectionTest extends AgentTestRunner {
where:
renameService << [false, true]
}
+
+ def "DatadogClassloader ClassNotFoundException doesn't create span"() {
+ given:
+ ClassLoader datadogLoader = new DatadogClassLoader(null, null, null)
+ ClassLoader childLoader = new URLClassLoader(new URL[0], datadogLoader)
+
+ when:
+ runUnderTrace("someTrace") {
+ childLoader.loadClass("datadog.doesnotexist")
+ }
+
+ then:
+ thrown ClassNotFoundException
+
+ expect:
+ assertTraces(1) {
+ trace(0, 1) {
+ span(0) {
+ operationName "someTrace"
+ parent()
+ errored true
+ tags {
+ errorTags ClassNotFoundException, String
+ defaultTags()
+ }
+ }
+ }
+ }
+ }
}

Add test for ClassNotFound creating a span bug