提交 a49e19c7 编写于 作者: wu-sheng's avatar wu-sheng

提交plugin模式大量重构,此重构会暂时造成dubbo插件失效(后续补充)

上级 da9192cf
package com.ai.cloud.skywalking.plugin.interceptor;
import static com.ai.cloud.skywalking.plugin.PluginBootstrap.CLASS_TYPE_POOL;
import static net.bytebuddy.matcher.ElementMatchers.any;
import static net.bytebuddy.matcher.ElementMatchers.not;
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;
import net.bytebuddy.implementation.MethodDelegation;
import net.bytebuddy.implementation.SuperMethodCall;
import net.bytebuddy.implementation.bind.annotation.FieldProxy;
import net.bytebuddy.matcher.ElementMatcher;
import net.bytebuddy.matcher.ElementMatchers;
import net.bytebuddy.pool.TypePool.Resolution;
import org.apache.logging.log4j.LogManager;
......@@ -22,12 +14,9 @@ import com.ai.cloud.skywalking.plugin.IPlugin;
import com.ai.cloud.skywalking.plugin.PluginException;
import com.ai.cloud.skywalking.util.StringUtil;
public abstract class InterceptorPluginDefine implements IPlugin {
private static Logger logger = LogManager.getLogger(InterceptorPluginDefine.class);
public abstract class AbstractClassEnhancePluginDefine implements IPlugin {
private static Logger logger = LogManager.getLogger(AbstractClassEnhancePluginDefine.class);
public static final String contextAttrName = "_$EnhancedClassInstanceContext";
@Override
public void define() throws PluginException {
String interceptorDefineClassName = this.getClass().getName();
......@@ -55,63 +44,8 @@ public abstract class InterceptorPluginDefine implements IPlugin {
DynamicType.Builder<?> newClassBuilder = new ByteBuddy()
.rebase(resolution.resolve(),
ClassFileLocator.ForClassLoader.ofClassPath());
/**
* alter class source code.<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 = instance();
if (interceptor == null) {
throw new EnhanceException("no IAroundInterceptor instance. ");
}
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))));
MethodMatcher[] methodMatchers = getBeInterceptedMethodsMatchers();
ClassMethodInterceptor classMethodInterceptor = new ClassMethodInterceptor(
interceptor);
StringBuilder enhanceRules = new StringBuilder("\nprepare to enhance class [" + enhanceOriginClassName + "] as following rules:\n");
int ruleIdx = 1;
for (MethodMatcher methodMatcher : methodMatchers) {
enhanceRules.append("\t" + ruleIdx++ + ". " + methodMatcher + "\n");
}
logger.debug(enhanceRules);
ElementMatcher.Junction<MethodDescription> matcher = null;
for (MethodMatcher methodMatcher : methodMatchers) {
logger.debug("enhance class {} by rule: {}",
enhanceOriginClassName, methodMatcher);
if (matcher == null) {
matcher = methodMatcher.buildMatcher();
continue;
}
matcher = matcher.or(methodMatcher.buildMatcher());
}
/**
* exclude static methods.
*/
matcher = matcher.and(not(ElementMatchers.isStatic()));
newClassBuilder = newClassBuilder.method(matcher).intercept(
MethodDelegation.to(classMethodInterceptor));
newClassBuilder = this.enhance(enhanceOriginClassName, newClassBuilder);
/**
* naming class as origin class name, make and load class to
......@@ -126,26 +60,13 @@ public abstract class InterceptorPluginDefine implements IPlugin {
logger.debug("enhance class {} by {} completely.",
enhanceOriginClassName, interceptorDefineClassName);
}
protected abstract DynamicType.Builder<?> enhance(String enhanceOriginClassName, DynamicType.Builder<?> newClassBuilder) throws PluginException;
/**
* 返回要被增强的类,应当返回类全名
*
* @return
*/
public abstract String getBeInterceptedClassName();
/**
* 返回需要被增强的方法列表
*
* @return
*/
public abstract MethodMatcher[] getBeInterceptedMethodsMatchers();
/**
* 返回增强拦截器的实现<br/>
* 每个拦截器在同一个被增强类的内部,保持单例
*
* @return
*/
public abstract IAroundInterceptor instance();
protected abstract String getBeInterceptedClassName();
}
......@@ -3,8 +3,8 @@ package com.ai.cloud.skywalking.plugin.interceptor.assist;
import java.util.concurrent.atomic.AtomicInteger;
import com.ai.cloud.skywalking.plugin.interceptor.EnhancedClassInstanceContext;
import com.ai.cloud.skywalking.plugin.interceptor.IAroundInterceptor;
import com.ai.cloud.skywalking.plugin.interceptor.InterceptorException;
import com.ai.cloud.skywalking.plugin.interceptor.enhance.IntanceMethodsAroundInterceptor;
/**
* 用于首次拦截方法调用,避免方法内部的方法调用被多次拦截。
......@@ -12,7 +12,7 @@ import com.ai.cloud.skywalking.plugin.interceptor.InterceptorException;
* @author wusheng
*
*/
public abstract class FirstInvokeInterceptor implements IAroundInterceptor {
public abstract class SimpleObjectFirstInvokeInterceptor implements IntanceMethodsAroundInterceptor {
protected String invokeCounterKey = "__$invokeCounterKey";
protected Object invokeCounterInstLock = new Object();
......
package com.ai.cloud.skywalking.plugin.interceptor;
package com.ai.cloud.skywalking.plugin.interceptor.enhance;
import net.bytebuddy.implementation.bind.annotation.AllArguments;
import net.bytebuddy.implementation.bind.annotation.FieldProxy;
......@@ -8,20 +8,22 @@ import net.bytebuddy.implementation.bind.annotation.This;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import com.ai.cloud.skywalking.plugin.interceptor.EnhancedClassInstanceContext;
public class ClassConstructorInterceptor {
private static Logger logger = LogManager
.getLogger(ClassConstructorInterceptor.class);
private IAroundInterceptor interceptor;
private IntanceMethodsAroundInterceptor interceptor;
public ClassConstructorInterceptor(IAroundInterceptor interceptor) {
public ClassConstructorInterceptor(IntanceMethodsAroundInterceptor interceptor) {
this.interceptor = interceptor;
}
@RuntimeType
public void intercept(
@This Object obj,
@FieldProxy(InterceptorPluginDefine.contextAttrName) FieldSetter accessor,
@FieldProxy(ClassEnhancePluginDefine.contextAttrName) FieldSetter accessor,
@AllArguments Object[] allArguments) {
try {
EnhancedClassInstanceContext context = new EnhancedClassInstanceContext();
......
package com.ai.cloud.skywalking.plugin.interceptor.enhance;
import static net.bytebuddy.matcher.ElementMatchers.any;
import static net.bytebuddy.matcher.ElementMatchers.not;
import net.bytebuddy.description.method.MethodDescription;
import net.bytebuddy.dynamic.DynamicType;
import net.bytebuddy.implementation.MethodDelegation;
import net.bytebuddy.implementation.SuperMethodCall;
import net.bytebuddy.implementation.bind.annotation.FieldProxy;
import net.bytebuddy.matcher.ElementMatcher;
import net.bytebuddy.matcher.ElementMatchers;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import com.ai.cloud.skywalking.plugin.PluginException;
import com.ai.cloud.skywalking.plugin.interceptor.AbstractClassEnhancePluginDefine;
import com.ai.cloud.skywalking.plugin.interceptor.EnhanceException;
import com.ai.cloud.skywalking.plugin.interceptor.EnhancedClassInstanceContext;
import com.ai.cloud.skywalking.plugin.interceptor.MethodMatcher;
public abstract class ClassEnhancePluginDefine extends AbstractClassEnhancePluginDefine {
private static Logger logger = LogManager
.getLogger(ClassEnhancePluginDefine.class);
public static final String contextAttrName = "_$EnhancedClassInstanceContext";
protected DynamicType.Builder<?> enhance(String enhanceOriginClassName,
DynamicType.Builder<?> newClassBuilder) throws PluginException {
newClassBuilder = this.enhanceClass(enhanceOriginClassName, newClassBuilder);
newClassBuilder = this.enhanceInstance(enhanceOriginClassName, newClassBuilder);
return newClassBuilder;
}
private DynamicType.Builder<?> enhanceInstance(String enhanceOriginClassName,
DynamicType.Builder<?> newClassBuilder) throws PluginException {
MethodMatcher[] methodMatchers = getInstanceMethodsMatchers();
if(methodMatchers == null){
return newClassBuilder;
}
/**
* alter class source code.<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/>
*/
IntanceMethodsAroundInterceptor interceptor = getInstanceMethodsInterceptor();
if (interceptor == null) {
throw new EnhanceException("no IntanceMethodsAroundInterceptor instance. ");
}
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))));
ClassInstanceMethodsInterceptor classMethodInterceptor = new ClassInstanceMethodsInterceptor(
interceptor);
StringBuilder enhanceRules = new StringBuilder(
"\nprepare to enhance class [" + enhanceOriginClassName
+ "] instance methods as following rules:\n");
int ruleIdx = 1;
for (MethodMatcher methodMatcher : methodMatchers) {
enhanceRules.append("\t" + ruleIdx++ + ". " + methodMatcher + "\n");
}
logger.debug(enhanceRules);
ElementMatcher.Junction<MethodDescription> matcher = null;
for (MethodMatcher methodMatcher : methodMatchers) {
logger.debug("enhance class {} instance methods by rule: {}",
enhanceOriginClassName, methodMatcher);
if (matcher == null) {
matcher = methodMatcher.buildMatcher();
continue;
}
matcher = matcher.or(methodMatcher.buildMatcher());
}
/**
* exclude static methods.
*/
matcher = matcher.and(not(ElementMatchers.isStatic()));
newClassBuilder = newClassBuilder.method(matcher).intercept(
MethodDelegation.to(classMethodInterceptor));
return newClassBuilder;
}
/**
* 返回需要被增强的方法列表
*
* @return
*/
protected abstract MethodMatcher[] getInstanceMethodsMatchers();
/**
* 返回增强拦截器的实现<br/>
* 每个拦截器在同一个被增强类的内部,保持单例
*
* @return
*/
protected abstract IntanceMethodsAroundInterceptor getInstanceMethodsInterceptor();
private DynamicType.Builder<?> enhanceClass(String enhanceOriginClassName,
DynamicType.Builder<?> newClassBuilder) throws PluginException {
MethodMatcher[] methodMatchers = getStaticMethodsMatchers();
if(methodMatchers == null){
return newClassBuilder;
}
StaticMethodsAroundInterceptor interceptor = getStaticMethodsInterceptor();
if (interceptor == null) {
throw new EnhanceException("no StaticMethodsAroundInterceptor instance. ");
}
ClassStaticMethodsInterceptor classMethodInterceptor = new ClassStaticMethodsInterceptor(
interceptor);
StringBuilder enhanceRules = new StringBuilder(
"\nprepare to enhance class [" + enhanceOriginClassName
+ "] static methods as following rules:\n");
int ruleIdx = 1;
for (MethodMatcher methodMatcher : methodMatchers) {
enhanceRules.append("\t" + ruleIdx++ + ". " + methodMatcher + "\n");
}
logger.debug(enhanceRules);
ElementMatcher.Junction<MethodDescription> matcher = null;
for (MethodMatcher methodMatcher : methodMatchers) {
logger.debug("enhance class {} static methods by rule: {}",
enhanceOriginClassName, methodMatcher);
if (matcher == null) {
matcher = methodMatcher.buildMatcher();
continue;
}
matcher = matcher.or(methodMatcher.buildMatcher());
}
/**
* restrict static methods.
*/
matcher = matcher.and(ElementMatchers.isStatic());
newClassBuilder = newClassBuilder.method(matcher).intercept(
MethodDelegation.to(classMethodInterceptor));
return newClassBuilder;
}
/**
* 返回需要被增强的方法列表
*
* @return
*/
protected abstract MethodMatcher[] getStaticMethodsMatchers();
/**
* 返回增强拦截器的实现<br/>
* 每个拦截器在同一个被增强类的内部,保持单例
*
* @return
*/
protected abstract StaticMethodsAroundInterceptor getStaticMethodsInterceptor();
}
package com.ai.cloud.skywalking.plugin.interceptor.enhance;
import com.ai.cloud.skywalking.plugin.interceptor.MethodMatcher;
/**
* 仅增强拦截实例方法
*
* @author wusheng
*
*/
public abstract class ClassInstanceMethodsEnhancePluginDefine extends
ClassEnhancePluginDefine {
@Override
protected MethodMatcher[] getStaticMethodsMatchers() {
return null;
}
@Override
protected StaticMethodsAroundInterceptor getStaticMethodsInterceptor() {
return null;
}
}
package com.ai.cloud.skywalking.plugin.interceptor;
package com.ai.cloud.skywalking.plugin.interceptor.enhance;
import java.lang.reflect.Method;
import java.util.concurrent.Callable;
......@@ -13,19 +13,21 @@ import net.bytebuddy.implementation.bind.annotation.This;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import com.ai.cloud.skywalking.plugin.interceptor.EnhancedClassInstanceContext;
/**
* 类方法拦截、控制器
*
* @author wusheng
*
*/
public class ClassMethodInterceptor {
public class ClassInstanceMethodsInterceptor {
private static Logger logger = LogManager
.getLogger(ClassMethodInterceptor.class);
.getLogger(ClassInstanceMethodsInterceptor.class);
private IAroundInterceptor interceptor;
private IntanceMethodsAroundInterceptor interceptor;
public ClassMethodInterceptor(IAroundInterceptor interceptor) {
public ClassInstanceMethodsInterceptor(IntanceMethodsAroundInterceptor interceptor) {
this.interceptor = interceptor;
}
......@@ -35,9 +37,9 @@ public class ClassMethodInterceptor {
@AllArguments Object[] allArguments,
@Origin Method method,
@SuperCall Callable<?> zuper,
@FieldValue(InterceptorPluginDefine.contextAttrName) EnhancedClassInstanceContext instanceContext)
@FieldValue(ClassEnhancePluginDefine.contextAttrName) EnhancedClassInstanceContext instanceContext)
throws Exception {
MethodInvokeContext interceptorContext = new MethodInvokeContext(obj,
InstanceMethodInvokeContext interceptorContext = new InstanceMethodInvokeContext(obj,
method.getName(), allArguments);
try {
interceptor.beforeMethod(instanceContext, interceptorContext);
......
package com.ai.cloud.skywalking.plugin.interceptor.enhance;
import com.ai.cloud.skywalking.plugin.interceptor.MethodMatcher;
/**
* 仅增强拦截类级别静态方法
*
* @author wusheng
*
*/
public abstract class ClassStaticMethodsEnhancePluginDefine extends
ClassEnhancePluginDefine {
@Override
protected MethodMatcher[] getInstanceMethodsMatchers() {
return null;
}
@Override
protected IntanceMethodsAroundInterceptor getInstanceMethodsInterceptor() {
return null;
}
}
package com.ai.cloud.skywalking.plugin.interceptor.enhance;
import java.lang.reflect.Method;
import java.util.concurrent.Callable;
import net.bytebuddy.implementation.bind.annotation.AllArguments;
import net.bytebuddy.implementation.bind.annotation.Origin;
import net.bytebuddy.implementation.bind.annotation.RuntimeType;
import net.bytebuddy.implementation.bind.annotation.SuperCall;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
/**
* 类静态方法拦截、控制器
*
* @author wusheng
*
*/
public class ClassStaticMethodsInterceptor {
private static Logger logger = LogManager
.getLogger(ClassStaticMethodsInterceptor.class);
private StaticMethodsAroundInterceptor interceptor;
public ClassStaticMethodsInterceptor(
StaticMethodsAroundInterceptor interceptor) {
this.interceptor = interceptor;
}
@RuntimeType
public Object intercept(@Origin Class<?> clazz,
@AllArguments Object[] allArguments, @Origin Method method,
@SuperCall Callable<?> zuper) throws Exception {
MethodInvokeContext interceptorContext = new MethodInvokeContext(
method.getName(), allArguments);
try {
interceptor.beforeMethod(interceptorContext);
} catch (Throwable t) {
logger.error("class[{}] before static method[{}] intercept failue:{}",
clazz, method.getName(), t.getMessage(), t);
}
Object ret = null;
try {
ret = zuper.call();
} catch (Throwable t) {
try {
interceptor.handleMethodException(t, interceptorContext, ret);
} catch (Throwable t2) {
logger.error("class[{}] handle static method[{}] exception failue:{}",
clazz, method.getName(), t2.getMessage(), t2);
}
throw t;
} finally {
try {
ret = interceptor.afterMethod(interceptorContext, ret);
} catch (Throwable t) {
logger.error("class[{}] after static method[{}] intercept failue:{}",
clazz, method.getName(), t.getMessage(), t);
}
}
return ret;
}
}
package com.ai.cloud.skywalking.plugin.interceptor;
package com.ai.cloud.skywalking.plugin.interceptor.enhance;
public class ConstructorInvokeContext {
/**
......
package com.ai.cloud.skywalking.plugin.interceptor;
package com.ai.cloud.skywalking.plugin.interceptor.enhance;
public interface FieldGetter {
Object getValue();
......
package com.ai.cloud.skywalking.plugin.interceptor;
package com.ai.cloud.skywalking.plugin.interceptor.enhance;
public interface FieldSetter {
void setValue(Object value);
......
package com.ai.cloud.skywalking.plugin.interceptor.enhance;
public class InstanceMethodInvokeContext extends MethodInvokeContext {
/**
* 代理类实例
*/
private Object objInst;
InstanceMethodInvokeContext(Object objInst, String methodName, Object[] allArguments) {
super(methodName, allArguments);
this.objInst = objInst;
}
public Object inst(){
return objInst;
}
}
package com.ai.cloud.skywalking.plugin.interceptor;
package com.ai.cloud.skywalking.plugin.interceptor.enhance;
public interface IAroundInterceptor {
import com.ai.cloud.skywalking.plugin.interceptor.EnhancedClassInstanceContext;
public interface IntanceMethodsAroundInterceptor {
public void onConstruct(EnhancedClassInstanceContext context, ConstructorInvokeContext interceptorContext);
public void beforeMethod(EnhancedClassInstanceContext context, MethodInvokeContext interceptorContext);
public void beforeMethod(EnhancedClassInstanceContext context, InstanceMethodInvokeContext interceptorContext);
public Object afterMethod(EnhancedClassInstanceContext context, MethodInvokeContext interceptorContext, Object ret);
public Object afterMethod(EnhancedClassInstanceContext context, InstanceMethodInvokeContext interceptorContext, Object ret);
public void handleMethodException(Throwable t, EnhancedClassInstanceContext context, MethodInvokeContext interceptorContext, Object ret);
public void handleMethodException(Throwable t, EnhancedClassInstanceContext context, InstanceMethodInvokeContext interceptorContext, Object ret);
}
package com.ai.cloud.skywalking.plugin.interceptor;
package com.ai.cloud.skywalking.plugin.interceptor.enhance;
/**
* 方法执行拦截上下文
......@@ -7,10 +7,6 @@ package com.ai.cloud.skywalking.plugin.interceptor;
*
*/
public class MethodInvokeContext {
/**
* 代理类实例
*/
private Object objInst;
/**
* 方法名称
*/
......@@ -20,8 +16,7 @@ public class MethodInvokeContext {
*/
private Object[] allArguments;
MethodInvokeContext(Object objInst, String methodName, Object[] allArguments) {
this.objInst = objInst;
MethodInvokeContext(String methodName, Object[] allArguments) {
this.methodName = methodName;
this.allArguments = allArguments;
}
......@@ -33,8 +28,4 @@ public class MethodInvokeContext {
public String methodName(){
return methodName;
}
public Object inst(){
return objInst;
}
}
package com.ai.cloud.skywalking.plugin.interceptor.enhance;
public interface StaticMethodsAroundInterceptor {
public void beforeMethod(MethodInvokeContext interceptorContext);
public Object afterMethod(MethodInvokeContext interceptorContext, Object ret);
public void handleMethodException(Throwable t, MethodInvokeContext interceptorContext, Object ret);
}
package test.ai.cloud.matcher;
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;
import com.ai.cloud.skywalking.plugin.interceptor.enhance.ConstructorInvokeContext;
import com.ai.cloud.skywalking.plugin.interceptor.enhance.InstanceMethodInvokeContext;
import com.ai.cloud.skywalking.plugin.interceptor.enhance.IntanceMethodsAroundInterceptor;
/**
* Created by xin on 16-6-8.
*/
public class TestAroundInterceptor implements IAroundInterceptor {
public class TestAroundInterceptor implements IntanceMethodsAroundInterceptor {
@Override
public void onConstruct(EnhancedClassInstanceContext context, ConstructorInvokeContext interceptorContext) {
}
@Override
public void beforeMethod(EnhancedClassInstanceContext context, MethodInvokeContext interceptorContext) {
public void beforeMethod(EnhancedClassInstanceContext context, InstanceMethodInvokeContext interceptorContext) {
System.out.println("before method");
}
@Override
public Object afterMethod(EnhancedClassInstanceContext context, MethodInvokeContext interceptorContext, Object ret) {
public Object afterMethod(EnhancedClassInstanceContext context, InstanceMethodInvokeContext interceptorContext, Object ret) {
System.out.println("after method");
return ret;
}
@Override
public void handleMethodException(Throwable t, EnhancedClassInstanceContext context, MethodInvokeContext interceptorContext, Object ret) {
public void handleMethodException(Throwable t, EnhancedClassInstanceContext context, InstanceMethodInvokeContext interceptorContext, Object ret) {
}
}
package test.ai.cloud.matcher;
import com.ai.cloud.skywalking.plugin.interceptor.IAroundInterceptor;
import com.ai.cloud.skywalking.plugin.interceptor.InterceptorPluginDefine;
import com.ai.cloud.skywalking.plugin.interceptor.MethodMatcher;
import com.ai.cloud.skywalking.plugin.interceptor.matcher.MethodsExclusiveMatcher;
import com.ai.cloud.skywalking.plugin.interceptor.enhance.ClassInstanceMethodsEnhancePluginDefine;
import com.ai.cloud.skywalking.plugin.interceptor.enhance.IntanceMethodsAroundInterceptor;
import com.ai.cloud.skywalking.plugin.interceptor.matcher.PrivateMethodMatcher;
import com.ai.cloud.skywalking.plugin.interceptor.matcher.SimpleMethodMatcher;
/**
* Created by xin on 16-6-8.
*/
public class TestMatcherDefine extends InterceptorPluginDefine {
public class TestMatcherDefine extends ClassInstanceMethodsEnhancePluginDefine {
@Override
public String getBeInterceptedClassName() {
return "test.ai.cloud.matcher.TestMatcherClass";
}
@Override
public MethodMatcher[] getBeInterceptedMethodsMatchers() {
return new MethodMatcher[]{
new PrivateMethodMatcher(),
new MethodsExclusiveMatcher(new SimpleMethodMatcher("set")),
new SimpleMethodMatcher(MethodMatcher.Modifier.Private, "set", 1)
};
//return new MethodMatcher[] { new SimpleMethodMatcher(Modifier.Public, "printabc", new Class[]{String.class, String.class}) };
//return new MethodMatcher[] { new PrivateMethodMatcher()};
protected MethodMatcher[] getInstanceMethodsMatchers() {
// return new MethodMatcher[]{
// new PrivateMethodMatcher(),
// new MethodsExclusiveMatcher(new SimpleMethodMatcher("set")),
// new SimpleMethodMatcher(MethodMatcher.Modifier.Private, "set", 1)
// };
// return new MethodMatcher[] { new SimpleMethodMatcher(Modifier.Public, "printabc", new Class[]{String.class, String.class}) };
return new MethodMatcher[] { new PrivateMethodMatcher()};
//return new MethodMatcher[]{new AnyMethodsMatcher()};
//return new MethodMatcher[]{new MethodsExclusiveMatcher(new SimpleMethodMatcher("set"), new SimpleMethodMatcher(MethodMatcher.Modifier.Public,"get"))};
}
@Override
public IAroundInterceptor instance() {
protected IntanceMethodsAroundInterceptor getInstanceMethodsInterceptor() {
return new TestAroundInterceptor();
}
}
......@@ -8,4 +8,8 @@ public class BeInterceptedClass {
public void printabc(){
System.out.println("printabc");
}
public static void call(){
System.out.println("static call");
}
}
......@@ -22,5 +22,7 @@ public class PluginMainTest {
inst.printabc();
long end = System.currentTimeMillis();
System.out.println(end - start + "ms");
BeInterceptedClass.call();
}
}
package test.ai.cloud.plugin;
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;
import com.ai.cloud.skywalking.plugin.interceptor.enhance.ConstructorInvokeContext;
import com.ai.cloud.skywalking.plugin.interceptor.enhance.InstanceMethodInvokeContext;
import com.ai.cloud.skywalking.plugin.interceptor.enhance.IntanceMethodsAroundInterceptor;
public class TestAroundInterceptor implements IAroundInterceptor {
public class TestAroundInterceptor implements IntanceMethodsAroundInterceptor {
@Override
public void onConstruct(EnhancedClassInstanceContext context, ConstructorInvokeContext interceptorContext) {
......@@ -14,19 +14,19 @@ public class TestAroundInterceptor implements IAroundInterceptor {
}
@Override
public void beforeMethod(EnhancedClassInstanceContext context, MethodInvokeContext interceptorContext) {
public void beforeMethod(EnhancedClassInstanceContext context, InstanceMethodInvokeContext interceptorContext) {
System.out.println("beforeMethod : " + context.get("test.key", String.class));
}
@Override
public Object afterMethod(EnhancedClassInstanceContext context, MethodInvokeContext interceptorContext, Object ret) {
public Object afterMethod(EnhancedClassInstanceContext context, InstanceMethodInvokeContext interceptorContext, Object ret) {
System.out.println("afterMethod: " + context.get("test.key", String.class));
return ret;
}
@Override
public void handleMethodException(Throwable t, EnhancedClassInstanceContext context,
MethodInvokeContext interceptorContext, Object ret) {
InstanceMethodInvokeContext interceptorContext, Object ret) {
// TODO Auto-generated method stub
}
......
package test.ai.cloud.plugin;
import com.ai.cloud.skywalking.plugin.interceptor.IAroundInterceptor;
import com.ai.cloud.skywalking.plugin.interceptor.InterceptorPluginDefine;
import com.ai.cloud.skywalking.plugin.interceptor.MethodMatcher;
import com.ai.cloud.skywalking.plugin.interceptor.enhance.ClassEnhancePluginDefine;
import com.ai.cloud.skywalking.plugin.interceptor.enhance.IntanceMethodsAroundInterceptor;
import com.ai.cloud.skywalking.plugin.interceptor.enhance.StaticMethodsAroundInterceptor;
import com.ai.cloud.skywalking.plugin.interceptor.matcher.SimpleMethodMatcher;
public class TestInterceptorDefine extends InterceptorPluginDefine {
public class TestInterceptorDefine extends ClassEnhancePluginDefine {
@Override
public String getBeInterceptedClassName() {
......@@ -13,13 +14,23 @@ public class TestInterceptorDefine extends InterceptorPluginDefine {
}
@Override
public MethodMatcher[] getBeInterceptedMethodsMatchers() {
public MethodMatcher[] getInstanceMethodsMatchers() {
return new MethodMatcher[] { new SimpleMethodMatcher("printabc") };
}
@Override
public IAroundInterceptor instance() {
public IntanceMethodsAroundInterceptor getInstanceMethodsInterceptor() {
return new TestAroundInterceptor();
}
@Override
protected MethodMatcher[] getStaticMethodsMatchers() {
return new MethodMatcher[] { new SimpleMethodMatcher("call") };
}
@Override
protected StaticMethodsAroundInterceptor getStaticMethodsInterceptor() {
return new TestStaticAroundInterceptor();
}
}
package test.ai.cloud.plugin;
import com.ai.cloud.skywalking.plugin.interceptor.enhance.MethodInvokeContext;
import com.ai.cloud.skywalking.plugin.interceptor.enhance.StaticMethodsAroundInterceptor;
public class TestStaticAroundInterceptor implements StaticMethodsAroundInterceptor {
@Override
public void beforeMethod(MethodInvokeContext interceptorContext) {
System.out.println("beforeMethod : static");
}
@Override
public Object afterMethod(MethodInvokeContext interceptorContext, Object ret) {
System.out.println("afterMethod: static");
return ret;
}
@Override
public void handleMethodException(Throwable t,
MethodInvokeContext interceptorContext, Object ret) {
// TODO Auto-generated method stub
}
}
package com.ai.cloud.skywalking.plugin;
import com.ai.cloud.skywalking.conf.AuthDesc;
import com.ai.cloud.skywalking.plugin.boot.BootException;
import com.ai.cloud.skywalking.plugin.boot.BootPluginDefine;
import com.alibaba.dubbo.rpc.Invoker;
import net.bytebuddy.ByteBuddy;
import net.bytebuddy.dynamic.ClassFileLocator;
import net.bytebuddy.dynamic.DynamicType;
import net.bytebuddy.dynamic.loading.ClassLoadingStrategy;
import net.bytebuddy.implementation.MethodDelegation;
import net.bytebuddy.pool.TypePool;
import com.ai.cloud.skywalking.plugin.interceptor.MethodMatcher;
import com.ai.cloud.skywalking.plugin.interceptor.enhance.ClassStaticMethodsEnhancePluginDefine;
import com.ai.cloud.skywalking.plugin.interceptor.enhance.StaticMethodsAroundInterceptor;
import com.ai.cloud.skywalking.plugin.interceptor.matcher.SimpleMethodMatcher;
import static com.ai.cloud.skywalking.plugin.PluginBootstrap.CLASS_TYPE_POOL;
import static net.bytebuddy.matcher.ElementMatchers.named;
import static net.bytebuddy.matcher.ElementMatchers.takesArguments;
public class DubboPluginDefine extends ClassStaticMethodsEnhancePluginDefine {
@Override
protected MethodMatcher[] getStaticMethodsMatchers() {
return new MethodMatcher[] { new SimpleMethodMatcher("buildInvokerChain") };
}
public class DubboPluginDefine extends BootPluginDefine {
@Override
protected StaticMethodsAroundInterceptor getStaticMethodsInterceptor() {
// TODO Auto-generated method stub
return null;
}
private static final String interceptorClassName = "com.alibaba.dubbo.rpc.protocol.ProtocolFilterWrapper";
@Override
protected void boot() throws BootException {
if (!AuthDesc.isAuth()) {
return;
}
TypePool.Resolution resolution = CLASS_TYPE_POOL.describe(interceptorClassName);
DynamicType.Builder<?> newClassBuilder = new ByteBuddy()
.rebase(resolution.resolve(),
ClassFileLocator.ForClassLoader.ofClassPath());
newClassBuilder = newClassBuilder.method(named("buildInvokerChain")
.and(takesArguments(Invoker.class, String.class, String.class)))
.intercept(MethodDelegation.to(new DubboFilterBuildInterceptor()));
newClassBuilder
.name(interceptorClassName)
.make()
.load(ClassLoader.getSystemClassLoader(),
ClassLoadingStrategy.Default.INJECTION).getLoaded();
}
@Override
protected String getBeInterceptedClassName() {
return "com.alibaba.dubbo.rpc.protocol.ProtocolFilterWrapper";
}
}
......@@ -2,11 +2,11 @@ package org.skywalking.httpClient.v4.plugin.dubbox.rest.attachment;
import org.skywalking.httpClient.v4.plugin.HttpClientExecuteInterceptor;
import com.ai.cloud.skywalking.plugin.interceptor.IAroundInterceptor;
import com.ai.cloud.skywalking.plugin.interceptor.InterceptorPluginDefine;
import com.ai.cloud.skywalking.plugin.interceptor.MethodMatcher;
import com.ai.cloud.skywalking.plugin.interceptor.enhance.ClassInstanceMethodsEnhancePluginDefine;
import com.ai.cloud.skywalking.plugin.interceptor.enhance.IntanceMethodsAroundInterceptor;
public class DubboxRestHeadSetterAttachment extends InterceptorPluginDefine {
public class DubboxRestHeadSetterAttachment extends ClassInstanceMethodsEnhancePluginDefine {
/**
* this method is called as InterceptorPluginDefine<br/>
......@@ -20,12 +20,12 @@ public class DubboxRestHeadSetterAttachment extends InterceptorPluginDefine {
}
@Override
public MethodMatcher[] getBeInterceptedMethodsMatchers() {
public MethodMatcher[] getInstanceMethodsMatchers() {
return null;
}
@Override
public IAroundInterceptor instance() {
public IntanceMethodsAroundInterceptor getInstanceMethodsInterceptor() {
return null;
}
......
......@@ -5,12 +5,12 @@ 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;
import com.ai.cloud.skywalking.plugin.interceptor.enhance.ConstructorInvokeContext;
import com.ai.cloud.skywalking.plugin.interceptor.enhance.InstanceMethodInvokeContext;
import com.ai.cloud.skywalking.plugin.interceptor.enhance.IntanceMethodsAroundInterceptor;
public class HttpClientExecuteInterceptor implements IAroundInterceptor {
public class HttpClientExecuteInterceptor implements IntanceMethodsAroundInterceptor {
/**
* default headname of sky walking context<br/>
*/
......@@ -25,7 +25,7 @@ public class HttpClientExecuteInterceptor implements IAroundInterceptor {
@Override
public void beforeMethod(EnhancedClassInstanceContext context,
MethodInvokeContext interceptorContext) {
InstanceMethodInvokeContext interceptorContext) {
Object[] allArguments = interceptorContext.allArguments();
if (allArguments[0] == null || allArguments[1] == null) {
// illegal args, can't trace. ignore.
......@@ -51,7 +51,7 @@ public class HttpClientExecuteInterceptor implements IAroundInterceptor {
@Override
public Object afterMethod(EnhancedClassInstanceContext context,
MethodInvokeContext interceptorContext, Object ret) {
InstanceMethodInvokeContext interceptorContext, Object ret) {
Object[] allArguments = interceptorContext.allArguments();
if (allArguments[0] == null || allArguments[1] == null) {
// illegal args, can't trace. ignore.
......@@ -64,7 +64,7 @@ public class HttpClientExecuteInterceptor implements IAroundInterceptor {
@Override
public void handleMethodException(Throwable t,
EnhancedClassInstanceContext context,
MethodInvokeContext interceptorContext, Object ret) {
InstanceMethodInvokeContext interceptorContext, Object ret) {
Object[] allArguments = interceptorContext.allArguments();
if (allArguments[0] == null || allArguments[1] == null) {
// illegal args, can't trace. ignore.
......
......@@ -17,7 +17,7 @@ public class AbstractHttpClientPluginDefine extends HttpClientPluginDefine {
*
*/
@Override
public MethodMatcher[] getBeInterceptedMethodsMatchers() {
public MethodMatcher[] getInstanceMethodsMatchers() {
return new MethodMatcher[] {
new SimpleMethodMatcher("doExecute")};
}
......
......@@ -15,7 +15,7 @@ public class DefaultRequestDirectorPluginDefine extends HttpClientPluginDefine {
}
@Override
public MethodMatcher[] getBeInterceptedMethodsMatchers() {
public MethodMatcher[] getInstanceMethodsMatchers() {
return new MethodMatcher[] {
new SimpleMethodMatcher("execute")};
}
......
......@@ -2,13 +2,13 @@ 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.InterceptorPluginDefine;
import com.ai.cloud.skywalking.plugin.interceptor.enhance.ClassInstanceMethodsEnhancePluginDefine;
import com.ai.cloud.skywalking.plugin.interceptor.enhance.IntanceMethodsAroundInterceptor;
public abstract class HttpClientPluginDefine extends InterceptorPluginDefine {
public abstract class HttpClientPluginDefine extends ClassInstanceMethodsEnhancePluginDefine {
@Override
public IAroundInterceptor instance() {
public IntanceMethodsAroundInterceptor getInstanceMethodsInterceptor() {
return new HttpClientExecuteInterceptor();
}
......
......@@ -5,7 +5,7 @@ import com.ai.cloud.skywalking.plugin.interceptor.matcher.SimpleMethodMatcher;
public class InternalHttpClientPluginDefine extends HttpClientPluginDefine {
@Override
public MethodMatcher[] getBeInterceptedMethodsMatchers() {
public MethodMatcher[] getInstanceMethodsMatchers() {
return new MethodMatcher[]{new SimpleMethodMatcher("doExecute")};
}
......
......@@ -5,7 +5,7 @@ import com.ai.cloud.skywalking.plugin.interceptor.matcher.SimpleMethodMatcher;
public class MinimalHttpClientPluginDefine extends HttpClientPluginDefine {
@Override
public MethodMatcher[] getBeInterceptedMethodsMatchers() {
public MethodMatcher[] getInstanceMethodsMatchers() {
return new MethodMatcher[]{new SimpleMethodMatcher("doExecute")};
}
......
......@@ -3,16 +3,16 @@ package com.ai.cloud.skywalking.jedis.v2.plugin;
import com.ai.cloud.skywalking.buriedpoint.RPCBuriedPointSender;
import com.ai.cloud.skywalking.model.Identification;
import com.ai.cloud.skywalking.plugin.interceptor.EnhancedClassInstanceContext;
import com.ai.cloud.skywalking.plugin.interceptor.MethodInvokeContext;
import com.ai.cloud.skywalking.plugin.interceptor.assist.FirstInvokeInterceptor;
import com.ai.cloud.skywalking.plugin.interceptor.assist.SimpleObjectFirstInvokeInterceptor;
import com.ai.cloud.skywalking.plugin.interceptor.enhance.InstanceMethodInvokeContext;
public abstract class JedisBaseInterceptor extends FirstInvokeInterceptor {
public abstract class JedisBaseInterceptor extends SimpleObjectFirstInvokeInterceptor {
protected static final String REDIS_CONN_INFO_KEY = "redisClusterConnInfo";
private static RPCBuriedPointSender sender = new RPCBuriedPointSender();
@Override
public void beforeMethod(EnhancedClassInstanceContext context, MethodInvokeContext interceptorContext) {
public void beforeMethod(EnhancedClassInstanceContext context, InstanceMethodInvokeContext interceptorContext) {
if (this.isFirstBeforeMethod(context)) {
/**
* redis server wouldn't process rpc context. ignore the
......@@ -34,7 +34,7 @@ public abstract class JedisBaseInterceptor extends FirstInvokeInterceptor {
}
@Override
public Object afterMethod(EnhancedClassInstanceContext context, MethodInvokeContext interceptorContext, Object ret) {
public Object afterMethod(EnhancedClassInstanceContext context, InstanceMethodInvokeContext interceptorContext, Object ret) {
if (this.isLastAfterMethod(context)) {
sender.afterSend();
}
......@@ -42,7 +42,7 @@ public abstract class JedisBaseInterceptor extends FirstInvokeInterceptor {
}
@Override
public void handleMethodException(Throwable t, EnhancedClassInstanceContext context, MethodInvokeContext interceptorContext, Object ret) {
public void handleMethodException(Throwable t, EnhancedClassInstanceContext context, InstanceMethodInvokeContext interceptorContext, Object ret) {
sender.handleException(t);
}
}
package com.ai.cloud.skywalking.jedis.v2.plugin;
import com.ai.cloud.skywalking.plugin.interceptor.ConstructorInvokeContext;
import com.ai.cloud.skywalking.plugin.interceptor.EnhancedClassInstanceContext;
import com.ai.cloud.skywalking.plugin.interceptor.enhance.ConstructorInvokeContext;
import redis.clients.jedis.HostAndPort;
......
......@@ -4,8 +4,8 @@ import java.net.URI;
import redis.clients.jedis.JedisShardInfo;
import com.ai.cloud.skywalking.plugin.interceptor.ConstructorInvokeContext;
import com.ai.cloud.skywalking.plugin.interceptor.EnhancedClassInstanceContext;
import com.ai.cloud.skywalking.plugin.interceptor.enhance.ConstructorInvokeContext;
public class JedisInterceptor extends JedisBaseInterceptor {
......
package com.ai.cloud.skywalking.jedis.v2.plugin.define;
import com.ai.cloud.skywalking.jedis.v2.plugin.JedisClusterInterceptor;
import com.ai.cloud.skywalking.plugin.interceptor.IAroundInterceptor;
import com.ai.cloud.skywalking.plugin.interceptor.InterceptorPluginDefine;
import com.ai.cloud.skywalking.plugin.interceptor.MethodMatcher;
import com.ai.cloud.skywalking.plugin.interceptor.enhance.ClassInstanceMethodsEnhancePluginDefine;
import com.ai.cloud.skywalking.plugin.interceptor.enhance.IntanceMethodsAroundInterceptor;
import com.ai.cloud.skywalking.plugin.interceptor.matcher.AnyMethodsMatcher;
public class JedisClusterPluginDefine extends InterceptorPluginDefine {
public class JedisClusterPluginDefine extends ClassInstanceMethodsEnhancePluginDefine {
@Override
public String getBeInterceptedClassName() {
......@@ -14,14 +14,14 @@ public class JedisClusterPluginDefine extends InterceptorPluginDefine {
}
@Override
public MethodMatcher[] getBeInterceptedMethodsMatchers() {
public MethodMatcher[] getInstanceMethodsMatchers() {
return new MethodMatcher[]{
new AnyMethodsMatcher()
};
}
@Override
public IAroundInterceptor instance() {
public IntanceMethodsAroundInterceptor getInstanceMethodsInterceptor() {
return new JedisClusterInterceptor();
}
}
package com.ai.cloud.skywalking.jedis.v2.plugin.define;
import com.ai.cloud.skywalking.jedis.v2.plugin.JedisInterceptor;
import com.ai.cloud.skywalking.plugin.interceptor.IAroundInterceptor;
import com.ai.cloud.skywalking.plugin.interceptor.InterceptorPluginDefine;
import com.ai.cloud.skywalking.plugin.interceptor.MethodMatcher;
import com.ai.cloud.skywalking.plugin.interceptor.enhance.ClassInstanceMethodsEnhancePluginDefine;
import com.ai.cloud.skywalking.plugin.interceptor.enhance.IntanceMethodsAroundInterceptor;
import com.ai.cloud.skywalking.plugin.interceptor.matcher.MethodsExclusiveMatcher;
import com.ai.cloud.skywalking.plugin.interceptor.matcher.PrivateMethodMatcher;
import com.ai.cloud.skywalking.plugin.interceptor.matcher.SimpleMethodMatcher;
public class JedisPluginDefine extends InterceptorPluginDefine {
public class JedisPluginDefine extends ClassInstanceMethodsEnhancePluginDefine {
@Override
public String getBeInterceptedClassName() {
......@@ -16,7 +16,7 @@ public class JedisPluginDefine extends InterceptorPluginDefine {
}
@Override
public MethodMatcher[] getBeInterceptedMethodsMatchers() {
public MethodMatcher[] getInstanceMethodsMatchers() {
return new MethodMatcher[]{
new MethodsExclusiveMatcher(
new PrivateMethodMatcher(),
......@@ -32,7 +32,7 @@ public class JedisPluginDefine extends InterceptorPluginDefine {
}
@Override
public IAroundInterceptor instance() {
public IntanceMethodsAroundInterceptor getInstanceMethodsInterceptor() {
return new JedisInterceptor();
}
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册