-
Notifications
You must be signed in to change notification settings - Fork 318
🪞 9251 - Add a span when waiting for an available database connection from a pool #9636
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
Merged
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
63f6167
HikariCP: Add a span when waiting on the pool
deejgregor 65a3918
Apache DBCP2: Add a span when waiting on the pool
deejgregor 0c4a7cf
Disable pool.waiting by default, other tweaks
deejgregor c759cfc
Docs: Update formatting settings for later versions of IntelliJ
deejgregor 3279725
Merge SaturatedPoolBlockingTest into JDBCInstrumentationTestBase
deejgregor a0acab9
Cleanup Hikari instrumentation
deejgregor 8edfbd5
Fix: injected bytecode shouldn't link back to instrumentation class
deejgregor 08cdcec
Add dbcp2/hikari aliases for pool waiting instrumentation
deejgregor 3f36f2e
Merge branch 'master' into community-pr-9251
mcculls 41f787c
Avoid leaking thread-locals in HikariBlockedTracker
mcculls 74ec816
Keep SpotBugs happy
mcculls 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
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
72 changes: 72 additions & 0 deletions
72
...main/java/datadog/trace/instrumentation/jdbc/Dbcp2LinkedBlockingDequeInstrumentation.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,72 @@ | ||
| package datadog.trace.instrumentation.jdbc; | ||
|
|
||
| import static datadog.trace.agent.tooling.bytebuddy.matcher.NameMatchers.named; | ||
| import static datadog.trace.bootstrap.instrumentation.api.AgentTracer.startSpan; | ||
| import static datadog.trace.instrumentation.jdbc.PoolWaitingDecorator.DECORATE; | ||
| import static datadog.trace.instrumentation.jdbc.PoolWaitingDecorator.POOL_WAITING; | ||
| import static net.bytebuddy.matcher.ElementMatchers.takesArguments; | ||
|
|
||
| import com.google.auto.service.AutoService; | ||
| import datadog.trace.agent.tooling.Instrumenter; | ||
| import datadog.trace.agent.tooling.InstrumenterModule; | ||
| import datadog.trace.api.InstrumenterConfig; | ||
| import datadog.trace.bootstrap.CallDepthThreadLocalMap; | ||
| import datadog.trace.bootstrap.instrumentation.api.AgentSpan; | ||
| import net.bytebuddy.asm.Advice; | ||
|
|
||
| @AutoService(InstrumenterModule.class) | ||
| public final class Dbcp2LinkedBlockingDequeInstrumentation extends InstrumenterModule.Tracing | ||
| implements Instrumenter.ForKnownTypes, Instrumenter.HasMethodAdvice { | ||
|
|
||
| public Dbcp2LinkedBlockingDequeInstrumentation() { | ||
| super("jdbc", "dbcp2"); | ||
| } | ||
|
|
||
| @Override | ||
| protected boolean defaultEnabled() { | ||
| return InstrumenterConfig.get().isJdbcPoolWaitingEnabled(); | ||
| } | ||
|
|
||
| @Override | ||
| public String[] knownMatchingTypes() { | ||
| return new String[] { | ||
| "org.apache.commons.pool2.impl.LinkedBlockingDeque", // standalone | ||
| "org.apache.tomcat.dbcp.pool2.impl.LinkedBlockingDeque" // bundled with Tomcat | ||
| }; | ||
| } | ||
|
|
||
| @Override | ||
| public String[] helperClassNames() { | ||
| return new String[] {packageName + ".PoolWaitingDecorator"}; | ||
| } | ||
|
|
||
| @Override | ||
| public void methodAdvice(MethodTransformer transformer) { | ||
| transformer.applyAdvice( | ||
| named("pollFirst").and(takesArguments(1)), | ||
| Dbcp2LinkedBlockingDequeInstrumentation.class.getName() + "$PollFirstAdvice"); | ||
| } | ||
|
|
||
| public static class PollFirstAdvice { | ||
| @Advice.OnMethodEnter(suppress = Throwable.class) | ||
| public static AgentSpan onEnter() { | ||
| if (CallDepthThreadLocalMap.getCallDepth(PoolWaitingDecorator.class) > 0) { | ||
| AgentSpan span = startSpan(POOL_WAITING); | ||
| DECORATE.afterStart(span); | ||
| span.setResourceName("dbcp2.waiting"); | ||
| return span; | ||
| } else { | ||
| return null; | ||
| } | ||
| } | ||
|
|
||
| @Advice.OnMethodExit(onThrowable = Throwable.class, suppress = Throwable.class) | ||
| public static void onExit( | ||
| @Advice.Enter final AgentSpan span, @Advice.Thrown final Throwable throwable) { | ||
| if (span != null) { | ||
| DECORATE.onError(span, throwable); | ||
| span.finish(); | ||
| } | ||
| } | ||
| } | ||
| } | ||
56 changes: 56 additions & 0 deletions
56
...c/main/java/datadog/trace/instrumentation/jdbc/Dbcp2ManagedConnectionInstrumentation.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,56 @@ | ||
| package datadog.trace.instrumentation.jdbc; | ||
|
|
||
| import static datadog.trace.agent.tooling.bytebuddy.matcher.NameMatchers.named; | ||
|
|
||
| import com.google.auto.service.AutoService; | ||
| import datadog.trace.agent.tooling.Instrumenter; | ||
| import datadog.trace.agent.tooling.InstrumenterModule; | ||
| import datadog.trace.api.InstrumenterConfig; | ||
| import datadog.trace.bootstrap.CallDepthThreadLocalMap; | ||
| import net.bytebuddy.asm.Advice; | ||
|
|
||
| @AutoService(InstrumenterModule.class) | ||
| public final class Dbcp2ManagedConnectionInstrumentation extends InstrumenterModule.Tracing | ||
| implements Instrumenter.ForKnownTypes, Instrumenter.HasMethodAdvice { | ||
|
|
||
| public Dbcp2ManagedConnectionInstrumentation() { | ||
| super("jdbc", "dbcp2"); | ||
| } | ||
|
|
||
| @Override | ||
| protected boolean defaultEnabled() { | ||
| return InstrumenterConfig.get().isJdbcPoolWaitingEnabled(); | ||
| } | ||
|
|
||
| @Override | ||
| public String[] knownMatchingTypes() { | ||
| return new String[] { | ||
| "org.apache.commons.dbcp2.managed.ManagedConnection", // standalone | ||
| "org.apache.tomcat.dbcp.dbcp2.managed.ManagedConnection" // bundled with Tomcat | ||
| }; | ||
| } | ||
|
|
||
| @Override | ||
| public String[] helperClassNames() { | ||
| return new String[] {packageName + ".PoolWaitingDecorator"}; | ||
| } | ||
|
|
||
| @Override | ||
| public void methodAdvice(MethodTransformer transformer) { | ||
| transformer.applyAdvice( | ||
| named("updateTransactionStatus"), | ||
| Dbcp2ManagedConnectionInstrumentation.class.getName() + "$UpdateTransactionStatusAdvice"); | ||
| } | ||
|
|
||
| public static class UpdateTransactionStatusAdvice { | ||
| @Advice.OnMethodEnter(suppress = Throwable.class) | ||
| public static void onEnter() { | ||
| CallDepthThreadLocalMap.incrementCallDepth(PoolWaitingDecorator.class); | ||
| } | ||
|
|
||
| @Advice.OnMethodExit(onThrowable = Throwable.class, suppress = Throwable.class) | ||
| public static void onExit() { | ||
| CallDepthThreadLocalMap.decrementCallDepth(PoolWaitingDecorator.class); | ||
| } | ||
| } | ||
| } |
57 changes: 57 additions & 0 deletions
57
...in/java/datadog/trace/instrumentation/jdbc/Dbcp2PerUserPoolDataSourceInstrumentation.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,57 @@ | ||
| package datadog.trace.instrumentation.jdbc; | ||
|
|
||
| import static datadog.trace.agent.tooling.bytebuddy.matcher.NameMatchers.named; | ||
|
|
||
| import com.google.auto.service.AutoService; | ||
| import datadog.trace.agent.tooling.Instrumenter; | ||
| import datadog.trace.agent.tooling.InstrumenterModule; | ||
| import datadog.trace.api.InstrumenterConfig; | ||
| import datadog.trace.bootstrap.CallDepthThreadLocalMap; | ||
| import net.bytebuddy.asm.Advice; | ||
|
|
||
| @AutoService(InstrumenterModule.class) | ||
| public final class Dbcp2PerUserPoolDataSourceInstrumentation extends InstrumenterModule.Tracing | ||
| implements Instrumenter.ForKnownTypes, Instrumenter.HasMethodAdvice { | ||
|
|
||
| public Dbcp2PerUserPoolDataSourceInstrumentation() { | ||
| super("jdbc", "dbcp2"); | ||
| } | ||
|
|
||
| @Override | ||
| protected boolean defaultEnabled() { | ||
| return InstrumenterConfig.get().isJdbcPoolWaitingEnabled(); | ||
| } | ||
|
|
||
| @Override | ||
| public String[] knownMatchingTypes() { | ||
| return new String[] { | ||
| "org.apache.commons.dbcp2.datasources.PerUserPoolDataSource", // standalone | ||
| "org.apache.tomcat.dbcp.dbcp2.datasources.PerUserPoolDataSource" // bundled with Tomcat | ||
| }; | ||
| } | ||
|
|
||
| @Override | ||
| public String[] helperClassNames() { | ||
| return new String[] {packageName + ".PoolWaitingDecorator"}; | ||
| } | ||
|
|
||
| @Override | ||
| public void methodAdvice(MethodTransformer transformer) { | ||
| transformer.applyAdvice( | ||
| named("getPooledConnectionAndInfo"), | ||
| Dbcp2PerUserPoolDataSourceInstrumentation.class.getName() | ||
| + "$GetPooledConnectionAndInfoAdvice"); | ||
| } | ||
|
|
||
| public static class GetPooledConnectionAndInfoAdvice { | ||
| @Advice.OnMethodEnter(suppress = Throwable.class) | ||
| public static void onEnter() { | ||
| CallDepthThreadLocalMap.incrementCallDepth(PoolWaitingDecorator.class); | ||
| } | ||
|
|
||
| @Advice.OnMethodExit(onThrowable = Throwable.class, suppress = Throwable.class) | ||
| public static void onExit() { | ||
| CallDepthThreadLocalMap.decrementCallDepth(PoolWaitingDecorator.class); | ||
| } | ||
| } | ||
| } |
56 changes: 56 additions & 0 deletions
56
...c/main/java/datadog/trace/instrumentation/jdbc/Dbcp2PoolingDataSourceInstrumentation.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,56 @@ | ||
| package datadog.trace.instrumentation.jdbc; | ||
|
|
||
| import static datadog.trace.agent.tooling.bytebuddy.matcher.NameMatchers.named; | ||
|
|
||
| import com.google.auto.service.AutoService; | ||
| import datadog.trace.agent.tooling.Instrumenter; | ||
| import datadog.trace.agent.tooling.InstrumenterModule; | ||
| import datadog.trace.api.InstrumenterConfig; | ||
| import datadog.trace.bootstrap.CallDepthThreadLocalMap; | ||
| import net.bytebuddy.asm.Advice; | ||
|
|
||
| @AutoService(InstrumenterModule.class) | ||
| public final class Dbcp2PoolingDataSourceInstrumentation extends InstrumenterModule.Tracing | ||
| implements Instrumenter.ForKnownTypes, Instrumenter.HasMethodAdvice { | ||
|
|
||
| public Dbcp2PoolingDataSourceInstrumentation() { | ||
| super("jdbc", "dbcp2"); | ||
| } | ||
|
|
||
| @Override | ||
| protected boolean defaultEnabled() { | ||
| return InstrumenterConfig.get().isJdbcPoolWaitingEnabled(); | ||
| } | ||
|
|
||
| @Override | ||
| public String[] knownMatchingTypes() { | ||
| return new String[] { | ||
| "org.apache.commons.dbcp2.PoolingDataSource", // standalone | ||
| "org.apache.tomcat.dbcp.dbcp2.PoolingDataSource" // bundled with Tomcat | ||
| }; | ||
| } | ||
|
|
||
| @Override | ||
| public String[] helperClassNames() { | ||
| return new String[] {packageName + ".PoolWaitingDecorator"}; | ||
| } | ||
|
|
||
| @Override | ||
| public void methodAdvice(MethodTransformer transformer) { | ||
| transformer.applyAdvice( | ||
| named("getConnection"), | ||
| Dbcp2PoolingDataSourceInstrumentation.class.getName() + "$GetConnectionAdvice"); | ||
| } | ||
|
|
||
| public static class GetConnectionAdvice { | ||
| @Advice.OnMethodEnter(suppress = Throwable.class) | ||
| public static void onEnter() { | ||
| CallDepthThreadLocalMap.incrementCallDepth(PoolWaitingDecorator.class); | ||
| } | ||
|
|
||
| @Advice.OnMethodExit(onThrowable = Throwable.class, suppress = Throwable.class) | ||
| public static void onExit() { | ||
| CallDepthThreadLocalMap.decrementCallDepth(PoolWaitingDecorator.class); | ||
| } | ||
| } | ||
| } |
55 changes: 55 additions & 0 deletions
55
...c/src/main/java/datadog/trace/instrumentation/jdbc/Dbcp2PoolingDriverInstrumentation.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,55 @@ | ||
| package datadog.trace.instrumentation.jdbc; | ||
|
|
||
| import static datadog.trace.agent.tooling.bytebuddy.matcher.NameMatchers.named; | ||
|
|
||
| import com.google.auto.service.AutoService; | ||
| import datadog.trace.agent.tooling.Instrumenter; | ||
| import datadog.trace.agent.tooling.InstrumenterModule; | ||
| import datadog.trace.api.InstrumenterConfig; | ||
| import datadog.trace.bootstrap.CallDepthThreadLocalMap; | ||
| import net.bytebuddy.asm.Advice; | ||
|
|
||
| @AutoService(InstrumenterModule.class) | ||
| public final class Dbcp2PoolingDriverInstrumentation extends InstrumenterModule.Tracing | ||
| implements Instrumenter.ForKnownTypes, Instrumenter.HasMethodAdvice { | ||
|
|
||
| public Dbcp2PoolingDriverInstrumentation() { | ||
| super("jdbc", "dbcp2"); | ||
| } | ||
|
|
||
| @Override | ||
| protected boolean defaultEnabled() { | ||
| return InstrumenterConfig.get().isJdbcPoolWaitingEnabled(); | ||
| } | ||
|
|
||
| @Override | ||
| public String[] knownMatchingTypes() { | ||
| return new String[] { | ||
| "org.apache.commons.dbcp2.PoolingDriver", // standalone | ||
| "org.apache.tomcat.dbcp.dbcp2.PoolingDriver" // bundled with Tomcat | ||
| }; | ||
| } | ||
|
|
||
| @Override | ||
| public String[] helperClassNames() { | ||
| return new String[] {packageName + ".PoolWaitingDecorator"}; | ||
| } | ||
|
|
||
| @Override | ||
| public void methodAdvice(MethodTransformer transformer) { | ||
| transformer.applyAdvice( | ||
| named("connect"), Dbcp2PoolingDriverInstrumentation.class.getName() + "$ConnectAdvice"); | ||
| } | ||
|
|
||
| public static class ConnectAdvice { | ||
| @Advice.OnMethodEnter(suppress = Throwable.class) | ||
| public static void onEnter() { | ||
| CallDepthThreadLocalMap.incrementCallDepth(PoolWaitingDecorator.class); | ||
| } | ||
|
|
||
| @Advice.OnMethodExit(onThrowable = Throwable.class, suppress = Throwable.class) | ||
| public static void onExit() { | ||
| CallDepthThreadLocalMap.decrementCallDepth(PoolWaitingDecorator.class); | ||
| } | ||
| } | ||
| } |
57 changes: 57 additions & 0 deletions
57
...ain/java/datadog/trace/instrumentation/jdbc/Dbcp2SharedPoolDataSourceInstrumentation.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,57 @@ | ||
| package datadog.trace.instrumentation.jdbc; | ||
|
|
||
| import static datadog.trace.agent.tooling.bytebuddy.matcher.NameMatchers.named; | ||
|
|
||
| import com.google.auto.service.AutoService; | ||
| import datadog.trace.agent.tooling.Instrumenter; | ||
| import datadog.trace.agent.tooling.InstrumenterModule; | ||
| import datadog.trace.api.InstrumenterConfig; | ||
| import datadog.trace.bootstrap.CallDepthThreadLocalMap; | ||
| import net.bytebuddy.asm.Advice; | ||
|
|
||
| @AutoService(InstrumenterModule.class) | ||
| public final class Dbcp2SharedPoolDataSourceInstrumentation extends InstrumenterModule.Tracing | ||
| implements Instrumenter.ForKnownTypes, Instrumenter.HasMethodAdvice { | ||
|
|
||
| public Dbcp2SharedPoolDataSourceInstrumentation() { | ||
| super("jdbc", "dbcp2"); | ||
| } | ||
|
|
||
| @Override | ||
| protected boolean defaultEnabled() { | ||
| return InstrumenterConfig.get().isJdbcPoolWaitingEnabled(); | ||
| } | ||
|
|
||
| @Override | ||
| public String[] knownMatchingTypes() { | ||
| return new String[] { | ||
| "org.apache.commons.dbcp2.datasources.SharePoolDataSource", // standalone | ||
| "org.apache.tomcat.dbcp.dbcp2.datasources.SharedPoolPoolDataSource" // bundled with Tomcat | ||
| }; | ||
| } | ||
|
|
||
| @Override | ||
| public String[] helperClassNames() { | ||
| return new String[] {packageName + ".PoolWaitingDecorator"}; | ||
| } | ||
|
|
||
| @Override | ||
| public void methodAdvice(MethodTransformer transformer) { | ||
| transformer.applyAdvice( | ||
| named("getPooledConnectionAndInfo"), | ||
| Dbcp2SharedPoolDataSourceInstrumentation.class.getName() | ||
| + "$GetPooledConnectionAndInfoAdvice"); | ||
| } | ||
|
|
||
| public static class GetPooledConnectionAndInfoAdvice { | ||
| @Advice.OnMethodEnter(suppress = Throwable.class) | ||
| public static void onEnter() { | ||
| CallDepthThreadLocalMap.incrementCallDepth(PoolWaitingDecorator.class); | ||
| } | ||
|
|
||
| @Advice.OnMethodExit(onThrowable = Throwable.class, suppress = Throwable.class) | ||
| public static void onExit() { | ||
| CallDepthThreadLocalMap.decrementCallDepth(PoolWaitingDecorator.class); | ||
| } | ||
| } | ||
| } |
20 changes: 20 additions & 0 deletions
20
...mentation/jdbc/src/main/java/datadog/trace/instrumentation/jdbc/HikariBlockedTracker.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,20 @@ | ||
| package datadog.trace.instrumentation.jdbc; | ||
|
|
||
| import static java.lang.Boolean.TRUE; | ||
|
|
||
| /** Shared blocked getConnection() tracking {@link ThreadLocal} for Hikari. */ | ||
| public class HikariBlockedTracker { | ||
| private static final ThreadLocal<Boolean> tracker = new ThreadLocal<>(); | ||
|
|
||
| public static void clearBlocked() { | ||
| tracker.remove(); | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @deejgregor I made a small change here to remove the thread-local when done, just in case a large number of threads end up calling into Hikari |
||
| } | ||
|
|
||
| public static void setBlocked() { | ||
| tracker.set(TRUE); | ||
| } | ||
|
|
||
| public static boolean wasBlocked() { | ||
| return TRUE.equals(tracker.get()); | ||
| } | ||
| } | ||
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.
Uh oh!
There was an error while loading. Please reload this page.