-
Notifications
You must be signed in to change notification settings - Fork 100
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: support com.ning.httpclient (#512)
- Loading branch information
Showing
24 changed files
with
1,091 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
...namic/common/listener/DirectExecutor.java → ...inst/runtime/listener/DirectExecutor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
18 changes: 18 additions & 0 deletions
18
arex-instrumentation-api/src/test/java/io/arex/inst/runtime/listener/DirectExecutorTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package io.arex.inst.runtime.listener; | ||
|
||
import org.junit.jupiter.api.Test; | ||
|
||
import java.util.concurrent.Executor; | ||
|
||
import static org.junit.jupiter.api.Assertions.*; | ||
|
||
class DirectExecutorTest { | ||
|
||
@Test | ||
void execute() { | ||
Executor executor = DirectExecutor.INSTANCE; | ||
final boolean[] ran = {false}; | ||
executor.execute(() -> ran[0] = true); | ||
assertTrue(ran[0]); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
28 changes: 28 additions & 0 deletions
28
arex-instrumentation/httpclient/arex-httpclient-ning/pom.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<project xmlns="http://maven.apache.org/POM/4.0.0" | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
<parent> | ||
<groupId>io.arex</groupId> | ||
<artifactId>arex-instrumentation-parent</artifactId> | ||
<version>${revision}</version> | ||
<relativePath>../../pom.xml</relativePath> | ||
</parent> | ||
<modelVersion>4.0.0</modelVersion> | ||
<artifactId>arex-httpclient-ning</artifactId> | ||
<dependencies> | ||
<dependency> | ||
<groupId>${project.groupId}</groupId> | ||
<artifactId>arex-httpclient-common</artifactId> | ||
<version>${project.version}</version> | ||
<scope>compile</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>com.ning</groupId> | ||
<artifactId>async-http-client</artifactId> | ||
<version>1.9.39</version> | ||
<scope>provided</scope> | ||
</dependency> | ||
</dependencies> | ||
|
||
</project> |
21 changes: 21 additions & 0 deletions
21
...ient-ning/src/main/java/io/arex/inst/httpclient/AsyncHttpClientModuleInstrumentation.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package io.arex.inst.httpclient; | ||
|
||
import com.google.auto.service.AutoService; | ||
import io.arex.inst.extension.ModuleInstrumentation; | ||
import io.arex.inst.extension.TypeInstrumentation; | ||
import io.arex.inst.httpclient.ning.AsyncHttpClientInstrumentation; | ||
|
||
import java.util.Collections; | ||
import java.util.List; | ||
|
||
@AutoService(ModuleInstrumentation.class) | ||
public class AsyncHttpClientModuleInstrumentation extends ModuleInstrumentation { | ||
public AsyncHttpClientModuleInstrumentation() { | ||
super("arex-httpclient-ning"); | ||
} | ||
|
||
@Override | ||
public List<TypeInstrumentation> instrumentationTypes() { | ||
return Collections.singletonList(new AsyncHttpClientInstrumentation()); | ||
} | ||
} |
81 changes: 81 additions & 0 deletions
81
...lient-ning/src/main/java/io/arex/inst/httpclient/ning/AsyncHttpClientInstrumentation.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
package io.arex.inst.httpclient.ning; | ||
|
||
import com.ning.http.client.ListenableFuture; | ||
import com.ning.http.client.Request; | ||
import com.ning.http.client.Response; | ||
import io.arex.agent.bootstrap.model.MockResult; | ||
import io.arex.inst.extension.MethodInstrumentation; | ||
import io.arex.inst.extension.TypeInstrumentation; | ||
import io.arex.inst.httpclient.common.HttpClientAdapter; | ||
import io.arex.inst.httpclient.common.HttpClientExtractor; | ||
import io.arex.inst.runtime.context.ContextManager; | ||
import io.arex.inst.runtime.context.RepeatedCollectManager; | ||
import io.arex.inst.runtime.listener.DirectExecutor; | ||
import io.arex.inst.runtime.util.IgnoreUtils; | ||
import net.bytebuddy.asm.Advice; | ||
import net.bytebuddy.description.type.TypeDescription; | ||
import net.bytebuddy.implementation.bytecode.assign.Assigner; | ||
import net.bytebuddy.matcher.ElementMatcher; | ||
|
||
import java.util.Collections; | ||
import java.util.List; | ||
|
||
import static net.bytebuddy.matcher.ElementMatchers.*; | ||
|
||
public class AsyncHttpClientInstrumentation extends TypeInstrumentation { | ||
@Override | ||
protected ElementMatcher<TypeDescription> typeMatcher() { | ||
return named("com.ning.http.client.AsyncHttpClient"); | ||
} | ||
|
||
@Override | ||
public List<MethodInstrumentation> methodAdvices() { | ||
return Collections.singletonList(new MethodInstrumentation( | ||
isMethod().and(named("executeRequest").and(takesArguments(2))), ExecuteRequestAdvice.class.getName())); | ||
} | ||
|
||
public static class ExecuteRequestAdvice { | ||
@Advice.OnMethodEnter(suppress = Throwable.class, skipOn = Advice.OnNonDefaultValue.class) | ||
public static boolean onEnter(@Advice.Argument(0) Request request, | ||
@Advice.Local("mockResult") MockResult mockResult, | ||
@Advice.Local("extractor") HttpClientExtractor<Request, Object> extractor){ | ||
if (IgnoreUtils.excludeOperation(request.getUri().getPath())) { | ||
return false; | ||
} | ||
if (ContextManager.needRecord()) { | ||
RepeatedCollectManager.enter(); | ||
} | ||
if (ContextManager.needRecordOrReplay()) { | ||
HttpClientAdapter<Request, Object> adapter = new NingHttpClientAdapter(request); | ||
extractor = new HttpClientExtractor<>(adapter); | ||
if (ContextManager.needReplay()) { | ||
mockResult = extractor.replay(); | ||
return mockResult != null && mockResult.notIgnoreMockResult(); | ||
} | ||
} | ||
return false; | ||
} | ||
|
||
@Advice.OnMethodExit(suppress = Throwable.class, onThrowable = Throwable.class) | ||
public static void onExit(@Advice.Local("mockResult") MockResult mockResult, | ||
@Advice.Local("extractor") HttpClientExtractor<Request, Object> extractor, | ||
@Advice.Return(readOnly = false) ListenableFuture responseFuture, | ||
@Advice.Thrown(readOnly = false) Throwable throwable) { | ||
if (mockResult != null && mockResult.notIgnoreMockResult()) { | ||
if (mockResult.getThrowable() != null) { | ||
throwable = mockResult.getThrowable(); | ||
} else { | ||
responseFuture = new ResponseFutureWrapper(mockResult.getResult()); | ||
} | ||
return; | ||
} | ||
if (ContextManager.needRecord() && RepeatedCollectManager.exitAndValidate() && extractor != null) { | ||
if (throwable != null) { | ||
extractor.record(throwable); | ||
} else { | ||
responseFuture.addListener(new ResponseFutureListener(extractor, responseFuture), DirectExecutor.INSTANCE); | ||
} | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.