|
| 1 | +package io.opentelemetry.javaagent.instrumentation.jsonrpc4j.v1_6; |
| 2 | + |
| 3 | +import static io.opentelemetry.javaagent.bootstrap.Java8BytecodeBridge.currentContext; |
| 4 | +import static io.opentelemetry.javaagent.extension.matcher.AgentElementMatchers.hasClassesNamed; |
| 5 | +import static io.opentelemetry.javaagent.extension.matcher.AgentElementMatchers.implementsInterface; |
| 6 | +import static net.bytebuddy.matcher.ElementMatchers.isMethod; |
| 7 | +import static net.bytebuddy.matcher.ElementMatchers.named; |
| 8 | +import static net.bytebuddy.matcher.ElementMatchers.returns; |
| 9 | +import static net.bytebuddy.matcher.ElementMatchers.takesArgument; |
| 10 | +import static net.bytebuddy.matcher.ElementMatchers.takesArguments; |
| 11 | + |
| 12 | +import io.opentelemetry.context.Context; |
| 13 | +import io.opentelemetry.context.Scope; |
| 14 | +import io.opentelemetry.instrumentation.jsonrpc4j.v1_6.SimpleJsonRpcRequest; |
| 15 | +import io.opentelemetry.instrumentation.jsonrpc4j.v1_6.SimpleJsonRpcResponse; |
| 16 | +import io.opentelemetry.javaagent.extension.instrumentation.TypeInstrumentation; |
| 17 | +import io.opentelemetry.javaagent.extension.instrumentation.TypeTransformer; |
| 18 | +import net.bytebuddy.asm.Advice; |
| 19 | +import net.bytebuddy.description.type.TypeDescription; |
| 20 | +import net.bytebuddy.matcher.ElementMatcher; |
| 21 | + |
| 22 | +public class JsonRpcClientBuilderInstrumentation implements TypeInstrumentation { |
| 23 | + |
| 24 | + @Override |
| 25 | + public ElementMatcher<ClassLoader> classLoaderOptimization() { |
| 26 | + return hasClassesNamed("com.googlecode.jsonrpc4j.IJsonRpcClient"); |
| 27 | + } |
| 28 | + |
| 29 | + @Override |
| 30 | + public ElementMatcher<TypeDescription> typeMatcher() { |
| 31 | + // match JsonRpcHttpClient and JsonRpcRestClient |
| 32 | + return implementsInterface(named("com.googlecode.jsonrpc4j.IJsonRpcClient")); |
| 33 | + } |
| 34 | + |
| 35 | + @Override |
| 36 | + public void transform(TypeTransformer transformer) { |
| 37 | + transformer.applyAdviceToMethod( |
| 38 | + isMethod() |
| 39 | + .and(named("invoke")) |
| 40 | + .and(takesArguments(4)) |
| 41 | + .and(takesArgument(0, String.class)) |
| 42 | + .and(takesArgument(1, Object.class)) |
| 43 | + .and(takesArgument(2, named("java.lang.reflect.Type"))) |
| 44 | + .and(takesArgument(3, named("java.util.Map"))) |
| 45 | + .and(returns(Object.class)), |
| 46 | + this.getClass().getName() + "$InvokeAdvice"); |
| 47 | + } |
| 48 | + |
| 49 | + @SuppressWarnings("unused") |
| 50 | + public static class InvokeAdvice { |
| 51 | + |
| 52 | + @Advice.OnMethodEnter(suppress = Throwable.class) |
| 53 | + public static void onEnter( |
| 54 | + @Advice.Argument(0) String methodName, |
| 55 | + @Advice.Argument(1) Object argument, |
| 56 | + @Advice.Local("otelContext") Context context, |
| 57 | + @Advice.Local("otelScope") Scope scope) { |
| 58 | + Context parentContext = currentContext(); |
| 59 | + SimpleJsonRpcRequest request = new SimpleJsonRpcRequest( |
| 60 | + methodName, |
| 61 | + argument |
| 62 | + ); |
| 63 | + if (!JsonRpcSingletons.CLIENT_INSTRUMENTER.shouldStart(parentContext, request)) { |
| 64 | + return; |
| 65 | + } |
| 66 | + |
| 67 | + context = JsonRpcSingletons.CLIENT_INSTRUMENTER.start(parentContext, request); |
| 68 | + scope = context.makeCurrent(); |
| 69 | + } |
| 70 | + |
| 71 | + @Advice.OnMethodExit(onThrowable = Throwable.class, suppress = Throwable.class) |
| 72 | + public static void onExit( |
| 73 | + @Advice.Argument(0) String methodName, |
| 74 | + @Advice.Argument(1) Object argument, |
| 75 | + @Advice.Return Object result, |
| 76 | + @Advice.Thrown Throwable throwable, |
| 77 | + @Advice.Local("otelContext") Context context, |
| 78 | + @Advice.Local("otelScope") Scope scope) { |
| 79 | + if (scope == null) { |
| 80 | + return; |
| 81 | + } |
| 82 | + |
| 83 | + scope.close(); |
| 84 | + JsonRpcSingletons.CLIENT_INSTRUMENTER.end(context, new SimpleJsonRpcRequest(methodName, argument), new SimpleJsonRpcResponse(result), throwable); |
| 85 | + } |
| 86 | + } |
| 87 | +} |
0 commit comments