forked from open-telemetry/opentelemetry-java
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathStackTraceRenderer.java
More file actions
154 lines (132 loc) · 4.46 KB
/
StackTraceRenderer.java
File metadata and controls
154 lines (132 loc) · 4.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
/*
* Copyright The OpenTelemetry Authors
* SPDX-License-Identifier: Apache-2.0
*/
package io.opentelemetry.sdk.internal;
import java.io.PrintStream;
import java.util.Collections;
import java.util.IdentityHashMap;
import java.util.Set;
/**
* An alternative to exception stacktrace renderer that replicates the behavior of {@link
* Throwable#printStackTrace(PrintStream)}, but which is aware of a maximum stacktrace length limit,
* and exits early when the length limit has been exceeded to avoid unnecessary computation.
*
* <p>Instances should only be used once.
*/
class StackTraceRenderer {
private static final String CAUSED_BY = "Caused by: ";
private static final String SUPPRESSED = "Suppressed: ";
private final Throwable throwable;
private final int lengthLimit;
private final StringBuilder builder = new StringBuilder();
StackTraceRenderer(Throwable throwable, int lengthLimit) {
this.throwable = throwable;
this.lengthLimit = lengthLimit;
}
String render() {
if (builder.length() == 0) {
appendStackTrace();
}
return builder.substring(0, Math.min(builder.length(), lengthLimit));
}
private void appendStackTrace() {
builder.append(throwable).append(System.lineSeparator());
if (isOverLimit()) {
return;
}
StackTraceElement[] stackTraceElements = throwable.getStackTrace();
for (StackTraceElement stackTraceElement : stackTraceElements) {
builder.append("\tat ").append(stackTraceElement).append(System.lineSeparator());
if (isOverLimit()) {
return;
}
}
Set<Throwable> seen = Collections.newSetFromMap(new IdentityHashMap<>());
seen.add(throwable);
for (Throwable suppressed : throwable.getSuppressed()) {
appendInnerStacktrace(stackTraceElements, suppressed, "\t", SUPPRESSED, seen);
}
Throwable cause = throwable.getCause();
if (cause != null) {
appendInnerStacktrace(stackTraceElements, cause, "", CAUSED_BY, seen);
}
}
/**
* Append the {@code innerThrowable} to the {@link #builder}, returning {@code true} if the
* builder now exceeds the length limit.
*/
private boolean appendInnerStacktrace(
StackTraceElement[] parentElements,
Throwable innerThrowable,
String prefix,
String caption,
Set<Throwable> seen) {
if (seen.contains(innerThrowable)) {
builder
.append(prefix)
.append(caption)
.append("[CIRCULAR REFERENCE: ")
.append(innerThrowable)
.append("]")
.append(System.lineSeparator());
return true;
}
seen.add(innerThrowable);
// Iterating back to front, compute the lastSharedFrameIndex, which tracks the point at which
// this exception's stacktrace elements start repeating the parent's elements
StackTraceElement[] currentElements = innerThrowable.getStackTrace();
int parentIndex = parentElements.length - 1;
int lastSharedFrameIndex = currentElements.length - 1;
while (true) {
if (parentIndex < 0 || lastSharedFrameIndex < 0) {
break;
}
if (!parentElements[parentIndex].equals(currentElements[lastSharedFrameIndex])) {
break;
}
parentIndex--;
lastSharedFrameIndex--;
}
builder.append(prefix).append(caption).append(innerThrowable).append(System.lineSeparator());
if (isOverLimit()) {
return true;
}
for (int i = 0; i <= lastSharedFrameIndex; i++) {
StackTraceElement stackTraceElement = currentElements[i];
builder
.append(prefix)
.append("\tat ")
.append(stackTraceElement)
.append(System.lineSeparator());
if (isOverLimit()) {
return true;
}
}
int duplicateFrames = currentElements.length - 1 - lastSharedFrameIndex;
if (duplicateFrames != 0) {
builder
.append(prefix)
.append("\t... ")
.append(duplicateFrames)
.append(" more")
.append(System.lineSeparator());
if (isOverLimit()) {
return true;
}
}
for (Throwable suppressed : innerThrowable.getSuppressed()) {
if (appendInnerStacktrace(currentElements, suppressed, prefix + "\t", SUPPRESSED, seen)) {
return true;
}
}
Throwable cause = innerThrowable.getCause();
if (cause != null) {
return appendInnerStacktrace(currentElements, cause, prefix, CAUSED_BY, seen);
}
return false;
}
private boolean isOverLimit() {
return builder.length() >= lengthLimit;
}
}