|
| 1 | +/* |
| 2 | + * Copyright The OpenTelemetry Authors |
| 3 | + * SPDX-License-Identifier: Apache-2.0 |
| 4 | + */ |
| 5 | + |
| 6 | +package io.opentelemetry.javaagent.instrumentation.vertx.v4_0.redis; |
| 7 | + |
| 8 | +import static io.opentelemetry.javaagent.bootstrap.Java8BytecodeBridge.currentContext; |
| 9 | +import static io.opentelemetry.javaagent.instrumentation.vertx.v4_0.redis.VertxRedisClientSingletons.instrumenter; |
| 10 | +import static net.bytebuddy.matcher.ElementMatchers.isConstructor; |
| 11 | +import static net.bytebuddy.matcher.ElementMatchers.named; |
| 12 | + |
| 13 | +import io.opentelemetry.context.Context; |
| 14 | +import io.opentelemetry.context.Scope; |
| 15 | +import io.opentelemetry.javaagent.extension.instrumentation.TypeInstrumentation; |
| 16 | +import io.opentelemetry.javaagent.extension.instrumentation.TypeTransformer; |
| 17 | +import io.vertx.core.Future; |
| 18 | +import io.vertx.core.net.NetSocket; |
| 19 | +import io.vertx.redis.client.Request; |
| 20 | +import io.vertx.redis.client.Response; |
| 21 | +import io.vertx.redis.client.impl.RedisStandaloneConnection; |
| 22 | +import io.vertx.redis.client.impl.RedisURI; |
| 23 | +import io.vertx.redis.client.impl.RequestUtil; |
| 24 | +import net.bytebuddy.asm.Advice; |
| 25 | +import net.bytebuddy.description.type.TypeDescription; |
| 26 | +import net.bytebuddy.matcher.ElementMatcher; |
| 27 | + |
| 28 | +public class RedisStandaloneConnectionInstrumentation implements TypeInstrumentation { |
| 29 | + @Override |
| 30 | + public ElementMatcher<TypeDescription> typeMatcher() { |
| 31 | + return named("io.vertx.redis.client.impl.RedisStandaloneConnection"); |
| 32 | + } |
| 33 | + |
| 34 | + @Override |
| 35 | + public void transform(TypeTransformer transformer) { |
| 36 | + transformer.applyAdviceToMethod(named("send"), this.getClass().getName() + "$SendAdvice"); |
| 37 | + transformer.applyAdviceToMethod( |
| 38 | + isConstructor(), this.getClass().getName() + "$ConstructorAdvice"); |
| 39 | + } |
| 40 | + |
| 41 | + @SuppressWarnings("unused") |
| 42 | + public static class SendAdvice { |
| 43 | + @Advice.OnMethodEnter(suppress = Throwable.class) |
| 44 | + public static void onEnter( |
| 45 | + @Advice.This RedisStandaloneConnection connection, |
| 46 | + @Advice.Argument(0) Request request, |
| 47 | + @Advice.FieldValue("netSocket") NetSocket netSocket, |
| 48 | + @Advice.Local("otelRequest") VertxRedisClientRequest otelRequest, |
| 49 | + @Advice.Local("otelContext") Context context, |
| 50 | + @Advice.Local("otelScope") Scope scope) { |
| 51 | + if (request == null) { |
| 52 | + return; |
| 53 | + } |
| 54 | + |
| 55 | + String commandName = VertxRedisClientSingletons.getCommandName(request.command()); |
| 56 | + RedisURI redisUri = VertxRedisClientSingletons.getRedisUri(connection); |
| 57 | + if (commandName == null || redisUri == null) { |
| 58 | + return; |
| 59 | + } |
| 60 | + |
| 61 | + otelRequest = |
| 62 | + new VertxRedisClientRequest( |
| 63 | + commandName, RequestUtil.getArgs(request), redisUri, netSocket); |
| 64 | + Context parentContext = currentContext(); |
| 65 | + if (!instrumenter().shouldStart(parentContext, otelRequest)) { |
| 66 | + return; |
| 67 | + } |
| 68 | + |
| 69 | + context = instrumenter().start(parentContext, otelRequest); |
| 70 | + scope = context.makeCurrent(); |
| 71 | + } |
| 72 | + |
| 73 | + @Advice.OnMethodExit(onThrowable = Throwable.class, suppress = Throwable.class) |
| 74 | + public static void onExit( |
| 75 | + @Advice.Thrown Throwable throwable, |
| 76 | + @Advice.Return(readOnly = false) Future<Response> responseFuture, |
| 77 | + @Advice.Local("otelRequest") VertxRedisClientRequest otelRequest, |
| 78 | + @Advice.Local("otelContext") Context context, |
| 79 | + @Advice.Local("otelScope") Scope scope) { |
| 80 | + if (scope == null) { |
| 81 | + return; |
| 82 | + } |
| 83 | + |
| 84 | + scope.close(); |
| 85 | + if (throwable != null) { |
| 86 | + instrumenter().end(context, otelRequest, null, throwable); |
| 87 | + } else { |
| 88 | + responseFuture = |
| 89 | + VertxRedisClientSingletons.wrapEndSpan(responseFuture, context, otelRequest); |
| 90 | + } |
| 91 | + } |
| 92 | + } |
| 93 | + |
| 94 | + @SuppressWarnings("unused") |
| 95 | + public static class ConstructorAdvice { |
| 96 | + @Advice.OnMethodExit(suppress = Throwable.class) |
| 97 | + public static void onExit(@Advice.This RedisStandaloneConnection connection) { |
| 98 | + // used in 4.1.0, for 4.0.0 it is set in RedisConnectionProviderInstrumentation |
| 99 | + VertxRedisClientSingletons.setRedisUri( |
| 100 | + connection, VertxRedisClientSingletons.getRedisUriThreadLocal()); |
| 101 | + } |
| 102 | + } |
| 103 | +} |
0 commit comments