Skip to content

Commit b9a9014

Browse files
committed
了解skywalking的agent
1 parent fbcdf29 commit b9a9014

File tree

6 files changed

+11
-2
lines changed

6 files changed

+11
-2
lines changed

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ public static void initializeCoreConfig(String agentOptions) {
7373
AGENT_SETTINGS.load(configFileStream);
7474
for (String key : AGENT_SETTINGS.stringPropertyNames()) {
7575
String value = (String) AGENT_SETTINGS.get(key);
76+
//替换占位符的变量
7677
AGENT_SETTINGS.put(key, PropertyPlaceholderHelper.INSTANCE.replacePlaceholders(value, AGENT_SETTINGS));
7778
}
7879

@@ -81,6 +82,7 @@ public static void initializeCoreConfig(String agentOptions) {
8182
}
8283

8384
try {
85+
//替换通过java -Dskywalking.指定的变量值
8486
overrideConfigBySystemProp();
8587
} catch (Exception e) {
8688
LOGGER.error(e, "Failed to read the system properties.");
@@ -91,7 +93,7 @@ public static void initializeCoreConfig(String agentOptions) {
9193
try {
9294
agentOptions = agentOptions.trim();
9395
LOGGER.info("Agent options is {}.", agentOptions);
94-
96+
//替换在 java -agent:/***.jar=key:value里的值
9597
overrideConfigByAgentOptions(agentOptions);
9698
} catch (Exception e) {
9799
LOGGER.error(e, "Failed to parse the agent options, val is {}.", agentOptions);
@@ -243,6 +245,7 @@ private static void setAgentVersion() {
243245
* @return the config file {@link InputStream}, or null if not needEnhance.
244246
*/
245247
private static InputStreamReader loadConfig() throws AgentPackageNotFoundException, ConfigNotFoundException {
248+
//从系统属性里取自定义路径或者从默认路径/config/agent.config初始化配置
246249
String specifiedConfigPath = System.getProperty(SPECIFIED_CONFIG_PATH);
247250
File configFile = StringUtil.isEmpty(specifiedConfigPath) ? new File(
248251
AgentPackagePath.getPath(), DEFAULT_CONFIG_FILE_NAME) : new File(specifiedConfigPath);

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
@@ -39,9 +39,12 @@ public class PluginBootstrap {
3939
* @return plugin definition list.
4040
*/
4141
public List<AbstractClassEnhancePluginDefine> loadPlugins() throws AgentPackageNotFoundException {
42+
//可以通过启动一次看日志,了解这里是先装载再识别出def
43+
//注意,这里自定义了一个类加载器,并且加载了/activations/plugins下的jar包
4244
AgentClassLoader.initDefaultLoader();
4345

4446
PluginResourcesResolver resolver = new PluginResourcesResolver();
47+
//获取所有的skywalking-plugin.def,由于上面的jar包已经添加,所以实际就是那些jar包里的skywalking-plugin.def
4548
List<URL> resources = resolver.getResources();
4649

4750
if (resources == null || resources.size() == 0) {

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ void load(InputStream input) throws IOException {
5252
LOGGER.error(e, "Failed to format plugin({}) define.", pluginDefine);
5353
}
5454
}
55+
//可以排除插件
5556
pluginClassList = pluginSelector.select(pluginClassList);
5657
} finally {
5758
input.close();

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

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

apm-sniffer/apm-agent/pom.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@
6868
<transformers>
6969
<transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
7070
<manifestEntries>
71+
<!-- 在这里指定了premain-class-->
7172
<Premain-Class>${premain.class}</Premain-Class>
7273
<Can-Redefine-Classes>${can.redefine.classes}</Can-Redefine-Classes>
7374
<Can-Retransform-Classes>${can.retransform.classes}</Can-Retransform-Classes>

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ public static void premain(String agentArgs, Instrumentation instrumentation) th
9090
LOGGER.error(e, "SkyWalking agent initialized failure. Shutting down.");
9191
return;
9292
}
93-
93+
//byte-buddy是一个字节码生成和操作库
9494
final ByteBuddy byteBuddy = new ByteBuddy().with(TypeValidation.of(Config.Agent.IS_OPEN_DEBUGGING_CLASS));
9595

9696
AgentBuilder agentBuilder = new AgentBuilder.Default(byteBuddy).ignore(

0 commit comments

Comments
 (0)