From 9ec0eb009f3c9346e9c9794f997960ac4b971fcf Mon Sep 17 00:00:00 2001 From: kazusa <409053122@qq.com> Date: Fri, 18 Jul 2025 18:09:33 +0800 Subject: [PATCH] =?UTF-8?q?agent=E6=BA=90=E7=A0=81=E6=B3=A8=E9=87=8A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../core/conf/SnifferConfigInitializer.java | 3 +- .../AbstractClassEnhancePluginDefine.java | 3 + .../apm/agent/core/plugin/PluginFinder.java | 3 + .../core/plugin/loader/AgentClassLoader.java | 1 + .../skywalking/apm/agent/SkyWalkingAgent.java | 6 + .../apm/agent/test/DemoApplication.java | 11 ++ debug-demo/pom.xml | 20 +++ .../main/java/org/apache/skywalking/Main.java | 21 +++ pom.xml | 121 +++++++++--------- 9 files changed, 128 insertions(+), 61 deletions(-) create mode 100644 apm-sniffer/apm-test-tools/src/main/java/org/apache/skywalking/apm/agent/test/DemoApplication.java create mode 100644 debug-demo/pom.xml create mode 100644 debug-demo/src/main/java/org/apache/skywalking/Main.java diff --git a/apm-sniffer/apm-agent-core/src/main/java/org/apache/skywalking/apm/agent/core/conf/SnifferConfigInitializer.java b/apm-sniffer/apm-agent-core/src/main/java/org/apache/skywalking/apm/agent/core/conf/SnifferConfigInitializer.java index c48b58a11..1f9f72b05 100644 --- a/apm-sniffer/apm-agent-core/src/main/java/org/apache/skywalking/apm/agent/core/conf/SnifferConfigInitializer.java +++ b/apm-sniffer/apm-agent-core/src/main/java/org/apache/skywalking/apm/agent/core/conf/SnifferConfigInitializer.java @@ -97,7 +97,7 @@ public class SnifferConfigInitializer { LOGGER.error(e, "Failed to parse the agent options, val is {}.", agentOptions); } } - + // 加载核心配置 initializeConfig(Config.class); // reconfigure logger after config initialization configureLogger(); @@ -105,6 +105,7 @@ public class SnifferConfigInitializer { setAgentVersion(); + // 服务名配置,可通过集群+命名空间进行隔离 if (StringUtil.isEmpty(Config.Agent.SERVICE_NAME)) { throw new ExceptionInInitializerError("`agent.service_name` is missing."); } else { diff --git a/apm-sniffer/apm-agent-core/src/main/java/org/apache/skywalking/apm/agent/core/plugin/AbstractClassEnhancePluginDefine.java b/apm-sniffer/apm-agent-core/src/main/java/org/apache/skywalking/apm/agent/core/plugin/AbstractClassEnhancePluginDefine.java index f0ab14a75..7215fa1bf 100644 --- a/apm-sniffer/apm-agent-core/src/main/java/org/apache/skywalking/apm/agent/core/plugin/AbstractClassEnhancePluginDefine.java +++ b/apm-sniffer/apm-agent-core/src/main/java/org/apache/skywalking/apm/agent/core/plugin/AbstractClassEnhancePluginDefine.java @@ -83,6 +83,7 @@ public abstract class AbstractClassEnhancePluginDefine { /** * find witness classes for enhance class */ + // 校验见证类是否在classpath中,见证类依赖于应用程序的classpath,如果见证类不存在,则不进行增强 String[] witnessClasses = witnessClasses(); if (witnessClasses != null) { for (String witnessClass : witnessClasses) { @@ -92,6 +93,8 @@ public abstract class AbstractClassEnhancePluginDefine { } } } + // 校验见证方法是否在classpath中,见证方法依赖于应用程序的classpath,如果见证方法不存在,则不进行增强 + // 同名的见证方法可能存在于多个版本的应用程序中,因此需要校验所有见证方法是否存在 List witnessMethods = witnessMethods(); if (!CollectionUtil.isEmpty(witnessMethods)) { for (WitnessMethod witnessMethod : witnessMethods) { diff --git a/apm-sniffer/apm-agent-core/src/main/java/org/apache/skywalking/apm/agent/core/plugin/PluginFinder.java b/apm-sniffer/apm-agent-core/src/main/java/org/apache/skywalking/apm/agent/core/plugin/PluginFinder.java index 5a7404e72..9a9f5f77f 100644 --- a/apm-sniffer/apm-agent-core/src/main/java/org/apache/skywalking/apm/agent/core/plugin/PluginFinder.java +++ b/apm-sniffer/apm-agent-core/src/main/java/org/apache/skywalking/apm/agent/core/plugin/PluginFinder.java @@ -40,8 +40,11 @@ import static net.bytebuddy.matcher.ElementMatchers.not; * AbstractClassEnhancePluginDefine} list. */ public class PluginFinder { + // 按类名匹配的插件 private final Map> nameMatchDefine = new HashMap>(); + // 按类结构信息匹配的插件 private final List signatureMatchDefine = new ArrayList(); + // 增强引导类的插件 private final List bootstrapClassMatchDefine = new ArrayList(); private static boolean IS_PLUGIN_INIT_COMPLETED = false; diff --git a/apm-sniffer/apm-agent-core/src/main/java/org/apache/skywalking/apm/agent/core/plugin/loader/AgentClassLoader.java b/apm-sniffer/apm-agent-core/src/main/java/org/apache/skywalking/apm/agent/core/plugin/loader/AgentClassLoader.java index 18e6ffd26..a82d4cf2e 100644 --- a/apm-sniffer/apm-agent-core/src/main/java/org/apache/skywalking/apm/agent/core/plugin/loader/AgentClassLoader.java +++ b/apm-sniffer/apm-agent-core/src/main/java/org/apache/skywalking/apm/agent/core/plugin/loader/AgentClassLoader.java @@ -86,6 +86,7 @@ public class AgentClassLoader extends ClassLoader { super(parent); File agentDictionary = AgentPackagePath.getPath(); classpath = new LinkedList<>(); + // 2.1加载插件目录 Config.Plugin.MOUNT.forEach(mountFolder -> classpath.add(new File(agentDictionary, mountFolder))); } diff --git a/apm-sniffer/apm-agent/src/main/java/org/apache/skywalking/apm/agent/SkyWalkingAgent.java b/apm-sniffer/apm-agent/src/main/java/org/apache/skywalking/apm/agent/SkyWalkingAgent.java index 2dd3fe8b3..d873ba8e0 100644 --- a/apm-sniffer/apm-agent/src/main/java/org/apache/skywalking/apm/agent/SkyWalkingAgent.java +++ b/apm-sniffer/apm-agent/src/main/java/org/apache/skywalking/apm/agent/SkyWalkingAgent.java @@ -75,6 +75,7 @@ public class SkyWalkingAgent { public static void premain(String agentArgs, Instrumentation instrumentation) throws PluginException { final PluginFinder pluginFinder; try { + // 1.加载配置 SnifferConfigInitializer.initializeCoreConfig(agentArgs); } catch (Exception e) { // try to resolve a new logger, and use the new logger to write the error log here @@ -92,6 +93,7 @@ public class SkyWalkingAgent { } try { + // 2.加载插件 pluginFinder = new PluginFinder(new PluginBootstrap().loadPlugins()); } catch (AgentPackageNotFoundException ape) { LOGGER.error(ape, "Locate agent.jar failure. Shutting down."); @@ -102,6 +104,7 @@ public class SkyWalkingAgent { } try { + // 注入增强字节码 installClassTransformer(instrumentation, pluginFinder); } catch (Exception e) { LOGGER.error(e, "Skywalking agent installed class transformer failure."); @@ -133,12 +136,14 @@ public class SkyWalkingAgent { JDK9ModuleExporter.EdgeClasses edgeClasses = new JDK9ModuleExporter.EdgeClasses(); try { + // 增强BootStrap类 agentBuilder = BootstrapInstrumentBoost.inject(pluginFinder, instrumentation, agentBuilder, edgeClasses); } catch (Exception e) { throw new Exception("SkyWalking agent inject bootstrap instrumentation failure. Shutting down.", e); } try { + // 兼容jdk9+ agentBuilder = JDK9ModuleExporter.openReadEdge(instrumentation, agentBuilder, edgeClasses); } catch (Exception e) { throw new Exception("SkyWalking agent open read edge in JDK 9+ failure. Shutting down.", e); @@ -185,6 +190,7 @@ public class SkyWalkingAgent { final JavaModule javaModule, final ProtectionDomain protectionDomain) { LoadedLibraryCollector.registerURLClassLoader(classLoader); + // 过滤匹配的插件 List pluginDefines = pluginFinder.find(typeDescription); if (pluginDefines.size() > 0) { DynamicType.Builder newBuilder = builder; diff --git a/apm-sniffer/apm-test-tools/src/main/java/org/apache/skywalking/apm/agent/test/DemoApplication.java b/apm-sniffer/apm-test-tools/src/main/java/org/apache/skywalking/apm/agent/test/DemoApplication.java new file mode 100644 index 000000000..7069402da --- /dev/null +++ b/apm-sniffer/apm-test-tools/src/main/java/org/apache/skywalking/apm/agent/test/DemoApplication.java @@ -0,0 +1,11 @@ +package org.apache.skywalking.apm.agent.test; + +/** + * @author Wen + * @date 2025/7/17 16:18 + */ +public class DemoApplication { + public static void main(String[] args) { + System.out.println(1); + } +} diff --git a/debug-demo/pom.xml b/debug-demo/pom.xml new file mode 100644 index 000000000..57e196c9f --- /dev/null +++ b/debug-demo/pom.xml @@ -0,0 +1,20 @@ + + + 4.0.0 + + org.apache.skywalking + java-agent + 9.5.0-SNAPSHOT + + + debug-demo + + + 17 + 17 + UTF-8 + + + \ No newline at end of file diff --git a/debug-demo/src/main/java/org/apache/skywalking/Main.java b/debug-demo/src/main/java/org/apache/skywalking/Main.java new file mode 100644 index 000000000..840f62dab --- /dev/null +++ b/debug-demo/src/main/java/org/apache/skywalking/Main.java @@ -0,0 +1,21 @@ +package org.apache.skywalking; + +/** + * @author Wen + * @date 2025/7/17 18:02 + */ +//TIP To Run code, press or +// click the icon in the gutter. +public class Main { + public static void main(String[] args) { + //TIP Press with your caret at the highlighted text + // to see how IntelliJ IDEA suggests fixing it. + System.out.printf("Hello and welcome!"); + + for (int i = 1; i <= 5; i++) { + //TIP Press to start debugging your code. We have set one breakpoint + // for you, but you can always add more by pressing . + System.out.println("i = " + i); + } + } +} \ No newline at end of file diff --git a/pom.xml b/pom.xml index 128d7f6f0..cabcd1154 100755 --- a/pom.xml +++ b/pom.xml @@ -36,6 +36,7 @@ apm-protocol apm-sniffer apm-application-toolkit + debug-demo pom @@ -390,66 +391,66 @@ - - maven-checkstyle-plugin - ${maven-checkstyle-plugin.version} - - ${maven.multiModuleProjectDirectory}/apm-checkstyle/checkStyle.xml - ${maven.multiModuleProjectDirectory}/apm-checkstyle/CHECKSTYLE_HEAD - UTF-8 - true - true - ${checkstyle.fails.on.error} - - ${project.build.sourceDirectory} - ${project.build.testSourceDirectory} -scenarios/resttemplate-6.x-scenario - - - **/*.properties, - **/*.sh, - **/*.bat, - **/*.yml, - **/*.yaml, - **/*.xml - - - **/.asf.yaml, - **/.github/**, - **/skywalking-agent-version.properties - - - **/target/generated-test-sources/**, - org/apache/skywalking/apm/network/register/v2/**/*.java, - org/apache/skywalking/apm/network/common/**/*.java, - org/apache/skywalking/apm/network/servicemesh/**/*.java, - org/apache/skywalking/apm/network/language/**/*.java, - org/apache/skywalking/oap/server/core/remote/grpc/proto/*.java, - org/apache/skywalking/oal/rt/grammar/*.java, - org/apache/skywalking/oap/server/exporter/grpc/*.java, - org/apache/skywalking/oap/server/configuration/service/*.java, - **/jmh_generated/*_jmhType*.java, - **/jmh_generated/*_jmhTest.java, - net/bytebuddy/**, - - com/ibm/websphere/servlet/request/IRequest.java, - com/ibm/ws/webcontainer/async/CompleteRunnable.java, - com/ibm/ws/webcontainer/async/DispatchRunnable.java - - - import.control=${maven.multiModuleProjectDirectory}/apm-checkstyle/importControl.xml - - - - - validate - process-sources - - check - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + org.apache.maven.plugins