You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@skywalking.apache.org by wu...@apache.org on 2018/09/18 15:38:57 UTC

[incubator-skywalking] 01/01: Add ignore to bytebuddy agent.

This is an automated email from the ASF dual-hosted git repository.

wusheng pushed a commit to branch adjust-agent-setting
in repository https://gitbox.apache.org/repos/asf/incubator-skywalking.git

commit 433cf3a8fdebbcdbc4a6a4c0316c22c49b814933
Author: Wu Sheng <wu...@foxmail.com>
AuthorDate: Tue Sep 18 23:38:45 2018 +0800

    Add ignore to bytebuddy agent.
---
 .../skywalking/apm/agent/SkyWalkingAgent.java      | 46 +++++++++++++++-------
 1 file changed, 31 insertions(+), 15 deletions(-)

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 c224aa5..dbfb4da 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
@@ -16,16 +16,20 @@
  *
  */
 
-
 package org.apache.skywalking.apm.agent;
 
 import java.lang.instrument.Instrumentation;
 import java.util.List;
+import net.bytebuddy.ByteBuddy;
 import net.bytebuddy.agent.builder.AgentBuilder;
 import net.bytebuddy.description.type.TypeDescription;
 import net.bytebuddy.dynamic.DynamicType;
+import net.bytebuddy.dynamic.scaffold.MethodGraph;
+import net.bytebuddy.dynamic.scaffold.TypeValidation;
 import net.bytebuddy.utility.JavaModule;
 import org.apache.skywalking.apm.agent.core.boot.ServiceManager;
+import org.apache.skywalking.apm.agent.core.conf.Config;
+import org.apache.skywalking.apm.agent.core.conf.SnifferConfigInitializer;
 import org.apache.skywalking.apm.agent.core.logging.api.ILog;
 import org.apache.skywalking.apm.agent.core.logging.api.LogManager;
 import org.apache.skywalking.apm.agent.core.plugin.AbstractClassEnhancePluginDefine;
@@ -33,11 +37,12 @@ import org.apache.skywalking.apm.agent.core.plugin.EnhanceContext;
 import org.apache.skywalking.apm.agent.core.plugin.PluginBootstrap;
 import org.apache.skywalking.apm.agent.core.plugin.PluginException;
 import org.apache.skywalking.apm.agent.core.plugin.PluginFinder;
-import org.apache.skywalking.apm.agent.core.conf.SnifferConfigInitializer;
+
+import static net.bytebuddy.matcher.ElementMatchers.nameContains;
+import static net.bytebuddy.matcher.ElementMatchers.nameStartsWith;
 
 /**
- * The main entrance of sky-waking agent,
- * based on javaagent mechanism.
+ * The main entrance of sky-waking agent, based on javaagent mechanism.
  *
  * @author wusheng
  */
@@ -45,8 +50,7 @@ public class SkyWalkingAgent {
     private static final ILog logger = LogManager.getLogger(SkyWalkingAgent.class);
 
     /**
-     * Main entrance.
-     * Use byte-buddy transform to enhance all classes, which define in plugins.
+     * Main entrance. Use byte-buddy transform to enhance all classes, which define in plugins.
      *
      * @param agentArgs
      * @param instrumentation
@@ -64,11 +68,22 @@ public class SkyWalkingAgent {
             return;
         }
 
-        new AgentBuilder.Default()
-                .type(pluginFinder.buildMatch())
-                .transform(new Transformer(pluginFinder))
-                .with(new Listener())
-                .installOn(instrumentation);
+        final ByteBuddy byteBuddy = new ByteBuddy()
+            .with(TypeValidation.of(Config.Agent.IS_OPEN_DEBUGGING_CLASS))
+            .with(MethodGraph.Compiler.ForDeclaredMethods.INSTANCE);
+
+        new AgentBuilder.Default(byteBuddy)
+            .ignore(nameStartsWith("net.bytebuddy."))
+            .ignore(nameStartsWith("org.slf4j."))
+            .ignore(nameStartsWith("org.apache.logging."))
+            .ignore(nameStartsWith("org.apache.skywalking"))
+            .or(nameStartsWith("org.groovy."))
+            .ignore(nameContains("javassist"))
+            .ignore(nameContains(".asm."))
+            .type(pluginFinder.buildMatch())
+            .transform(new Transformer(pluginFinder))
+            .with(new Listener())
+            .installOn(instrumentation);
 
         try {
             ServiceManager.INSTANCE.boot();
@@ -91,7 +106,8 @@ public class SkyWalkingAgent {
         }
 
         @Override
-        public DynamicType.Builder<?> transform(DynamicType.Builder<?> builder, TypeDescription typeDescription, ClassLoader classLoader, JavaModule module) {
+        public DynamicType.Builder<?> transform(DynamicType.Builder<?> builder, TypeDescription typeDescription,
+            ClassLoader classLoader, JavaModule module) {
             List<AbstractClassEnhancePluginDefine> pluginDefines = pluginFinder.find(typeDescription, classLoader);
             if (pluginDefines.size() > 0) {
                 DynamicType.Builder<?> newBuilder = builder;
@@ -122,7 +138,7 @@ public class SkyWalkingAgent {
 
         @Override
         public void onTransformation(TypeDescription typeDescription, ClassLoader classLoader, JavaModule module,
-                                     boolean loaded, DynamicType dynamicType) {
+            boolean loaded, DynamicType dynamicType) {
             if (logger.isDebugEnable()) {
                 logger.debug("On Transformation class {}.", typeDescription.getName());
             }
@@ -132,13 +148,13 @@ public class SkyWalkingAgent {
 
         @Override
         public void onIgnored(TypeDescription typeDescription, ClassLoader classLoader, JavaModule module,
-                              boolean loaded) {
+            boolean loaded) {
 
         }
 
         @Override
         public void onError(String typeName, ClassLoader classLoader, JavaModule module, boolean loaded,
-                            Throwable throwable) {
+            Throwable throwable) {
             logger.error("Enhance class " + typeName + " error.", throwable);
         }