-
Notifications
You must be signed in to change notification settings - Fork 363
Skip repeated AbstractMethodError from JDBC getClientInfo #12702
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
Draft
dougqh
wants to merge
15
commits into
master
Choose a base branch
from
dougqh/abstract-method-guard
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+1,569
−2
Draft
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
0e357ab
Stop repeating AbstractMethodError from JDBC getClientInfo
dougqh 2e8a192
Add AbstractMethodGuard benchmark; make the latch race-free
dougqh cf84e68
Tidy AbstractMethodGuard after review
dougqh f4b29e6
Rebuild the guard as Latch and ClassLatch with a bound helper
dougqh 5ceffc1
Move Latch out of this PR
dougqh 3af387a
Let the caller choose the fallback: tryGetOrNull and tryGetOrDefault
dougqh 4c07455
Add ThrowingFunction and use it for ClassLatch.handleAbstractMethod
dougqh 9dabe53
Mark handleAbstractMethod as a StrategyConsumer with a Strategy slot
dougqh b404508
Add handleNoSuchMethod and handleNoSuchOrAbstractMethod
dougqh 424bbf3
Name the JDBC latch constant CLIENT_INFO_LATCH
dougqh d715340
Name the ClassLatch hook handle
dougqh 1510280
Add ClassLatchBenchmark results
dougqh b630167
Rename the ClassLatch hook to apply and the public methods to tryApply*
dougqh ff0fca7
Remove a stray blank line in JDBCDecorator
dougqh d176106
Keep catching Throwable around getClientInfo
dougqh File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
133 changes: 133 additions & 0 deletions
133
...tion/jdbc/src/test/java/datadog/trace/instrumentation/jdbc/ParseDBInfoClientInfoTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,133 @@ | ||
| package datadog.trace.instrumentation.jdbc; | ||
|
|
||
| import static org.junit.jupiter.api.Assertions.assertEquals; | ||
|
|
||
| import datadog.trace.bootstrap.instrumentation.jdbc.DBInfo; | ||
| import java.lang.reflect.InvocationHandler; | ||
| import java.lang.reflect.InvocationTargetException; | ||
| import java.lang.reflect.Proxy; | ||
| import java.sql.Connection; | ||
| import java.sql.DatabaseMetaData; | ||
| import java.sql.SQLException; | ||
| import java.util.Properties; | ||
| import java.util.concurrent.atomic.AtomicInteger; | ||
| import org.junit.jupiter.api.Test; | ||
|
|
||
| /** How {@code parseDBInfoFromConnection} copes with connections whose getClientInfo fails. */ | ||
| class ParseDBInfoClientInfoTest { | ||
| private static final String URL = "jdbc:postgresql://db.example.com:5432/orders"; | ||
|
|
||
| interface ClientInfoAnswer { | ||
| Properties get() throws Throwable; | ||
| } | ||
|
|
||
| private static Connection connection(AtomicInteger clientInfoCalls, ClientInfoAnswer answer) { | ||
| DatabaseMetaData metaData = | ||
| (DatabaseMetaData) | ||
| Proxy.newProxyInstance( | ||
| DatabaseMetaData.class.getClassLoader(), | ||
| new Class<?>[] {DatabaseMetaData.class}, | ||
| (proxy, method, args) -> "getURL".equals(method.getName()) ? URL : null); | ||
| InvocationHandler handler = | ||
| (proxy, method, args) -> { | ||
| switch (method.getName()) { | ||
| case "getMetaData": | ||
| return metaData; | ||
| case "getClientInfo": | ||
| clientInfoCalls.incrementAndGet(); | ||
| try { | ||
| return answer.get(); | ||
| } catch (InvocationTargetException e) { | ||
| throw e.getCause(); | ||
| } | ||
| default: | ||
| return null; | ||
| } | ||
| }; | ||
| return (Connection) | ||
| Proxy.newProxyInstance( | ||
| Connection.class.getClassLoader(), new Class<?>[] {Connection.class}, handler); | ||
| } | ||
|
|
||
| @Test | ||
| void urlIsStillParsedWhenGetClientInfoThrowsSqlException() { | ||
| AtomicInteger calls = new AtomicInteger(); | ||
| Connection connection = | ||
| connection( | ||
| calls, | ||
| () -> { | ||
| throw new SQLException("not allowed"); | ||
| }); | ||
|
|
||
| DBInfo info = JDBCDecorator.parseDBInfoFromConnection(connection); | ||
|
|
||
| assertEquals("postgresql", info.getType()); | ||
| assertEquals("orders", info.getDb()); | ||
| } | ||
|
|
||
| @Test | ||
| void urlIsStillParsedWhenGetClientInfoIsUnsupported() { | ||
| AtomicInteger calls = new AtomicInteger(); | ||
| Connection connection = | ||
| connection( | ||
| calls, | ||
| () -> { | ||
| throw new UnsupportedOperationException(); | ||
| }); | ||
|
|
||
| DBInfo info = JDBCDecorator.parseDBInfoFromConnection(connection); | ||
|
|
||
| assertEquals("postgresql", info.getType()); | ||
| assertEquals("orders", info.getDb()); | ||
| } | ||
|
|
||
| @Test | ||
| void urlIsStillParsedWhenGetClientInfoIsMissing() { | ||
| AtomicInteger calls = new AtomicInteger(); | ||
| Connection connection = | ||
| connection( | ||
| calls, | ||
| () -> { | ||
| throw new AbstractMethodError("driver predates JDBC 4.0"); | ||
| }); | ||
|
|
||
| DBInfo info = JDBCDecorator.parseDBInfoFromConnection(connection); | ||
|
|
||
| assertEquals("postgresql", info.getType()); | ||
| assertEquals("orders", info.getDb()); | ||
| } | ||
|
|
||
| @Test | ||
| void anyOtherFailureStillYieldsUrlBasedDbInfo() { | ||
| AtomicInteger calls = new AtomicInteger(); | ||
| for (Throwable failure : | ||
| new Throwable[] { | ||
| new IllegalStateException("unexpected"), new Throwable("not even an Exception") | ||
| }) { | ||
| Connection connection = | ||
| connection( | ||
| calls, | ||
| () -> { | ||
| throw failure; | ||
| }); | ||
|
|
||
| // getClientInfo can fail in any way; the URL alone is still enough for the DB info | ||
| DBInfo info = JDBCDecorator.parseDBInfoFromConnection(connection); | ||
|
|
||
| assertEquals("postgresql", info.getType()); | ||
| assertEquals("orders", info.getDb()); | ||
| } | ||
| } | ||
|
|
||
| @Test | ||
| void returnsTheClientInfoWhenAvailable() { | ||
| AtomicInteger calls = new AtomicInteger(); | ||
| Properties clientInfo = new Properties(); | ||
| Connection connection = connection(calls, () -> clientInfo); | ||
|
|
||
| DBInfo info = JDBCDecorator.parseDBInfoFromConnection(connection); | ||
|
|
||
| assertEquals("postgresql", info.getType()); | ||
| assertEquals(1, calls.get()); | ||
| } | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
With the latch handling AbstractMethodError, I'm not sure that we need to EXCLUDE_TELEMETRY anymore. I'm curious what others think.