You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@skywalking.apache.org by wu...@apache.org on 2019/06/23 00:42:11 UTC

[skywalking] branch master updated: Fix OOM by empty stack of exception. (#2931)

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

wusheng pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/skywalking.git


The following commit(s) were added to refs/heads/master by this push:
     new d844a52  Fix OOM by empty stack of exception. (#2931)
d844a52 is described below

commit d844a5258777c5b8c4f6596cdeeb14dd5cda9547
Author: 吴晟 Wu Sheng <wu...@foxmail.com>
AuthorDate: Sun Jun 23 08:42:03 2019 +0800

    Fix OOM by empty stack of exception. (#2931)
    
    * Support bootstrap class enhance and fix OOM by empty stack of exception.
    
    * Agent document update
    
    * Remove a word.
    
    * Fix wrong cause exception search.
    
    * Fix the condition
    
    * Revert bootstrap class loader enhance.
    
    * Remove import.
    
    * no message
---
 .../skywalking/apm/agent/core/conf/Config.java     |  5 ++++
 .../core/context/util/ThrowableTransformer.java    | 21 ++++++++++++---
 .../skywalking/apm/agent/SkyWalkingAgent.java      | 31 +++++++++++++---------
 docs/en/setup/service-agent/java-agent/README.md   |  1 +
 4 files changed, 41 insertions(+), 17 deletions(-)

diff --git a/apm-sniffer/apm-agent-core/src/main/java/org/apache/skywalking/apm/agent/core/conf/Config.java b/apm-sniffer/apm-agent-core/src/main/java/org/apache/skywalking/apm/agent/core/conf/Config.java
index 1c12100..9efab98 100644
--- a/apm-sniffer/apm-agent-core/src/main/java/org/apache/skywalking/apm/agent/core/conf/Config.java
+++ b/apm-sniffer/apm-agent-core/src/main/java/org/apache/skywalking/apm/agent/core/conf/Config.java
@@ -88,6 +88,11 @@ public class Config {
          * The identify of the instance
          */
         public static String INSTANCE_UUID = "";
+
+        /**
+         * How depth the agent goes, when log cause exceptions.
+         */
+        public static int CAUSE_EXCEPTION_DEPTH = 5;
     }
 
     public static class Collector {
diff --git a/apm-sniffer/apm-agent-core/src/main/java/org/apache/skywalking/apm/agent/core/context/util/ThrowableTransformer.java b/apm-sniffer/apm-agent-core/src/main/java/org/apache/skywalking/apm/agent/core/context/util/ThrowableTransformer.java
index b5f4ef1..ea02f45 100644
--- a/apm-sniffer/apm-agent-core/src/main/java/org/apache/skywalking/apm/agent/core/context/util/ThrowableTransformer.java
+++ b/apm-sniffer/apm-agent-core/src/main/java/org/apache/skywalking/apm/agent/core/context/util/ThrowableTransformer.java
@@ -19,6 +19,8 @@
 
 package org.apache.skywalking.apm.agent.core.context.util;
 
+import static org.apache.skywalking.apm.agent.core.conf.Config.Agent.CAUSE_EXCEPTION_DEPTH;
+
 /**
  * {@link ThrowableTransformer} is responsible for transferring stack trace of throwable.
  */
@@ -30,10 +32,12 @@ public enum ThrowableTransformer {
     public String convert2String(Throwable throwable, final int maxLength) {
         final StringBuilder stackMessage = new StringBuilder();
         Throwable causeException = throwable;
-        while (causeException != null) {
+
+        int depth = CAUSE_EXCEPTION_DEPTH;
+        while (causeException != null && depth != 0) {
             stackMessage.append(printExceptionInfo(causeException));
 
-            boolean overMaxLength = printStackElement(throwable.getStackTrace(), new AppendListener() {
+            boolean isLookDeeper = printStackElement(causeException.getStackTrace(), new AppendListener() {
                 public void append(String value) {
                     stackMessage.append(value);
                 }
@@ -43,11 +47,12 @@ public enum ThrowableTransformer {
                 }
             });
 
-            if (overMaxLength) {
+            if (isLookDeeper) {
                 break;
             }
 
-            causeException = throwable.getCause();
+            causeException = causeException.getCause();
+            depth--;
         }
 
         return stackMessage.toString();
@@ -58,6 +63,14 @@ public enum ThrowableTransformer {
     }
 
     private boolean printStackElement(StackTraceElement[] stackTrace, AppendListener printListener) {
+        if (stackTrace.length == 0) {
+            /**
+             * In some cases, people would fill empty stackTrace intentionally.
+             * This is a quick stop.
+             */
+            return true;
+        }
+
         for (StackTraceElement traceElement : stackTrace) {
             printListener.append("at " + traceElement + LINE_SEPARATOR);
             if (printListener.overMaxLength()) {
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 516c1cd..39c396d 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
@@ -18,6 +18,8 @@
 
 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;
@@ -34,12 +36,15 @@ import org.apache.skywalking.apm.agent.core.conf.ConfigNotFoundException;
 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.*;
-
-import java.lang.instrument.Instrumentation;
-import java.util.List;
+import org.apache.skywalking.apm.agent.core.plugin.AbstractClassEnhancePluginDefine;
+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 static net.bytebuddy.matcher.ElementMatchers.*;
+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-walking agent, based on javaagent mechanism.
@@ -80,14 +85,14 @@ public class SkyWalkingAgent {
         new AgentBuilder.Default(byteBuddy)
             .ignore(
                 nameStartsWith("net.bytebuddy.")
-                .or(nameStartsWith("org.slf4j."))
-                .or(nameStartsWith("org.apache.logging."))
-                .or(nameStartsWith("org.groovy."))
-                .or(nameContains("javassist"))
-                .or(nameContains(".asm."))
-                .or(nameStartsWith("sun.reflect"))
-                .or(allSkyWalkingAgentExcludeToolkit())
-                .or(ElementMatchers.<TypeDescription>isSynthetic()))
+                    .or(nameStartsWith("org.slf4j."))
+                    .or(nameStartsWith("org.apache.logging."))
+                    .or(nameStartsWith("org.groovy."))
+                    .or(nameContains("javassist"))
+                    .or(nameContains(".asm."))
+                    .or(nameStartsWith("sun.reflect"))
+                    .or(allSkyWalkingAgentExcludeToolkit())
+                    .or(ElementMatchers.<TypeDescription>isSynthetic()))
             .type(pluginFinder.buildMatch())
             .transform(new Transformer(pluginFinder))
             .with(new Listener())
diff --git a/docs/en/setup/service-agent/java-agent/README.md b/docs/en/setup/service-agent/java-agent/README.md
index 2d18851..247dd43 100644
--- a/docs/en/setup/service-agent/java-agent/README.md
+++ b/docs/en/setup/service-agent/java-agent/README.md
@@ -65,6 +65,7 @@ property key | Description | Default |
 `agent.is_open_debugging_class`|If true, skywalking agent will save all instrumented classes files in `/debugging` folder.Skywalking team may ask for these files in order to resolve compatible problem.|Not set|
 `agent.active_v2_header`|Active V2 header in default.|`true`|
 `agent.instance_uuid` |Instance uuid is the identity of an instance, skywalking treat same instance uuid as one instance.if empty, skywalking agent will generate an 32-bit uuid.   |`""`|
+`agent.cause_exception_depth`|How depth the agent goes, when log all cause exceptions.|5|
 `agent.active_v1_header `|Deactive V1 header in default.|`false`|
 `collector.grpc_channel_check_interval`|grpc channel status check interval.|`30`|
 `collector.app_and_service_register_check_interval`|application and service registry check interval.|`3`|