Merge pull request #221 from ascrutae/zhangxin/fix/219
Fix OutofMemory of Span.ThrowableTransformer.convert2String
This commit is contained in:
commit
d06555cf88
|
|
@ -3,9 +3,8 @@ package org.skywalking.apm.trace;
|
|||
import com.google.gson.annotations.Expose;
|
||||
import com.google.gson.annotations.SerializedName;
|
||||
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.util.*;
|
||||
import org.skywalking.apm.trace.util.ThrowableTransformer;
|
||||
|
||||
/**
|
||||
* Span is a concept from OpenTracing Spec, also from Google Dapper Paper.
|
||||
|
|
@ -290,32 +289,6 @@ public class Span {
|
|||
return log(exceptionFields);
|
||||
}
|
||||
|
||||
private enum ThrowableTransformer {
|
||||
INSTANCE;
|
||||
|
||||
private String convert2String(Throwable e, int maxLength) {
|
||||
ByteArrayOutputStream buf = null;
|
||||
StringBuilder expMessage = new StringBuilder();
|
||||
try {
|
||||
buf = new ByteArrayOutputStream();
|
||||
Throwable causeException = e;
|
||||
while (expMessage.length() < maxLength && causeException != null) {
|
||||
causeException.printStackTrace(new java.io.PrintWriter(buf, true));
|
||||
expMessage.append(buf.toString());
|
||||
causeException = causeException.getCause();
|
||||
}
|
||||
|
||||
} finally {
|
||||
try {
|
||||
buf.close();
|
||||
} catch (IOException ioe) {
|
||||
}
|
||||
}
|
||||
|
||||
return (maxLength > expMessage.length() ? expMessage : expMessage.substring(0, maxLength)).toString();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* This method is from opentracing-java. {@see https://github.com/opentracing/opentracing-java/blob/release-0.20.9/opentracing-api/src/main/java/io/opentracing/Span.java#L120}
|
||||
* <p>
|
||||
|
|
|
|||
|
|
@ -0,0 +1,56 @@
|
|||
package org.skywalking.apm.trace.util;
|
||||
|
||||
/**
|
||||
* {@link ThrowableTransformer} is responsible for transferring stack trace of throwable.
|
||||
*/
|
||||
public enum ThrowableTransformer {
|
||||
INSTANCE;
|
||||
|
||||
private static final String LINE_SEPARATOR = System.getProperty("line.separator");
|
||||
|
||||
public String convert2String(Throwable throwable, final int maxLength) {
|
||||
final StringBuilder stackMessage = new StringBuilder();
|
||||
Throwable causeException = throwable;
|
||||
while (causeException != null) {
|
||||
stackMessage.append(printExceptionInfo(causeException));
|
||||
|
||||
boolean overMaxLength = printStackElement(throwable.getStackTrace(), new AppendListener() {
|
||||
public void append(String value) {
|
||||
stackMessage.append(value);
|
||||
}
|
||||
|
||||
public boolean overMaxLength() {
|
||||
return stackMessage.length() > maxLength;
|
||||
}
|
||||
});
|
||||
|
||||
if (overMaxLength) {
|
||||
break;
|
||||
}
|
||||
|
||||
causeException = throwable.getCause();
|
||||
}
|
||||
|
||||
return stackMessage.toString();
|
||||
}
|
||||
|
||||
private String printExceptionInfo(Throwable causeException) {
|
||||
return causeException.toString() + LINE_SEPARATOR;
|
||||
}
|
||||
|
||||
private boolean printStackElement(StackTraceElement[] stackTrace, AppendListener printListener) {
|
||||
for (StackTraceElement traceElement : stackTrace) {
|
||||
printListener.append("at " + traceElement + LINE_SEPARATOR);
|
||||
if (printListener.overMaxLength()) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
private interface AppendListener {
|
||||
void append(String value);
|
||||
|
||||
boolean overMaxLength();
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue