完成自定义拦截插件
This commit is contained in:
parent
e52c50c06b
commit
fa2fdb4e95
|
|
@ -18,6 +18,12 @@ public class Config {
|
|||
public static boolean IS_PREMAIN_MODE = false;
|
||||
|
||||
public static String AGENT_BASE_PATH = "";
|
||||
|
||||
public static boolean SELF_DEFINE_METHOD_INTERCEPTOR = false;
|
||||
|
||||
public static String SELF_DEFINE_METHOD_PACKAGE = "";
|
||||
|
||||
public static boolean RECORD_PARAM = false;
|
||||
}
|
||||
|
||||
|
||||
|
|
|
|||
|
|
@ -60,11 +60,6 @@ public class Identification {
|
|||
return this;
|
||||
}
|
||||
|
||||
public IdentificationBuilder setParameter(String key, String value) {
|
||||
sendData.parameters.put(key, value);
|
||||
return this;
|
||||
}
|
||||
|
||||
public IdentificationBuilder addParameter(String value){
|
||||
parameterIdx++;
|
||||
sendData.parameters.put("_" + parameterIdx, value);
|
||||
|
|
|
|||
|
|
@ -29,7 +29,7 @@ public class ClassStaticMethodsInterceptor {
|
|||
public Object intercept(@Origin Class<?> clazz, @AllArguments Object[] allArguments, @Origin Method method, @SuperCall Callable<?> zuper) throws Exception {
|
||||
StaticMethodsAroundInterceptor interceptor = InterceptorInstanceLoader.load(staticMethodsAroundInterceptorClassName, clazz.getClassLoader());
|
||||
|
||||
MethodInvokeContext interceptorContext = new MethodInvokeContext(method.getName(), allArguments);
|
||||
MethodInvokeContext interceptorContext = new MethodInvokeContext(clazz,method.getName(), allArguments);
|
||||
MethodInterceptResult result = new MethodInterceptResult();
|
||||
try {
|
||||
interceptor.beforeMethod(interceptorContext, result);
|
||||
|
|
|
|||
|
|
@ -1,17 +1,17 @@
|
|||
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;
|
||||
}
|
||||
/**
|
||||
* 代理类实例
|
||||
*/
|
||||
private Object objInst;
|
||||
|
||||
InstanceMethodInvokeContext(Object objInst, String methodName, Object[] allArguments) {
|
||||
super(objInst.getClass(), methodName, allArguments);
|
||||
this.objInst = objInst;
|
||||
}
|
||||
|
||||
public Object inst() {
|
||||
return objInst;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -2,30 +2,43 @@ package com.ai.cloud.skywalking.plugin.interceptor.enhance;
|
|||
|
||||
/**
|
||||
* 方法执行拦截上下文
|
||||
*
|
||||
* @author wusheng
|
||||
*
|
||||
* @author wusheng
|
||||
*/
|
||||
public class MethodInvokeContext {
|
||||
/**
|
||||
* 方法名称
|
||||
*/
|
||||
private String methodName;
|
||||
/**
|
||||
* 方法参数
|
||||
*/
|
||||
private Object[] allArguments;
|
||||
|
||||
MethodInvokeContext(String methodName, Object[] allArguments) {
|
||||
this.methodName = methodName;
|
||||
this.allArguments = allArguments;
|
||||
}
|
||||
|
||||
public Object[] allArguments(){
|
||||
return this.allArguments;
|
||||
}
|
||||
|
||||
public String methodName(){
|
||||
return methodName;
|
||||
}
|
||||
/**
|
||||
* 方法名称
|
||||
*/
|
||||
private String methodName;
|
||||
/**
|
||||
* 方法参数
|
||||
*/
|
||||
private Object[] allArguments;
|
||||
|
||||
MethodInvokeContext(Class clazz, String methodName, Object[] allArguments) {
|
||||
this.methodName = appendMethodName(clazz, methodName, allArguments);
|
||||
this.allArguments = allArguments;
|
||||
}
|
||||
|
||||
public Object[] allArguments() {
|
||||
return this.allArguments;
|
||||
}
|
||||
|
||||
public String methodName() {
|
||||
return methodName;
|
||||
}
|
||||
|
||||
private String appendMethodName(Class clazz, String simpleMethodName, Object[] allArguments) {
|
||||
StringBuilder methodName = new StringBuilder(clazz.getName() + "." + simpleMethodName + "(");
|
||||
for (Object argument : allArguments) {
|
||||
methodName.append(argument.getClass() + ",");
|
||||
}
|
||||
|
||||
if (allArguments.length > 0){
|
||||
methodName.deleteCharAt(methodName.length() - 1);
|
||||
}
|
||||
|
||||
methodName.append(")");
|
||||
return methodName.toString();
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,6 +1,5 @@
|
|||
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.MethodMatcher;
|
||||
import com.ai.cloud.skywalking.plugin.interceptor.enhance.ClassInstanceMethodsEnhancePluginDefine;
|
||||
import com.ai.cloud.skywalking.plugin.interceptor.matcher.AnyMethodsMatcher;
|
||||
|
|
|
|||
|
|
@ -16,6 +16,7 @@
|
|||
<module>httpClient-4.x-plugin</module>
|
||||
<module>jedis-2.x-plugin</module>
|
||||
<module>tomcat-7.x-8.x-plugin</module>
|
||||
<module>self-define-interceptor-plugin</module>
|
||||
</modules>
|
||||
<packaging>pom</packaging>
|
||||
|
||||
|
|
|
|||
|
|
@ -0,0 +1,19 @@
|
|||
<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>
|
||||
<artifactId>skywalking-sdk-plugin</artifactId>
|
||||
<groupId>com.ai.cloud</groupId>
|
||||
<version>1.0-Final</version>
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<artifactId>self-define-interceptor-plugin</artifactId>
|
||||
<packaging>jar</packaging>
|
||||
|
||||
<name>self-define-interceptor-plugin</name>
|
||||
<url>http://maven.apache.org</url>
|
||||
|
||||
<properties>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
</properties>
|
||||
</project>
|
||||
|
|
@ -0,0 +1,69 @@
|
|||
package com.ai.cloud.skywalking.self.define.plugin;
|
||||
|
||||
import com.ai.cloud.skywalking.conf.Config;
|
||||
import com.ai.cloud.skywalking.invoke.monitor.LocalMethodInvokeMonitor;
|
||||
import com.ai.cloud.skywalking.model.Identification;
|
||||
import com.ai.cloud.skywalking.plugin.interceptor.EnhancedClassInstanceContext;
|
||||
import com.ai.cloud.skywalking.plugin.interceptor.enhance.*;
|
||||
import com.google.gson.Gson;
|
||||
|
||||
public class SelfDefineMethodInterceptor implements InstanceMethodsAroundInterceptor, StaticMethodsAroundInterceptor {
|
||||
|
||||
@Override
|
||||
public void onConstruct(EnhancedClassInstanceContext context, ConstructorInvokeContext interceptorContext) {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void beforeMethod(EnhancedClassInstanceContext context, InstanceMethodInvokeContext interceptorContext,
|
||||
MethodInterceptResult result) {
|
||||
Identification.IdentificationBuilder identificationBuilder = buildIdentificationBuilder(interceptorContext);
|
||||
new LocalMethodInvokeMonitor().beforeInvoke(identificationBuilder.build());
|
||||
}
|
||||
|
||||
private Identification.IdentificationBuilder buildIdentificationBuilder(MethodInvokeContext interceptorContext) {
|
||||
Identification.IdentificationBuilder identificationBuilder = Identification.newBuilder();
|
||||
if (Config.SkyWalking.RECORD_PARAM) {
|
||||
for (Object param : interceptorContext.allArguments()) {
|
||||
String paramStr;
|
||||
try {
|
||||
paramStr = new Gson().toJson(param);
|
||||
} catch (Exception e) {
|
||||
paramStr = "N/A";
|
||||
}
|
||||
identificationBuilder.addParameter(paramStr);
|
||||
}
|
||||
}
|
||||
|
||||
identificationBuilder.spanType(new SelfDefineSpanType()).viewPoint(interceptorContext.methodName());
|
||||
return identificationBuilder;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object afterMethod(EnhancedClassInstanceContext context, InstanceMethodInvokeContext interceptorContext,
|
||||
Object ret) {
|
||||
new LocalMethodInvokeMonitor().afterInvoke();
|
||||
return ret;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void handleMethodException(Throwable t, EnhancedClassInstanceContext context,
|
||||
InstanceMethodInvokeContext interceptorContext) {
|
||||
new LocalMethodInvokeMonitor().occurException(t);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void beforeMethod(MethodInvokeContext interceptorContext, MethodInterceptResult result) {
|
||||
new LocalMethodInvokeMonitor().beforeInvoke(buildIdentificationBuilder(interceptorContext).build());
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object afterMethod(MethodInvokeContext interceptorContext, Object ret) {
|
||||
new LocalMethodInvokeMonitor().afterInvoke();
|
||||
return ret;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void handleMethodException(Throwable t, MethodInvokeContext interceptorContext) {
|
||||
new LocalMethodInvokeMonitor().occurException(t);
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,16 @@
|
|||
package com.ai.cloud.skywalking.self.define.plugin;
|
||||
|
||||
import com.ai.cloud.skywalking.api.IBuriedPointType;
|
||||
import com.ai.cloud.skywalking.protocol.common.CallType;
|
||||
|
||||
public class SelfDefineSpanType implements IBuriedPointType {
|
||||
@Override
|
||||
public String getTypeName() {
|
||||
return "L";
|
||||
}
|
||||
|
||||
@Override
|
||||
public CallType getCallType() {
|
||||
return CallType.SYNC;
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,47 @@
|
|||
package com.ai.cloud.skywalking.self.define.plugin.define;
|
||||
|
||||
import com.ai.cloud.skywalking.conf.Config;
|
||||
import com.ai.cloud.skywalking.plugin.PluginException;
|
||||
import com.ai.cloud.skywalking.plugin.interceptor.MethodMatcher;
|
||||
import com.ai.cloud.skywalking.plugin.interceptor.enhance.ClassEnhancePluginDefine;
|
||||
import com.ai.cloud.skywalking.plugin.interceptor.matcher.AnyMethodsMatcher;
|
||||
import net.bytebuddy.dynamic.DynamicType;
|
||||
|
||||
public class SelfDefineMethodPluginDefine extends ClassEnhancePluginDefine {
|
||||
|
||||
@Override
|
||||
protected DynamicType.Builder<?> enhance(String enhanceOriginClassName, DynamicType.Builder<?> newClassBuilder)
|
||||
throws PluginException {
|
||||
return Config.SkyWalking.SELF_DEFINE_METHOD_INTERCEPTOR ?
|
||||
super.enhance(enhanceOriginClassName, newClassBuilder) :
|
||||
newClassBuilder;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected MethodMatcher[] getInstanceMethodsMatchers() {
|
||||
return new MethodMatcher[] {new AnyMethodsMatcher()};
|
||||
}
|
||||
|
||||
@Override
|
||||
protected String getInstanceMethodsInterceptor() {
|
||||
return "com.ai.cloud.skywalking.self.define.plugin.SelfDefineMethodInterceptor";
|
||||
}
|
||||
|
||||
@Override
|
||||
protected MethodMatcher[] getStaticMethodsMatchers() {
|
||||
return new MethodMatcher[] {new AnyMethodsMatcher()};
|
||||
}
|
||||
|
||||
@Override
|
||||
protected String getStaticMethodsInterceptor() {
|
||||
return "com.ai.cloud.skywalking.self.define.plugin.SelfDefineMethodInterceptor";
|
||||
}
|
||||
|
||||
@Override
|
||||
protected String enhanceClassName() {
|
||||
if (!Config.SkyWalking.SELF_DEFINE_METHOD_PACKAGE.endsWith(".*")) {
|
||||
return Config.SkyWalking.SELF_DEFINE_METHOD_PACKAGE + ".*";
|
||||
}
|
||||
return Config.SkyWalking.SELF_DEFINE_METHOD_PACKAGE;
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1 @@
|
|||
com.ai.cloud.skywalking.self.define.plugin.define.SelfDefineMethodPluginDefine
|
||||
Loading…
Reference in New Issue