|
| 1 | +/* |
| 2 | + * Copyright The OpenTelemetry Authors |
| 3 | + * SPDX-License-Identifier: Apache-2.0 |
| 4 | + */ |
| 5 | + |
| 6 | +package io.opentelemetry.javaagent.instrumentation.clickhouse; |
| 7 | + |
| 8 | +import static io.opentelemetry.javaagent.bootstrap.Java8BytecodeBridge.currentContext; |
| 9 | +import static io.opentelemetry.javaagent.extension.matcher.AgentElementMatchers.implementsInterface; |
| 10 | +import static io.opentelemetry.javaagent.instrumentation.clickhouse.ClickHouseSingletons.instrumenter; |
| 11 | +import static net.bytebuddy.matcher.ElementMatchers.isMethod; |
| 12 | +import static net.bytebuddy.matcher.ElementMatchers.named; |
| 13 | +import static net.bytebuddy.matcher.ElementMatchers.namedOneOf; |
| 14 | +import static net.bytebuddy.matcher.ElementMatchers.takesArgument; |
| 15 | + |
| 16 | +import com.clickhouse.client.ClickHouseClient; |
| 17 | +import com.clickhouse.client.ClickHouseRequest; |
| 18 | +import io.opentelemetry.context.Context; |
| 19 | +import io.opentelemetry.context.Scope; |
| 20 | +import io.opentelemetry.javaagent.bootstrap.CallDepth; |
| 21 | +import io.opentelemetry.javaagent.extension.instrumentation.TypeInstrumentation; |
| 22 | +import io.opentelemetry.javaagent.extension.instrumentation.TypeTransformer; |
| 23 | +import net.bytebuddy.asm.Advice; |
| 24 | +import net.bytebuddy.description.type.TypeDescription; |
| 25 | +import net.bytebuddy.matcher.ElementMatcher; |
| 26 | + |
| 27 | +public class ClickHouseClientInstrumentation implements TypeInstrumentation { |
| 28 | + @Override |
| 29 | + public ElementMatcher<TypeDescription> typeMatcher() { |
| 30 | + return implementsInterface(named("com.clickhouse.client.ClickHouseClient")); |
| 31 | + } |
| 32 | + |
| 33 | + @Override |
| 34 | + public void transform(TypeTransformer transformer) { |
| 35 | + transformer.applyAdviceToMethod( |
| 36 | + isMethod() |
| 37 | + .and(namedOneOf("executeAndWait", "execute")) |
| 38 | + .and(takesArgument(0, named("com.clickhouse.client.ClickHouseRequest"))), |
| 39 | + this.getClass().getName() + "$ClickHouseExecuteAndWaitAdvice"); |
| 40 | + } |
| 41 | + |
| 42 | + @SuppressWarnings("unused") |
| 43 | + public static class ClickHouseExecuteAndWaitAdvice { |
| 44 | + @Advice.OnMethodEnter(suppress = Throwable.class) |
| 45 | + public static void onEnter( |
| 46 | + @Advice.Argument(0) ClickHouseRequest<?> clickHouseRequest, |
| 47 | + @Advice.Local("otelContext") Context context, |
| 48 | + @Advice.Local("otelScope") Scope scope, |
| 49 | + @Advice.Local("otelCallDepth") CallDepth callDepth) { |
| 50 | + |
| 51 | + callDepth = CallDepth.forClass(ClickHouseClient.class); |
| 52 | + if (callDepth.getAndIncrement() > 0) { |
| 53 | + return; |
| 54 | + } |
| 55 | + |
| 56 | + if (clickHouseRequest == null) { |
| 57 | + return; |
| 58 | + } |
| 59 | + |
| 60 | + Context parentContext = currentContext(); |
| 61 | + |
| 62 | + ClickHouseDbRequest request = |
| 63 | + ClickHouseDbRequest.create( |
| 64 | + clickHouseRequest.getServer().getHost(), |
| 65 | + clickHouseRequest.getServer().getPort(), |
| 66 | + clickHouseRequest.getServer().getDatabase().get(), |
| 67 | + clickHouseRequest.getPreparedQuery().getOriginalQuery()); |
| 68 | + |
| 69 | + if (!instrumenter().shouldStart(parentContext, request)) { |
| 70 | + return; |
| 71 | + } |
| 72 | + |
| 73 | + context = instrumenter().start(parentContext, request); |
| 74 | + scope = context.makeCurrent(); |
| 75 | + } |
| 76 | + |
| 77 | + @Advice.OnMethodExit(onThrowable = Throwable.class, suppress = Throwable.class) |
| 78 | + public static void onExit( |
| 79 | + @Advice.Thrown Throwable throwable, |
| 80 | + @Advice.Local("otelRequest") ClickHouseDbRequest clickHouseRequest, |
| 81 | + @Advice.Local("otelContext") Context context, |
| 82 | + @Advice.Local("otelScope") Scope scope, |
| 83 | + @Advice.Local("otelCallDepth") CallDepth callDepth) { |
| 84 | + |
| 85 | + if (callDepth.decrementAndGet() > 0) { |
| 86 | + return; |
| 87 | + } |
| 88 | + |
| 89 | + if (scope == null) { |
| 90 | + return; |
| 91 | + } |
| 92 | + |
| 93 | + scope.close(); |
| 94 | + instrumenter().end(context, clickHouseRequest, null, throwable); |
| 95 | + } |
| 96 | + } |
| 97 | +} |
0 commit comments