Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Skip error.stack_trace if empty #267

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -208,22 +208,21 @@ public static void serializeException(StringBuilder builder, Throwable thrown, b
if (thrown != null) {
builder.append("\"error.type\":\"");
JsonUtils.quoteAsString(thrown.getClass().getName(), builder);
builder.append("\",");
builder.append('\"');

String message = thrown.getMessage();
if (message != null) {
builder.append("\"error.message\":\"");
builder.append(",\"error.message\":\"");
JsonUtils.quoteAsString(message, builder);
builder.append("\",");
builder.append('\"');
}
if (stackTraceAsArray) {
builder.append("\"error.stack_trace\":[").append(NEW_LINE);
formatThrowableAsArray(builder, thrown);
builder.append("]");

int prevLength = builder.length();
builder.append(",\"error.stack_trace\":").append(stackTraceAsArray ? '[' : "\"");
if (formatThrowable(builder, thrown, stackTraceAsArray)) {
builder.append(stackTraceAsArray ? ']' : '\"');
} else {
builder.append("\"error.stack_trace\":\"");
JsonUtils.quoteAsString(formatThrowable(thrown), builder);
builder.append("\"");
builder.setLength(prevLength); // reset if no stacktrace was written
}
}
}
Expand All @@ -249,30 +248,38 @@ public static void serializeException(StringBuilder builder, String exceptionCla
}
}

private static CharSequence formatThrowable(final Throwable throwable) {
StringBuilder buffer = getMessageStringBuilder();
final PrintWriter pw = new PrintWriter(new StringBuilderWriter(buffer));
throwable.printStackTrace(pw);
pw.flush();
return buffer;
}

private static void formatThrowableAsArray(final StringBuilder jsonBuilder, final Throwable throwable) {
private static boolean formatThrowable(final StringBuilder jsonBuilder, final Throwable throwable, final boolean stackTraceAsArray) {
final StringBuilder buffer = getMessageStringBuilder();
final int initialLength = jsonBuilder.length();
final PrintWriter pw = new PrintWriter(new StringBuilderWriter(buffer), true) {
private int lines = 0;

@Override
public void println() {
flush();
jsonBuilder.append("\t\"");
JsonUtils.quoteAsString(buffer, jsonBuilder);
jsonBuilder.append("\",");
jsonBuilder.append(NEW_LINE);
if (stackTraceAsArray) {
if (lines > 0) jsonBuilder.append(',');
jsonBuilder.append(NEW_LINE).append("\t\"");
JsonUtils.quoteAsString(buffer, jsonBuilder);
jsonBuilder.append('\"');
} else {
JsonUtils.quoteAsString(buffer, jsonBuilder);
JsonUtils.quoteAsString(NEW_LINE, jsonBuilder);
}
buffer.setLength(0);
lines++;
}

@Override
public void close() {
if (lines <= 1) {
jsonBuilder.setLength(initialLength); // skip the first line (message) if no stacktrace follows
}
}
};
throwable.printStackTrace(pw);
removeIfEndsWith(jsonBuilder, NEW_LINE);
removeIfEndsWith(jsonBuilder, ",");
pw.close();
return jsonBuilder.length() > initialLength;
}

private static void formatStackTraceAsArray(StringBuilder builder, CharSequence stackTrace) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,25 @@ void serializeExceptionAsString() throws IOException {
assertThat(jsonNode.get(ERROR_STACK_TRACE).textValue()).isEqualTo(stringWriter.toString());
}

@Test
void serializeExceptionWithoutStacktraceAsString() throws IOException {
Exception exception = new Exception("no stacktrace"){
@Override
public synchronized Throwable fillInStackTrace() {
return this;
}
};
StringBuilder jsonBuilder = new StringBuilder();
jsonBuilder.append('{');
EcsJsonSerializer.serializeException(jsonBuilder, exception, false);
jsonBuilder.append('}');
JsonNode jsonNode = objectMapper.readTree(jsonBuilder.toString());

assertThat(jsonNode.get(ERROR_TYPE).textValue()).isEqualTo(exception.getClass().getName());
assertThat(jsonNode.get(ERROR_MESSAGE).textValue()).isEqualTo("no stacktrace");
assertThat(jsonNode.get(ERROR_STACK_TRACE)).isNull();
}

@Test
void testEscaping() throws IOException {
String loggerName = "logger\"";
Expand Down Expand Up @@ -125,6 +144,26 @@ void serializeExceptionAsArray() throws IOException {
.isEqualTo(stringWriter.toString());
}

@Test
void serializeExceptionWithoutStacktraceAsArray() throws IOException {
Exception exception = new Exception("no stacktrace"){
@Override
public synchronized Throwable fillInStackTrace() {
return this;
}
};
StringBuilder jsonBuilder = new StringBuilder();
jsonBuilder.append('{');
EcsJsonSerializer.serializeException(jsonBuilder, exception, true);
jsonBuilder.append('}');
System.out.println(jsonBuilder);
JsonNode jsonNode = objectMapper.readTree(jsonBuilder.toString());

assertThat(jsonNode.get(ERROR_TYPE).textValue()).isEqualTo(exception.getClass().getName());
assertThat(jsonNode.get(ERROR_MESSAGE).textValue()).isEqualTo("no stacktrace");
assertThat(jsonNode.get(ERROR_STACK_TRACE)).isNull();
}

@Test
void testRemoveIfEndsWith() {
assertRemoveIfEndsWith("", "foo", "");
Expand Down
Loading