aws-xray-recorder-sdk-sql-mysql/src/main/java/com/amazonaws/xray/sql/mysql/TracingInterceptor.java [45:133]:
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
public class TracingInterceptor extends JdbcInterceptor {

    protected static final String CREATE_STATEMENT = "createStatement";
    protected static final int CREATE_STATEMENT_INDEX = 0;
    protected static final String PREPARE_STATEMENT = "prepareStatement";
    protected static final int PREPARE_STATEMENT_INDEX = 1;
    protected static final String PREPARE_CALL = "prepareCall";
    protected static final int PREPARE_CALL_INDEX = 2;

    protected static final String[] STATEMENT_TYPES = {CREATE_STATEMENT, PREPARE_STATEMENT, PREPARE_CALL};
    protected static final int STATEMENT_TYPE_COUNT = STATEMENT_TYPES.length;

    protected static final String EXECUTE = "execute";
    protected static final String EXECUTE_QUERY = "executeQuery";
    protected static final String EXECUTE_UPDATE = "executeUpdate";
    protected static final String EXECUTE_BATCH = "executeBatch";

    protected static final String[] EXECUTE_TYPES = {EXECUTE, EXECUTE_QUERY, EXECUTE_UPDATE, EXECUTE_BATCH};

    /**
     * @deprecated For internal use only.
     */
    @SuppressWarnings("checkstyle:ConstantName")
    @Deprecated
    protected static final Constructor<?>[] constructors =
        new Constructor[STATEMENT_TYPE_COUNT];

    private static final Log logger =
        LogFactory.getLog(TracingInterceptor.class);

    private static final String DEFAULT_DATABASE_NAME = "database";

    /**
     * Creates a constructor for a proxy class, if one doesn't already exist
     *
     * @param index - the index of the constructor
     * @param clazz - the interface that the proxy will implement
     * @return - returns a constructor used to create new instances
     * @throws NoSuchMethodException
     */
    protected Constructor<?> getConstructor(int index, Class<?> clazz) throws NoSuchMethodException {
        if (constructors[index] == null) {
            Class<?> proxyClass = Proxy.getProxyClass(TracingInterceptor.class.getClassLoader(), new Class[] {clazz});
            constructors[index] = proxyClass.getConstructor(new Class[] {InvocationHandler.class});
        }
        return constructors[index];
    }

