Skip to content

Reduce stacktrace serialization allocations further #1

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

Merged
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 @@ -279,34 +279,30 @@ private static void formatStackTraceAsArray(StringBuilder builder, CharSequence
continue;
}

// append line
CharSequence match = stackTrace.subSequence(index, matcher.start());
builder.append("\t\"");
JsonUtils.quoteAsString(match, builder);
builder.append("\",");
// append non-last line
appendStackTraceLine(builder, stackTrace, index, start);
builder.append(',');
builder.append(NEW_LINE);
index = end;
} while (matcher.find());

int length = stackTrace.length();
if (index < length) {
// append remaining line
CharSequence remaining = stackTrace.subSequence(index, length);
builder.append("\t\"");
JsonUtils.quoteAsString(remaining, builder);
builder.append("\"");
appendStackTraceLine(builder, stackTrace, index, length);
}

removeIfEndsWith(builder, NEW_LINE);
removeIfEndsWith(builder, ",");
} else {
// no newlines found, add entire stack trace as single element
builder.append("\t\"");
JsonUtils.quoteAsString(stackTrace, builder);
builder.append("\"");
appendStackTraceLine(builder, stackTrace, 0, stackTrace.length());
}
}

private static void appendStackTraceLine(StringBuilder builder, CharSequence stackTrace, int start, int end) {
builder.append("\t\"");
JsonUtils.quoteAsString(stackTrace, start, end, builder);
builder.append("\"");
}

public static void removeIfEndsWith(StringBuilder sb, String ending) {
if (endsWith(sb, ending)) {
sb.setLength(sb.length() - ending.length());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,13 +58,21 @@ public final class JsonUtils {
}

public static void quoteAsString(CharSequence content, StringBuilder sb) {
if (content == null) {
sb.append("null");
return;
}
quoteAsString(content, 0, content.length(), sb);
}

public static void quoteAsString(CharSequence content, int start, int end, StringBuilder sb) {
if (content == null) {
sb.append("null");
return;
}
final int[] escCodes = sOutputEscapes128;
final int escLen = escCodes.length;
for (int i = 0, len = content.length(); i < len; ++i) {
for (int i = start; i < end; ++i) {
char c = content.charAt(i);
if (c >= escLen || escCodes[c] == 0) {
sb.append(c);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -148,13 +148,31 @@ void serializeExceptionWithStackTraceAsArray() throws JsonProcessingException {
jsonBuilder.append('}');

JsonNode jsonNode = objectMapper.readTree(jsonBuilder.toString());
System.out.println(jsonNode.toPrettyString());
assertThat(jsonNode.get(ERROR_TYPE).textValue()).isEqualTo("className");
assertThat(jsonNode.get(ERROR_STACK_TRACE).isArray()).isTrue();
assertThat(jsonNode.get(ERROR_STACK_TRACE).size()).isEqualTo(2);
assertThat(jsonNode.get(ERROR_STACK_TRACE).get(0).textValue()).isEqualTo("stacktrace");
assertThat(jsonNode.get(ERROR_STACK_TRACE).get(1).textValue()).isEqualTo("caused by error");
assertThat(jsonNode.get(ERROR_MESSAGE).textValue()).isEqualTo("message");
}

@Test
void serializeExceptionWithSingleLineStackTraceAsArray() throws JsonProcessingException {
StringBuilder jsonBuilder = new StringBuilder();
jsonBuilder.append('{');
EcsJsonSerializer.serializeException(jsonBuilder, "className", "message", "caused by error", true);
jsonBuilder.append('}');
System.out.println(jsonBuilder);
JsonNode jsonNode = objectMapper.readTree(jsonBuilder.toString());
System.out.println(jsonNode.toPrettyString());
assertThat(jsonNode.get(ERROR_TYPE).textValue()).isEqualTo("className");
assertThat(jsonNode.get(ERROR_STACK_TRACE).isArray()).isTrue();
assertThat(jsonNode.get(ERROR_STACK_TRACE).size()).isEqualTo(1);
assertThat(jsonNode.get(ERROR_STACK_TRACE).get(0).textValue()).isEqualTo("caused by error");
assertThat(jsonNode.get(ERROR_MESSAGE).textValue()).isEqualTo("message");
}

@Test
void serializeExceptionWithNullMessage() throws JsonProcessingException {
StringBuilder jsonBuilder = new StringBuilder();
Expand Down