TracingJdbcEventListener extends SimpleJdbcEventListener { void parseServerIpAndPort(Connection connection, Span span) { try { String urlAsString = connection.getMetaData().getURL().substring(5); URI url = URI.create(urlAsString.replace(" ", "")); String defaultRemoteServiceName = remoteServiceName; Matcher matcher = URL_SERVICE_NAME_FINDER.matcher(url.toString()); if (matcher.find() && matcher.groupCount() == 1) { String parsedServiceName = matcher.group(1); if (parsedServiceName != null && !parsedServiceName.isEmpty()) { defaultRemoteServiceName = parsedServiceName; } } if (defaultRemoteServiceName == null || "".equals(defaultRemoteServiceName)) { String databaseName = connection.getCatalog(); if (databaseName != null && !databaseName.isEmpty()) { span.remoteServiceName(databaseName); } } else { span.remoteServiceName(defaultRemoteServiceName); } span.remoteIpAndPort(url.getHost(), url.getPort()); } catch (Exception e) { } } TracingJdbcEventListener(@Nullable String remoteServiceName, boolean includeParameterValues,
    P6LogLoadableOptions logOptions); @Override void onBeforeAnyExecute(StatementInformation info); @Override void onAfterAnyExecute(StatementInformation info, long elapsed, SQLException e);  }
@Test public void parseServerIpAndPort_IpAndPortFromUrl() throws SQLException { when(connection.getMetaData()).thenReturn(metaData); when(metaData.getURL()).thenReturn(url); new TracingJdbcEventListener("", false, logOptions).parseServerIpAndPort(connection, span); verify(span).remoteIpAndPort("1.2.3.4", 5555); }
@Test public void parseServerIpAndPort_serviceNameFromDatabaseName() throws SQLException { when(connection.getMetaData()).thenReturn(metaData); when(metaData.getURL()).thenReturn(url); when(connection.getCatalog()).thenReturn("mydatabase"); new TracingJdbcEventListener("", false, logOptions).parseServerIpAndPort(connection, span); verify(span).remoteServiceName("mydatabase"); verify(span).remoteIpAndPort("1.2.3.4", 5555); }
@Test public void parseServerIpAndPort_serviceNameFromUrl() throws SQLException { when(connection.getMetaData()).thenReturn(metaData); when(metaData.getURL()).thenReturn(urlWithServiceName); new TracingJdbcEventListener("", false, logOptions).parseServerIpAndPort(connection, span); verify(span).remoteServiceName("mysql_service"); verify(span).remoteIpAndPort("1.2.3.4", 5555); }
@Test public void parseServerIpAndPort_emptyServiceNameFromUrl() throws SQLException { when(connection.getMetaData()).thenReturn(metaData); when(metaData.getURL()).thenReturn(urlWithEmptyServiceName); when(connection.getCatalog()).thenReturn("mydatabase"); new TracingJdbcEventListener("", false, logOptions).parseServerIpAndPort(connection, span); verify(span).remoteServiceName("mydatabase"); verify(span).remoteIpAndPort("1.2.3.4", 5555); }
@Test public void parseServerIpAndPort_overrideServiceName() throws SQLException { when(connection.getMetaData()).thenReturn(metaData); when(metaData.getURL()).thenReturn(url); new TracingJdbcEventListener("foo", false, logOptions).parseServerIpAndPort(connection, span); verify(span).remoteServiceName("foo"); verify(span).remoteIpAndPort("1.2.3.4", 5555); }
@Test public void parseServerIpAndPort_doesntCrash() throws SQLException { when(connection.getMetaData()).thenThrow(new SQLException()); new TracingJdbcEventListener("", false, logOptions).parseServerIpAndPort(connection, span); verifyNoMoreInteractions(span); }
@Test public void parseServerIpAndPort_withWhiteSpace() throws SQLException { when(connection.getMetaData()).thenReturn(metaData); when(metaData.getURL()).thenReturn(urlWithWhiteSpace); new TracingJdbcEventListener("foo", false, logOptions).parseServerIpAndPort(connection, span); verify(span).remoteServiceName("foo"); }