-
Couldn't load subscription status.
- Fork 314
Add a span when waiting for an available database connection from a pool #9251
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
Changes from 5 commits
fc57059
92e2d9e
b9f26e6
c033b74
b5e714a
192f3f4
9c2fbe9
faf7ae1
732693f
046a462
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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"); | ||
| } | ||
|
|
||
| @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(Dbcp2LinkedBlockingDequeInstrumentation.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(); | ||
| } | ||
| } | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,51 @@ | ||
| 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"); | ||
deejgregor marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| @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 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(Dbcp2LinkedBlockingDequeInstrumentation.class); | ||
|
||
| } | ||
|
|
||
| @Advice.OnMethodExit(onThrowable = Throwable.class, suppress = Throwable.class) | ||
| public static void onExit() { | ||
| CallDepthThreadLocalMap.decrementCallDepth(Dbcp2LinkedBlockingDequeInstrumentation.class); | ||
|
||
| } | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,52 @@ | ||
| 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"); | ||
deejgregor marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| @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 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(Dbcp2LinkedBlockingDequeInstrumentation.class); | ||
|
||
| } | ||
|
|
||
| @Advice.OnMethodExit(onThrowable = Throwable.class, suppress = Throwable.class) | ||
| public static void onExit() { | ||
| CallDepthThreadLocalMap.decrementCallDepth(Dbcp2LinkedBlockingDequeInstrumentation.class); | ||
|
||
| } | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,51 @@ | ||
| 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"); | ||
deejgregor marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| @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 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(Dbcp2LinkedBlockingDequeInstrumentation.class); | ||
|
||
| } | ||
|
|
||
| @Advice.OnMethodExit(onThrowable = Throwable.class, suppress = Throwable.class) | ||
| public static void onExit() { | ||
| CallDepthThreadLocalMap.decrementCallDepth(Dbcp2LinkedBlockingDequeInstrumentation.class); | ||
|
||
| } | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,50 @@ | ||
| 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"); | ||
deejgregor marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| @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 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(Dbcp2LinkedBlockingDequeInstrumentation.class); | ||
|
||
| } | ||
|
|
||
| @Advice.OnMethodExit(onThrowable = Throwable.class, suppress = Throwable.class) | ||
| public static void onExit() { | ||
| CallDepthThreadLocalMap.decrementCallDepth(Dbcp2LinkedBlockingDequeInstrumentation.class); | ||
|
||
| } | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,52 @@ | ||
| 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"); | ||
deejgregor marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| @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 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(Dbcp2LinkedBlockingDequeInstrumentation.class); | ||
|
||
| } | ||
|
|
||
| @Advice.OnMethodExit(onThrowable = Throwable.class, suppress = Throwable.class) | ||
| public static void onExit() { | ||
| CallDepthThreadLocalMap.decrementCallDepth(Dbcp2LinkedBlockingDequeInstrumentation.class); | ||
|
||
| } | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| package datadog.trace.instrumentation.jdbc; | ||
|
|
||
| /** Shared blocked getConnection() tracking ThreadLocking for Hikari. */ | ||
| public class HikariBlockedTracker { | ||
| private static final ThreadLocal<Boolean> tracker = ThreadLocal.withInitial(() -> false); | ||
|
|
||
| public static void clearBlocked() { | ||
| tracker.set(false); | ||
| } | ||
|
|
||
| public static void setBlocked() { | ||
| tracker.set(true); | ||
| } | ||
|
|
||
| public static boolean wasBlocked() { | ||
| return tracker.get(); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| package datadog.trace.instrumentation.jdbc; | ||
|
|
||
| import java.util.concurrent.SynchronousQueue; | ||
| import java.util.concurrent.TimeUnit; | ||
|
|
||
| /** Blocked getConnection() tracking for Hikari starting with commit f0b3c520c. */ | ||
| public class HikariBlockedTrackingSynchronousQueue<T> extends SynchronousQueue<T> { | ||
| public HikariBlockedTrackingSynchronousQueue() { | ||
| // This assumes the initialization of the SynchronousQueue in ConcurrentBag doesn't change | ||
| super(true); | ||
| } | ||
|
|
||
| @Override | ||
| public T poll(long timeout, TimeUnit unit) throws InterruptedException { | ||
| HikariBlockedTracker.setBlocked(); | ||
| return super.poll(timeout, unit); | ||
| } | ||
| } |
Uh oh!
There was an error while loading. Please reload this page.