|
| 1 | +package datadog.trace.instrumentation.valkey; |
| 2 | + |
| 3 | +import static datadog.trace.agent.tooling.bytebuddy.matcher.NameMatchers.named; |
| 4 | +import static datadog.trace.bootstrap.instrumentation.api.AgentTracer.activateSpan; |
| 5 | +import static datadog.trace.bootstrap.instrumentation.api.AgentTracer.startSpan; |
| 6 | +import static io.valkey.ValkeyClientDecorator.DECORATE; |
| 7 | +import static net.bytebuddy.matcher.ElementMatchers.isMethod; |
| 8 | +import static net.bytebuddy.matcher.ElementMatchers.isPublic; |
| 9 | +import static net.bytebuddy.matcher.ElementMatchers.takesArgument; |
| 10 | + |
| 11 | +import com.google.auto.service.AutoService; |
| 12 | +import datadog.trace.agent.tooling.Instrumenter; |
| 13 | +import datadog.trace.agent.tooling.InstrumenterModule; |
| 14 | +import datadog.trace.bootstrap.instrumentation.api.AgentScope; |
| 15 | +import datadog.trace.bootstrap.instrumentation.api.AgentSpan; |
| 16 | +import io.valkey.CommandObject; |
| 17 | +import io.valkey.Connection; |
| 18 | +import io.valkey.Protocol; |
| 19 | +import io.valkey.ValkeyClientDecorator; |
| 20 | +import io.valkey.commands.ProtocolCommand; |
| 21 | +import net.bytebuddy.asm.Advice; |
| 22 | + |
| 23 | +@AutoService(InstrumenterModule.class) |
| 24 | +public final class ValkeyInstrumentation extends InstrumenterModule.Tracing |
| 25 | + implements Instrumenter.ForSingleType, Instrumenter.HasMethodAdvice { |
| 26 | + |
| 27 | + public ValkeyInstrumentation() { |
| 28 | + super("valkey"); |
| 29 | + } |
| 30 | + |
| 31 | + @Override |
| 32 | + public String instrumentedType() { |
| 33 | + return "io.valkey.Connection"; |
| 34 | + } |
| 35 | + |
| 36 | + @Override |
| 37 | + public String[] helperClassNames() { |
| 38 | + return new String[] { |
| 39 | + "io.valkey.ValkeyClientDecorator", |
| 40 | + }; |
| 41 | + } |
| 42 | + |
| 43 | + @Override |
| 44 | + public void methodAdvice(MethodTransformer transformer) { |
| 45 | + transformer.applyAdvice( |
| 46 | + isMethod() |
| 47 | + .and(isPublic()) |
| 48 | + .and(named("executeCommand")) |
| 49 | + .and(takesArgument(0, named("io.valkey.CommandObject"))), |
| 50 | + ValkeyInstrumentation.class.getName() + "$ValkeyAdvice"); |
| 51 | + } |
| 52 | + |
| 53 | + public static class ValkeyAdvice { |
| 54 | + |
| 55 | + @Advice.OnMethodEnter(suppress = Throwable.class) |
| 56 | + public static AgentScope onEnter( |
| 57 | + @Advice.Argument(0) final CommandObject<?> commandObject, |
| 58 | + @Advice.This final Connection thiz) { |
| 59 | + final AgentSpan span = startSpan("valkey", ValkeyClientDecorator.OPERATION_NAME); |
| 60 | + DECORATE.afterStart(span); |
| 61 | + DECORATE.onConnection(span, thiz); |
| 62 | + |
| 63 | + final ProtocolCommand command = commandObject.getArguments().getCommand(); |
| 64 | + |
| 65 | + if (command instanceof Protocol.Command) { |
| 66 | + DECORATE.onStatement(span, ((Protocol.Command) command).name()); |
| 67 | + } else { |
| 68 | + DECORATE.onStatement(span, new String(command.getRaw())); |
| 69 | + } |
| 70 | + return activateSpan(span); |
| 71 | + } |
| 72 | + |
| 73 | + @Advice.OnMethodExit(onThrowable = Throwable.class, suppress = Throwable.class) |
| 74 | + public static void stopSpan( |
| 75 | + @Advice.Enter final AgentScope scope, @Advice.Thrown final Throwable throwable) { |
| 76 | + DECORATE.onError(scope.span(), throwable); |
| 77 | + DECORATE.beforeFinish(scope.span()); |
| 78 | + scope.close(); |
| 79 | + scope.span().finish(); |
| 80 | + } |
| 81 | + } |
| 82 | +} |
0 commit comments