Fixed java 9 JDBC integration test issue.

Refactored JDBCMaps's getDBInfo utlity function because JDBCMaps is in the bootstrap classloader, and the use of java.sql.* packages in getDBInfo is failing because java.sql.* packages are part of the platform classloader in java 9.
This commit is contained in:
Gary Huang 2018-05-15 15:14:33 -04:00
parent 4b71a21487
commit 38dfe96eb0
3 changed files with 68 additions and 38 deletions

View File

@ -2,7 +2,6 @@ package datadog.trace.bootstrap;
import java.sql.Connection; import java.sql.Connection;
import java.sql.PreparedStatement; import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.Collections; import java.util.Collections;
import java.util.Map; import java.util.Map;
import java.util.WeakHashMap; import java.util.WeakHashMap;
@ -40,39 +39,4 @@ public class JDBCMaps {
private static String propToEnvName(final String name) { private static String propToEnvName(final String name) {
return name.toUpperCase().replace(".", "_"); return name.toUpperCase().replace(".", "_");
} }
/**
* Utility function to get the DBInfo from a JDBC Connection, if the connection was never seen
* before, the connectionInfo map will return null and will attempt to extract DBInfo from the
* connection. If the DBInfo can't be extracted, then the connection will be stored with the
* UNKNOWN DBInfo as the value in the connectionInfo map to avoid retry overhead.
*
* @param connection The JDBC connection
* @return A DBInfo that contains JDBC connection info
*/
public static DBInfo getDBInfo(Connection connection) {
DBInfo dbInfo = connectionInfo.get(connection);
if (dbInfo == null) {
try {
final String url = connection.getMetaData().getURL();
if (url != null) {
// Remove end of url to prevent passwords from leaking:
final String sanitizedURL = url.replaceAll("[?;].*", "");
final String type = url.split(":", -1)[1];
String user = connection.getMetaData().getUserName();
if (user != null && user.trim().equals("")) {
user = null;
}
dbInfo = new JDBCMaps.DBInfo(sanitizedURL, type, user);
} else {
dbInfo = DBInfo.DEFAULT;
}
} catch (SQLException se) {
dbInfo = DBInfo.DEFAULT;
}
connectionInfo.put(connection, dbInfo);
}
return dbInfo;
}
} }

View File

@ -23,6 +23,7 @@ import io.opentracing.tag.Tags;
import io.opentracing.util.GlobalTracer; import io.opentracing.util.GlobalTracer;
import java.sql.Connection; import java.sql.Connection;
import java.sql.PreparedStatement; import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.Collections; import java.util.Collections;
import net.bytebuddy.agent.builder.AgentBuilder; import net.bytebuddy.agent.builder.AgentBuilder;
import net.bytebuddy.asm.Advice; import net.bytebuddy.asm.Advice;
@ -63,7 +64,39 @@ public final class PreparedStatementInstrumentation extends Instrumenter.Configu
return NoopScope.INSTANCE; return NoopScope.INSTANCE;
} }
JDBCMaps.DBInfo dbInfo = JDBCMaps.getDBInfo(connection); JDBCMaps.DBInfo dbInfo = JDBCMaps.connectionInfo.get(connection);
/**
* Logic to get the DBInfo from a JDBC Connection, if the connection was never seen before,
* the connectionInfo map will return null and will attempt to extract DBInfo from the
* connection. If the DBInfo can't be extracted, then the connection will be stored with the
* DEFAULT DBInfo as the value in the connectionInfo map to avoid retry overhead.
*
* <p>This should be a util method to be shared between PreparedStatementInstrumentation and
* StatementInstrumentation class, but java.sql.* are on the platform classloaders in Java 9,
* which prevents us from referencing them in the bootstrap utils.
*/
{
if (dbInfo == null) {
try {
final String url = connection.getMetaData().getURL();
if (url != null) {
// Remove end of url to prevent passwords from leaking:
final String sanitizedURL = url.replaceAll("[?;].*", "");
final String type = url.split(":", -1)[1];
String user = connection.getMetaData().getUserName();
if (user != null && user.trim().equals("")) {
user = null;
}
dbInfo = new JDBCMaps.DBInfo(sanitizedURL, type, user);
} else {
dbInfo = JDBCMaps.DBInfo.DEFAULT;
}
} catch (SQLException se) {
dbInfo = JDBCMaps.DBInfo.DEFAULT;
}
JDBCMaps.connectionInfo.put(connection, dbInfo);
}
}
final Scope scope = final Scope scope =
GlobalTracer.get().buildSpan(dbInfo.getType() + ".query").startActive(true); GlobalTracer.get().buildSpan(dbInfo.getType() + ".query").startActive(true);

View File

@ -22,6 +22,7 @@ import io.opentracing.noop.NoopScopeManager.NoopScope;
import io.opentracing.tag.Tags; import io.opentracing.tag.Tags;
import io.opentracing.util.GlobalTracer; import io.opentracing.util.GlobalTracer;
import java.sql.Connection; import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement; import java.sql.Statement;
import java.util.Collections; import java.util.Collections;
import net.bytebuddy.agent.builder.AgentBuilder; import net.bytebuddy.agent.builder.AgentBuilder;
@ -64,7 +65,39 @@ public final class StatementInstrumentation extends Instrumenter.Configurable {
return NoopScope.INSTANCE; return NoopScope.INSTANCE;
} }
JDBCMaps.DBInfo dbInfo = JDBCMaps.getDBInfo(connection); JDBCMaps.DBInfo dbInfo = JDBCMaps.connectionInfo.get(connection);
/**
* Logic to get the DBInfo from a JDBC Connection, if the connection was never seen before,
* the connectionInfo map will return null and will attempt to extract DBInfo from the
* connection. If the DBInfo can't be extracted, then the connection will be stored with the
* DEFAULT DBInfo as the value in the connectionInfo map to avoid retry overhead.
*
* <p>This should be a util method to be shared between PreparedStatementInstrumentation and
* StatementInstrumentation class, but java.sql.* are on the platform classloaders in Java 9,
* which prevents us from referencing them in the bootstrap utils.
*/
{
if (dbInfo == null) {
try {
final String url = connection.getMetaData().getURL();
if (url != null) {
// Remove end of url to prevent passwords from leaking:
final String sanitizedURL = url.replaceAll("[?;].*", "");
final String type = url.split(":", -1)[1];
String user = connection.getMetaData().getUserName();
if (user != null && user.trim().equals("")) {
user = null;
}
dbInfo = new JDBCMaps.DBInfo(sanitizedURL, type, user);
} else {
dbInfo = JDBCMaps.DBInfo.DEFAULT;
}
} catch (SQLException se) {
dbInfo = JDBCMaps.DBInfo.DEFAULT;
}
JDBCMaps.connectionInfo.put(connection, dbInfo);
}
}
final Scope scope = final Scope scope =
GlobalTracer.get().buildSpan(dbInfo.getType() + ".query").startActive(true); GlobalTracer.get().buildSpan(dbInfo.getType() + ".query").startActive(true);