Use `safeHasSuperType` instead of `isSubType`
`isSubType` may fail on certain class lookup problems, even on classes
unrelated to given instrumentation, preventing instrumentation from
being applied.
MODIFY
dd-java-agent/instrumentation/classloaders/src/main/java/datadog/trace/instrumentation/classloaders/ClassLoaderInstrumentation.java
dd-java-agent/instrumentation/classloaders/src/main/java/datadog/trace/instrumentation/classloaders/ClassLoaderInstrumentation.java
package datadog.trace.instrumentation.classloaders;
import static net.bytebuddy.matcher.ElementMatchers.isConstructor;
-import static net.bytebuddy.matcher.ElementMatchers.isSubTypeOf;
import com.google.auto.service.AutoService;
import datadog.opentracing.DDTracer;
@@ -28,12 +27,12 @@ public final class ClassLoaderInstrumentation extends Instrumenter.Default {
@Override
public ElementMatcher typeMatcher() {
- return isSubTypeOf(ClassLoader.class);
+ return safeHasSuperType(named("java.lang.ClassLoader"));
}
@Override
public Map<ElementMatcher, String> transformers() {
- Map<ElementMatcher, String> transformers = new HashMap<>();
+ final Map<ElementMatcher, String> transformers = new HashMap<>();
transformers.put(isConstructor(), ClassloaderAdvice.class.getName());
return transformers;
}

MODIFY
dd-java-agent/instrumentation/http-url-connection/src/main/java/datadog/trace/instrumentation/http_url_connection/HttpUrlConnectionInstrumentation.java
dd-java-agent/instrumentation/http-url-connection/src/main/java/datadog/trace/instrumentation/http_url_connection/HttpUrlConnectionInstrumentation.java
package datadog.trace.instrumentation.http_url_connection;
+import static datadog.trace.agent.tooling.ByteBuddyElementMatchers.safeHasSuperType;
import static io.opentracing.log.Fields.ERROR_OBJECT;
import static net.bytebuddy.matcher.ElementMatchers.isMethod;
import static net.bytebuddy.matcher.ElementMatchers.isPublic;
-import static net.bytebuddy.matcher.ElementMatchers.isSubTypeOf;
import static net.bytebuddy.matcher.ElementMatchers.nameStartsWith;
import static net.bytebuddy.matcher.ElementMatchers.named;
import static net.bytebuddy.matcher.ElementMatchers.not;
@@ -43,7 +43,7 @@ public class HttpUrlConnectionInstrumentation extends Instrumenter.Default {
@Override
public ElementMatcher typeMatcher() {
- return isSubTypeOf(HttpURLConnection.class)
+ return safeHasSuperType(named("java.net.HttpURLConnection"))
// This class is a simple delegator. Skip because it does not update its `connected` field.
.and(not(named("sun.net.www.protocol.https.HttpsURLConnectionImpl")));
}
@@ -58,7 +58,7 @@ public class HttpUrlConnectionInstrumentation extends Instrumenter.Default {
@Override
public Map<ElementMatcher, String> transformers() {
- Map<ElementMatcher, String> transformers = new HashMap<>();
+ final Map<ElementMatcher, String> transformers = new HashMap<>();
transformers.put(
isMethod()
.and(isPublic())
@@ -177,7 +177,7 @@ public class HttpUrlConnectionInstrumentation extends Instrumenter.Default {
private static final Map<HttpURLConnection, HttpURLState> STATE_MAP =
Collections.synchronizedMap(new WeakHashMap<HttpURLConnection, HttpURLState>());
- public static HttpURLState get(HttpURLConnection connection) {
+ public static HttpURLState get(final HttpURLConnection connection) {
HttpURLState state = STATE_MAP.get(connection);
if (state == null) {
// not thread-safe, but neither is HttpURLConnection
@@ -195,8 +195,8 @@ public class HttpUrlConnectionInstrumentation extends Instrumenter.Default {
return hasDoneIO;
}
- public void setHasDoneIO(boolean value) {
- this.hasDoneIO = value;
+ public void setHasDoneIO(final boolean value) {
+ hasDoneIO = value;
}
}
}

MODIFY
dd-java-agent/instrumentation/jdbc/src/main/java/datadog/trace/instrumentation/jdbc/ConnectionInstrumentation.java
dd-java-agent/instrumentation/jdbc/src/main/java/datadog/trace/instrumentation/jdbc/ConnectionInstrumentation.java
package datadog.trace.instrumentation.jdbc;
+import static datadog.trace.agent.tooling.ByteBuddyElementMatchers.safeHasSuperType;
import static net.bytebuddy.matcher.ElementMatchers.isInterface;
-import static net.bytebuddy.matcher.ElementMatchers.isSubTypeOf;
import static net.bytebuddy.matcher.ElementMatchers.nameStartsWith;
+import static net.bytebuddy.matcher.ElementMatchers.named;
import static net.bytebuddy.matcher.ElementMatchers.not;
import static net.bytebuddy.matcher.ElementMatchers.returns;
import static net.bytebuddy.matcher.ElementMatchers.takesArgument;
@@ -10,7 +11,6 @@ import static net.bytebuddy.matcher.ElementMatchers.takesArgument;
import com.google.auto.service.AutoService;
import datadog.trace.agent.tooling.Instrumenter;
import datadog.trace.bootstrap.JDBCMaps;
-import java.sql.Connection;
import java.sql.PreparedStatement;
import java.util.HashMap;
import java.util.Map;
@@ -26,7 +26,7 @@ public final class ConnectionInstrumentation extends Instrumenter.Default {
@Override
public ElementMatcher typeMatcher() {
- return not(isInterface()).and(isSubTypeOf(Connection.class));
+ return not(isInterface()).and(safeHasSuperType(named("java.sql.Connection")));
}
@Override

MODIFY
dd-java-agent/instrumentation/jdbc/src/main/java/datadog/trace/instrumentation/jdbc/PreparedStatementInstrumentation.java
dd-java-agent/instrumentation/jdbc/src/main/java/datadog/trace/instrumentation/jdbc/PreparedStatementInstrumentation.java
package datadog.trace.instrumentation.jdbc;
+import static datadog.trace.agent.tooling.ByteBuddyElementMatchers.safeHasSuperType;
import static io.opentracing.log.Fields.ERROR_OBJECT;
import static net.bytebuddy.matcher.ElementMatchers.isInterface;
import static net.bytebuddy.matcher.ElementMatchers.isPublic;
-import static net.bytebuddy.matcher.ElementMatchers.isSubTypeOf;
import static net.bytebuddy.matcher.ElementMatchers.nameStartsWith;
+import static net.bytebuddy.matcher.ElementMatchers.named;
import static net.bytebuddy.matcher.ElementMatchers.not;
import static net.bytebuddy.matcher.ElementMatchers.takesArguments;
@@ -36,7 +37,7 @@ public final class PreparedStatementInstrumentation extends Instrumenter.Default
@Override
public ElementMatcher typeMatcher() {
- return not(isInterface()).and(isSubTypeOf(PreparedStatement.class));
+ return not(isInterface()).and(safeHasSuperType(named("java.sql.PreparedStatement")));
}
@Override

MODIFY
dd-java-agent/instrumentation/jdbc/src/main/java/datadog/trace/instrumentation/jdbc/StatementInstrumentation.java
dd-java-agent/instrumentation/jdbc/src/main/java/datadog/trace/instrumentation/jdbc/StatementInstrumentation.java
package datadog.trace.instrumentation.jdbc;
+import static datadog.trace.agent.tooling.ByteBuddyElementMatchers.safeHasSuperType;
import static io.opentracing.log.Fields.ERROR_OBJECT;
import static net.bytebuddy.matcher.ElementMatchers.isInterface;
import static net.bytebuddy.matcher.ElementMatchers.isPublic;
-import static net.bytebuddy.matcher.ElementMatchers.isSubTypeOf;
import static net.bytebuddy.matcher.ElementMatchers.nameStartsWith;
+import static net.bytebuddy.matcher.ElementMatchers.named;
import static net.bytebuddy.matcher.ElementMatchers.not;
import static net.bytebuddy.matcher.ElementMatchers.takesArgument;
@@ -37,7 +38,7 @@ public final class StatementInstrumentation extends Instrumenter.Default {
@Override
public ElementMatcher typeMatcher() {
- return not(isInterface()).and(isSubTypeOf(Statement.class));
+ return not(isInterface()).and(safeHasSuperType(named("java.sql.Statement")));
}
@Override

Use `safeHasSuperType` instead of `isSubType`

`isSubType` may fail on certain class lookup problems, even on classes
unrelated to given instrumentation, preventing instrumentation from
being applied.