Skip to content

Commit 2fae67b

Browse files
committed
跟着controller走到了enhance
1 parent b9a9014 commit 2fae67b

File tree

7 files changed

+33
-10
lines changed

7 files changed

+33
-10
lines changed

apm-sniffer/apm-agent-core/src/main/java/org/apache/skywalking/apm/agent/core/plugin/AbstractClassEnhancePluginDefine.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ public DynamicType.Builder<?> define(TypeDescription typeDescription, DynamicTyp
6868

6969
LOGGER.debug("prepare to enhance class {} by {}.", transformClassName, interceptorDefineClassName);
7070
WitnessFinder finder = WitnessFinder.INSTANCE;
71+
//witness感觉更像是在加载某类之前,必须有些类已经加载
7172
/**
7273
* find witness classes for enhance class
7374
*/
@@ -110,6 +111,7 @@ public DynamicType.Builder<?> define(TypeDescription typeDescription, DynamicTyp
110111
*/
111112
protected DynamicType.Builder<?> enhance(TypeDescription typeDescription, DynamicType.Builder<?> newClassBuilder,
112113
ClassLoader classLoader, EnhanceContext context) throws PluginException {
114+
//这里是实际和插件作用的地方
113115
newClassBuilder = this.enhanceClass(typeDescription, newClassBuilder, classLoader);
114116

115117
newClassBuilder = this.enhanceInstance(typeDescription, newClassBuilder, classLoader, context);

apm-sniffer/apm-agent-core/src/main/java/org/apache/skywalking/apm/agent/core/plugin/PluginBootstrap.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,26 +54,30 @@ public List<AbstractClassEnhancePluginDefine> loadPlugins() throws AgentPackageN
5454

5555
for (URL pluginUrl : resources) {
5656
try {
57+
//最终都加载到了List<PluginDefine> pluginClassList里
5758
PluginCfg.INSTANCE.load(pluginUrl.openStream());
5859
} catch (Throwable t) {
5960
LOGGER.error(t, "plugin file [{}] init failure.", pluginUrl);
6061
}
6162
}
6263

6364
List<PluginDefine> pluginClassList = PluginCfg.INSTANCE.getPluginClassList();
64-
65+
//从这里往下的暂时没有看懂
6566
List<AbstractClassEnhancePluginDefine> plugins = new ArrayList<AbstractClassEnhancePluginDefine>();
6667
for (PluginDefine pluginDefine : pluginClassList) {
6768
try {
6869
LOGGER.debug("loading plugin class {}.", pluginDefine.getDefineClass());
70+
//Q&A 2023/8/22
71+
// Q:为什么这里所有的类都能强转为AbstractClassEnhancePluginDefine
72+
// A:因为插件继承的顶层最终都是这个抽象类
6973
AbstractClassEnhancePluginDefine plugin = (AbstractClassEnhancePluginDefine) Class.forName(pluginDefine.getDefineClass(), true, AgentClassLoader
7074
.getDefault()).newInstance();
7175
plugins.add(plugin);
7276
} catch (Throwable t) {
7377
LOGGER.error(t, "load plugin [{}] failure.", pluginDefine.getDefineClass());
7478
}
7579
}
76-
80+
//这个是通过SPI用所有的InstrumentationLoader来添加AbstractClassEnhancePluginDefine,不知道用在哪
7781
plugins.addAll(DynamicPluginLoader.INSTANCE.load(AgentClassLoader.getDefault()));
7882

7983
return plugins;

apm-sniffer/apm-agent-core/src/main/java/org/apache/skywalking/apm/agent/core/plugin/PluginCfg.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,11 @@ void load(InputStream input) throws IOException {
4646
if (pluginDefine.trim().length() == 0 || pluginDefine.startsWith("#")) {
4747
continue;
4848
}
49+
//e.g.
50+
//spring-mvc-annotation-3.x=org.apache.skywalking.apm.plugin.spring.mvc.v3.define.ControllerInstrumentation
51+
//spring-mvc-annotation-3.x=org.apache.skywalking.apm.plugin.spring.mvc.v3.define.HandlerMethodInstrumentation
52+
//spring-mvc-annotation-3.x=org.apache.skywalking.apm.plugin.spring.mvc.v3.define.InvocableHandlerInstrumentation
53+
//spring-mvc-annotation-3.x=org.apache.skywalking.apm.plugin.spring.mvc.v3.define.HandlerMethodInvokerInstrumentation
4954
PluginDefine plugin = PluginDefine.build(pluginDefine);
5055
pluginClassList.add(plugin);
5156
} catch (IllegalPluginDefineException e) {

apm-sniffer/apm-agent-core/src/main/java/org/apache/skywalking/apm/agent/core/plugin/PluginFinder.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ public PluginFinder(List<AbstractClassEnhancePluginDefine> plugins) {
5252
if (match == null) {
5353
continue;
5454
}
55-
55+
//重写enhanceClass,以restTemplate为例,NameMatch.byName("org.springframework.web.client.RestTemplate")
5656
if (match instanceof NameMatch) {
5757
NameMatch nameMatch = (NameMatch) match;
5858
LinkedList<AbstractClassEnhancePluginDefine> pluginDefines = nameMatchDefine.get(nameMatch.getClassName());
@@ -88,6 +88,10 @@ public List<AbstractClassEnhancePluginDefine> find(TypeDescription typeDescripti
8888
return matchedPlugins;
8989
}
9090

91+
/**
92+
* 主要是通过插件里定义的NameMatch和IndirectMatch来匹配,IndirectMatch就是要更复杂,比如匹配的更多,前缀,后缀之类的,参考IndirectMatch的实现类
93+
* @return
94+
*/
9195
public ElementMatcher<? super TypeDescription> buildMatch() {
9296
ElementMatcher.Junction judge = new AbstractJunction<NamedElement>() {
9397
@Override

apm-sniffer/apm-agent-core/src/main/java/org/apache/skywalking/apm/agent/core/plugin/bootstrap/BootstrapInstrumentBoost.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ public class BootstrapInstrumentBoost {
8787
public static AgentBuilder inject(PluginFinder pluginFinder, Instrumentation instrumentation,
8888
AgentBuilder agentBuilder, JDK9ModuleExporter.EdgeClasses edgeClasses) throws PluginException {
8989
Map<String, byte[]> classesTypeMap = new LinkedHashMap<>();
90-
90+
//这两个方法都似乎是在处理bootstrap相关的类
9191
if (!prepareJREInstrumentation(pluginFinder, classesTypeMap)) {
9292
return agentBuilder;
9393
}
@@ -116,6 +116,7 @@ public static AgentBuilder inject(PluginFinder pluginFinder, Instrumentation ins
116116
*/
117117
ClassInjector.UsingUnsafe.Factory factory = ClassInjector.UsingUnsafe.Factory.resolve(instrumentation);
118118
factory.make(null, null).injectRaw(classesTypeMap);
119+
//实际在这个地方注入了所有需要注入到引导类加载器对应的类
119120
agentBuilder = agentBuilder.with(new AgentBuilder.InjectionStrategy.UsingUnsafe.OfFactory(factory));
120121

121122
return agentBuilder;

apm-sniffer/apm-agent-core/src/main/java/org/apache/skywalking/apm/agent/core/plugin/loader/AgentClassLoader.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ public AgentClassLoader(ClassLoader parent) throws AgentPackageNotFoundException
8686
super(parent);
8787
File agentDictionary = AgentPackagePath.getPath();
8888
classpath = new LinkedList<>();
89-
//将jar包加到类路径
89+
//将jar包加到类路径;MOUNT("plugins", "activations")
9090
Config.Plugin.MOUNT.forEach(mountFolder -> classpath.add(new File(agentDictionary, mountFolder)));
9191
}
9292

apm-sniffer/apm-agent/src/main/java/org/apache/skywalking/apm/agent/SkyWalkingAgent.java

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ public class SkyWalkingAgent {
6565
public static void premain(String agentArgs, Instrumentation instrumentation) throws PluginException {
6666
final PluginFinder pluginFinder;
6767
try {
68+
//将各个途径的配置都处理汇总到CONFIG类里,里面全是静态字段,所以直接赋值也不需要实例化对象
6869
SnifferConfigInitializer.initializeCoreConfig(agentArgs);
6970
} catch (Exception e) {
7071
// try to resolve a new logger, and use the new logger to write the error log here
@@ -82,6 +83,7 @@ public static void premain(String agentArgs, Instrumentation instrumentation) th
8283
}
8384

8485
try {
86+
//这一步实际上是加载了插件jar包里的增加类def里面描述的类,最终都传到了PluginFinder,存储为三种不同的对象
8587
pluginFinder = new PluginFinder(new PluginBootstrap().loadPlugins());
8688
} catch (AgentPackageNotFoundException ape) {
8789
LOGGER.error(ape, "Locate agent.jar failure. Shutting down.");
@@ -92,7 +94,7 @@ public static void premain(String agentArgs, Instrumentation instrumentation) th
9294
}
9395
//byte-buddy是一个字节码生成和操作库
9496
final ByteBuddy byteBuddy = new ByteBuddy().with(TypeValidation.of(Config.Agent.IS_OPEN_DEBUGGING_CLASS));
95-
97+
//bytebuddy提供的更便捷的工具
9698
AgentBuilder agentBuilder = new AgentBuilder.Default(byteBuddy).ignore(
9799
nameStartsWith("net.bytebuddy.")
98100
.or(nameStartsWith("org.slf4j."))
@@ -103,7 +105,9 @@ public static void premain(String agentArgs, Instrumentation instrumentation) th
103105
.or(nameStartsWith("sun.reflect"))
104106
.or(allSkyWalkingAgentExcludeToolkit())
105107
.or(ElementMatchers.isSynthetic()));
106-
108+
//Q&A 2023/8/14
109+
// Q: 这里好像是jdk9开始引入的模块化的概念,需要了解学习
110+
// A:
107111
JDK9ModuleExporter.EdgeClasses edgeClasses = new JDK9ModuleExporter.EdgeClasses();
108112
try {
109113
agentBuilder = BootstrapInstrumentBoost.inject(pluginFinder, instrumentation, agentBuilder, edgeClasses);
@@ -113,6 +117,9 @@ public static void premain(String agentArgs, Instrumentation instrumentation) th
113117
}
114118

115119
try {
120+
//Q&A 2023/8/14
121+
// Q: 先不看,毕竟不懂模块
122+
// A:
116123
agentBuilder = JDK9ModuleExporter.openReadEdge(instrumentation, agentBuilder, edgeClasses);
117124
} catch (Exception e) {
118125
LOGGER.error(e, "SkyWalking agent open read edge in JDK 9+ failure. Shutting down.");
@@ -128,15 +135,15 @@ public static void premain(String agentArgs, Instrumentation instrumentation) th
128135
}
129136
}
130137

131-
agentBuilder.type(pluginFinder.buildMatch())
132-
.transform(new Transformer(pluginFinder))
138+
agentBuilder.type(pluginFinder.buildMatch())//type是命中要处理的类
139+
.transform(new Transformer(pluginFinder))//transform是实际要做的修改,准确的说define的enhance
133140
.with(AgentBuilder.RedefinitionStrategy.RETRANSFORMATION)
134141
.with(new RedefinitionListener())
135142
.with(new Listener())
136143
.installOn(instrumentation);
137144

138145
PluginFinder.pluginInitCompleted();
139-
146+
//以上部分主要是注入工作,下面这个猜测主要是上报
140147
try {
141148
ServiceManager.INSTANCE.boot();
142149
} catch (Exception e) {

0 commit comments

Comments
 (0)