TracingStatementInterceptor implements StatementInterceptor { static void parseServerIpAndPort(MysqlConnection connection, Span span) { try { URI url = URI.create(connection.getURL().substring(5)); String remoteServiceName = connection.getProperties().getProperty("zipkinServiceName"); if (remoteServiceName == null || "".equals(remoteServiceName)) { String databaseName = getDatabaseName(connection); if (databaseName != null && !databaseName.isEmpty()) { remoteServiceName = "mysql-" + databaseName; } else { remoteServiceName = "mysql"; } } span.remoteServiceName(remoteServiceName); String host = getHost(connection); if (host != null) { span.remoteIpAndPort(host, url.getPort() == -1 ? 3306 : url.getPort()); } } catch (Exception e) { } }  @Override T preProcess(String sql, Statement interceptedStatement); @Override T postProcess(String sql, Statement interceptedStatement,
    T originalResultSet, int warningCount, boolean noIndexUsed, boolean noGoodIndexUsed,
    Exception error); @Override boolean executeTopLevelOnly(); @Override StatementInterceptor init(MysqlConnection mysqlConnection, Properties properties,
    Log log); @Override void destroy();  }
@Test public void parseServerIpAndPort_ipFromHost_portFromUrl() throws SQLException { setupAndReturnPropertiesForHost("1.2.3.4"); TracingStatementInterceptor.parseServerIpAndPort(connection, span); verify(span).remoteServiceName("mysql"); verify(span).remoteIpAndPort("1.2.3.4", 5555); }
@Test public void parseServerIpAndPort_serviceNameFromDatabaseName() throws SQLException { setupAndReturnPropertiesForHost("1.2.3.4"); when(connection.getCatalog()).thenReturn("mydatabase"); TracingStatementInterceptor.parseServerIpAndPort(connection, span); verify(span).remoteServiceName("mysql-mydatabase"); verify(span).remoteIpAndPort("1.2.3.4", 5555); }
@Test public void parseServerIpAndPort_propertiesOverrideServiceName() throws SQLException { setupAndReturnPropertiesForHost("1.2.3.4").setProperty("zipkinServiceName", "foo"); TracingStatementInterceptor.parseServerIpAndPort(connection, span); verify(span).remoteServiceName("foo"); verify(span).remoteIpAndPort("1.2.3.4", 5555); }
@Test public void parseServerIpAndPort_emptyZipkinServiceNameIgnored() throws SQLException { setupAndReturnPropertiesForHost("1.2.3.4").setProperty("zipkinServiceName", ""); TracingStatementInterceptor.parseServerIpAndPort(connection, span); verify(span).remoteServiceName("mysql"); verify(span).remoteIpAndPort("1.2.3.4", 5555); }
@Test public void parseServerIpAndPort_doesntCrash() { when(connection.getURL()).thenThrow(new RuntimeException()); TracingStatementInterceptor.parseServerIpAndPort(connection, span); verifyNoMoreInteractions(span); }