为transform增加Listenser。修改AbstractClassEnhancePluginDefine,满足模糊匹配类名进行transform的要求。
This commit is contained in:
parent
0149955444
commit
f0c0474197
|
|
@ -16,7 +16,7 @@
|
|||
|
||||
<properties>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
<premain.class>com.ai.cloud.skywalking.agent.SkywalkingAgent</premain.class>
|
||||
<premain.class>com.ai.cloud.skywalking.agent.SkyWalkingAgent</premain.class>
|
||||
<shade.net.bytebuddy.source>net.bytebuddy</shade.net.bytebuddy.source>
|
||||
<shade.net.bytebuddy.target>com.ai.cloud.skywalking.api.dependencies.net.bytebuddy</shade.net.bytebuddy.target>
|
||||
<shade.io.netty.source>io.netty</shade.io.netty.source>
|
||||
|
|
|
|||
|
|
@ -12,31 +12,47 @@ import net.bytebuddy.description.NamedElement;
|
|||
import net.bytebuddy.description.type.TypeDescription;
|
||||
import net.bytebuddy.dynamic.DynamicType;
|
||||
import net.bytebuddy.matcher.ElementMatcher;
|
||||
import net.bytebuddy.utility.JavaModule;
|
||||
|
||||
import java.io.File;
|
||||
import java.lang.instrument.Instrumentation;
|
||||
import java.net.URL;
|
||||
import java.util.List;
|
||||
|
||||
public class SkywalkingAgent {
|
||||
|
||||
private static Logger logger = LogManager.getLogger(SkywalkingAgent.class);
|
||||
private static final PluginDefineCategory pluginDefineCategory = PluginDefineCategory.category(new PluginBootstrap().loadPlugins());
|
||||
public class SkyWalkingAgent {
|
||||
private static Logger logger = LogManager.getLogger(SkyWalkingAgent.class);
|
||||
|
||||
public static void premain(String agentArgs, Instrumentation instrumentation) throws PluginException {
|
||||
initConfig();
|
||||
if (AuthDesc.isAuth()) {
|
||||
final PluginDefineCategory pluginDefineCategory = PluginDefineCategory.category(new PluginBootstrap().loadPlugins());
|
||||
|
||||
startBootPluginDefines(pluginDefineCategory.getBootPluginsDefines());
|
||||
|
||||
new AgentBuilder.Default().type(enhanceClassMatcher()).transform(new AgentBuilder.Transformer() {
|
||||
new AgentBuilder.Default().type(enhanceClassMatcher(pluginDefineCategory)).transform(new AgentBuilder.Transformer() {
|
||||
public DynamicType.Builder<?> transform(DynamicType.Builder<?> builder, TypeDescription typeDescription, ClassLoader classLoader) {
|
||||
AbstractClassEnhancePluginDefine pluginDefine = pluginDefineCategory.findPluginDefine(typeDescription.getTypeName());
|
||||
try {
|
||||
return pluginDefine.define(builder);
|
||||
} catch (Throwable e) {
|
||||
logger.error("Failed to enhance plugin " + pluginDefine.getClass().getName(), e);
|
||||
return builder;
|
||||
}
|
||||
return pluginDefine.define(typeDescription.getTypeName(), builder);
|
||||
}
|
||||
}).with(new AgentBuilder.Listener() {
|
||||
@Override
|
||||
public void onTransformation(TypeDescription typeDescription, ClassLoader classLoader, JavaModule module, DynamicType dynamicType) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onIgnored(TypeDescription typeDescription, ClassLoader classLoader, JavaModule module) {
|
||||
logger.info("ignore to enhance class " + typeDescription.getTypeName());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onError(String typeName, ClassLoader classLoader, JavaModule module, Throwable throwable) {
|
||||
logger.error("Failed to enhance class " + typeName, throwable);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onComplete(String typeName, ClassLoader classLoader, JavaModule module) {
|
||||
logger.info("enhance class " + typeName + " complete.");
|
||||
}
|
||||
}).installOn(instrumentation);
|
||||
|
||||
|
|
@ -44,7 +60,7 @@ public class SkywalkingAgent {
|
|||
}
|
||||
|
||||
|
||||
private static <T extends NamedElement> ElementMatcher.Junction<T> enhanceClassMatcher() {
|
||||
private static <T extends NamedElement> ElementMatcher.Junction<T> enhanceClassMatcher(PluginDefineCategory pluginDefineCategory) {
|
||||
return new SkyWalkingEnhanceMatcher<T>(pluginDefineCategory);
|
||||
}
|
||||
|
||||
|
|
@ -57,7 +73,7 @@ public class SkywalkingAgent {
|
|||
|
||||
|
||||
private static String generateLocationPath() {
|
||||
return SkywalkingAgent.class.getName().replaceAll("\\.", "/") + ".class";
|
||||
return SkyWalkingAgent.class.getName().replaceAll("\\.", "/") + ".class";
|
||||
}
|
||||
|
||||
|
||||
|
|
@ -68,7 +84,7 @@ public class SkywalkingAgent {
|
|||
|
||||
private static String initAgentBasePath() {
|
||||
try {
|
||||
String urlString = SkywalkingAgent.class.getClassLoader().getSystemClassLoader().getResource(generateLocationPath()).toString();
|
||||
String urlString = SkyWalkingAgent.class.getClassLoader().getSystemClassLoader().getResource(generateLocationPath()).toString();
|
||||
urlString = urlString.substring(urlString.indexOf("file:"), urlString.indexOf('!'));
|
||||
return new File(new URL(urlString).getFile()).getParentFile().getAbsolutePath();
|
||||
} catch (Exception e) {
|
||||
|
|
|
|||
|
|
@ -29,7 +29,7 @@
|
|||
<dependency>
|
||||
<groupId>net.bytebuddy</groupId>
|
||||
<artifactId>byte-buddy</artifactId>
|
||||
<version>1.3.0</version>
|
||||
<version>1.4.16</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
|
|
|
|||
|
|
@ -14,16 +14,15 @@ import static com.ai.cloud.skywalking.plugin.PluginBootstrap.CLASS_TYPE_POOL;
|
|||
public abstract class AbstractClassEnhancePluginDefine implements IPlugin {
|
||||
private static Logger logger = LogManager.getLogger(AbstractClassEnhancePluginDefine.class);
|
||||
|
||||
public DynamicType.Builder<?> define(DynamicType.Builder<?> builder) throws PluginException {
|
||||
public DynamicType.Builder<?> define(String transformClassName, DynamicType.Builder<?> builder) throws PluginException {
|
||||
String interceptorDefineClassName = this.getClass().getName();
|
||||
|
||||
String enhanceOriginClassName = enhanceClassName();
|
||||
if (StringUtil.isEmpty(enhanceOriginClassName)) {
|
||||
if (StringUtil.isEmpty(transformClassName)) {
|
||||
logger.warn("classname of being intercepted is not defined by {}.", interceptorDefineClassName);
|
||||
return builder;
|
||||
}
|
||||
|
||||
logger.debug("prepare to enhance class {} by {}.", enhanceOriginClassName, interceptorDefineClassName);
|
||||
logger.debug("prepare to enhance class {} by {}.", transformClassName, interceptorDefineClassName);
|
||||
|
||||
/**
|
||||
* find witness classes for enhance class
|
||||
|
|
@ -33,7 +32,7 @@ public abstract class AbstractClassEnhancePluginDefine implements IPlugin {
|
|||
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,
|
||||
logger.warn("enhance class {} by plugin {} is not working. Because witness class {} is not existed.", transformClassName, interceptorDefineClassName,
|
||||
witnessClass);
|
||||
return builder;
|
||||
}
|
||||
|
|
@ -43,10 +42,10 @@ public abstract class AbstractClassEnhancePluginDefine implements IPlugin {
|
|||
/**
|
||||
* find origin class source code for interceptor
|
||||
*/
|
||||
DynamicType.Builder<?> newClassBuilder = this.enhance(enhanceOriginClassName, builder);
|
||||
DynamicType.Builder<?> newClassBuilder = this.enhance(transformClassName, builder);
|
||||
|
||||
|
||||
logger.debug("enhance class {} by {} completely.", enhanceOriginClassName, interceptorDefineClassName);
|
||||
logger.debug("enhance class {} by {} completely.", transformClassName, interceptorDefineClassName);
|
||||
|
||||
return newClassBuilder;
|
||||
}
|
||||
|
|
@ -54,7 +53,7 @@ public abstract class AbstractClassEnhancePluginDefine implements IPlugin {
|
|||
protected abstract DynamicType.Builder<?> enhance(String enhanceOriginClassName, DynamicType.Builder<?> newClassBuilder) throws PluginException;
|
||||
|
||||
/**
|
||||
* 返回要被增强的类,应当返回类全名
|
||||
* 返回要被增强的类,应当返回类全名或前匹配(返回*号结尾)
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
|
|
|
|||
|
|
@ -1,6 +1,6 @@
|
|||
package com.ai.cloud.skywalking.plugin;
|
||||
|
||||
public class PluginException extends Exception {
|
||||
public class PluginException extends RuntimeException {
|
||||
private static final long serialVersionUID = -6020188711867490724L;
|
||||
|
||||
public PluginException(String message) {
|
||||
|
|
|
|||
|
|
@ -48,7 +48,7 @@ public class TracingBootstrap {
|
|||
continue;
|
||||
}
|
||||
DynamicType.Builder<?> newClassBuilder = new ByteBuddy().rebase(resolution.resolve(), ClassFileLocator.ForClassLoader.ofClassPath());
|
||||
newClassBuilder = ((AbstractClassEnhancePluginDefine)plugin).define(newClassBuilder);
|
||||
newClassBuilder = ((AbstractClassEnhancePluginDefine)plugin).define(enhanceClassName, newClassBuilder);
|
||||
newClassBuilder.make().load(ClassLoader.getSystemClassLoader(), ClassLoadingStrategy.Default.INJECTION).getLoaded();
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Reference in New Issue