提交 55592615 编写于 作者: A ascrutae

重构拦截点的代码,将拦截点改为继承式

上级 a5c9cd0c
package com.ai.cloud.skywalking.plugin.interceptor;
import static net.bytebuddy.matcher.ElementMatchers.any;
import static net.bytebuddy.matcher.ElementMatchers.named;
import static net.bytebuddy.matcher.ElementMatchers.takesArguments;
import java.util.List;
import com.ai.cloud.skywalking.plugin.PluginCfg;
import com.ai.cloud.skywalking.util.StringUtil;
import net.bytebuddy.ByteBuddy;
import net.bytebuddy.description.method.MethodDescription;
import net.bytebuddy.dynamic.ClassFileLocator;
import net.bytebuddy.dynamic.DynamicType;
import net.bytebuddy.dynamic.loading.ClassLoadingStrategy;
......@@ -16,146 +13,131 @@ 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;
import com.ai.cloud.skywalking.plugin.PluginCfg;
import com.ai.cloud.skywalking.util.StringUtil;
import java.util.List;
import static net.bytebuddy.matcher.ElementMatchers.any;
public class EnhanceClazz4Interceptor {
private static Logger logger = LogManager
.getLogger(EnhanceClazz4Interceptor.class);
private TypePool typePool;
public static final String contextAttrName = "_$EnhancedClassInstanceContext";
public EnhanceClazz4Interceptor() {
typePool = TypePool.Default.ofClassPath();
}
public void enhance() {
List<String> interceptorClassList = PluginCfg.CFG
.getInterceptorClassList();
for (String interceptorClassName : interceptorClassList) {
try {
enhance0(interceptorClassName);
} catch (Throwable t) {
logger.error("enhance class [{}] for intercept failure.",
interceptorClassName, t);
}
}
}
private void enhance0(String interceptorDefineClassName)
throws InstantiationException, IllegalAccessException,
ClassNotFoundException, EnhanceException {
logger.debug("prepare to enhance class by {}.",
interceptorDefineClassName);
InterceptorDefine define = (InterceptorDefine) Class.forName(
interceptorDefineClassName).newInstance();
String enhanceOriginClassName = define.getBeInterceptedClassName();
if(StringUtil.isEmpty(enhanceOriginClassName)){
logger.warn("classname of being intercepted is not defined by {}.",
interceptorDefineClassName);
return;
}
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/>
* such as: class com.ai.cloud.TestClass to class
* com.ai.cloud.TestClass$$Origin
*/
String renameClassName = enhanceOriginClassName + "$$Origin";
Class<?> originClass = new ByteBuddy()
.redefine(resolution.resolve(),
ClassFileLocator.ForClassLoader.ofClassPath())
.name(renameClassName)
.make()
.load(ClassLoader.getSystemClassLoader(),
ClassLoadingStrategy.Default.INJECTION).getLoaded();
/**
* create a new class using origin classname.<br/>
*
* new class need:<br/>
* 1.add field '_$EnhancedClassInstanceContext' of type
* EnhancedClassInstanceContext <br/>
*
* 2.intercept constructor by default, and intercept method which it's
* required by interceptorDefineClass. <br/>
*/
IAroundInterceptor interceptor = define.instance();
if(interceptor == null){
throw new EnhanceException("no IAroundInterceptor instance. ");
}
DynamicType.Builder<?> newClassBuilder = new ByteBuddy().subclass(
originClass, ConstructorStrategy.Default.IMITATE_SUPER_CLASS);
newClassBuilder = newClassBuilder
.defineField(contextAttrName,
EnhancedClassInstanceContext.class)
.constructor(any())
.intercept(
SuperMethodCall.INSTANCE.andThen(MethodDelegation.to(
new ClassConstructorInterceptor(interceptor))
.appendParameterBinder(
FieldProxy.Binder.install(
FieldGetter.class,
FieldSetter.class))));
InterceptPoint[] methodNameList = define.getBeInterceptedMethods();
ClassMethodInterceptor classMethodInterceptor = new ClassMethodInterceptor(
interceptor);
for (InterceptPoint method : methodNameList) {
logger.debug("prepare to enhance class {} method [{}] ",
enhanceOriginClassName, method.getMethodName());
if (method.getArgTypeArray() != null) {
newClassBuilder = newClassBuilder.method(
named(method.getMethodName()).and(
takesArguments(method.getArgTypeArray()))).intercept(
MethodDelegation.to(classMethodInterceptor));
} else if (method.getArgNum() > -1) {
newClassBuilder = newClassBuilder.method(
named(method.getMethodName()).and(
takesArguments(method.getArgNum()))).intercept(
MethodDelegation.to(classMethodInterceptor));
} else if("*".equals(method.getMethodName())){
newClassBuilder = newClassBuilder.method(any()).intercept(
MethodDelegation.to(classMethodInterceptor));
} else {
newClassBuilder = newClassBuilder.method(
named(method.getMethodName())).intercept(
MethodDelegation.to(classMethodInterceptor));
}
}
/**
* naming class as origin class name, make and load class to
* classloader.
*/
newClassBuilder
.name(enhanceOriginClassName)
.make()
.load(ClassLoader.getSystemClassLoader(),
ClassLoadingStrategy.Default.INJECTION).getLoaded();
logger.debug("enhance class {} by {} completely.",
enhanceOriginClassName, interceptorDefineClassName);
}
private static Logger logger = LogManager
.getLogger(EnhanceClazz4Interceptor.class);
private TypePool typePool;
public static final String contextAttrName = "_$EnhancedClassInstanceContext";
public EnhanceClazz4Interceptor() {
typePool = TypePool.Default.ofClassPath();
}
public void enhance() {
List<String> interceptorClassList = PluginCfg.CFG
.getInterceptorClassList();
for (String interceptorClassName : interceptorClassList) {
try {
enhance0(interceptorClassName);
} catch (Throwable t) {
logger.error("enhance class [{}] for intercept failure.",
interceptorClassName, t);
}
}
}
private void enhance0(String interceptorDefineClassName)
throws InstantiationException, IllegalAccessException,
ClassNotFoundException, EnhanceException {
logger.debug("prepare to enhance class by {}.",
interceptorDefineClassName);
InterceptorDefine define = (InterceptorDefine) Class.forName(
interceptorDefineClassName).newInstance();
String enhanceOriginClassName = define.getBeInterceptedClassName();
if (StringUtil.isEmpty(enhanceOriginClassName)) {
logger.warn("classname of being intercepted is not defined by {}.",
interceptorDefineClassName);
return;
}
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/>
* such as: class com.ai.cloud.TestClass to class
* com.ai.cloud.TestClass$$Origin
*/
String renameClassName = enhanceOriginClassName + "$$Origin";
Class<?> originClass = new ByteBuddy()
.redefine(resolution.resolve(),
ClassFileLocator.ForClassLoader.ofClassPath())
.name(renameClassName)
.make()
.load(ClassLoader.getSystemClassLoader(),
ClassLoadingStrategy.Default.INJECTION).getLoaded();
/**
* create a new class using origin classname.<br/>
*
* new class need:<br/>
* 1.add field '_$EnhancedClassInstanceContext' of type
* EnhancedClassInstanceContext <br/>
*
* 2.intercept constructor by default, and intercept method which it's
* required by interceptorDefineClass. <br/>
*/
IAroundInterceptor interceptor = define.instance();
if (interceptor == null) {
throw new EnhanceException("no IAroundInterceptor instance. ");
}
DynamicType.Builder<?> newClassBuilder = new ByteBuddy().subclass(
originClass, ConstructorStrategy.Default.IMITATE_SUPER_CLASS);
newClassBuilder = newClassBuilder
.defineField(contextAttrName,
EnhancedClassInstanceContext.class)
.constructor(any())
.intercept(
SuperMethodCall.INSTANCE.andThen(MethodDelegation.to(
new ClassConstructorInterceptor(interceptor))
.appendParameterBinder(
FieldProxy.Binder.install(
FieldGetter.class,
FieldSetter.class))));
MethodNameMatcher[] methodNameList = define.getBeInterceptedMethods();
ClassMethodInterceptor classMethodInterceptor = new ClassMethodInterceptor(
interceptor);
for (MethodNameMatcher method : methodNameList) {
logger.debug("prepare to enhance class {} method [{}] ",
enhanceOriginClassName, method.getMethodMatchDescribe());
newClassBuilder = newClassBuilder.method(
method.<MethodDescription>builderMatcher()).intercept(
MethodDelegation.to(classMethodInterceptor));
}
/**
* naming class as origin class name, make and load class to
* classloader.
*/
newClassBuilder
.name(enhanceOriginClassName)
.make()
.load(ClassLoader.getSystemClassLoader(),
ClassLoadingStrategy.Default.INJECTION).getLoaded();
logger.debug("enhance class {} by {} completely.",
enhanceOriginClassName, interceptorDefineClassName);
}
}
......@@ -13,7 +13,7 @@ public interface InterceptorDefine {
*
* @return
*/
public InterceptPoint[] getBeInterceptedMethods();
public MethodNameMatcher[] getBeInterceptedMethods();
/**
* 返回增强拦截器的实现<br/>
......
package com.ai.cloud.skywalking.plugin.interceptor;
import net.bytebuddy.matcher.ElementMatcher;
public abstract class MethodNameMatcher<T> {
private String methodMatchDescribe;
private int argNum = -1;
private Class<?>[] argTypeArray;
public MethodNameMatcher(String methodMatchDescribe) {
this.methodMatchDescribe = methodMatchDescribe;
}
public MethodNameMatcher(String methodMatchDescribe, int argNum) {
this.methodMatchDescribe = methodMatchDescribe;
this.argNum = argNum;
}
public MethodNameMatcher(String methodMatchDescribe, Class<?>[] argTypeArray) {
this.argTypeArray = argTypeArray;
this.methodMatchDescribe = methodMatchDescribe;
}
public abstract <T> ElementMatcher<T> builderMatcher();
protected String getMethodMatchDescribe() {
return methodMatchDescribe;
}
protected int getArgNum() {
return argNum;
}
protected Class<?>[] getArgTypeArray() {
return argTypeArray;
}
}
package com.ai.cloud.skywalking.plugin.interceptor.matcher;
import com.ai.cloud.skywalking.plugin.interceptor.MethodNameMatcher;
import net.bytebuddy.matcher.ElementMatcher;
import static net.bytebuddy.matcher.ElementMatchers.any;
public class AnyMethodMatcher extends MethodNameMatcher {
public AnyMethodMatcher() {
super("*");
}
@Override
public ElementMatcher builderMatcher() {
return any();
}
}
package com.ai.cloud.skywalking.plugin.interceptor.matcher;
import com.ai.cloud.skywalking.plugin.interceptor.MethodNameMatcher;
import net.bytebuddy.matcher.ElementMatcher;
import static net.bytebuddy.matcher.ElementMatchers.*;
public class ExclusionNameMatcher extends MethodNameMatcher {
public ExclusionNameMatcher(String methodMatchDescribe) {
super(methodMatchDescribe);
}
public ExclusionNameMatcher(String methodMatchDescribe, int argNum) {
super(methodMatchDescribe, argNum);
}
public ExclusionNameMatcher(String methodMatchDescribe, Class[] argTypeArray) {
super(methodMatchDescribe, argTypeArray);
}
@Override
public ElementMatcher builderMatcher() {
ElementMatcher.Junction matcher = not(named(getMethodMatchDescribe()));
if (getArgTypeArray() != null) {
matcher.and(takesArguments(getArgTypeArray()));
}
if (getArgNum() > -1) {
matcher.and(takesArguments(getArgNum()));
}
return matcher;
}
}
package com.ai.cloud.skywalking.plugin.interceptor.matcher;
import com.ai.cloud.skywalking.plugin.interceptor.MethodNameMatcher;
import net.bytebuddy.matcher.ElementMatcher;
import static net.bytebuddy.matcher.ElementMatchers.named;
import static net.bytebuddy.matcher.ElementMatchers.takesArguments;
public class FullNameMatcher extends MethodNameMatcher {
public FullNameMatcher(String methodName) {
super(methodName);
}
public FullNameMatcher(String methodName, int argNum) {
super(methodName, argNum);
}
public FullNameMatcher(String methodName, Class<?>... args) {
super(methodName, args);
}
@Override
public ElementMatcher builderMatcher() {
ElementMatcher.Junction matcher = named(getMethodMatchDescribe());
if (getArgTypeArray() != null) {
matcher.and(takesArguments(getArgTypeArray()));
}
if (getArgNum() > -1) {
matcher.and(takesArguments(getArgNum()));
}
return matcher;
}
}
package com.ai.cloud.skywalking.plugin.interceptor.matcher;
import com.ai.cloud.skywalking.plugin.interceptor.MethodNameMatcher;
import net.bytebuddy.matcher.ElementMatcher;
import static net.bytebuddy.matcher.ElementMatchers.nameMatches;
import static net.bytebuddy.matcher.ElementMatchers.takesArguments;
public class RegexNameMatcher extends MethodNameMatcher {
public RegexNameMatcher(String methodMatchDescribe) {
super(methodMatchDescribe);
}
public RegexNameMatcher(String methodMatchDescribe, int argNum) {
super(methodMatchDescribe, argNum);
}
public RegexNameMatcher(String methodMatchDescribe, Class[] argTypeArray) {
super(methodMatchDescribe, argTypeArray);
}
@Override
public ElementMatcher builderMatcher() {
ElementMatcher.Junction matcher = nameMatches(getMethodMatchDescribe());
if (getArgTypeArray() != null) {
matcher.and(takesArguments(getArgTypeArray()));
}
if (getArgNum() > -1) {
matcher.and(takesArguments(getArgNum()));
}
return matcher;
}
}
......@@ -3,6 +3,8 @@ package test.ai.cloud.plugin;
import com.ai.cloud.skywalking.plugin.interceptor.IAroundInterceptor;
import com.ai.cloud.skywalking.plugin.interceptor.InterceptPoint;
import com.ai.cloud.skywalking.plugin.interceptor.InterceptorDefine;
import com.ai.cloud.skywalking.plugin.interceptor.MethodNameMatcher;
import com.ai.cloud.skywalking.plugin.interceptor.matcher.FullNameMatcher;
public class TestInterceptorDefine implements InterceptorDefine {
......@@ -12,8 +14,8 @@ public class TestInterceptorDefine implements InterceptorDefine {
}
@Override
public InterceptPoint[] getBeInterceptedMethods() {
return new InterceptPoint[] { new InterceptPoint("printabc") };
public MethodNameMatcher[] getBeInterceptedMethods() {
return new MethodNameMatcher[] { new FullNameMatcher("printabc") };
}
@Override
......
package org.skywalking.httpClient.v4.plugin.dubbox.rest.attachment;
import com.ai.cloud.skywalking.plugin.interceptor.MethodNameMatcher;
import org.skywalking.httpClient.v4.plugin.HttpClientExecuteInterceptor;
import com.ai.cloud.skywalking.plugin.interceptor.IAroundInterceptor;
......@@ -20,7 +21,7 @@ public class DubboxRestHeadSetterAttachment implements InterceptorDefine {
}
@Override
public InterceptPoint[] getBeInterceptedMethods() {
public MethodNameMatcher[] getBeInterceptedMethods() {
return null;
}
......
package org.skywalking.httpClient.v4.plugin.define;
import com.ai.cloud.skywalking.plugin.interceptor.InterceptPoint;
import com.ai.cloud.skywalking.plugin.interceptor.MethodNameMatcher;
import com.ai.cloud.skywalking.plugin.interceptor.matcher.FullNameMatcher;
public class AbstractHttpClientPluginDefine extends HttpClientPluginDefine {
......@@ -16,8 +18,8 @@ public class AbstractHttpClientPluginDefine extends HttpClientPluginDefine {
*
*/
@Override
public InterceptPoint[] getBeInterceptedMethods() {
return new InterceptPoint[] {
new InterceptPoint("doExecute")};
public MethodNameMatcher[] getBeInterceptedMethods() {
return new MethodNameMatcher[] {
new FullNameMatcher("doExecute")};
}
}
package org.skywalking.httpClient.v4.plugin.define;
import com.ai.cloud.skywalking.plugin.interceptor.InterceptPoint;
import com.ai.cloud.skywalking.plugin.interceptor.MethodNameMatcher;
import com.ai.cloud.skywalking.plugin.interceptor.matcher.FullNameMatcher;
public class DefaultRequestDirectorPluginDefine extends HttpClientPluginDefine {
/**
......@@ -14,9 +16,9 @@ public class DefaultRequestDirectorPluginDefine extends HttpClientPluginDefine {
}
@Override
public InterceptPoint[] getBeInterceptedMethods() {
return new InterceptPoint[] {
new InterceptPoint("execute")};
public MethodNameMatcher[] getBeInterceptedMethods() {
return new MethodNameMatcher[] {
new FullNameMatcher("execute")};
}
}
package org.skywalking.httpClient.v4.plugin.define;
import com.ai.cloud.skywalking.plugin.interceptor.InterceptPoint;
import com.ai.cloud.skywalking.plugin.interceptor.MethodNameMatcher;
import com.ai.cloud.skywalking.plugin.interceptor.matcher.FullNameMatcher;
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";
}
@Override
public MethodNameMatcher[] getBeInterceptedMethods() {
return new MethodNameMatcher[]{new FullNameMatcher("doExecute")};
}
@Override
public String getBeInterceptedClassName() {
return "org.apache.http.impl.client.InternalHttpClient";
}
}
package org.skywalking.httpClient.v4.plugin.define;
import com.ai.cloud.skywalking.plugin.interceptor.InterceptPoint;
import com.ai.cloud.skywalking.plugin.interceptor.MethodNameMatcher;
import com.ai.cloud.skywalking.plugin.interceptor.matcher.FullNameMatcher;
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";
}
@Override
public MethodNameMatcher[] getBeInterceptedMethods() {
return new MethodNameMatcher[]{new FullNameMatcher("doExecute")};
}
@Override
public String getBeInterceptedClassName() {
return "org.apache.http.impl.client.MinimalHttpClient";
}
}
package org.skywalking.jedis.v2.plugin.define;
import org.skywalking.jedis.v2.plugin.JedisInterceptor;
import com.ai.cloud.skywalking.plugin.interceptor.IAroundInterceptor;
import com.ai.cloud.skywalking.plugin.interceptor.InterceptPoint;
import com.ai.cloud.skywalking.plugin.interceptor.InterceptorDefine;
import com.ai.cloud.skywalking.plugin.interceptor.MethodNameMatcher;
import com.ai.cloud.skywalking.plugin.interceptor.matcher.ExclusionNameMatcher;
import org.skywalking.jedis.v2.plugin.JedisInterceptor;
public class JedisPluginDefine implements InterceptorDefine {
@Override
public String getBeInterceptedClassName() {
return "redis.clients.jedis.Jedis";
}
@Override
public String getBeInterceptedClassName() {
return "redis.clients.jedis.Jedis";
}
@Override
public InterceptPoint[] getBeInterceptedMethods() {
return new InterceptPoint[] { new InterceptPoint("*") };
}
@Override
public MethodNameMatcher[] getBeInterceptedMethods() {
return new MethodNameMatcher[]{
new ExclusionNameMatcher("set"),
};
}
@Override
public IAroundInterceptor instance() {
return new JedisInterceptor();
}
@Override
public IAroundInterceptor instance() {
return new JedisInterceptor();
}
}
......@@ -3,6 +3,8 @@ package com.ai.cloud.skywalking.plugin.mysql;
import com.ai.cloud.skywalking.plugin.interceptor.IAroundInterceptor;
import com.ai.cloud.skywalking.plugin.interceptor.InterceptPoint;
import com.ai.cloud.skywalking.plugin.interceptor.InterceptorDefine;
import com.ai.cloud.skywalking.plugin.interceptor.MethodNameMatcher;
import com.ai.cloud.skywalking.plugin.interceptor.matcher.FullNameMatcher;
public class ConnectionPluginDefine implements InterceptorDefine {
......@@ -12,12 +14,12 @@ public class ConnectionPluginDefine implements InterceptorDefine {
}
@Override
public InterceptPoint[] getBeInterceptedMethods() {
return new InterceptPoint[] { new InterceptPoint("createStatement", 2),
new InterceptPoint("prepareStatement", 3),
new InterceptPoint("prepareCall", 3),
new InterceptPoint("commit"), new InterceptPoint("rollback"),
new InterceptPoint("close") };
public MethodNameMatcher[] getBeInterceptedMethods() {
return new MethodNameMatcher[] { new FullNameMatcher("createStatement", 2),
new FullNameMatcher("prepareStatement", 3),
new FullNameMatcher("prepareCall", 3),
new FullNameMatcher("commit"), new FullNameMatcher("rollback"),
new FullNameMatcher("close") };
}
@Override
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册