From 7d3e699efa9b9b64d65f0f7fb67bf05542fb7082 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=90=B4=E6=99=9F=20Wu=20Sheng?= Date: Sun, 23 Sep 2018 09:35:07 +0800 Subject: [PATCH] Adjust agent setting (#1682) * Add ignore to bytebuddy agent. * Fix a wrong name. * Make sure toolkits are excluded. * Fix CI * Format codes. * Remove MethodGraph compiler --- .../skywalking/apm/agent/SkyWalkingAgent.java | 51 +++++++++++++------ 1 file changed, 36 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 c224aa5a2..c6ca2ca20 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,21 @@ * */ - 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.NamedElement; import net.bytebuddy.description.type.TypeDescription; import net.bytebuddy.dynamic.DynamicType; +import net.bytebuddy.dynamic.scaffold.TypeValidation; +import net.bytebuddy.matcher.ElementMatcher; 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 +38,13 @@ 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; +import static net.bytebuddy.matcher.ElementMatchers.not; /** - * 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 +52,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 +70,21 @@ 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)); + + new AgentBuilder.Default(byteBuddy) + .ignore(nameStartsWith("net.bytebuddy.")) + .ignore(nameStartsWith("org.slf4j.")) + .ignore(nameStartsWith("org.apache.logging.")) + .ignore(nameStartsWith("org.groovy.")) + .ignore(nameContains("javassist")) + .ignore(nameContains(".asm.")) + .ignore(allSkyWalkingAgentExcludeToolkit()) + .type(pluginFinder.buildMatch()) + .transform(new Transformer(pluginFinder)) + .with(new Listener()) + .installOn(instrumentation); try { ServiceManager.INSTANCE.boot(); @@ -91,7 +107,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 pluginDefines = pluginFinder.find(typeDescription, classLoader); if (pluginDefines.size() > 0) { DynamicType.Builder newBuilder = builder; @@ -114,6 +131,10 @@ public class SkyWalkingAgent { } } + private static ElementMatcher.Junction allSkyWalkingAgentExcludeToolkit() { + return nameStartsWith("org.apache.skywalking.").and(not(nameStartsWith("org.apache.skywalking.apm.toolkit."))); + } + private static class Listener implements AgentBuilder.Listener { @Override public void onDiscovery(String typeName, ClassLoader classLoader, JavaModule module, boolean loaded) { @@ -122,7 +143,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 +153,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); }