|
| 1 | +package datadog.trace.instrumentation.jdbc; |
| 2 | + |
| 3 | +import com.google.auto.service.AutoService; |
| 4 | +import com.zaxxer.hikari.pool.HikariPool; |
| 5 | +import com.zaxxer.hikari.util.ConcurrentBag; |
| 6 | +import datadog.trace.agent.tooling.Instrumenter; |
| 7 | +import datadog.trace.agent.tooling.InstrumenterModule; |
| 8 | +import datadog.trace.bootstrap.InstrumentationContext; |
| 9 | +import datadog.trace.bootstrap.instrumentation.api.AgentSpan; |
| 10 | +import net.bytebuddy.asm.Advice; |
| 11 | +import org.slf4j.Logger; |
| 12 | +import org.slf4j.LoggerFactory; |
| 13 | + |
| 14 | +import java.lang.reflect.Field; |
| 15 | +import java.util.Map; |
| 16 | +import java.util.concurrent.TimeUnit; |
| 17 | + |
| 18 | +import static datadog.trace.agent.tooling.bytebuddy.matcher.NameMatchers.named; |
| 19 | +import static datadog.trace.bootstrap.instrumentation.api.AgentTracer.startSpan; |
| 20 | +import static datadog.trace.bootstrap.instrumentation.api.Tags.DB_POOL_NAME; |
| 21 | +import static java.util.Collections.singletonMap; |
| 22 | +import static net.bytebuddy.matcher.ElementMatchers.isConstructor; |
| 23 | + |
| 24 | +/** |
| 25 | + * Instrument Hikari's ConcurrentBag class to detect when blocking occurs trying to get |
| 26 | + * an entry from the connection pool. |
| 27 | + */ |
| 28 | +@AutoService(InstrumenterModule.class) |
| 29 | +public final class HikariConcurrentBagInstrumentation extends InstrumenterModule.Tracing |
| 30 | + implements Instrumenter.ForSingleType, Instrumenter.HasMethodAdvice { |
| 31 | + |
| 32 | + private static final Logger log = LoggerFactory.getLogger(HikariConcurrentBagInstrumentation.class); |
| 33 | + |
| 34 | + public HikariConcurrentBagInstrumentation() { |
| 35 | + super("jdbc-datasource"); |
| 36 | + } |
| 37 | + |
| 38 | + @Override |
| 39 | + public String instrumentedType() { |
| 40 | + return "com.zaxxer.hikari.util.ConcurrentBag"; |
| 41 | + } |
| 42 | + |
| 43 | + @Override |
| 44 | + public String[] helperClassNames() { |
| 45 | + return new String[]{packageName + ".HikariBlockedTrackingSynchronousQueue", |
| 46 | + packageName + ".HikariBlockedTracker"}; |
| 47 | + } |
| 48 | + |
| 49 | + @Override |
| 50 | + public Map<String, String> contextStore() { |
| 51 | + // For getting the poolName |
| 52 | + return singletonMap("com.zaxxer.hikari.util.ConcurrentBag", String.class.getName()); |
| 53 | + } |
| 54 | + |
| 55 | + @Override |
| 56 | + public void methodAdvice(MethodTransformer transformer) { |
| 57 | + transformer.applyAdvice( |
| 58 | + isConstructor(), HikariConcurrentBagInstrumentation.class.getName() + "$ConstructorAdvice"); |
| 59 | + transformer.applyAdvice( |
| 60 | + named("borrow"), |
| 61 | + HikariConcurrentBagInstrumentation.class.getName() + "$BorrowAdvice"); |
| 62 | + } |
| 63 | + |
| 64 | + public static class ConstructorAdvice { |
| 65 | + @Advice.OnMethodExit(suppress = Throwable.class) |
| 66 | + static void after(@Advice.This ConcurrentBag<?> thiz) |
| 67 | + throws IllegalAccessException, NoSuchFieldException { |
| 68 | + try { |
| 69 | + Field handoffQueueField = thiz.getClass().getDeclaredField("handoffQueue"); |
| 70 | + handoffQueueField.setAccessible(true); |
| 71 | + handoffQueueField.set(thiz, new HikariBlockedTrackingSynchronousQueue<>()); |
| 72 | + } catch (NoSuchFieldException e) { |
| 73 | + // ignore -- see HikariQueuedSequenceSynchronizerInstrumentation for older Hikari versions |
| 74 | + } |
| 75 | + |
| 76 | + Field hikariPoolField = thiz.getClass().getDeclaredField("listener"); |
| 77 | + hikariPoolField.setAccessible(true); |
| 78 | + HikariPool hikariPool = (HikariPool) hikariPoolField.get(thiz); |
| 79 | + |
| 80 | + /* |
| 81 | + * In earlier versions of Hikari, poolName is directly inside HikariPool, and |
| 82 | + * in later versions it is in the PoolBase superclass. |
| 83 | + */ |
| 84 | + final Class<?> hikariPoolSuper = hikariPool.getClass().getSuperclass(); |
| 85 | + final Class<?> poolNameContainingClass; |
| 86 | + if (!hikariPoolSuper.getName().equals("java.lang.Object")) { |
| 87 | + poolNameContainingClass = hikariPoolSuper; |
| 88 | + } else { |
| 89 | + poolNameContainingClass = hikariPool.getClass(); |
| 90 | + } |
| 91 | + Field poolNameField = poolNameContainingClass.getDeclaredField("poolName"); |
| 92 | + poolNameField.setAccessible(true); |
| 93 | + String poolName = (String) poolNameField.get(hikariPool); |
| 94 | + InstrumentationContext.get(ConcurrentBag.class, String.class).put(thiz, poolName); |
| 95 | + } |
| 96 | + } |
| 97 | + |
| 98 | + public static class BorrowAdvice { |
| 99 | + private static final String POOL_WAITING = "pool.waiting"; |
| 100 | + |
| 101 | + @Advice.OnMethodEnter(suppress = Throwable.class) |
| 102 | + public static Long onEnter() { |
| 103 | + HikariBlockedTracker.clearBlocked(); |
| 104 | + return System.currentTimeMillis(); |
| 105 | + } |
| 106 | + |
| 107 | + @Advice.OnMethodExit(onThrowable = Throwable.class, suppress = Throwable.class) |
| 108 | + public static void stopSpan(@Advice.This ConcurrentBag thiz, |
| 109 | + @Advice.Enter final Long startTimeMillis, @Advice.Thrown final Throwable throwable) { |
| 110 | + if (HikariBlockedTracker.wasBlocked()) { |
| 111 | + final AgentSpan span = startSpan("hikari", POOL_WAITING, TimeUnit.MILLISECONDS.toMicros(startTimeMillis)); |
| 112 | + final String poolName = InstrumentationContext.get(ConcurrentBag.class, String.class).get(thiz); |
| 113 | + if (poolName != null) { |
| 114 | + span.setTag(DB_POOL_NAME, poolName); |
| 115 | + } |
| 116 | + // XXX should we do anything with the throwable? |
| 117 | + span.finish(); |
| 118 | + } |
| 119 | + HikariBlockedTracker.clearBlocked(); |
| 120 | + } |
| 121 | + } |
| 122 | +} |
0 commit comments