|
| 1 | +package io.arex.inst.httpclient.asynchttpclient; |
| 2 | + |
| 3 | +import io.arex.agent.bootstrap.model.MockResult; |
| 4 | +import io.arex.inst.extension.MethodInstrumentation; |
| 5 | +import io.arex.inst.extension.TypeInstrumentation; |
| 6 | +import io.arex.inst.httpclient.asynchttpclient.listener.AsyncHttpClientConsumer; |
| 7 | +import io.arex.inst.httpclient.asynchttpclient.listener.AsyncHttpClientListenableFuture; |
| 8 | +import io.arex.inst.httpclient.asynchttpclient.wrapper.AsyncHandlerWrapper; |
| 9 | +import io.arex.inst.httpclient.asynchttpclient.wrapper.ResponseWrapper; |
| 10 | +import io.arex.inst.runtime.context.ContextManager; |
| 11 | +import io.arex.inst.runtime.context.RepeatedCollectManager; |
| 12 | +import io.arex.inst.runtime.util.IgnoreUtils; |
| 13 | +import java.util.Collections; |
| 14 | +import java.util.List; |
| 15 | +import net.bytebuddy.asm.Advice.Argument; |
| 16 | +import net.bytebuddy.asm.Advice.Local; |
| 17 | +import net.bytebuddy.asm.Advice.OnMethodEnter; |
| 18 | +import net.bytebuddy.asm.Advice.OnMethodExit; |
| 19 | +import net.bytebuddy.asm.Advice.OnNonDefaultValue; |
| 20 | +import net.bytebuddy.asm.Advice.Return; |
| 21 | +import net.bytebuddy.description.type.TypeDescription; |
| 22 | +import net.bytebuddy.implementation.bytecode.assign.Assigner.Typing; |
| 23 | +import net.bytebuddy.matcher.ElementMatcher; |
| 24 | +import org.asynchttpclient.AsyncHandler; |
| 25 | +import org.asynchttpclient.ListenableFuture; |
| 26 | +import org.asynchttpclient.Request; |
| 27 | + |
| 28 | +import static net.bytebuddy.matcher.ElementMatchers.*; |
| 29 | + |
| 30 | +public class AsyncHttpClientInstrumentation extends TypeInstrumentation { |
| 31 | + |
| 32 | + @Override |
| 33 | + protected ElementMatcher<TypeDescription> typeMatcher() { |
| 34 | + return named("org.asynchttpclient.DefaultAsyncHttpClient"); |
| 35 | + } |
| 36 | + |
| 37 | + @Override |
| 38 | + public List<MethodInstrumentation> methodAdvices() { |
| 39 | + return Collections.singletonList(new MethodInstrumentation(named("execute").and(takesArguments(2)) |
| 40 | + .and(takesArgument(0, named("org.asynchttpclient.Request"))), ExecuteAdvice.class.getName())); |
| 41 | + } |
| 42 | + |
| 43 | + public static class ExecuteAdvice { |
| 44 | + @OnMethodEnter(skipOn = OnNonDefaultValue.class, suppress = Throwable.class) |
| 45 | + public static boolean onEnter(@Argument(0) Request request, |
| 46 | + @Argument(value = 1, readOnly = false) AsyncHandler<?> handler, |
| 47 | + @Local("extractor") AsyncHttpClientExtractor extractor, |
| 48 | + @Local("mockResult") MockResult mockResult) { |
| 49 | + if (IgnoreUtils.excludeOperation(request.getUri().getPath())) { |
| 50 | + return false; |
| 51 | + } |
| 52 | + |
| 53 | + if (ContextManager.needRecord()) { |
| 54 | + RepeatedCollectManager.enter(); |
| 55 | + } |
| 56 | + |
| 57 | + if (ContextManager.needRecordOrReplay()) { |
| 58 | + ResponseWrapper response = new ResponseWrapper(); |
| 59 | + extractor = new AsyncHttpClientExtractor(request, response); |
| 60 | + if (ContextManager.needReplay()) { |
| 61 | + mockResult = extractor.replay(); |
| 62 | + return mockResult != null && mockResult.notIgnoreMockResult(); |
| 63 | + } |
| 64 | + handler = new AsyncHandlerWrapper(handler, response); |
| 65 | + } |
| 66 | + return false; |
| 67 | + } |
| 68 | + |
| 69 | + @OnMethodExit(onThrowable = Throwable.class, suppress = Throwable.class) |
| 70 | + public static void onExit(@Argument(1) AsyncHandler<?> handler, |
| 71 | + @Return(readOnly = false, typing = Typing.DYNAMIC) ListenableFuture future, |
| 72 | + @Local("extractor") AsyncHttpClientExtractor extractor, |
| 73 | + @Local("mockResult") MockResult mockResult) { |
| 74 | + if (extractor == null) { |
| 75 | + return; |
| 76 | + } |
| 77 | + |
| 78 | + if (mockResult != null && mockResult.notIgnoreMockResult()) { |
| 79 | + if (mockResult.getThrowable() != null) { |
| 80 | + future = new AsyncHttpClientListenableFuture(null, mockResult.getThrowable(), handler); |
| 81 | + } else { |
| 82 | + future = new AsyncHttpClientListenableFuture(mockResult.getResult(), null, handler); |
| 83 | + } |
| 84 | + return; |
| 85 | + } |
| 86 | + |
| 87 | + if (ContextManager.needRecord() && RepeatedCollectManager.exitAndValidate()) { |
| 88 | + future.toCompletableFuture().whenComplete(new AsyncHttpClientConsumer(extractor)); |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + } |
| 93 | +} |
0 commit comments