Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make interceptor mode of TracingDriver work with other wrapper drivers #69

Merged
merged 1 commit into from
Feb 28, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
18 changes: 11 additions & 7 deletions src/main/java/io/opentracing/contrib/jdbc/TracingDriver.java
Original file line number Diff line number Diff line change
Expand Up @@ -150,17 +150,18 @@ public Connection connect(String url, Properties info) throws SQLException {

final Set<String> ignoreStatements;
final boolean withActiveSpanOnly;
if (acceptsURL(url)) {
if (interceptorMode) {
withActiveSpanOnly = TracingDriver.withActiveSpanOnly;
ignoreStatements = TracingDriver.ignoreStatements;
} else if (acceptsURL(url)) {
withActiveSpanOnly = url.contains(WITH_ACTIVE_SPAN_ONLY);
ignoreStatements = extractIgnoredStatements(url);
url = extractRealUrl(url);
} else if (!interceptorMode) {
return null;
} else {
withActiveSpanOnly = TracingDriver.withActiveSpanOnly;
ignoreStatements = TracingDriver.ignoreStatements;
return null;
}

url = extractRealUrl(url);

// find the real driver for the URL
final Driver wrappedDriver = findDriver(url);

Expand All @@ -182,7 +183,10 @@ public Connection connect(String url, Properties info) throws SQLException {

@Override
public boolean acceptsURL(String url) throws SQLException {
return url != null && url.startsWith(getUrlPrefix());
return url != null && (
url.startsWith(getUrlPrefix()) ||
(interceptorMode && url.startsWith("jdbc:"))
);
}

@Override
Expand Down
16 changes: 14 additions & 2 deletions src/test/java/io/opentracing/contrib/jdbc/JdbcTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,18 +26,30 @@
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Enumeration;
import java.util.List;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;

public class JdbcTest {
public static void assertGetDriver(final Connection connection) throws SQLException {
private static void assertGetDriver(final Connection connection) throws SQLException {
final String originalURL = connection.getMetaData().getURL();
final Driver driver = DriverManager.getDriver(originalURL);
final Driver driver = getUnderlyingDriver(originalURL);
assertEquals("org.h2.Driver", driver.getClass().getName());
}

private static Driver getUnderlyingDriver(final String url) throws SQLException {
final Enumeration<Driver> enumeration = DriverManager.getDrivers();
while (enumeration.hasMoreElements()) {
final Driver driver = enumeration.nextElement();
if (driver.acceptsURL(url) && !(driver instanceof TracingDriver)) {
return driver;
}
}
return null;
}

private static final MockTracer mockTracer = new MockTracer();

@BeforeClass
Expand Down