TracingStatementInterceptor implements StatementInterceptorV2 { static void parseServerIpAndPort(Connection connection, Span span) { try { URI url = URI.create(connection.getMetaData().getURL().substring(5)); String remoteServiceName = connection.getProperties().getProperty("zipkinServiceName"); if (remoteServiceName == null || "".equals(remoteServiceName)) { String databaseName = connection.getCatalog(); if (databaseName != null && !databaseName.isEmpty()) { remoteServiceName = "mysql-" + databaseName; } else { remoteServiceName = "mysql"; } } span.remoteServiceName(remoteServiceName); String host = connection.getHost(); if (host != null) { span.remoteIpAndPort(host, url.getPort() == -1 ? 3306 : url.getPort()); } } catch (Exception e) { } }  @Override ResultSetInternalMethods preProcess(String sql, Statement interceptedStatement,
    Connection connection); @Override ResultSetInternalMethods postProcess(String sql, Statement interceptedStatement,
    ResultSetInternalMethods originalResultSet, Connection connection, int warningCount,
    boolean noIndexUsed, boolean noGoodIndexUsed, SQLException statementException); @Override boolean executeTopLevelOnly(); @Override void init(Connection conn, Properties props); @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() throws SQLException { when(connection.getMetaData()).thenThrow(new SQLException()); TracingStatementInterceptor.parseServerIpAndPort(connection, span); verifyNoMoreInteractions(span); }