    public Object createStatement(Object proxy, Method method, Object[] args, Object statementObject) {
        try {
            String name = method.getName();
            String sql = null;
            Constructor<?> constructor = null;
            Map<String, Object> additionalParams = new HashMap<>();
            if (compare(CREATE_STATEMENT, name)) {
                //createStatement
                constructor = getConstructor(CREATE_STATEMENT_INDEX, Statement.class);
            } else if (compare(PREPARE_STATEMENT, name)) {
                additionalParams.put("preparation", "statement");
                sql = (String) args[0];
                constructor = getConstructor(PREPARE_STATEMENT_INDEX, PreparedStatement.class);
            } else if (compare(PREPARE_CALL, name)) {
                additionalParams.put("preparation", "call");
                sql = (String) args[0];
                constructor = getConstructor(PREPARE_CALL_INDEX, CallableStatement.class);
            } else {
                //do nothing, might be a future unsupported method
                //so we better bail out and let the system continue
                return statementObject;
            }
            Statement statement = ((Statement) statementObject);
            Connection connection = statement.getConnection();
            DatabaseMetaData metadata = connection.getMetaData();
            // parse cname for subsegment name
            additionalParams.put("url", metadata.getURL());
            additionalParams.put("user", metadata.getUserName());
            additionalParams.put("driver_version", metadata.getDriverVersion());
            additionalParams.put("database_type", metadata.getDatabaseProductName());
            additionalParams.put("database_version", metadata.getDatabaseProductVersion());
            String hostname = DEFAULT_DATABASE_NAME;
            try {
                URI normalizedUri = new URI(new URI(metadata.getURL()).getSchemeSpecificPart());
                hostname = connection.getCatalog() + "@" + normalizedUri.getHost();
            } catch (URISyntaxException e) {
                logger.warn("Unable to parse database URI. Falling back to default '" + DEFAULT_DATABASE_NAME
                            + "' for subsegment name.", e);
            }

            logger.debug("Instantiating new statement proxy.");
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -



aws-xray-recorder-sdk-sql-postgres/src/main/java/com/amazonaws/xray/sql/postgres/TracingInterceptor.java [45:133]:
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
public class TracingInterceptor extends JdbcInterceptor {

    protected static final String CREATE_STATEMENT = "createStatement";
    protected static final int CREATE_STATEMENT_INDEX = 0;
    protected static final String PREPARE_STATEMENT = "prepareStatement";
    protected static final int PREPARE_STATEMENT_INDEX = 1;
    protected static final String PREPARE_CALL = "prepareCall";
    protected static final int PREPARE_CALL_INDEX = 2;

    protected static final String[] STATEMENT_TYPES = {CREATE_STATEMENT, PREPARE_STATEMENT, PREPARE_CALL};
    protected static final int STATEMENT_TYPE_COUNT = STATEMENT_TYPES.length;

    protected static final String EXECUTE = "execute";
    protected static final String EXECUTE_QUERY = "executeQuery";
    protected static final String EXECUTE_UPDATE = "executeUpdate";
    protected static final String EXECUTE_BATCH = "executeBatch";

    protected static final String[] EXECUTE_TYPES = {EXECUTE, EXECUTE_QUERY, EXECUTE_UPDATE, EXECUTE_BATCH};

    /**
     * @deprecated For internal use only.
     */
    @SuppressWarnings("checkstyle:ConstantName")
    @Deprecated
    protected static final Constructor<?>[] constructors =
        new Constructor[STATEMENT_TYPE_COUNT];

    private static final Log logger =
        LogFactory.getLog(TracingInterceptor.class);

    private static final String DEFAULT_DATABASE_NAME = "database";

    /**
     * Creates a constructor for a proxy class, if one doesn't already exist
     *
     * @param index the index of the constructor
     * @param clazz the interface that the proxy will implement
     * @return returns a constructor used to create new instances
     * @throws NoSuchMethodException
     */
    protected Constructor<?> getConstructor(int index, Class<?> clazz) throws NoSuchMethodException {
        if (constructors[index] == null) {
            Class<?> proxyClass = Proxy.getProxyClass(TracingInterceptor.class.getClassLoader(), new Class[] {clazz});
            constructors[index] = proxyClass.getConstructor(new Class[] {InvocationHandler.class});
        }
        return constructors[index];
    }

    public Object createStatement(Object proxy, Method method, Object[] args, Object statementObject) {
        try {
            String name = method.getName();
            String sql = null;
            Constructor<?> constructor = null;
            Map<String, Object> additionalParams = new HashMap<>();
            if (compare(CREATE_STATEMENT, name)) {
                //createStatement
                constructor = getConstructor(CREATE_STATEMENT_INDEX, Statement.class);
            } else if (compare(PREPARE_STATEMENT, name)) {
                additionalParams.put("preparation", "statement");
                sql = (String) args[0];
                constructor = getConstructor(PREPARE_STATEMENT_INDEX, PreparedStatement.class);
            } else if (compare(PREPARE_CALL, name)) {
                additionalParams.put("preparation", "call");
                sql = (String) args[0];
                constructor = getConstructor(PREPARE_CALL_INDEX, CallableStatement.class);
            } else {
                //do nothing, might be a future unsupported method
                //so we better bail out and let the system continue
                return statementObject;
            }
            Statement statement = ((Statement) statementObject);
            Connection connection = statement.getConnection();
            DatabaseMetaData metadata = connection.getMetaData();
            // parse cname for subsegment name
            additionalParams.put("url", metadata.getURL());
            additionalParams.put("user", metadata.getUserName());
            additionalParams.put("driver_version", metadata.getDriverVersion());
            additionalParams.put("database_type", metadata.getDatabaseProductName());
            additionalParams.put("database_version", metadata.getDatabaseProductVersion());
            String hostname = DEFAULT_DATABASE_NAME;
            try {
                URI normalizedUri = new URI(new URI(metadata.getURL()).getSchemeSpecificPart());
                hostname = connection.getCatalog() + "@" + normalizedUri.getHost();
            } catch (URISyntaxException e) {
                logger.warn("Unable to parse database URI. Falling back to default '" + DEFAULT_DATABASE_NAME
                            + "' for subsegment name.", e);
            }

            logger.debug("Instantiating new statement proxy.");
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -



