Skip to content

Commit 9573243

Browse files
committed
研究启动和上报逻辑
1 parent 2fae67b commit 9573243

File tree

6 files changed

+18
-3
lines changed

6 files changed

+18
-3
lines changed

apm-sniffer/apm-agent-core/src/main/java/org/apache/skywalking/apm/agent/core/conf/SnifferConfigInitializer.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -245,7 +245,7 @@ private static void setAgentVersion() {
245245
* @return the config file {@link InputStream}, or null if not needEnhance.
246246
*/
247247
private static InputStreamReader loadConfig() throws AgentPackageNotFoundException, ConfigNotFoundException {
248-
//从系统属性里取自定义路径或者从默认路径/config/agent.config初始化配置
248+
//从系统属性(即 java -Dskywalking_config=xxx/custom.config)里取自定义配置或者从默认路径/config/agent.config初始化配置
249249
String specifiedConfigPath = System.getProperty(SPECIFIED_CONFIG_PATH);
250250
File configFile = StringUtil.isEmpty(specifiedConfigPath) ? new File(
251251
AgentPackagePath.getPath(), DEFAULT_CONFIG_FILE_NAME) : new File(specifiedConfigPath);

apm-sniffer/apm-agent-core/src/main/java/org/apache/skywalking/apm/agent/core/context/trace/AbstractTracingSpan.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,7 @@ public AbstractTracingSpan tag(AbstractTag<?> tag, String value) {
145145
*/
146146
public boolean finish(TraceSegment owner) {
147147
this.endTime = System.currentTimeMillis();
148+
//在这里进行的封装和消费
148149
owner.archive(this);
149150
return true;
150151
}

apm-sniffer/apm-agent-core/src/main/java/org/apache/skywalking/apm/agent/core/context/trace/TraceSegment.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ public void setIgnore(boolean ignore) {
140140

141141
/**
142142
* This is a high CPU cost method, only called when sending to collector or test cases.
143-
*
143+
* 在这里对span进行封装,通过消费者线程池进行消费发送到oap
144144
* @return the segment as GRPC service parameter
145145
*/
146146
public SegmentObject transform() {

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,9 @@ 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感觉更像是在加载某类之前,必须有些类已经加载
71+
//witness机制
72+
// 如果必须在特定的版本中激活工具,则设置 witessClass 和/或 witessMethod。
73+
// 必须已经存在指定类或者方法之后,才能让某些插件起效
7274
/**
7375
* find witness classes for enhance class
7476
*/

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,9 @@ public class PluginBootstrap {
4141
public List<AbstractClassEnhancePluginDefine> loadPlugins() throws AgentPackageNotFoundException {
4242
//可以通过启动一次看日志,了解这里是先装载再识别出def
4343
//注意,这里自定义了一个类加载器,并且加载了/activations/plugins下的jar包
44+
//Q&A 2023/8/27
45+
// Q:为什么要自定义类加载器
46+
// A:为了加载插件的那些jar包,继承自AppClassLodder
4447
AgentClassLoader.initDefaultLoader();
4548

4649
PluginResourcesResolver resolver = new PluginResourcesResolver();

apm-sniffer/apm-agent-core/src/main/java/org/apache/skywalking/apm/agent/core/plugin/interceptor/enhance/ClassEnhancePluginDefine.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,10 @@ protected DynamicType.Builder<?> enhanceInstance(TypeDescription typeDescription
9696
* And make sure the source codes manipulation only occurs once.
9797
*
9898
*/
99+
//生成一个新的类,该类继承EnhancedInstance接口,并且自定义了一个字段叫做_$EnhancedClassField_ws,同时生成了这个的set、get方法
100+
//Q&A 2023/8/29
101+
// Q: 为什么非要指定这样一个字段呢?
102+
// A: 说是为了解决上下文问题,需要这样一个字段缓存,但是我不理解 https://github.com/apache/skywalking/issues/7146
99103
if (!typeDescription.isAssignableTo(EnhancedInstance.class)) {
100104
if (!context.isObjectExtended()) {
101105
newClassBuilder = newClassBuilder.defineField(
@@ -105,13 +109,18 @@ protected DynamicType.Builder<?> enhanceInstance(TypeDescription typeDescription
105109
context.extendObjectCompleted();
106110
}
107111
}
112+
//下面的增强构造和实例方法其实很类似
113+
//核心点在于InstanceMethodsInterceptPoint,
114+
//通过getMethodsMatcher获取需要加强的方法
115+
//通过getMethodsInterceptor获取需要插件定义的拦截器,然后将其构造到agentBuilder里
108116

109117
/**
110118
* 2. enhance constructors
111119
*/
112120
if (existedConstructorInterceptPoint) {
113121
for (ConstructorInterceptPoint constructorInterceptPoint : constructorInterceptPoints) {
114122
if (isBootstrapInstrumentation()) {
123+
//看不懂具体的intercept的原理,但目前也不关心
115124
newClassBuilder = newClassBuilder.constructor(constructorInterceptPoint.getConstructorMatcher())
116125
.intercept(SuperMethodCall.INSTANCE.andThen(MethodDelegation.withDefaultConfiguration()
117126
.to(BootstrapInstrumentBoost

0 commit comments

Comments
 (0)