mirror of
https://github.com/plan-player-analytics/Plan.git
synced 2025-01-01 14:07:54 +01:00
Updated error hashing function
- Reduces memory usage by creating a single int instead of StringBuilder. - Reduces memory usage by reducing new object allocations when building readable stacktrace Affects issues: - Fixed #1491 (Root cause unknown)
This commit is contained in:
parent
dd99c7cac3
commit
2fa98b4967
@ -36,7 +36,8 @@ import java.io.IOException;
|
|||||||
import java.nio.file.Files;
|
import java.nio.file.Files;
|
||||||
import java.nio.file.Path;
|
import java.nio.file.Path;
|
||||||
import java.nio.file.StandardOpenOption;
|
import java.nio.file.StandardOpenOption;
|
||||||
import java.util.*;
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
import java.util.logging.Level;
|
import java.util.logging.Level;
|
||||||
import java.util.logging.Logger;
|
import java.util.logging.Logger;
|
||||||
import java.util.stream.Collectors;
|
import java.util.stream.Collectors;
|
||||||
@ -194,7 +195,7 @@ public class ErrorLogger implements ErrorHandler {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private void logNew(Path errorLog, Throwable throwable, ErrorContext context, String hash) {
|
private void logNew(Path errorLog, Throwable throwable, ErrorContext context, String hash) {
|
||||||
List<String> stacktrace = buildReadableStacktrace(throwable);
|
List<String> stacktrace = buildReadableStacktrace(new ArrayList<>(), throwable);
|
||||||
List<String> lines = Lists.builder(String.class)
|
List<String> lines = Lists.builder(String.class)
|
||||||
.add(hash + " - Last occurred: " + getTimeStamp() + " Occurrences: 1")
|
.add(hash + " - Last occurred: " + getTimeStamp() + " Occurrences: 1")
|
||||||
.apply(builder -> this.buildContext(context, 1, builder))
|
.apply(builder -> this.buildContext(context, 1, builder))
|
||||||
@ -223,41 +224,46 @@ public class ErrorLogger implements ErrorHandler {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private String hash(Throwable e) {
|
private String hash(Throwable e) {
|
||||||
StringBuilder seed = new StringBuilder();
|
int seed = 0;
|
||||||
Throwable cause = e;
|
Throwable cause = e;
|
||||||
String previousLine = null;
|
String previousLine = null;
|
||||||
while (cause != null) {
|
while (cause != null) {
|
||||||
for (StackTraceElement element : cause.getStackTrace()) {
|
for (StackTraceElement element : cause.getStackTrace()) {
|
||||||
String asLine = element.toString();
|
String asLine = element.toString();
|
||||||
if (asLine.equals(previousLine)) continue;
|
if (asLine.equals(previousLine)) continue;
|
||||||
seed.append(asLine);
|
if (seed == 0) {
|
||||||
|
seed = asLine.hashCode();
|
||||||
|
} else {
|
||||||
|
seed *= asLine.hashCode();
|
||||||
|
}
|
||||||
previousLine = asLine;
|
previousLine = asLine;
|
||||||
}
|
}
|
||||||
cause = e.getCause();
|
cause = e.getCause();
|
||||||
}
|
}
|
||||||
return DigestUtils.sha256Hex(seed.toString()).substring(0, 10);
|
return DigestUtils.sha256Hex(Integer.toString(seed)).substring(0, 10);
|
||||||
}
|
}
|
||||||
|
|
||||||
private List<String> buildReadableStacktrace(Throwable e) {
|
private List<String> buildReadableStacktrace(List<String> trace, Throwable e) {
|
||||||
List<String> trace = new ArrayList<>();
|
|
||||||
trace.add(e.toString());
|
trace.add(e.toString());
|
||||||
Deduplicator deduplicator = new Deduplicator();
|
Deduplicator deduplicator = new Deduplicator();
|
||||||
for (StackTraceElement element : e.getStackTrace()) {
|
for (StackTraceElement element : e.getStackTrace()) {
|
||||||
String line = element.toString();
|
String line = element.toString();
|
||||||
trace.addAll(deduplicator.getLines(line));
|
deduplicator.addLines(trace, line);
|
||||||
}
|
}
|
||||||
deduplicator.getLeftoverDuplicateCountLine().ifPresent(trace::add);
|
deduplicator.addLeftoverDuplicateCountLine(trace);
|
||||||
Throwable[] suppressed = e.getSuppressed();
|
Throwable[] suppressed = e.getSuppressed();
|
||||||
if (suppressed.length > 0) {
|
if (suppressed.length > 0) {
|
||||||
for (Throwable suppressedThrowable : suppressed) {
|
for (Throwable suppressedThrowable : suppressed) {
|
||||||
trace.add(" Suppressed:");
|
trace.add(" Suppressed:");
|
||||||
buildReadableStacktrace(suppressedThrowable).stream().map(line -> " " + line).forEach(trace::add);
|
for (String line : buildReadableStacktrace(new ArrayList<>(), suppressedThrowable)) {
|
||||||
|
trace.add(" " + line);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Throwable cause = e.getCause();
|
Throwable cause = e.getCause();
|
||||||
if (cause != null) {
|
if (cause != null) {
|
||||||
trace.add("Caused by:");
|
trace.add("Caused by:");
|
||||||
trace.addAll(buildReadableStacktrace(cause));
|
buildReadableStacktrace(trace, cause);
|
||||||
}
|
}
|
||||||
return trace;
|
return trace;
|
||||||
}
|
}
|
||||||
@ -267,26 +273,27 @@ public class ErrorLogger implements ErrorHandler {
|
|||||||
private String lastDuplicate = null;
|
private String lastDuplicate = null;
|
||||||
private int duplicateCount = 0;
|
private int duplicateCount = 0;
|
||||||
|
|
||||||
public List<String> getLines(String line) {
|
public void addLines(List<String> trace, String line) {
|
||||||
if (duplicateCount > 0 && !line.equals(lastDuplicate)) {
|
if (duplicateCount > 0 && !line.equals(lastDuplicate)) {
|
||||||
String returnLine = " x " + duplicateCount;
|
String returnLine = " x " + duplicateCount;
|
||||||
duplicateCount = 1;
|
duplicateCount = 1;
|
||||||
return Arrays.asList(returnLine, " " + line);
|
trace.add(returnLine);
|
||||||
|
trace.add(" " + line);
|
||||||
} else if (line.equals(lastDuplicate)) {
|
} else if (line.equals(lastDuplicate)) {
|
||||||
duplicateCount++;
|
duplicateCount++;
|
||||||
return Collections.emptyList();
|
|
||||||
} else if (line.equals(previousLine)) {
|
} else if (line.equals(previousLine)) {
|
||||||
lastDuplicate = line;
|
lastDuplicate = line;
|
||||||
duplicateCount = 2;
|
duplicateCount = 2;
|
||||||
return Collections.emptyList();
|
|
||||||
} else {
|
} else {
|
||||||
previousLine = line;
|
previousLine = line;
|
||||||
return Collections.singletonList(" " + line);
|
trace.add(" " + line);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public Optional<String> getLeftoverDuplicateCountLine() {
|
public void addLeftoverDuplicateCountLine(List<String> trace) {
|
||||||
return duplicateCount > 0 ? Optional.of(" x " + duplicateCount) : Optional.empty();
|
if (duplicateCount > 0) {
|
||||||
|
trace.add(" x " + duplicateCount);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user