diff --git a/skywalking-collector/skywalking-agent/pom.xml b/skywalking-collector/skywalking-agent/pom.xml index d5479b675..f975b5554 100644 --- a/skywalking-collector/skywalking-agent/pom.xml +++ b/skywalking-collector/skywalking-agent/pom.xml @@ -17,8 +17,6 @@ UTF-8 com.ai.cloud.skywalking.agent.SkywalkingAgent - net.bytebuddy - com.ai.cloud.skywalking.api.dependencies.net.bytebuddy io.netty com.ai.cloud.skywalking.api.dependencies.io.netty com.google.protobuf @@ -85,10 +83,6 @@ - - ${shade.net.bytebuddy.source} - ${shade.net.bytebuddy.target} - ${shade.io.netty.source} ${shade.io.netty.target} diff --git a/skywalking-collector/skywalking-agent/src/main/java/com/ai/cloud/skywalking/agent/SkywalkingAgent.java b/skywalking-collector/skywalking-agent/src/main/java/com/ai/cloud/skywalking/agent/SkywalkingAgent.java index 7aab8ae7b..be1e39bab 100644 --- a/skywalking-collector/skywalking-agent/src/main/java/com/ai/cloud/skywalking/agent/SkywalkingAgent.java +++ b/skywalking-collector/skywalking-agent/src/main/java/com/ai/cloud/skywalking/agent/SkywalkingAgent.java @@ -1,23 +1,35 @@ package com.ai.cloud.skywalking.agent; +import com.ai.cloud.skywalking.agent.transformer.PluginsTransformer; +import com.ai.cloud.skywalking.conf.AuthDesc; import com.ai.cloud.skywalking.conf.Config; import com.ai.cloud.skywalking.conf.ConfigInitializer; +import com.ai.cloud.skywalking.plugin.IPlugin; import com.ai.cloud.skywalking.plugin.PluginBootstrap; +import com.ai.cloud.skywalking.plugin.PluginCfg; +import com.ai.cloud.skywalking.plugin.interceptor.AbstractClassEnhancePluginDefine; +import com.ai.cloud.skywalking.plugin.interceptor.enhance.ClassEnhancePluginDefine; import com.ai.cloud.skywalking.transformer.ClassTransformer; import java.lang.instrument.Instrumentation; +import java.util.List; +import java.util.Map; public class SkywalkingAgent { public static void premain(String agentArgs, Instrumentation inst) { ConfigInitializer.initialize(); + PluginBootstrap bootstrap = new PluginBootstrap(); + Map pluginDefineMap = bootstrap.loadPlugins(); + + if (AuthDesc.isAuth()) { + inst.addTransformer(new PluginsTransformer(pluginDefineMap)); + } + if (Config.SkyWalking.ALL_METHOD_MONITOR) { String interceptorPackage = System.getProperty("interceptor.package", ""); inst.addTransformer(new ClassTransformer(interceptorPackage)); } - - PluginBootstrap bootstrap = new PluginBootstrap(); - bootstrap.start(); } } diff --git a/skywalking-collector/skywalking-agent/src/main/java/com/ai/cloud/skywalking/agent/transformer/PluginsTransformer.java b/skywalking-collector/skywalking-agent/src/main/java/com/ai/cloud/skywalking/agent/transformer/PluginsTransformer.java index 4a1552a77..60ed15f29 100644 --- a/skywalking-collector/skywalking-agent/src/main/java/com/ai/cloud/skywalking/agent/transformer/PluginsTransformer.java +++ b/skywalking-collector/skywalking-agent/src/main/java/com/ai/cloud/skywalking/agent/transformer/PluginsTransformer.java @@ -1,7 +1,55 @@ package com.ai.cloud.skywalking.agent.transformer; -/** - * Created by xin on 16/7/24. - */ -public class PluginsTransformer { +import com.ai.cloud.skywalking.logging.LogManager; +import com.ai.cloud.skywalking.logging.Logger; +import com.ai.cloud.skywalking.plugin.interceptor.enhance.ClassEnhancePluginDefine; +import javassist.ClassPool; +import javassist.CtClass; + +import java.lang.instrument.ClassFileTransformer; +import java.lang.instrument.IllegalClassFormatException; +import java.security.ProtectionDomain; +import java.util.Map; + +public class PluginsTransformer implements ClassFileTransformer { + + private Logger logger = LogManager.getLogger(PluginsTransformer.class); + + private Map pluginDefineMap; + + + public PluginsTransformer(Map pluginDefineMap) { + this.pluginDefineMap = pluginDefineMap; + } + + @Override + public byte[] transform(ClassLoader loader, String className, Class classBeingRedefined, ProtectionDomain protectionDomain, byte[] classfileBuffer) throws IllegalClassFormatException { + if (pluginDefineMap.isEmpty()) { + return classfileBuffer; + } + + if (className.startsWith("com/ai/cloud/skywalking")){ + return classfileBuffer; + } + + ClassEnhancePluginDefine pluginDefine = pluginDefineMap.get(className.replaceAll("/", ".")); + if (pluginDefine != null) { + ClassPool classPool = ClassPool.getDefault(); + try { + CtClass ctClass = classPool.get(className.replaceAll("/", ".")); + if (ctClass.isInterface()) { + return classfileBuffer; + } + + pluginDefine.enhance(ctClass); + + return ctClass.toBytecode(); + } catch (Exception e) { + logger.error("Failed to enhance class[" + className + "]", e); + return classfileBuffer; + } + } + + return classfileBuffer; + } } diff --git a/skywalking-collector/skywalking-api/pom.xml b/skywalking-collector/skywalking-api/pom.xml index f5876477c..d27bf42ee 100644 --- a/skywalking-collector/skywalking-api/pom.xml +++ b/skywalking-collector/skywalking-api/pom.xml @@ -26,12 +26,6 @@ 1.0-Final - - net.bytebuddy - byte-buddy - 1.3.0 - - io.netty netty-all diff --git a/skywalking-collector/skywalking-api/src/main/java/com/ai/cloud/skywalking/plugin/IPlugin.java b/skywalking-collector/skywalking-api/src/main/java/com/ai/cloud/skywalking/plugin/IPlugin.java index ecfadcb2f..00956556a 100644 --- a/skywalking-collector/skywalking-api/src/main/java/com/ai/cloud/skywalking/plugin/IPlugin.java +++ b/skywalking-collector/skywalking-api/src/main/java/com/ai/cloud/skywalking/plugin/IPlugin.java @@ -1,6 +1,8 @@ package com.ai.cloud.skywalking.plugin; +import com.ai.cloud.skywalking.plugin.exception.PluginException; + public interface IPlugin { - public void define() throws PluginException; + byte[] define() throws PluginException; } diff --git a/skywalking-collector/skywalking-api/src/main/java/com/ai/cloud/skywalking/plugin/PluginBootstrap.java b/skywalking-collector/skywalking-api/src/main/java/com/ai/cloud/skywalking/plugin/PluginBootstrap.java index 94e82d381..16c748ad7 100644 --- a/skywalking-collector/skywalking-api/src/main/java/com/ai/cloud/skywalking/plugin/PluginBootstrap.java +++ b/skywalking-collector/skywalking-api/src/main/java/com/ai/cloud/skywalking/plugin/PluginBootstrap.java @@ -3,54 +3,57 @@ package com.ai.cloud.skywalking.plugin; import com.ai.cloud.skywalking.conf.AuthDesc; import com.ai.cloud.skywalking.logging.LogManager; import com.ai.cloud.skywalking.logging.Logger; -import net.bytebuddy.pool.TypePool; +import com.ai.cloud.skywalking.plugin.interceptor.enhance.ClassEnhancePluginDefine; +import javassist.ClassPool; import java.net.URL; +import java.util.HashMap; import java.util.List; +import java.util.Map; public class PluginBootstrap { private static Logger logger = LogManager.getLogger(PluginBootstrap.class); - public static TypePool CLASS_TYPE_POOL = null; + public static ClassPool CLASS_TYPE_POOL = null; - public void start() { + public Map loadPlugins() { if (!AuthDesc.isAuth()) { - return; + return null; } - CLASS_TYPE_POOL = TypePool.Default.ofClassPath(); + CLASS_TYPE_POOL = ClassPool.getDefault(); PluginResourcesResolver resolver = new PluginResourcesResolver(); List resources = resolver.getResources(); if (resources == null || resources.size() == 0) { logger.info("no plugin files (skywalking-plugin.properties) found, continue to start application."); - return; + return new HashMap(); } for (URL pluginUrl : resources) { try { PluginCfg.CFG.load(pluginUrl.openStream()); } catch (Throwable t) { - logger.error("plugin [{}] init failure.", new Object[]{pluginUrl}, t); + logger.error("plugin [{}] init failure.", new Object[] {pluginUrl}, t); } } - List pluginClassList = PluginCfg.CFG - .getPluginClassList(); + List pluginClassList = PluginCfg.CFG.getPluginClassList(); + Map pluginDefineMap = new HashMap(); for (String pluginClassName : pluginClassList) { try { - logger.debug("prepare to enhance class by plugin {}.", - pluginClassName); - IPlugin plugin = (IPlugin) Class.forName( - pluginClassName).newInstance(); - plugin.define(); + logger.debug("prepare to enhance class by plugin {}.", pluginClassName); + IPlugin plugin = (IPlugin) Class.forName(pluginClassName).newInstance(); + if (plugin instanceof ClassEnhancePluginDefine) { + pluginDefineMap.put(pluginClassName, (ClassEnhancePluginDefine) plugin); + } } catch (Throwable t) { - logger.error("prepare to enhance class by plugin [{}] failure.", - new Object[]{pluginClassName}, t); + logger.error("prepare to enhance class by plugin [{}] failure.", new Object[] {pluginClassName}, t); } } + return pluginDefineMap; } } diff --git a/skywalking-collector/skywalking-api/src/main/java/com/ai/cloud/skywalking/plugin/TracingBootstrap.java b/skywalking-collector/skywalking-api/src/main/java/com/ai/cloud/skywalking/plugin/TracingBootstrap.java index aa17bb438..d0f8e4b67 100644 --- a/skywalking-collector/skywalking-api/src/main/java/com/ai/cloud/skywalking/plugin/TracingBootstrap.java +++ b/skywalking-collector/skywalking-api/src/main/java/com/ai/cloud/skywalking/plugin/TracingBootstrap.java @@ -29,7 +29,7 @@ public class TracingBootstrap { try { PluginBootstrap bootstrap = new PluginBootstrap(); - bootstrap.start(); + bootstrap.loadPlugins(); } catch (Throwable t) { logger.error("PluginBootstrap start failure.", t); } diff --git a/skywalking-collector/skywalking-api/src/main/java/com/ai/cloud/skywalking/plugin/boot/BootException.java b/skywalking-collector/skywalking-api/src/main/java/com/ai/cloud/skywalking/plugin/boot/BootException.java index 95b17f9f9..4bd7c5d4e 100644 --- a/skywalking-collector/skywalking-api/src/main/java/com/ai/cloud/skywalking/plugin/boot/BootException.java +++ b/skywalking-collector/skywalking-api/src/main/java/com/ai/cloud/skywalking/plugin/boot/BootException.java @@ -1,6 +1,6 @@ package com.ai.cloud.skywalking.plugin.boot; -import com.ai.cloud.skywalking.plugin.PluginException; +import com.ai.cloud.skywalking.plugin.exception.PluginException; public class BootException extends PluginException { private static final long serialVersionUID = 8618884011525098003L; diff --git a/skywalking-collector/skywalking-api/src/main/java/com/ai/cloud/skywalking/plugin/boot/BootPluginDefine.java b/skywalking-collector/skywalking-api/src/main/java/com/ai/cloud/skywalking/plugin/boot/BootPluginDefine.java index b0a61a3fe..480f38a62 100644 --- a/skywalking-collector/skywalking-api/src/main/java/com/ai/cloud/skywalking/plugin/boot/BootPluginDefine.java +++ b/skywalking-collector/skywalking-api/src/main/java/com/ai/cloud/skywalking/plugin/boot/BootPluginDefine.java @@ -1,15 +1,15 @@ package com.ai.cloud.skywalking.plugin.boot; import com.ai.cloud.skywalking.plugin.IPlugin; -import com.ai.cloud.skywalking.plugin.PluginException; +import com.ai.cloud.skywalking.plugin.exception.PluginException; public abstract class BootPluginDefine implements IPlugin { @Override - public void define() throws PluginException { - this.boot(); + public byte[] define() throws PluginException { + return this.boot(); } - protected abstract void boot() throws BootException; + protected abstract byte[] boot() throws BootException; } diff --git a/skywalking-collector/skywalking-api/src/main/java/com/ai/cloud/skywalking/plugin/exception/EnhanceClassEmptyException.java b/skywalking-collector/skywalking-api/src/main/java/com/ai/cloud/skywalking/plugin/exception/EnhanceClassEmptyException.java index eb25bd0de..46de8b78e 100644 --- a/skywalking-collector/skywalking-api/src/main/java/com/ai/cloud/skywalking/plugin/exception/EnhanceClassEmptyException.java +++ b/skywalking-collector/skywalking-api/src/main/java/com/ai/cloud/skywalking/plugin/exception/EnhanceClassEmptyException.java @@ -1,7 +1,12 @@ package com.ai.cloud.skywalking.plugin.exception; -/** - * Created by xin on 16/7/25. - */ -public class EnhanceClassEmptyException { +public class EnhanceClassEmptyException extends PluginException{ + + public EnhanceClassEmptyException(String message) { + super(message); + } + + public EnhanceClassEmptyException(String message, Throwable cause) { + super(message, cause); + } } diff --git a/skywalking-collector/skywalking-api/src/main/java/com/ai/cloud/skywalking/plugin/exception/EnhanceClassNotFoundException.java b/skywalking-collector/skywalking-api/src/main/java/com/ai/cloud/skywalking/plugin/exception/EnhanceClassNotFoundException.java index ef7ac0a23..c13bfc90e 100644 --- a/skywalking-collector/skywalking-api/src/main/java/com/ai/cloud/skywalking/plugin/exception/EnhanceClassNotFoundException.java +++ b/skywalking-collector/skywalking-api/src/main/java/com/ai/cloud/skywalking/plugin/exception/EnhanceClassNotFoundException.java @@ -1,7 +1,8 @@ package com.ai.cloud.skywalking.plugin.exception; -/** - * Created by xin on 16/7/25. - */ -public class EnhanceClassNotFoundException { +public class EnhanceClassNotFoundException extends PluginException{ + + public EnhanceClassNotFoundException(String message) { + super(message); + } } diff --git a/skywalking-collector/skywalking-api/src/main/java/com/ai/cloud/skywalking/plugin/exception/WitnessClassesCannotFound.java b/skywalking-collector/skywalking-api/src/main/java/com/ai/cloud/skywalking/plugin/exception/WitnessClassesCannotFound.java index bb12bbba3..ee4cfad8b 100644 --- a/skywalking-collector/skywalking-api/src/main/java/com/ai/cloud/skywalking/plugin/exception/WitnessClassesCannotFound.java +++ b/skywalking-collector/skywalking-api/src/main/java/com/ai/cloud/skywalking/plugin/exception/WitnessClassesCannotFound.java @@ -3,5 +3,8 @@ package com.ai.cloud.skywalking.plugin.exception; /** * Created by xin on 16/7/25. */ -public class WitnessClassesCannotFound { +public class WitnessClassesCannotFound extends PluginException { + public WitnessClassesCannotFound(String message) { + super(message); + } } diff --git a/skywalking-collector/skywalking-api/src/main/java/com/ai/cloud/skywalking/plugin/interceptor/AbstractClassEnhancePluginDefine.java b/skywalking-collector/skywalking-api/src/main/java/com/ai/cloud/skywalking/plugin/interceptor/AbstractClassEnhancePluginDefine.java index 4cff5b2e1..7c19c8edd 100644 --- a/skywalking-collector/skywalking-api/src/main/java/com/ai/cloud/skywalking/plugin/interceptor/AbstractClassEnhancePluginDefine.java +++ b/skywalking-collector/skywalking-api/src/main/java/com/ai/cloud/skywalking/plugin/interceptor/AbstractClassEnhancePluginDefine.java @@ -3,13 +3,13 @@ package com.ai.cloud.skywalking.plugin.interceptor; import com.ai.cloud.skywalking.logging.LogManager; import com.ai.cloud.skywalking.logging.Logger; import com.ai.cloud.skywalking.plugin.IPlugin; -import com.ai.cloud.skywalking.plugin.PluginException; +import com.ai.cloud.skywalking.plugin.exception.EnhanceClassEmptyException; +import com.ai.cloud.skywalking.plugin.exception.EnhanceClassNotFoundException; +import com.ai.cloud.skywalking.plugin.exception.PluginException; +import com.ai.cloud.skywalking.plugin.exception.WitnessClassesCannotFound; import com.ai.cloud.skywalking.protocol.util.StringUtil; -import net.bytebuddy.ByteBuddy; -import net.bytebuddy.dynamic.ClassFileLocator; -import net.bytebuddy.dynamic.DynamicType; -import net.bytebuddy.dynamic.loading.ClassLoadingStrategy; -import net.bytebuddy.pool.TypePool.Resolution; +import javassist.ClassPool; +import javassist.CtClass; import static com.ai.cloud.skywalking.plugin.PluginBootstrap.CLASS_TYPE_POOL; @@ -17,64 +17,58 @@ public abstract class AbstractClassEnhancePluginDefine implements IPlugin { private static Logger logger = LogManager.getLogger(AbstractClassEnhancePluginDefine.class); @Override - public void define() throws PluginException { + public byte[] define() throws PluginException { String interceptorDefineClassName = this.getClass().getName(); String enhanceOriginClassName = enhanceClassName(); if (StringUtil.isEmpty(enhanceOriginClassName)) { - logger.warn("classname of being intercepted is not defined by {}.", - interceptorDefineClassName); - return; + logger.warn("classname of being intercepted is not defined by {}.", interceptorDefineClassName); + throw new EnhanceClassEmptyException("class name of being is not deined by " + interceptorDefineClassName); } - logger.debug("prepare to enhance class {} by {}.", - enhanceOriginClassName, interceptorDefineClassName); + logger.debug("prepare to enhance class {} by {}.", enhanceOriginClassName, interceptorDefineClassName); - Resolution resolution = CLASS_TYPE_POOL.describe(enhanceOriginClassName); - if (!resolution.isResolved()) { - logger.warn("class {} can't be resolved, enhance by {} failue.", - enhanceOriginClassName, interceptorDefineClassName); - return; + CtClass ctClass = findEnhanceClasses(interceptorDefineClassName, enhanceOriginClassName); + + if (ctClass == null) { + logger.warn("class {} can't be resolved, enhance by {} failue.", enhanceOriginClassName, interceptorDefineClassName); + throw new EnhanceClassNotFoundException("class " + enhanceOriginClassName + " can't be resolved, enhance by " + interceptorDefineClassName + " failue."); } /** * find witness classes for enhance class */ String[] witnessClasses = witnessClasses(); - if(witnessClasses != null) { - for (String witnessClass : witnessClasses) { - Resolution witnessClassResolution = CLASS_TYPE_POOL.describe(witnessClass); - if (!witnessClassResolution.isResolved()) { - logger.warn("enhance class {} by plugin {} is not working. Because witness class {} is not existed.", enhanceOriginClassName, interceptorDefineClassName, witnessClass); - return; + if (witnessClasses != null) { + for (String witnessClassName : witnessClasses) { + try { + CtClass witnessClass = CLASS_TYPE_POOL.get(witnessClassName); + if (witnessClass != null) { + logger.warn("enhance class {} by plugin {} is not working. Because witness class {} is not existed.", enhanceOriginClassName, interceptorDefineClassName, + witnessClass); + throw new WitnessClassesCannotFound( + "enhance class " + enhanceOriginClassName + " by plugin " + interceptorDefineClassName + " is not working. Because witness class " + witnessClass + + " is not existed."); + } + } catch (Exception e) { + } } } - /** - * find origin class source code for interceptor - */ - DynamicType.Builder newClassBuilder = new ByteBuddy() - .rebase(resolution.resolve(), - ClassFileLocator.ForClassLoader.ofClassPath()); - - newClassBuilder = this.enhance(enhanceOriginClassName, newClassBuilder); - - /** - * 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); + return enhance(ctClass); } - protected abstract DynamicType.Builder enhance(String enhanceOriginClassName, DynamicType.Builder newClassBuilder) throws PluginException; + private CtClass findEnhanceClasses(String interceptorDefineClassName, String enhanceOriginClassName) throws EnhanceClassNotFoundException { + try { + ClassPool classPool = ClassPool.getDefault(); + return classPool.get(enhanceOriginClassName); + } catch (Exception e) { + throw new EnhanceClassNotFoundException("class " + enhanceOriginClassName + " can't be resolved, enhance by " + interceptorDefineClassName + " failue."); + } + } + + protected abstract byte[] enhance(CtClass ctClass) throws PluginException; /** * 返回要被增强的类,应当返回类全名 @@ -89,7 +83,7 @@ public abstract class AbstractClassEnhancePluginDefine implements IPlugin { * * @return */ - protected String[] witnessClasses(){ - return new String[]{}; + protected String[] witnessClasses() { + return new String[] {}; } } diff --git a/skywalking-collector/skywalking-api/src/main/java/com/ai/cloud/skywalking/plugin/interceptor/EnhanceException.java b/skywalking-collector/skywalking-api/src/main/java/com/ai/cloud/skywalking/plugin/interceptor/EnhanceException.java index 1cdd22061..0181cb01d 100644 --- a/skywalking-collector/skywalking-api/src/main/java/com/ai/cloud/skywalking/plugin/interceptor/EnhanceException.java +++ b/skywalking-collector/skywalking-api/src/main/java/com/ai/cloud/skywalking/plugin/interceptor/EnhanceException.java @@ -1,6 +1,6 @@ package com.ai.cloud.skywalking.plugin.interceptor; -import com.ai.cloud.skywalking.plugin.PluginException; +import com.ai.cloud.skywalking.plugin.exception.PluginException; public class EnhanceException extends PluginException { private static final long serialVersionUID = -2234782755784217255L; diff --git a/skywalking-collector/skywalking-api/src/main/java/com/ai/cloud/skywalking/plugin/interceptor/MethodMatcher.java b/skywalking-collector/skywalking-api/src/main/java/com/ai/cloud/skywalking/plugin/interceptor/MethodMatcher.java index 2375b270c..33df29d21 100644 --- a/skywalking-collector/skywalking-api/src/main/java/com/ai/cloud/skywalking/plugin/interceptor/MethodMatcher.java +++ b/skywalking-collector/skywalking-api/src/main/java/com/ai/cloud/skywalking/plugin/interceptor/MethodMatcher.java @@ -1,9 +1,6 @@ package com.ai.cloud.skywalking.plugin.interceptor; -import net.bytebuddy.description.method.MethodDescription; -import net.bytebuddy.matcher.ElementMatcher; - -import static net.bytebuddy.matcher.ElementMatchers.*; +import javassist.CtMethod; public abstract class MethodMatcher { @@ -45,48 +42,29 @@ public abstract class MethodMatcher { this.modifier = modifier; } - public abstract ElementMatcher.Junction buildMatcher(); - protected String getMethodMatchDescribe() { return methodMatchDescribe; } - protected ElementMatcher.Junction mergeArgumentsIfNecessary(ElementMatcher.Junction matcher) { - if (argTypeArray != null) { - matcher = matcher.and(takesArguments(argTypeArray)); - } - if (argNum > -1) { - matcher = matcher.and(takesArguments(argNum)); - } - - if (modifier != null) { - matcher = matcher.and(modifier.elementMatcher()); - } - - return matcher; - } + public abstract boolean match(CtMethod ctMethod); public enum Modifier { - Public, Default, Private, Protected; + Public(0x00000001), + Default(0x00000000), + Private(0x00000002), + Protected(0x00000004); - private ElementMatcher.Junction elementMatcher() { - switch (this) { - case Private: { - return isPrivate(); - } - case Default: { - return isPackagePrivate(); - } - case Public: { - return isPublic(); - } - case Protected: { - return isProtected(); - } - default: - return isPublic(); - } + private int value; + + + Modifier(int value) { + this.value = value; + } + + + public int getValue() { + return value; } } @@ -94,7 +72,7 @@ public abstract class MethodMatcher { public String toString() { StringBuilder stringBuilder = new StringBuilder("method name=" + getMethodMatchDescribe()); if (getModifier() != null) { - stringBuilder.insert(0, getModifier() + " "); + stringBuilder.insert(0, getModifier() + " "); } if (getArgNum() > -1) { @@ -105,11 +83,11 @@ public abstract class MethodMatcher { stringBuilder.append(", types of arguments are "); boolean isFirst = true; for (Class argType : getArgTypeArray()) { - if(isFirst){ - isFirst = false; - }else{ - stringBuilder.append(","); - } + if (isFirst) { + isFirst = false; + } else { + stringBuilder.append(","); + } stringBuilder.append(argType.getName()); } } diff --git a/skywalking-collector/skywalking-api/src/main/java/com/ai/cloud/skywalking/plugin/interceptor/enhance/ClassConstructorInterceptor.java b/skywalking-collector/skywalking-api/src/main/java/com/ai/cloud/skywalking/plugin/interceptor/enhance/ClassConstructorInterceptor.java index c85d925bc..8034bf9ce 100644 --- a/skywalking-collector/skywalking-api/src/main/java/com/ai/cloud/skywalking/plugin/interceptor/enhance/ClassConstructorInterceptor.java +++ b/skywalking-collector/skywalking-api/src/main/java/com/ai/cloud/skywalking/plugin/interceptor/enhance/ClassConstructorInterceptor.java @@ -2,37 +2,27 @@ package com.ai.cloud.skywalking.plugin.interceptor.enhance; import com.ai.cloud.skywalking.logging.LogManager; import com.ai.cloud.skywalking.logging.Logger; -import net.bytebuddy.implementation.bind.annotation.AllArguments; -import net.bytebuddy.implementation.bind.annotation.FieldProxy; -import net.bytebuddy.implementation.bind.annotation.RuntimeType; -import net.bytebuddy.implementation.bind.annotation.This; - import com.ai.cloud.skywalking.plugin.interceptor.EnhancedClassInstanceContext; public class ClassConstructorInterceptor { - private static Logger logger = LogManager - .getLogger(ClassConstructorInterceptor.class); + private static Logger logger = LogManager.getLogger(ClassConstructorInterceptor.class); - private InstanceMethodsAroundInterceptor interceptor; + private InstanceMethodsAroundInterceptor interceptor; - public ClassConstructorInterceptor(InstanceMethodsAroundInterceptor interceptor) { - this.interceptor = interceptor; - } + public ClassConstructorInterceptor(InstanceMethodsAroundInterceptor interceptor) { + this.interceptor = interceptor; + } - @RuntimeType - public void intercept( - @This Object obj, - @FieldProxy(ClassEnhancePluginDefine.contextAttrName) FieldSetter accessor, - @AllArguments Object[] allArguments) { - try { - EnhancedClassInstanceContext context = new EnhancedClassInstanceContext(); - accessor.setValue(context); - ConstructorInvokeContext interceptorContext = new ConstructorInvokeContext(obj, - allArguments); - interceptor.onConstruct(context, interceptorContext); - } catch (Throwable t) { - logger.error("ClassConstructorInterceptor failue.", t); - } + public void intercept(Object obj, Object instanceContext, Object[] allArguments) { + try { + EnhancedClassInstanceContext context = new EnhancedClassInstanceContext(); + // accessor.setValue(context); + instanceContext = context; + ConstructorInvokeContext interceptorContext = new ConstructorInvokeContext(obj, allArguments); + interceptor.onConstruct(context, interceptorContext); + } catch (Throwable t) { + logger.error("ClassConstructorInterceptor failue.", t); + } - } + } } diff --git a/skywalking-collector/skywalking-api/src/main/java/com/ai/cloud/skywalking/plugin/interceptor/enhance/ClassEnhancePluginDefine.java b/skywalking-collector/skywalking-api/src/main/java/com/ai/cloud/skywalking/plugin/interceptor/enhance/ClassEnhancePluginDefine.java index 28d55a313..b48f162f2 100644 --- a/skywalking-collector/skywalking-api/src/main/java/com/ai/cloud/skywalking/plugin/interceptor/enhance/ClassEnhancePluginDefine.java +++ b/skywalking-collector/skywalking-api/src/main/java/com/ai/cloud/skywalking/plugin/interceptor/enhance/ClassEnhancePluginDefine.java @@ -1,182 +1,129 @@ package com.ai.cloud.skywalking.plugin.interceptor.enhance; -import static net.bytebuddy.matcher.ElementMatchers.any; -import static net.bytebuddy.matcher.ElementMatchers.not; - import com.ai.cloud.skywalking.logging.LogManager; import com.ai.cloud.skywalking.logging.Logger; -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 com.ai.cloud.skywalking.plugin.PluginException; +import com.ai.cloud.skywalking.plugin.exception.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; +import javassist.*; + +import java.lang.reflect.Modifier; public abstract class ClassEnhancePluginDefine extends AbstractClassEnhancePluginDefine { - private static Logger logger = LogManager - .getLogger(ClassEnhancePluginDefine.class); + private static Logger logger = LogManager.getLogger(ClassEnhancePluginDefine.class); - public static final String contextAttrName = "_$EnhancedClassInstanceContext"; + 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; - } + public byte[] enhance(CtClass ctClass) throws PluginException { + try { + CtMethod[] ctMethod = ctClass.getDeclaredMethods(); + for (CtMethod method : ctMethod) { + if (Modifier.isStatic(method.getModifiers())) { + this.enhanceClass(ctClass, method); + } else { + this.enhanceInstance(ctClass, method); + } + } - private DynamicType.Builder enhanceInstance(String enhanceOriginClassName, - DynamicType.Builder newClassBuilder) throws PluginException { - MethodMatcher[] methodMatchers = getInstanceMethodsMatchers(); - if(methodMatchers == null){ - return newClassBuilder; - } - - - /** - * alter class source code.
- * - * new class need:
- * 1.add field '_$EnhancedClassInstanceContext' of type - * EnhancedClassInstanceContext
- * - * 2.intercept constructor by default, and intercept method which it's - * required by interceptorDefineClass.
- */ - InstanceMethodsAroundInterceptor interceptor = getInstanceMethodsInterceptor(); - if (interceptor == null) { - throw new EnhanceException("no InstanceMethodsAroundInterceptor instance. "); - } + return ctClass.toBytecode(); + } catch (Exception e) { + throw new PluginException("Can not compile the class", e); + } + } - 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)))); + private void enhanceClass(CtClass ctClass, CtMethod method) throws CannotCompileException, NotFoundException { + boolean isMatch = false; + for (MethodMatcher methodMatcher : getStaticMethodsMatchers()) { + if (methodMatcher.match(method)) { + isMatch = true; + break; + } + } - ClassInstanceMethodsInterceptor classMethodInterceptor = new ClassInstanceMethodsInterceptor( - interceptor); + if (isMatch) { + // 修改方法名, + String methodName = method.getName(); + String newMethodName = methodName + "_$SkywalkingEnhance"; + method.setName(newMethodName); - 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 matcher = null; - for (MethodMatcher methodMatcher : methodMatchers) { - logger.debug("enhance class {} instance methods by rule: {}", - enhanceOriginClassName, methodMatcher); - if (matcher == null) { - matcher = methodMatcher.buildMatcher(); - continue; - } + CtMethod newMethod = new CtMethod(method.getReturnType(), methodName, method.getParameterTypes(), method.getDeclaringClass()); + newMethod.setBody( + "{ new " + ClassStaticMethodsInterceptor.class.getName() + "(new " + getStaticMethodsInterceptor().getClass().getName() + ").intercept($class,$args,\"" + + methodName + "\"," + OriginCallCodeGenerator.generateStaticMethodOriginCallCode(ctClass.getName(), newMethodName) + ");}"); - matcher = matcher.or(methodMatcher.buildMatcher()); + ctClass.addMethod(newMethod); + } - } + } - /** - * exclude static methods. - */ - matcher = matcher.and(not(ElementMatchers.isStatic())); - newClassBuilder = newClassBuilder.method(matcher).intercept( - MethodDelegation.to(classMethodInterceptor)); + private void enhanceInstance(CtClass ctClass, CtMethod method) throws CannotCompileException, NotFoundException { + // 添加一个字段,并且带上get/set方法 + CtField ctField = CtField.make("{public " + EnhancedClassInstanceContext.class.getName() + " " + contextAttrName + ";}", ctClass); + ctClass.addMethod( + CtMethod.make("public " + EnhancedClassInstanceContext.class.getName() + " get" + contextAttrName + "(){ return this." + contextAttrName + ";}", ctClass)); + ctClass.addMethod(CtMethod.make( + "public void set" + contextAttrName + "(" + EnhancedClassInstanceContext.class.getName() + " " + contextAttrName + "){this." + contextAttrName + "=" + + contextAttrName + ";}", ctClass)); - return newClassBuilder; - } - - /** - * 返回需要被增强的方法列表 - * - * @return - */ - protected abstract MethodMatcher[] getInstanceMethodsMatchers(); - /** - * 返回增强拦截器的实现
- * 每个拦截器在同一个被增强类的内部,保持单例 - * - * @return - */ - protected abstract InstanceMethodsAroundInterceptor 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); + // 初始化构造函数 + CtConstructor[] constructors = ctClass.getDeclaredConstructors(); + for (CtConstructor constructor : constructors) { + constructor.insertAfter(" new " + ClassConstructorInterceptor.class.getName() + "(new " + getInstanceMethodsInterceptor().getClass().getName() + "()).intercept($0,$0." + + contextAttrName + ",$args);"); + } - 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 matcher = null; - for (MethodMatcher methodMatcher : methodMatchers) { - logger.debug("enhance class {} static methods by rule: {}", - enhanceOriginClassName, methodMatcher); - if (matcher == null) { - matcher = methodMatcher.buildMatcher(); - continue; - } + boolean isMatch = false; + for (MethodMatcher methodMatcher : getInstanceMethodsMatchers()) { + if (methodMatcher.match(method)) { + isMatch = true; + break; + } + } - matcher = matcher.or(methodMatcher.buildMatcher()); + if (isMatch) { + // 修改方法名, + String methodName = method.getName(); + String newMethodName = methodName + "_$SkywalkingEnhance"; + method.setName(newMethodName); + CtMethod newMethod = new CtMethod(method.getReturnType(), methodName, method.getParameterTypes(), method.getDeclaringClass()); - } + newMethod.setBody( + "{ new " + ClassInstanceMethodsInterceptor.class.getName() + "(new " + getInstanceMethodsInterceptor().getClass().getName() + "()).intercept($0,$args,\"" + + methodName + "\"," + OriginCallCodeGenerator.generateInstanceMethodOriginCallCode("$0", methodName) + ",$0." + contextAttrName + ");}"); - /** - * restrict static methods. - */ - matcher = matcher.and(ElementMatchers.isStatic()); - newClassBuilder = newClassBuilder.method(matcher).intercept( - MethodDelegation.to(classMethodInterceptor)); + ctClass.addMethod(newMethod); + } + } - return newClassBuilder; - } - - /** - * 返回需要被增强的方法列表 - * - * @return - */ - protected abstract MethodMatcher[] getStaticMethodsMatchers(); + /** + * 返回需要被增强的方法列表 + * + * @return + */ + protected abstract MethodMatcher[] getInstanceMethodsMatchers(); - /** - * 返回增强拦截器的实现
- * 每个拦截器在同一个被增强类的内部,保持单例 - * - * @return - */ - protected abstract StaticMethodsAroundInterceptor getStaticMethodsInterceptor(); + /** + * 返回增强拦截器的实现
+ * 每个拦截器在同一个被增强类的内部,保持单例 + * + * @return + */ + protected abstract InstanceMethodsAroundInterceptor getInstanceMethodsInterceptor(); + + /** + * 返回需要被增强的方法列表 + * + * @return + */ + protected abstract MethodMatcher[] getStaticMethodsMatchers(); + + /** + * 返回增强拦截器的实现
+ * 每个拦截器在同一个被增强类的内部,保持单例 + * + * @return + */ + protected abstract StaticMethodsAroundInterceptor getStaticMethodsInterceptor(); } diff --git a/skywalking-collector/skywalking-api/src/main/java/com/ai/cloud/skywalking/plugin/interceptor/enhance/ClassInstanceMethodsInterceptor.java b/skywalking-collector/skywalking-api/src/main/java/com/ai/cloud/skywalking/plugin/interceptor/enhance/ClassInstanceMethodsInterceptor.java index 04f4d05fd..79a5900eb 100644 --- a/skywalking-collector/skywalking-api/src/main/java/com/ai/cloud/skywalking/plugin/interceptor/enhance/ClassInstanceMethodsInterceptor.java +++ b/skywalking-collector/skywalking-api/src/main/java/com/ai/cloud/skywalking/plugin/interceptor/enhance/ClassInstanceMethodsInterceptor.java @@ -3,10 +3,6 @@ package com.ai.cloud.skywalking.plugin.interceptor.enhance; import com.ai.cloud.skywalking.logging.LogManager; import com.ai.cloud.skywalking.logging.Logger; import com.ai.cloud.skywalking.plugin.interceptor.EnhancedClassInstanceContext; -import net.bytebuddy.implementation.bind.annotation.*; - -import java.lang.reflect.Method; -import java.util.concurrent.Callable; /** * 类方法拦截、控制器 @@ -14,8 +10,7 @@ import java.util.concurrent.Callable; * @author wusheng */ public class ClassInstanceMethodsInterceptor { - private static Logger logger = LogManager - .getLogger(ClassInstanceMethodsInterceptor.class); + private static Logger logger = LogManager.getLogger(ClassInstanceMethodsInterceptor.class); private InstanceMethodsAroundInterceptor interceptor; @@ -23,22 +18,13 @@ public class ClassInstanceMethodsInterceptor { this.interceptor = interceptor; } - @RuntimeType - public Object intercept( - @This Object obj, - @AllArguments Object[] allArguments, - @Origin Method method, - @SuperCall Callable zuper, - @FieldValue(ClassEnhancePluginDefine.contextAttrName) EnhancedClassInstanceContext instanceContext) - throws Exception { - InstanceMethodInvokeContext interceptorContext = new InstanceMethodInvokeContext(obj, - method.getName(), allArguments); + public Object intercept(Object obj, Object[] allArguments, String methodName, OriginCall zuper, EnhancedClassInstanceContext instanceContext) throws Exception { + InstanceMethodInvokeContext interceptorContext = new InstanceMethodInvokeContext(obj, methodName, allArguments); MethodInterceptResult result = new MethodInterceptResult(); try { interceptor.beforeMethod(instanceContext, interceptorContext, result); } catch (Throwable t) { - logger.error("class[{}] before method[{}] intercept failue:{}", - new Object[]{obj.getClass(), method.getName(), t.getMessage()}, t); + logger.error("class[{}] before method[{}] intercept failue:{}", new Object[] {obj.getClass(), methodName, t.getMessage()}, t); } if (!result.isContinue()) { return result._ret(); @@ -51,16 +37,14 @@ public class ClassInstanceMethodsInterceptor { try { interceptor.handleMethodException(t, instanceContext, interceptorContext, ret); } catch (Throwable t2) { - logger.error("class[{}] handle method[{}] exception failue:{}", - new Object[]{obj.getClass(), method.getName(), t2.getMessage()}, t2); + logger.error("class[{}] handle method[{}] exception failue:{}", new Object[] {obj.getClass(), methodName, t2.getMessage()}, t2); } throw t; } finally { try { ret = interceptor.afterMethod(instanceContext, interceptorContext, ret); } catch (Throwable t) { - logger.error("class[{}] after method[{}] intercept failue:{}", - new Object[]{obj.getClass(), method.getName(), t.getMessage()}, t); + logger.error("class[{}] after method[{}] intercept failue:{}", new Object[] {obj.getClass(), methodName, t.getMessage()}, t); } } return ret; diff --git a/skywalking-collector/skywalking-api/src/main/java/com/ai/cloud/skywalking/plugin/interceptor/enhance/ClassStaticMethodsInterceptor.java b/skywalking-collector/skywalking-api/src/main/java/com/ai/cloud/skywalking/plugin/interceptor/enhance/ClassStaticMethodsInterceptor.java index 364f8f716..a0cfed24b 100644 --- a/skywalking-collector/skywalking-api/src/main/java/com/ai/cloud/skywalking/plugin/interceptor/enhance/ClassStaticMethodsInterceptor.java +++ b/skywalking-collector/skywalking-api/src/main/java/com/ai/cloud/skywalking/plugin/interceptor/enhance/ClassStaticMethodsInterceptor.java @@ -2,13 +2,6 @@ package com.ai.cloud.skywalking.plugin.interceptor.enhance; import com.ai.cloud.skywalking.logging.LogManager; import com.ai.cloud.skywalking.logging.Logger; -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 java.lang.reflect.Method; -import java.util.concurrent.Callable; /** * 类静态方法拦截、控制器 @@ -24,14 +17,13 @@ public class ClassStaticMethodsInterceptor { 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); + public Object intercept(Class clazz, Object[] allArguments, String methodName, OriginCall zuper) throws Exception { + MethodInvokeContext interceptorContext = new MethodInvokeContext(methodName, allArguments); MethodInterceptResult result = new MethodInterceptResult(); try { interceptor.beforeMethod(interceptorContext, result); } catch (Throwable t) { - logger.error("class[{}] before static method[{}] intercept failue:{}", new Object[] {clazz, method.getName(), t.getMessage()}, t); + logger.error("class[{}] before static method[{}] intercept failue:{}", new Object[] {clazz, methodName, t.getMessage()}, t); } if (!result.isContinue()) { return result._ret(); @@ -44,14 +36,14 @@ public class ClassStaticMethodsInterceptor { try { interceptor.handleMethodException(t, interceptorContext, ret); } catch (Throwable t2) { - logger.error("class[{}] handle static method[{}] exception failue:{}", new Object[] {clazz, method.getName(), t2.getMessage()}, t2); + logger.error("class[{}] handle static method[{}] exception failue:{}", new Object[] {clazz, methodName, t2.getMessage()}, t2); } throw t; } finally { try { ret = interceptor.afterMethod(interceptorContext, ret); } catch (Throwable t) { - logger.error("class[{}] after static method[{}] intercept failue:{}", new Object[] {clazz, method.getName(), t.getMessage()}, t); + logger.error("class[{}] after static method[{}] intercept failue:{}", new Object[] {clazz, methodName, t.getMessage()}, t); } } return ret; diff --git a/skywalking-collector/skywalking-api/src/main/java/com/ai/cloud/skywalking/plugin/interceptor/enhance/OriginCallCodeGenerator.java b/skywalking-collector/skywalking-api/src/main/java/com/ai/cloud/skywalking/plugin/interceptor/enhance/OriginCallCodeGenerator.java index bcd16d9ba..72d110b03 100644 --- a/skywalking-collector/skywalking-api/src/main/java/com/ai/cloud/skywalking/plugin/interceptor/enhance/OriginCallCodeGenerator.java +++ b/skywalking-collector/skywalking-api/src/main/java/com/ai/cloud/skywalking/plugin/interceptor/enhance/OriginCallCodeGenerator.java @@ -1,7 +1,51 @@ package com.ai.cloud.skywalking.plugin.interceptor.enhance; -/** - * Created by xin on 16/7/26. - */ +import com.ai.cloud.skywalking.logging.LogManager; +import com.ai.cloud.skywalking.logging.Logger; + +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStreamReader; + public class OriginCallCodeGenerator { + private static Logger logger = LogManager.getLogger(OriginCallCodeGenerator.class); + private static String staticMethodOriginCallCode; + private static String instanceMethodOriginCallCode; + + static { + try { + instanceMethodOriginCallCode = loadInstanceMethodOriginCallCode(); + staticMethodOriginCallCode = loadStaticMethodOriginCallCode(); + } catch (Exception e) { + + } + } + + private static String loadInstanceMethodOriginCallCode() throws IOException { + return loadCodeSegment("/instance_method_call_origin_code.conf"); + } + + private static String loadStaticMethodOriginCallCode() throws IOException { + return loadCodeSegment("/static_method_call_origin_code.conf"); + } + + private static String loadCodeSegment(String fileName) throws IOException { + BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(OriginCallCodeGenerator.class.getResourceAsStream(fileName))); + StringBuilder code = new StringBuilder(); + String codeSegment; + while ((codeSegment = bufferedReader.readLine()) != null) { + code.append(codeSegment); + } + + return code.toString(); + } + + public static String generateInstanceMethodOriginCallCode(String originObject, String methodName) { + return instanceMethodOriginCallCode.toString().replaceAll("%origin_object%", originObject).replaceAll("%method_name%", methodName); + } + + public static String generateStaticMethodOriginCallCode(String className, String methodName) { + return staticMethodOriginCallCode.toString().replaceAll("%class_name%", className).replaceAll("%method_name%", methodName); + } + } diff --git a/skywalking-collector/skywalking-api/src/main/java/com/ai/cloud/skywalking/plugin/interceptor/matcher/AnyMethodsMatcher.java b/skywalking-collector/skywalking-api/src/main/java/com/ai/cloud/skywalking/plugin/interceptor/matcher/AnyMethodsMatcher.java index 12d1b5a86..6e73f622d 100644 --- a/skywalking-collector/skywalking-api/src/main/java/com/ai/cloud/skywalking/plugin/interceptor/matcher/AnyMethodsMatcher.java +++ b/skywalking-collector/skywalking-api/src/main/java/com/ai/cloud/skywalking/plugin/interceptor/matcher/AnyMethodsMatcher.java @@ -1,9 +1,6 @@ package com.ai.cloud.skywalking.plugin.interceptor.matcher; -import net.bytebuddy.description.method.MethodDescription; -import net.bytebuddy.matcher.ElementMatcher; - -import static net.bytebuddy.matcher.ElementMatchers.any; +import javassist.CtMethod; public class AnyMethodsMatcher extends ExclusiveObjectDefaultMethodsMatcher { @@ -11,13 +8,14 @@ public class AnyMethodsMatcher extends ExclusiveObjectDefaultMethodsMatcher { super("any method"); } - @Override - public ElementMatcher.Junction match() { - return any(); - } @Override public String toString() { return getMethodMatchDescribe(); } + + @Override + public boolean matchMethod(CtMethod ctMethod) { + return true; + } } diff --git a/skywalking-collector/skywalking-api/src/main/java/com/ai/cloud/skywalking/plugin/interceptor/matcher/ExclusiveObjectDefaultMethodsMatcher.java b/skywalking-collector/skywalking-api/src/main/java/com/ai/cloud/skywalking/plugin/interceptor/matcher/ExclusiveObjectDefaultMethodsMatcher.java index 6da3fbf02..bf10081ae 100644 --- a/skywalking-collector/skywalking-api/src/main/java/com/ai/cloud/skywalking/plugin/interceptor/matcher/ExclusiveObjectDefaultMethodsMatcher.java +++ b/skywalking-collector/skywalking-api/src/main/java/com/ai/cloud/skywalking/plugin/interceptor/matcher/ExclusiveObjectDefaultMethodsMatcher.java @@ -1,51 +1,37 @@ package com.ai.cloud.skywalking.plugin.interceptor.matcher; import com.ai.cloud.skywalking.plugin.interceptor.MethodMatcher; -import net.bytebuddy.description.method.MethodDescription; -import net.bytebuddy.matcher.ElementMatcher; - -import static net.bytebuddy.matcher.ElementMatchers.not; +import javassist.CtMethod; public abstract class ExclusiveObjectDefaultMethodsMatcher extends MethodMatcher { - private static final MethodMatcher[] EXCLUSIVE_DEFAULT_METHOD_NAME = new MethodMatcher[]{ - new SimpleMethodMatcher(Modifier.Public, "finalize", 0), - new SimpleMethodMatcher(Modifier.Public, "wait", long.class, int.class), - new SimpleMethodMatcher(Modifier.Public, "wait", long.class), - new SimpleMethodMatcher(Modifier.Public, "wait", 0), - new SimpleMethodMatcher(Modifier.Public, "equals", Object.class), - new SimpleMethodMatcher(Modifier.Public, "toString", 0), - new SimpleMethodMatcher(Modifier.Public, "hashCode", 0), - new SimpleMethodMatcher(Modifier.Public, "getClass", 0), - new SimpleMethodMatcher(Modifier.Public, "clone", 0), - new SimpleMethodMatcher(Modifier.Public, "notify", 0), - new SimpleMethodMatcher(Modifier.Public, "notifyAll", 0) - }; + private static final MethodMatcher[] EXCLUSIVE_DEFAULT_METHOD_NAME = + new MethodMatcher[] {new SimpleMethodMatcher(Modifier.Public, "finalize", 0), new SimpleMethodMatcher(Modifier.Public, "wait", long.class, int.class), + new SimpleMethodMatcher(Modifier.Public, "wait", long.class), new SimpleMethodMatcher(Modifier.Public, "wait", 0), + new SimpleMethodMatcher(Modifier.Public, "equals", Object.class), new SimpleMethodMatcher(Modifier.Public, "toString", 0), + new SimpleMethodMatcher(Modifier.Public, "hashCode", 0), new SimpleMethodMatcher(Modifier.Public, "getClass", 0), + new SimpleMethodMatcher(Modifier.Public, "clone", 0), new SimpleMethodMatcher(Modifier.Public, "notify", 0), + new SimpleMethodMatcher(Modifier.Public, "notifyAll", 0)}; public ExclusiveObjectDefaultMethodsMatcher(String methodMatchDescribe) { super(methodMatchDescribe); } @Override - public ElementMatcher.Junction buildMatcher() { - return this.match().and(excludeObjectDefaultMethod()); + public boolean match(CtMethod ctMethod) { + return this.matchMethod(ctMethod) && excludeObjectDefaultMethod(ctMethod); } - protected ElementMatcher.Junction excludeObjectDefaultMethod() { - ElementMatcher.Junction exclusiveMatcher = null; + + protected boolean excludeObjectDefaultMethod(CtMethod ctMethod) { for (MethodMatcher methodMatcher : EXCLUSIVE_DEFAULT_METHOD_NAME) { - if (exclusiveMatcher == null) { - exclusiveMatcher = methodMatcher.buildMatcher(); - continue; + if (methodMatcher.match(ctMethod)) { + return false; } - - exclusiveMatcher = exclusiveMatcher.or(methodMatcher.buildMatcher()); - } - return not(exclusiveMatcher); + return true; } - public abstract ElementMatcher.Junction match(); - + public abstract boolean matchMethod(CtMethod ctMethod); } diff --git a/skywalking-collector/skywalking-api/src/main/java/com/ai/cloud/skywalking/plugin/interceptor/matcher/MethodsExclusiveMatcher.java b/skywalking-collector/skywalking-api/src/main/java/com/ai/cloud/skywalking/plugin/interceptor/matcher/MethodsExclusiveMatcher.java index 120a04447..8a9a3221e 100644 --- a/skywalking-collector/skywalking-api/src/main/java/com/ai/cloud/skywalking/plugin/interceptor/matcher/MethodsExclusiveMatcher.java +++ b/skywalking-collector/skywalking-api/src/main/java/com/ai/cloud/skywalking/plugin/interceptor/matcher/MethodsExclusiveMatcher.java @@ -1,15 +1,12 @@ package com.ai.cloud.skywalking.plugin.interceptor.matcher; import com.ai.cloud.skywalking.plugin.interceptor.MethodMatcher; -import net.bytebuddy.description.method.MethodDescription; -import net.bytebuddy.matcher.ElementMatcher; +import javassist.CtMethod; import java.util.ArrayList; import java.util.Arrays; import java.util.List; -import static net.bytebuddy.matcher.ElementMatchers.not; - public class MethodsExclusiveMatcher extends ExclusiveObjectDefaultMethodsMatcher { private List matchers = new ArrayList(); @@ -26,30 +23,25 @@ public class MethodsExclusiveMatcher extends ExclusiveObjectDefaultMethodsMatche this.matchers.addAll(Arrays.asList(matchers)); } - @Override - public ElementMatcher.Junction match() { - ElementMatcher.Junction result = null; - - for (MethodMatcher matcher : matchers) { - if (result == null) { - result = matcher.buildMatcher(); - continue; - } - - result = result.or(matcher.buildMatcher()); - } - - return not(result); - } - @Override public String toString() { StringBuilder stringBuilder = new StringBuilder("exclude following method(s): "); int idx = 1; for (MethodMatcher methodMatcher : matchers) { - stringBuilder.append(idx++ + "." + methodMatcher.toString() + ". "); + stringBuilder.append(idx++ + "." + methodMatcher.toString() + ". "); } return stringBuilder.toString(); } + + @Override + public boolean matchMethod(CtMethod ctMethod) { + boolean result = false; + for (MethodMatcher methodMatcher : matchers) { + if (methodMatcher.match(ctMethod)) { + result = result || result; + } + } + return !result; + } } diff --git a/skywalking-collector/skywalking-api/src/main/java/com/ai/cloud/skywalking/plugin/interceptor/matcher/PrivateMethodMatcher.java b/skywalking-collector/skywalking-api/src/main/java/com/ai/cloud/skywalking/plugin/interceptor/matcher/PrivateMethodMatcher.java index 2d7f414b4..9671ded88 100644 --- a/skywalking-collector/skywalking-api/src/main/java/com/ai/cloud/skywalking/plugin/interceptor/matcher/PrivateMethodMatcher.java +++ b/skywalking-collector/skywalking-api/src/main/java/com/ai/cloud/skywalking/plugin/interceptor/matcher/PrivateMethodMatcher.java @@ -1,11 +1,7 @@ package com.ai.cloud.skywalking.plugin.interceptor.matcher; import com.ai.cloud.skywalking.plugin.interceptor.MethodMatcher; -import net.bytebuddy.description.method.MethodDescription; -import net.bytebuddy.matcher.ElementMatcher; -import net.bytebuddy.matcher.ElementMatchers; - -import static net.bytebuddy.matcher.ElementMatchers.any; +import javassist.CtMethod; public class PrivateMethodMatcher extends MethodMatcher { public PrivateMethodMatcher() { @@ -13,8 +9,8 @@ public class PrivateMethodMatcher extends MethodMatcher { } @Override - public ElementMatcher.Junction buildMatcher() { - return any().and(ElementMatchers.isPrivate()); + public boolean match(CtMethod ctMethod) { + return java.lang.reflect.Modifier.isPrivate(ctMethod.getModifiers()); } @Override diff --git a/skywalking-collector/skywalking-api/src/main/java/com/ai/cloud/skywalking/plugin/interceptor/matcher/SimpleMethodMatcher.java b/skywalking-collector/skywalking-api/src/main/java/com/ai/cloud/skywalking/plugin/interceptor/matcher/SimpleMethodMatcher.java index d651475d5..5ccee25f6 100644 --- a/skywalking-collector/skywalking-api/src/main/java/com/ai/cloud/skywalking/plugin/interceptor/matcher/SimpleMethodMatcher.java +++ b/skywalking-collector/skywalking-api/src/main/java/com/ai/cloud/skywalking/plugin/interceptor/matcher/SimpleMethodMatcher.java @@ -1,10 +1,8 @@ package com.ai.cloud.skywalking.plugin.interceptor.matcher; -import static net.bytebuddy.matcher.ElementMatchers.named; -import net.bytebuddy.description.method.MethodDescription; -import net.bytebuddy.matcher.ElementMatcher; - import com.ai.cloud.skywalking.plugin.interceptor.MethodMatcher; +import javassist.CtMethod; +import javassist.NotFoundException; public class SimpleMethodMatcher extends MethodMatcher { @@ -33,10 +31,42 @@ public class SimpleMethodMatcher extends MethodMatcher { super(modifier, methodMatchDescribe, argTypeArray); } - @Override - public ElementMatcher.Junction buildMatcher() { - ElementMatcher.Junction matcher = named(getMethodMatchDescribe()); - return mergeArgumentsIfNecessary(matcher); + public boolean match(CtMethod ctMethod) { + int result = 1; + try { + result <<= matchArgTypeArrayIfNecessary(ctMethod); + result <<= matchArgNumIfNecessary(ctMethod); + result <<= matchModifierIfNecessary(ctMethod); + return result == 1 ? true : false; + } catch (Exception e) { + return false; + } } + + private int matchModifierIfNecessary(CtMethod ctMethod) { + if (getModifier() != null) { + return getModifier().getValue() == ctMethod.getModifiers() ? 0 : 1; + } + return 0; + } + + private int matchArgNumIfNecessary(CtMethod ctMethod) throws NotFoundException { + if (getArgNum() > -1) { + return getArgNum() == ctMethod.getParameterTypes().length ? 0 : 1; + } + return 0; + } + + private int matchArgTypeArrayIfNecessary(CtMethod ctMethod) throws NotFoundException { + if (getArgTypeArray() != null) { + for (int i = 0; i < getArgTypeArray().length; i++) { + if (!getArgTypeArray()[i].getName().equals(ctMethod.getParameterTypes()[i].getName())) { + return 1; + } + } + } + return 0; + } + } diff --git a/skywalking-collector/skywalking-api/src/main/java/com/ai/cloud/skywalking/protocol/util/ContextGenerator.java b/skywalking-collector/skywalking-api/src/main/java/com/ai/cloud/skywalking/protocol/util/ContextGenerator.java index 45267faca..62430044d 100644 --- a/skywalking-collector/skywalking-api/src/main/java/com/ai/cloud/skywalking/protocol/util/ContextGenerator.java +++ b/skywalking-collector/skywalking-api/src/main/java/com/ai/cloud/skywalking/protocol/util/ContextGenerator.java @@ -37,7 +37,13 @@ public final class ContextGenerator { spanData = new Span(TraceIdGenerator.generate(), Config.SkyWalking.APPLICATION_CODE, Config.SkyWalking.USER_ID); } else { // 如果不为空,则将当前的Context存放到上下文 - spanData = new Span(context.getTraceId(), context.getParentLevel(), context.getLevelId(), Config.SkyWalking.APPLICATION_CODE, Config.SkyWalking.USER_ID); + Span previousSpanData = CurrentThreadSpanStack.peek(); + if (previousSpanData == null){ + spanData = new Span(context.getTraceId(), context.getParentLevel(), context.getLevelId(), Config.SkyWalking.APPLICATION_CODE, Config.SkyWalking.USER_ID); + }else{ + spanData = new Span(previousSpanData.getTraceId(), Config.SkyWalking.APPLICATION_CODE, Config.SkyWalking.USER_ID); + spanData.setParentLevel(previousSpanData.getParentLevel() + "." + previousSpanData.getLevelId()); + } } spanData.setStartDate(System.currentTimeMillis()); diff --git a/skywalking-collector/skywalking-api/src/main/resources/instance_method_call_origin_code.conf b/skywalking-collector/skywalking-api/src/main/resources/instance_method_call_origin_code.conf index e69de29bb..cb9aff289 100644 --- a/skywalking-collector/skywalking-api/src/main/resources/instance_method_call_origin_code.conf +++ b/skywalking-collector/skywalking-api/src/main/resources/instance_method_call_origin_code.conf @@ -0,0 +1,11 @@ +new com.ai.cloud.skywalking.plugin.interceptor.enhance.OriginCall(%origin_object%){ + private Object _this; + + public com.ai.cloud.skywalking.plugin.interceptor.enhance.OriginCall(Object _this){ + this._this = _this; + } + + public Object call(){ + return _this.%method_name%($$); + } +} diff --git a/skywalking-collector/skywalking-api/src/main/resources/static_method_call_origin_code.conf b/skywalking-collector/skywalking-api/src/main/resources/static_method_call_origin_code.conf index 4c658f62c..ef2e15c6a 100644 --- a/skywalking-collector/skywalking-api/src/main/resources/static_method_call_origin_code.conf +++ b/skywalking-collector/skywalking-api/src/main/resources/static_method_call_origin_code.conf @@ -1 +1,6 @@ -new Origin +new com.ai.cloud.skywalking.plugin.interceptor.enhance.OriginCall(){ + + public Object call(){ + return %class_name%.%method_name%($$); + } +} diff --git a/skywalking-collector/skywalking-api/src/test/java/test/ai/cloud/assertspandata/SDKGeneratedDataTest.java b/skywalking-collector/skywalking-api/src/test/java/test/ai/cloud/assertspandata/SDKGeneratedDataTest.java index a287bef47..97c7650e5 100644 --- a/skywalking-collector/skywalking-api/src/test/java/test/ai/cloud/assertspandata/SDKGeneratedDataTest.java +++ b/skywalking-collector/skywalking-api/src/test/java/test/ai/cloud/assertspandata/SDKGeneratedDataTest.java @@ -15,7 +15,7 @@ public class SDKGeneratedDataTest { @Test public void traceTreeAssertTest() { Config.Consumer.MAX_CONSUMER = 0; - Span testSpan = new Span("1.0b.1465224457414.7e57f54.22905.61.2691", "", 0, "test-application", "5"); + Span testSpan = new Span("1.0b.1465224457414.7e57f54.22905.61.2691", "", 0, "sample-application", "5"); RequestSpan requestSpan = RequestSpan.RequestSpanBuilder.newBuilder(testSpan).viewPoint("http://hire.asiainfo.com/Aisse-Mobile-Web/aisseWorkPage/submitReimbursement").build(); ContextBuffer.save(requestSpan); diff --git a/skywalking-collector/skywalking-api/src/test/java/test/ai/cloud/matcher/ExclusionMatcherTest.java b/skywalking-collector/skywalking-api/src/test/java/test/ai/cloud/matcher/ExclusionMatcherTest.java index ee0255cc1..787e51fdd 100644 --- a/skywalking-collector/skywalking-api/src/test/java/test/ai/cloud/matcher/ExclusionMatcherTest.java +++ b/skywalking-collector/skywalking-api/src/test/java/test/ai/cloud/matcher/ExclusionMatcherTest.java @@ -9,8 +9,8 @@ import com.ai.cloud.skywalking.plugin.PluginBootstrap; public class ExclusionMatcherTest extends TestCase{ @Test public void testMatcher() throws ClassNotFoundException, IllegalAccessException, InstantiationException, InterruptedException { - new PluginBootstrap().start(); - TestMatcherClass testMatcherClass = (TestMatcherClass) Class.forName("test.ai.cloud.matcher.TestMatcherClass").newInstance(); + //new PluginBootstrap().start(); + TestMatcherClass testMatcherClass = (TestMatcherClass) Class.forName("sample.ai.cloud.matcher.TestMatcherClass").newInstance(); testMatcherClass.set(); testMatcherClass.seta("a"); diff --git a/skywalking-collector/skywalking-api/src/test/java/test/ai/cloud/matcher/TestMatcherDefine.java b/skywalking-collector/skywalking-api/src/test/java/test/ai/cloud/matcher/TestMatcherDefine.java index 9c424f7c4..81cdeebe7 100644 --- a/skywalking-collector/skywalking-api/src/test/java/test/ai/cloud/matcher/TestMatcherDefine.java +++ b/skywalking-collector/skywalking-api/src/test/java/test/ai/cloud/matcher/TestMatcherDefine.java @@ -11,7 +11,7 @@ import com.ai.cloud.skywalking.plugin.interceptor.matcher.PrivateMethodMatcher; public class TestMatcherDefine extends ClassInstanceMethodsEnhancePluginDefine { @Override public String enhanceClassName() { - return "test.ai.cloud.matcher.TestMatcherClass"; + return "sample.ai.cloud.matcher.TestMatcherClass"; } @Override diff --git a/skywalking-collector/skywalking-api/src/test/java/test/ai/cloud/plugin/PluginMainTest.java b/skywalking-collector/skywalking-api/src/test/java/test/ai/cloud/plugin/PluginMainTest.java index b41d38d25..ad87e19d6 100644 --- a/skywalking-collector/skywalking-api/src/test/java/test/ai/cloud/plugin/PluginMainTest.java +++ b/skywalking-collector/skywalking-api/src/test/java/test/ai/cloud/plugin/PluginMainTest.java @@ -12,13 +12,13 @@ public class PluginMainTest { IllegalArgumentException, InvocationTargetException, NoSuchMethodException, SecurityException, ClassNotFoundException { TracingBootstrap - .main(new String[] { "test.ai.cloud.plugin.PluginMainTest" }); + .main(new String[] { "sample.ai.cloud.plugin.PluginMainTest" }); } public static void main(String[] args) throws InstantiationException, IllegalAccessException, ClassNotFoundException, IllegalArgumentException, InvocationTargetException, NoSuchMethodException, SecurityException { long start = System.currentTimeMillis(); - BeInterceptedClass inst = (BeInterceptedClass) Class.forName("test.ai.cloud.plugin.BeInterceptedClass").newInstance(); + BeInterceptedClass inst = (BeInterceptedClass) Class.forName("sample.ai.cloud.plugin.BeInterceptedClass").newInstance(); inst.printabc(); long end = System.currentTimeMillis(); System.out.println(end - start + "ms"); diff --git a/skywalking-collector/skywalking-api/src/test/java/test/ai/cloud/plugin/TestAroundInterceptor.java b/skywalking-collector/skywalking-api/src/test/java/test/ai/cloud/plugin/TestAroundInterceptor.java index 7e146e9da..e481f53dd 100644 --- a/skywalking-collector/skywalking-api/src/test/java/test/ai/cloud/plugin/TestAroundInterceptor.java +++ b/skywalking-collector/skywalking-api/src/test/java/test/ai/cloud/plugin/TestAroundInterceptor.java @@ -10,18 +10,18 @@ public class TestAroundInterceptor implements InstanceMethodsAroundInterceptor { @Override public void onConstruct(EnhancedClassInstanceContext context, ConstructorInvokeContext interceptorContext) { - context.set("test.key", "123"); + context.set("sample.key", "123"); System.out.println("onConstruct, args size=" + interceptorContext.allArguments().length); } @Override public void beforeMethod(EnhancedClassInstanceContext context, InstanceMethodInvokeContext interceptorContext, MethodInterceptResult result) { - System.out.println("beforeMethod : " + context.get("test.key", String.class)); + System.out.println("beforeMethod : " + context.get("sample.key", String.class)); } @Override public Object afterMethod(EnhancedClassInstanceContext context, InstanceMethodInvokeContext interceptorContext, Object ret) { - System.out.println("afterMethod: " + context.get("test.key", String.class)); + System.out.println("afterMethod: " + context.get("sample.key", String.class)); return ret; } diff --git a/skywalking-collector/skywalking-api/src/test/java/test/ai/cloud/plugin/TestInterceptorDefine.java b/skywalking-collector/skywalking-api/src/test/java/test/ai/cloud/plugin/TestInterceptorDefine.java index ae0b805c4..98b3b12fc 100644 --- a/skywalking-collector/skywalking-api/src/test/java/test/ai/cloud/plugin/TestInterceptorDefine.java +++ b/skywalking-collector/skywalking-api/src/test/java/test/ai/cloud/plugin/TestInterceptorDefine.java @@ -10,7 +10,7 @@ public class TestInterceptorDefine extends ClassEnhancePluginDefine { @Override public String enhanceClassName() { - return "test.ai.cloud.plugin.BeInterceptedClass"; + return "sample.ai.cloud.plugin.BeInterceptedClass"; } @Override diff --git a/skywalking-collector/skywalking-api/src/test/java/test/ai/cloud/serialize/SerializeTest.java b/skywalking-collector/skywalking-api/src/test/java/test/ai/cloud/serialize/SerializeTest.java index a4775c427..8771e7411 100644 --- a/skywalking-collector/skywalking-api/src/test/java/test/ai/cloud/serialize/SerializeTest.java +++ b/skywalking-collector/skywalking-api/src/test/java/test/ai/cloud/serialize/SerializeTest.java @@ -8,7 +8,7 @@ import com.ai.cloud.skywalking.protocol.common.SpanType; public class SerializeTest { public static void main(String[] args) throws InterruptedException { while (true) { - Span spandata = new Span("1.0b.1461060884539.7d6d06e.22489.1271.103", "", 0, "test-application", "test"); + Span spandata = new Span("1.0b.1461060884539.7d6d06e.22489.1271.103", "", 0, "sample-application", "test"); spandata.setSpanType(SpanType.LOCAL); spandata.setStartDate(System.currentTimeMillis() - 1000 * 60); AckSpan requestSpan = new AckSpan(spandata); diff --git a/skywalking-collector/skywalking-sdk-plugin/dubbo-plugin/src/test/java/com/ai/cloud/skywalking/plugin/test/dubbo/consumer/DubboConsumer.java b/skywalking-collector/skywalking-sdk-plugin/dubbo-plugin/src/test/java/com/ai/cloud/skywalking/plugin/test/dubbo/consumer/DubboConsumer.java index 924817064..770e572d7 100644 --- a/skywalking-collector/skywalking-sdk-plugin/dubbo-plugin/src/test/java/com/ai/cloud/skywalking/plugin/test/dubbo/consumer/DubboConsumer.java +++ b/skywalking-collector/skywalking-sdk-plugin/dubbo-plugin/src/test/java/com/ai/cloud/skywalking/plugin/test/dubbo/consumer/DubboConsumer.java @@ -15,7 +15,7 @@ public class DubboConsumer { @Test public void test() throws InvocationTargetException, NoSuchMethodException, ClassNotFoundException, IllegalAccessException { TracingBootstrap - .main(new String[]{"com.ai.cloud.skywalking.plugin.test.dubbo.consumer.DubboConsumer"}); + .main(new String[]{"com.ai.cloud.skywalking.plugin.sample.dubbo.consumer.DubboConsumer"}); } public static void main(String[] args) throws InterruptedException { @@ -23,7 +23,7 @@ public class DubboConsumer { IDubboInterA dubboInterA = context.getBean(IDubboInterA.class); dubboInterA.doBusiness("AAAAA"); RequestSpanAssert.assertEquals(new String[][]{ - {"0", "dubbo://127.0.0.1:20880/com.ai.cloud.skywalking.plugin.test.dubbo.interfaces.IDubboInterA.doBusiness(String)", ""} + {"0", "dubbo://127.0.0.1:20880/com.ai.cloud.skywalking.plugin.sample.dubbo.interfaces.IDubboInterA.doBusiness(String)", ""} }); } } diff --git a/skywalking-collector/skywalking-sdk-plugin/dubbo-plugin/src/test/java/com/ai/cloud/skywalking/plugin/test/dubbo/impl/DubboStart.java b/skywalking-collector/skywalking-sdk-plugin/dubbo-plugin/src/test/java/com/ai/cloud/skywalking/plugin/test/dubbo/impl/DubboStart.java index 9f48f0781..b5e6d7400 100644 --- a/skywalking-collector/skywalking-sdk-plugin/dubbo-plugin/src/test/java/com/ai/cloud/skywalking/plugin/test/dubbo/impl/DubboStart.java +++ b/skywalking-collector/skywalking-sdk-plugin/dubbo-plugin/src/test/java/com/ai/cloud/skywalking/plugin/test/dubbo/impl/DubboStart.java @@ -11,7 +11,7 @@ public class DubboStart { @Test public void test() throws InvocationTargetException, NoSuchMethodException, ClassNotFoundException, IllegalAccessException { TracingBootstrap - .main(new String[]{"com.ai.cloud.skywalking.plugin.test.dubbo.impl.DubboStart"}); + .main(new String[]{"com.ai.cloud.skywalking.plugin.sample.dubbo.impl.DubboStart"}); } public static void main(String[] args) throws InterruptedException { diff --git a/skywalking-collector/skywalking-sdk-plugin/dubbo-plugin/src/test/java/com/ai/cloud/skywalking/plugin/test/dubbox283/consumer/DubboxRestConsumer.java b/skywalking-collector/skywalking-sdk-plugin/dubbo-plugin/src/test/java/com/ai/cloud/skywalking/plugin/test/dubbox283/consumer/DubboxRestConsumer.java index d8a7cb13d..9360a7417 100644 --- a/skywalking-collector/skywalking-sdk-plugin/dubbo-plugin/src/test/java/com/ai/cloud/skywalking/plugin/test/dubbox283/consumer/DubboxRestConsumer.java +++ b/skywalking-collector/skywalking-sdk-plugin/dubbo-plugin/src/test/java/com/ai/cloud/skywalking/plugin/test/dubbox283/consumer/DubboxRestConsumer.java @@ -20,7 +20,7 @@ public class DubboxRestConsumer { @Test public void test() throws InvocationTargetException, NoSuchMethodException, ClassNotFoundException, IllegalAccessException { - TracingBootstrap.main(new String[] {"com.ai.cloud.skywalking.plugin.test.dubbox283.consumer.DubboxRestConsumer"}); + TracingBootstrap.main(new String[] {"com.ai.cloud.skywalking.plugin.sample.dubbox283.consumer.DubboxRestConsumer"}); } public static void main(String[] args) throws IOException, URISyntaxException, InterruptedException { @@ -29,6 +29,6 @@ public class DubboxRestConsumer { IDubboxRestInterA dubboxRestInterA = context.getBean(IDubboxRestInterA.class); dubboxRestInterA.doBusiness(new DubboxRestInterAParameter("AAAAA")); RequestSpanAssert.assertEquals(new String[][] { - {"0", "rest://127.0.0.1:20880/com.ai.cloud.skywalking.plugin.test.dubbox283.interfaces.IDubboxRestInterA.doBusiness(DubboxRestInterAParameter)", ""}}); + {"0", "rest://127.0.0.1:20880/com.ai.cloud.skywalking.plugin.sample.dubbox283.interfaces.IDubboxRestInterA.doBusiness(DubboxRestInterAParameter)", ""}}); } } diff --git a/skywalking-collector/skywalking-sdk-plugin/dubbo-plugin/src/test/java/com/ai/cloud/skywalking/plugin/test/dubbox283/consumer/DubboxRestStart.java b/skywalking-collector/skywalking-sdk-plugin/dubbo-plugin/src/test/java/com/ai/cloud/skywalking/plugin/test/dubbox283/consumer/DubboxRestStart.java index 96116300e..28991934c 100644 --- a/skywalking-collector/skywalking-sdk-plugin/dubbo-plugin/src/test/java/com/ai/cloud/skywalking/plugin/test/dubbox283/consumer/DubboxRestStart.java +++ b/skywalking-collector/skywalking-sdk-plugin/dubbo-plugin/src/test/java/com/ai/cloud/skywalking/plugin/test/dubbox283/consumer/DubboxRestStart.java @@ -12,7 +12,7 @@ public class DubboxRestStart { @Test public void test() throws InvocationTargetException, NoSuchMethodException, ClassNotFoundException, IllegalAccessException { TracingBootstrap - .main(new String[]{"com.ai.cloud.skywalking.plugin.test.dubbox283.consumer.DubboxRestStart"}); + .main(new String[]{"com.ai.cloud.skywalking.plugin.sample.dubbox283.consumer.DubboxRestStart"}); } public static void main(String[] args) throws InterruptedException { diff --git a/skywalking-collector/skywalking-sdk-plugin/dubbo-plugin/src/test/java/com/ai/cloud/skywalking/plugin/test/dubbox284/consumer/DubboxRestConsumer.java b/skywalking-collector/skywalking-sdk-plugin/dubbo-plugin/src/test/java/com/ai/cloud/skywalking/plugin/test/dubbox284/consumer/DubboxRestConsumer.java index 84b81083d..0eb6e5cbc 100644 --- a/skywalking-collector/skywalking-sdk-plugin/dubbo-plugin/src/test/java/com/ai/cloud/skywalking/plugin/test/dubbox284/consumer/DubboxRestConsumer.java +++ b/skywalking-collector/skywalking-sdk-plugin/dubbo-plugin/src/test/java/com/ai/cloud/skywalking/plugin/test/dubbox284/consumer/DubboxRestConsumer.java @@ -19,7 +19,7 @@ public class DubboxRestConsumer { @Test public void test() throws InvocationTargetException, NoSuchMethodException, ClassNotFoundException, IllegalAccessException { - TracingBootstrap.main(new String[] {"com.ai.cloud.skywalking.plugin.test.dubbox284.consumer.DubboxRestConsumer"}); + TracingBootstrap.main(new String[] {"com.ai.cloud.skywalking.plugin.sample.dubbox284.consumer.DubboxRestConsumer"}); } public static void main(String[] args) throws IOException, URISyntaxException, InterruptedException { @@ -27,6 +27,6 @@ public class DubboxRestConsumer { IDubboxRestInterA dubboxRestInterA = context.getBean(IDubboxRestInterA.class); dubboxRestInterA.doBusiness(new DubboxRestInterAParameter("AAAAA")); RequestSpanAssert.assertEquals(new String[][] { - {"0", "rest://127.0.0.1:20880/com.ai.cloud.skywalking.plugin.test.dubbox284.interfaces.IDubboxRestInterA.doBusiness(DubboxRestInterAParameter)", ""}}); + {"0", "rest://127.0.0.1:20880/com.ai.cloud.skywalking.plugin.sample.dubbox284.interfaces.IDubboxRestInterA.doBusiness(DubboxRestInterAParameter)", ""}}); } } diff --git a/skywalking-collector/skywalking-sdk-plugin/dubbo-plugin/src/test/java/com/ai/cloud/skywalking/plugin/test/dubbox284/consumer/DubboxRestStart.java b/skywalking-collector/skywalking-sdk-plugin/dubbo-plugin/src/test/java/com/ai/cloud/skywalking/plugin/test/dubbox284/consumer/DubboxRestStart.java index 2c384bc59..b5ddf898c 100644 --- a/skywalking-collector/skywalking-sdk-plugin/dubbo-plugin/src/test/java/com/ai/cloud/skywalking/plugin/test/dubbox284/consumer/DubboxRestStart.java +++ b/skywalking-collector/skywalking-sdk-plugin/dubbo-plugin/src/test/java/com/ai/cloud/skywalking/plugin/test/dubbox284/consumer/DubboxRestStart.java @@ -12,7 +12,7 @@ public class DubboxRestStart { @Test public void test() throws InvocationTargetException, NoSuchMethodException, ClassNotFoundException, IllegalAccessException { TracingBootstrap - .main(new String[]{"com.ai.cloud.skywalking.plugin.test.dubbox284.consumer.DubboxRestStart"}); + .main(new String[]{"com.ai.cloud.skywalking.plugin.sample.dubbox284.consumer.DubboxRestStart"}); } public static void main(String[] args) throws InterruptedException { diff --git a/skywalking-collector/skywalking-sdk-plugin/jdbc-plugin/src/main/java/com/ai/cloud/skywalking/plugin/jdbc/JDBCPluginDefine.java b/skywalking-collector/skywalking-sdk-plugin/jdbc-plugin/src/main/java/com/ai/cloud/skywalking/plugin/jdbc/JDBCPluginDefine.java index 93e5b6c8c..585ff63f7 100644 --- a/skywalking-collector/skywalking-sdk-plugin/jdbc-plugin/src/main/java/com/ai/cloud/skywalking/plugin/jdbc/JDBCPluginDefine.java +++ b/skywalking-collector/skywalking-sdk-plugin/jdbc-plugin/src/main/java/com/ai/cloud/skywalking/plugin/jdbc/JDBCPluginDefine.java @@ -15,7 +15,7 @@ public class JDBCPluginDefine extends BootPluginDefine { private static Logger logger = LogManager.getLogger(JDBCPluginDefine.class); @Override - protected void boot() throws BootException { + protected byte[] boot() throws BootException { try { Class classes = Class.forName("java.sql.DriverInfo"); Object traceDriverInfo = newDriverInfoInstance(classes); @@ -31,6 +31,8 @@ public class JDBCPluginDefine extends BootPluginDefine { e); TracingDriver.registerDriver(); } + + return null; } private Object newDriverInfoInstance(Class classes) throws NoSuchMethodException, InstantiationException, IllegalAccessException, java.lang.reflect.InvocationTargetException { diff --git a/skywalking-collector/skywalking-sdk-plugin/jdbc-plugin/src/test/java/test/ai/cloud/skywalking/plugin/drivermanger/TestMyDriver.java b/skywalking-collector/skywalking-sdk-plugin/jdbc-plugin/src/test/java/test/ai/cloud/skywalking/plugin/drivermanger/TestMyDriver.java index aa00c7168..2cd9fb7f5 100644 --- a/skywalking-collector/skywalking-sdk-plugin/jdbc-plugin/src/test/java/test/ai/cloud/skywalking/plugin/drivermanger/TestMyDriver.java +++ b/skywalking-collector/skywalking-sdk-plugin/jdbc-plugin/src/test/java/test/ai/cloud/skywalking/plugin/drivermanger/TestMyDriver.java @@ -10,7 +10,7 @@ import java.sql.SQLException; */ public class TestMyDriver { public static void main(String[] args) throws ClassNotFoundException, SQLException { - Class.forName("test.ai.cloud.skywalking.plugin.drivermanger.MyDriver"); + Class.forName("sample.ai.cloud.skywalking.plugin.drivermanger.MyDriver"); String url = "jdbc:oracle:thin:@10.1.130.239:1521:ora"; Connection con = DriverManager.getConnection(url, "edc_export", "edc_export"); con.setAutoCommit(false); diff --git a/skywalking-collector/skywalking-sdk-plugin/jdbc-plugin/src/test/java/test/ai/cloud/skywalking/plugin/h2/H2JDBCTest.java b/skywalking-collector/skywalking-sdk-plugin/jdbc-plugin/src/test/java/test/ai/cloud/skywalking/plugin/h2/H2JDBCTest.java index 3d208f312..cdd679f24 100644 --- a/skywalking-collector/skywalking-sdk-plugin/jdbc-plugin/src/test/java/test/ai/cloud/skywalking/plugin/h2/H2JDBCTest.java +++ b/skywalking-collector/skywalking-sdk-plugin/jdbc-plugin/src/test/java/test/ai/cloud/skywalking/plugin/h2/H2JDBCTest.java @@ -18,7 +18,7 @@ public class H2JDBCTest { public static void main(String[] args) throws ClassNotFoundException, SQLException, InterruptedException { Class.forName("org.h2.Driver"); - String url = "jdbc:h2:" + H2JDBCTest.class.getResource("/") + "test.db"; + String url = "jdbc:h2:" + H2JDBCTest.class.getResource("/") + "sample.db"; Connection con = DriverManager.getConnection(url); con.setAutoCommit(false); @@ -28,9 +28,9 @@ public class H2JDBCTest { con.commit(); con.close(); RequestSpanAssert.assertEquals( - new String[][]{{"0", "jdbc:h2:" +H2JDBCTest.class.getResource("/") + "test.db" + "(null)", "preaparedStatement.executeUpdate:select 1 from dual where 1=?"}, - {"0", "jdbc:h2:" +H2JDBCTest.class.getResource("/") + "test.db" + "(null)", "connection.commit"}, - {"0", "jdbc:h2:" +H2JDBCTest.class.getResource("/") + "test.db" + "(null)", "connection.close"},}, true); + new String[][]{{"0", "jdbc:h2:" +H2JDBCTest.class.getResource("/") + "sample.db" + "(null)", "preaparedStatement.executeUpdate:select 1 from dual where 1=?"}, + {"0", "jdbc:h2:" +H2JDBCTest.class.getResource("/") + "sample.db" + "(null)", "connection.commit"}, + {"0", "jdbc:h2:" +H2JDBCTest.class.getResource("/") + "sample.db" + "(null)", "connection.close"},}, true); } } diff --git a/skywalking-collector/skywalking-sdk-plugin/jdbc-plugin/src/test/java/test/ai/cloud/skywalking/plugin/mysql/MysqlJDBCTest.java b/skywalking-collector/skywalking-sdk-plugin/jdbc-plugin/src/test/java/test/ai/cloud/skywalking/plugin/mysql/MysqlJDBCTest.java index d93214333..f82b22e05 100644 --- a/skywalking-collector/skywalking-sdk-plugin/jdbc-plugin/src/test/java/test/ai/cloud/skywalking/plugin/mysql/MysqlJDBCTest.java +++ b/skywalking-collector/skywalking-sdk-plugin/jdbc-plugin/src/test/java/test/ai/cloud/skywalking/plugin/mysql/MysqlJDBCTest.java @@ -14,12 +14,12 @@ public class MysqlJDBCTest { @Test public void testMySqlJDBC() throws InvocationTargetException, NoSuchMethodException, ClassNotFoundException, IllegalAccessException { - TracingBootstrap.main(new String[] {"test.ai.cloud.skywalking.plugin.mysql.MysqlJDBCTest"}); + TracingBootstrap.main(new String[] {"sample.ai.cloud.skywalking.plugin.mysql.MysqlJDBCTest"}); } public static void main(String[] args) throws ClassNotFoundException, SQLException, InterruptedException { Class.forName("com.mysql.jdbc.Driver"); - String url = "tracing:jdbc:mysql://127.0.0.1:3306/test?user=root&password=root"; + String url = "tracing:jdbc:mysql://127.0.0.1:3306/sample?user=root&password=root"; Connection con = DriverManager.getConnection(url); con.setAutoCommit(false); @@ -29,9 +29,9 @@ public class MysqlJDBCTest { con.commit(); con.close(); RequestSpanAssert.assertEquals( - new String[][] {{"0", "jdbc:mysql://127.0.0.1:3306/test?user=root&password=root(null)", "preaparedStatement.executeUpdate:select 1 from dual where 1=?"}, - {"0", "jdbc:mysql://127.0.0.1:3306/test?user=root&password=root(null)", "connection.commit"}, - {"0", "jdbc:mysql://127.0.0.1:3306/test?user=root&password=root(null)", "connection.close"},}, true); + new String[][] {{"0", "jdbc:mysql://127.0.0.1:3306/sample?user=root&password=root(null)", "preaparedStatement.executeUpdate:select 1 from dual where 1=?"}, + {"0", "jdbc:mysql://127.0.0.1:3306/sample?user=root&password=root(null)", "connection.commit"}, + {"0", "jdbc:mysql://127.0.0.1:3306/sample?user=root&password=root(null)", "connection.close"},}, true); } diff --git a/skywalking-collector/skywalking-sdk-plugin/jdbc-plugin/src/test/java/test/ai/cloud/skywalking/plugin/oracle/OracleJDBCTest.java b/skywalking-collector/skywalking-sdk-plugin/jdbc-plugin/src/test/java/test/ai/cloud/skywalking/plugin/oracle/OracleJDBCTest.java index fa0bf6b86..874495cb7 100644 --- a/skywalking-collector/skywalking-sdk-plugin/jdbc-plugin/src/test/java/test/ai/cloud/skywalking/plugin/oracle/OracleJDBCTest.java +++ b/skywalking-collector/skywalking-sdk-plugin/jdbc-plugin/src/test/java/test/ai/cloud/skywalking/plugin/oracle/OracleJDBCTest.java @@ -20,7 +20,7 @@ public class OracleJDBCTest { NoSuchMethodException, ClassNotFoundException, IllegalAccessException { TracingBootstrap - .main(new String[]{"test.ai.cloud.skywalking.plugin.oracle.OracleJDBCTest"}); + .main(new String[]{"sample.ai.cloud.skywalking.plugin.oracle.OracleJDBCTest"}); } public static void main(String[] args) throws ClassNotFoundException, diff --git a/skywalking-collector/skywalking-sdk-plugin/spring-plugin/pom.xml b/skywalking-collector/skywalking-sdk-plugin/spring-plugin/pom.xml index 4999ab091..62bd3545e 100644 --- a/skywalking-collector/skywalking-sdk-plugin/spring-plugin/pom.xml +++ b/skywalking-collector/skywalking-sdk-plugin/spring-plugin/pom.xml @@ -52,7 +52,7 @@ com.ai.cloud skywalking-auth ${project.version} - test + sample
--> diff --git a/skywalking-collector/skywalking-sdk-plugin/tomcat-7.x-8.x-plugin/target/maven-archiver/pom.properties b/skywalking-collector/skywalking-sdk-plugin/tomcat-7.x-8.x-plugin/target/maven-archiver/pom.properties new file mode 100644 index 000000000..35499f84c --- /dev/null +++ b/skywalking-collector/skywalking-sdk-plugin/tomcat-7.x-8.x-plugin/target/maven-archiver/pom.properties @@ -0,0 +1,5 @@ +#Generated by Maven +#Mon Jul 25 22:41:11 CST 2016 +version=1.0-Final +groupId=com.ai.cloud +artifactId=tomcat-7.x-8.x-plugin diff --git a/skywalking-collector/skywalking-sdk-plugin/tomcat-7.x-8.x-plugin/target/maven-status/maven-compiler-plugin/compile/default-compile/createdFiles.lst b/skywalking-collector/skywalking-sdk-plugin/tomcat-7.x-8.x-plugin/target/maven-status/maven-compiler-plugin/compile/default-compile/createdFiles.lst new file mode 100644 index 000000000..1f4e0fe40 --- /dev/null +++ b/skywalking-collector/skywalking-sdk-plugin/tomcat-7.x-8.x-plugin/target/maven-status/maven-compiler-plugin/compile/default-compile/createdFiles.lst @@ -0,0 +1,3 @@ +com/ai/cloud/skywalking/plugin/tomcat78x/TomcatPluginInterceptor.class +com/ai/cloud/skywalking/plugin/tomcat78x/WebBuriedPointType.class +com/ai/cloud/skywalking/plugin/tomcat78x/define/TomcatPluginDefine.class diff --git a/skywalking-collector/skywalking-sdk-plugin/tomcat-7.x-8.x-plugin/target/maven-status/maven-compiler-plugin/compile/default-compile/inputFiles.lst b/skywalking-collector/skywalking-sdk-plugin/tomcat-7.x-8.x-plugin/target/maven-status/maven-compiler-plugin/compile/default-compile/inputFiles.lst new file mode 100644 index 000000000..4ad1c6fd9 --- /dev/null +++ b/skywalking-collector/skywalking-sdk-plugin/tomcat-7.x-8.x-plugin/target/maven-status/maven-compiler-plugin/compile/default-compile/inputFiles.lst @@ -0,0 +1,3 @@ +/Users/xin/workbench/sky-walking/skywalking-collector/skywalking-sdk-plugin/tomcat-7.x-8.x-plugin/src/main/java/com/ai/cloud/skywalking/plugin/tomcat78x/TomcatPluginInterceptor.java +/Users/xin/workbench/sky-walking/skywalking-collector/skywalking-sdk-plugin/tomcat-7.x-8.x-plugin/src/main/java/com/ai/cloud/skywalking/plugin/tomcat78x/define/TomcatPluginDefine.java +/Users/xin/workbench/sky-walking/skywalking-collector/skywalking-sdk-plugin/tomcat-7.x-8.x-plugin/src/main/java/com/ai/cloud/skywalking/plugin/tomcat78x/WebBuriedPointType.java diff --git a/skywalking-collector/skywalking-sdk-plugin/web-plugin/pom.xml b/skywalking-collector/skywalking-sdk-plugin/web-plugin/pom.xml index c1a414900..4a265deff 100644 --- a/skywalking-collector/skywalking-sdk-plugin/web-plugin/pom.xml +++ b/skywalking-collector/skywalking-sdk-plugin/web-plugin/pom.xml @@ -28,7 +28,7 @@ com.ai.cloud skywalking-auth ${project.version} - test + sample -->