1.修复api拦截器,异常没有正常抛出的BUG。

2.提交基于httpClient 4.x的插件代码。暂未完成所有功能。完成4.3下的测试,4.2待测试。
This commit is contained in:
wusheng 2016-03-16 17:07:57 +08:00
parent 80da19e610
commit 09fd93c5a8
16 changed files with 395 additions and 14 deletions

View File

@ -51,11 +51,11 @@ public class ClassMethodInterceptor {
} catch(Throwable t){
try {
interceptor.handleMethodException(t, instanceContext, interceptorContext, ret);
throw t;
} catch (Throwable t2) {
logger.error("class[{}] handle method[{}] exception failue:{}",
obj.getClass(), method.getName(), t2.getMessage(), t2);
}
throw t;
}finally {
try {
ret = interceptor.afterMethod(instanceContext, interceptorContext, ret);

View File

@ -15,6 +15,7 @@ import net.bytebuddy.implementation.MethodDelegation;
import net.bytebuddy.implementation.SuperMethodCall;
import net.bytebuddy.implementation.bind.annotation.FieldProxy;
import net.bytebuddy.pool.TypePool;
import net.bytebuddy.pool.TypePool.Resolution;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
@ -59,6 +60,14 @@ public class EnhanceClazz4Interceptor {
logger.debug("prepare to enhance class {} by {}.",
enhanceOriginClassName, interceptorDefineClassName);
Resolution resolution = typePool.describe(enhanceOriginClassName);
if(!resolution.isResolved()){
logger.warn("class {} can't be resolved, enhance by {} failue.",
enhanceOriginClassName, interceptorDefineClassName);
return;
}
/**
* rename origin class <br/>
* add '$$Origin' at the end of be enhanced classname <br/>
@ -67,7 +76,7 @@ public class EnhanceClazz4Interceptor {
*/
String renameClassName = enhanceOriginClassName + "$$Origin";
Class<?> originClass = new ByteBuddy()
.redefine(typePool.describe(enhanceOriginClassName).resolve(),
.redefine(resolution.resolve(),
ClassFileLocator.ForClassLoader.ofClassPath())
.name(renameClassName)
.make()

View File

@ -14,6 +14,7 @@ public class SimulateMain {
public static void main(String[] args) throws NoSuchFieldException,
SecurityException, InstantiationException, IllegalAccessException, ClassNotFoundException {
TypePool typePool = TypePool.Default.ofClassPath();
System.out.println(typePool.describe("test.ai.cloud.bytebuddy.TestClass").isResolved());
Class<?> newClazz = new ByteBuddy()
.redefine(

View File

@ -33,6 +33,7 @@
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.3</version>
<scope>compile</scope>
</dependency>
</dependencies>
<build>

View File

@ -1,6 +1,5 @@
<?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">
<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">
<modelVersion>4.0.0</modelVersion>
<groupId>com.ai.cloud</groupId>
@ -14,7 +13,8 @@
<module>web-plugin</module>
<module>httpclient-4.2.x-plugin</module>
<module>httpclient-4.3.x-plugin</module>
</modules>
<module>skywalking-httpClient-4.x-plugin</module>
</modules>
<packaging>pom</packaging>
<name>skywalking-sdk-plugin</name>
@ -53,13 +53,4 @@
</plugin>
</plugins>
</build>
<!--发布 -->
<distributionManagement>
<snapshotRepository>
<id>company-private-nexus-library-snapshots</id>
<name>company-private-nexus-library-snapshots</name>
<url>http://10.1.228.199:18081/nexus/content/repositories/snapshots/</url>
</snapshotRepository>
</distributionManagement>
</project>

View File

@ -0,0 +1,67 @@
<?xml version="1.0"?>
<project
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.ai.cloud</groupId>
<artifactId>skywalking-sdk-plugin</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
<artifactId>skywalking-httpClient-4.x-plugin</artifactId>
<name>skywalking-httpClient-4.x-plugin</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>com.ai.cloud</groupId>
<artifactId>skywalking-api</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.3</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.4.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.7</source>
<target>1.7</target>
<encoding>${project.build.sourceEncoding}</encoding>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<version>2.4.3</version>
<configuration>
<encoding>${project.build.sourceEncoding}</encoding>
</configuration>
</plugin>
</plugins>
</build>
</project>

View File

@ -0,0 +1,76 @@
package org.skywalking.httpClient.v4.plugin;
import org.apache.http.HttpHost;
import org.apache.http.HttpRequest;
import com.ai.cloud.skywalking.buriedpoint.RPCBuriedPointSender;
import com.ai.cloud.skywalking.model.Identification;
import com.ai.cloud.skywalking.plugin.interceptor.ConstructorInvokeContext;
import com.ai.cloud.skywalking.plugin.interceptor.EnhancedClassInstanceContext;
import com.ai.cloud.skywalking.plugin.interceptor.IAroundInterceptor;
import com.ai.cloud.skywalking.plugin.interceptor.MethodInvokeContext;
public class HttpClientExecuteInterceptor implements IAroundInterceptor {
/**
* TODO: need laod from config
*/
public static String traceHearName = "";
private static RPCBuriedPointSender sender = new RPCBuriedPointSender();
@Override
public void onConstruct(EnhancedClassInstanceContext context,
ConstructorInvokeContext interceptorContext) {
}
@Override
public void beforeMethod(EnhancedClassInstanceContext context,
MethodInvokeContext interceptorContext) {
Object[] allArguments = interceptorContext.allArguments();
if (allArguments[0] == null || allArguments[1] == null) {
// illegal args, can't trace. ignore.
return;
}
HttpHost httpHost = (HttpHost) allArguments[0];
HttpRequest httpRequest = (HttpRequest) allArguments[1];
httpRequest
.setHeader(
traceHearName,
"ContextData="
+ sender.beforeSend(
Identification
.newBuilder()
.viewPoint(
httpHost.toURI()
.toString())
.spanType(
WebBuriedPointType
.instance())
.build()).toString());
}
@Override
public Object afterMethod(EnhancedClassInstanceContext context,
MethodInvokeContext interceptorContext, Object ret) {
Object[] allArguments = interceptorContext.allArguments();
if (allArguments[0] == null || allArguments[1] == null) {
// illegal args, can't trace. ignore.
return ret;
}
sender.afterSend();
return ret;
}
@Override
public void handleMethodException(Throwable t,
EnhancedClassInstanceContext context,
MethodInvokeContext interceptorContext, Object ret) {
Object[] allArguments = interceptorContext.allArguments();
if (allArguments[0] == null || allArguments[1] == null) {
// illegal args, can't trace. ignore.
return;
}
sender.handleException(t);
}
}

View File

@ -0,0 +1,31 @@
package org.skywalking.httpClient.v4.plugin;
import com.ai.cloud.skywalking.api.IBuriedPointType;
import com.ai.cloud.skywalking.protocol.CallType;
public class WebBuriedPointType implements IBuriedPointType {
private static WebBuriedPointType webBuriedPointType;
public static IBuriedPointType instance() {
if (webBuriedPointType == null) {
webBuriedPointType = new WebBuriedPointType();
}
return webBuriedPointType;
}
@Override
public String getTypeName() {
return "W";
}
@Override
public CallType getCallType() {
return CallType.SYNC;
}
private WebBuriedPointType() {
// Non
}
}

View File

@ -0,0 +1,32 @@
package org.skywalking.httpClient.v4.plugin.define;
import io.netty.handler.codec.http.HttpRequest;
import javax.xml.ws.spi.http.HttpContext;
import org.apache.http.HttpHost;
import com.ai.cloud.skywalking.plugin.interceptor.InterceptPoint;
public class AbstractHttpClientPluginDefine extends HttpClientPluginDefine {
@Override
public String getBeInterceptedClassName() {
return "org.apache.http.impl.client.AbstractHttpClient";
}
/**
* version 4.2, intercept method: execute, intercept<br/>
* public final HttpResponse execute(HttpHost target, HttpRequest request,
* HttpContext context)<br/>
*
* since version 4.3intercept method: doExecute<br/>
*/
@Override
public InterceptPoint[] getBeInterceptedMethods() {
return new InterceptPoint[] {
new InterceptPoint("doExecute"),
new InterceptPoint("execute", HttpHost.class,
HttpRequest.class, HttpContext.class) };
}
}

View File

@ -0,0 +1,15 @@
package org.skywalking.httpClient.v4.plugin.define;
import org.skywalking.httpClient.v4.plugin.HttpClientExecuteInterceptor;
import com.ai.cloud.skywalking.plugin.interceptor.IAroundInterceptor;
import com.ai.cloud.skywalking.plugin.interceptor.InterceptorDefine;
public abstract class HttpClientPluginDefine implements InterceptorDefine {
@Override
public IAroundInterceptor instance() {
return new HttpClientExecuteInterceptor();
}
}

View File

@ -0,0 +1,16 @@
package org.skywalking.httpClient.v4.plugin.define;
import com.ai.cloud.skywalking.plugin.interceptor.InterceptPoint;
public class InternalHttpClientPluginDefine extends HttpClientPluginDefine {
@Override
public InterceptPoint[] getBeInterceptedMethods() {
return new InterceptPoint[]{new InterceptPoint("doExecute")};
}
@Override
public String getBeInterceptedClassName() {
return "org.apache.http.impl.client.InternalHttpClient";
}
}

View File

@ -0,0 +1,16 @@
package org.skywalking.httpClient.v4.plugin.define;
import com.ai.cloud.skywalking.plugin.interceptor.InterceptPoint;
public class MinimalHttpClientPluginDefine extends HttpClientPluginDefine {
@Override
public InterceptPoint[] getBeInterceptedMethods() {
return new InterceptPoint[]{new InterceptPoint("doExecute")};
}
@Override
public String getBeInterceptedClassName() {
return "org.apache.http.impl.client.MinimalHttpClient";
}
}

View File

@ -0,0 +1,3 @@
org.skywalking.httpClient.v4.plugin.define.AbstractHttpClientPluginDefine
org.skywalking.httpClient.v4.plugin.define.InternalHttpClientPluginDefine
org.skywalking.httpClient.v4.plugin.define.MinimalHttpClientPluginDefine

View File

@ -0,0 +1,60 @@
package org.skywalking.httpClient.v4.plugin;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.sql.SQLException;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.util.EntityUtils;
import org.junit.Test;
import com.ai.cloud.skywalking.plugin.TracingBootstrap;
public class TestHttpClientV43 {
@Test
public void testsql() throws IllegalAccessException,
IllegalArgumentException, InvocationTargetException,
NoSuchMethodException, SecurityException, ClassNotFoundException {
TracingBootstrap
.main(new String[] { "org.skywalking.httpClient.v4.plugin.TestHttpClientV43" });
}
public static void main(String[] args) throws ClassNotFoundException,
SQLException, InterruptedException {
HttpClientBuilder httpClientBuilder = HttpClientBuilder.create();
// HttpClient
CloseableHttpClient closeableHttpClient = httpClientBuilder.build();
HttpGet httpGet = new HttpGet("http://www.baidu.com");
System.out.println(httpGet.getRequestLine());
try {
// 执行get请求
HttpResponse httpResponse = closeableHttpClient.execute(httpGet);
// 获取响应消息实体
HttpEntity entity = httpResponse.getEntity();
// 响应状态
System.out.println("status:" + httpResponse.getStatusLine());
// 判断响应实体是否为空
if (entity != null) {
System.out.println("contentEncoding:"
+ entity.getContentEncoding());
System.out.println("response content:"
+ EntityUtils.toString(entity));
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try { // 关闭流并释放资源
closeableHttpClient.close();
} catch (IOException e) {
e.printStackTrace();
}
}
Thread.sleep(5*1000);
}
}

View File

@ -0,0 +1,16 @@
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="error">
<Appenders>
<Console name="Console" target="SYSTEM_OUT">
<PatternLayout pattern="%d [%t](%F:%L) %-5level %logger{36} - %msg%n" />
</Console>
<Console name="Console2" target="SYSTEM_OUT">
<PatternLayout pattern="%d [%t](%F:%L) %-5level %logger{36} - %msg%n" />
</Console>
</Appenders>
<Loggers>
<Root level="debug">
<AppenderRef ref="Console" />
</Root>
</Loggers>
</Configuration>

View File

@ -0,0 +1,47 @@
#skyWalking用户ID
skywalking.user_id=123
#skyWalking应用编码
skywalking.application_code=test
#skywalking auth的环境变量名字
skywalking.auth_system_env_name=SKYWALKING_RUN
#skywalking数据编码
skywalking.charset=UTF-8
#是否打印数据
buriedpoint.printf=true
#埋点异常的最大长度
buriedpoint.max_exception_stack_length=4000
#业务字段的最大长度
buriedpoint.businesskey_max_length=300
#过滤异常
buriedpoint.exclusive_exceptions=java.lang.RuntimeException
#最大发送者的连接数阀比例
sender.connect_percent=100
#发送服务端配置
sender.servers_addr=127.0.0.1:34000
#最大发送的副本数量
sender.max_copy_num=2
#发送的最大长度
sender.max_send_length=20000
#当没有Sender时,尝试获取sender的等待周期
sender.retry_get_sender_wait_interval=2000
#是否开启发送消息
sender.is_off=false
#最大消费线程数
consumer.max_consumer=2
#消费者最大等待时间
consumer.max_wait_time=5
#发送失败等待时间
consumer.consumer_fail_retry_wait_interval=50
#每个Buffer的最大个数
buffer.buffer_max_size=18000
#Buffer池的最大长度
buffer.pool_size=5
#发送检查线程检查周期
senderchecker.check_polling_time=200