-
Notifications
You must be signed in to change notification settings - Fork 966
Add custom stacktrace renderer which is length limit aware #7281
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
jack-berg
merged 8 commits into
open-telemetry:main
from
jack-berg:better-stacktrace-render
Jul 10, 2025
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
3da5929
Add custom stacktrace renderer which is length limit aware
jack-berg b2d3b58
suppress warnings
jack-berg a098e83
Fix typo
jack-berg 47aae74
Merge branch 'main' of https://github.com/open-telemetry/opentelemetr…
jack-berg 2159b0e
more perf
jack-berg 30d25a2
Merge branch 'main' of https://github.com/open-telemetry/opentelemetr…
jack-berg 7408c08
Add property to render stacktrace using jvm default
jack-berg a0ded5e
Improve API ergonomics
jack-berg File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
107 changes: 107 additions & 0 deletions
107
sdk/common/src/jmh/java/io/opentelemetry/sdk/internal/StacktraceRenderBenchmark.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,107 @@ | ||
| /* | ||
| * Copyright The OpenTelemetry Authors | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
|
|
||
| package io.opentelemetry.sdk.internal; | ||
|
|
||
| import java.io.PrintStream; | ||
| import java.io.PrintWriter; | ||
| import java.io.StringWriter; | ||
| import java.util.concurrent.TimeUnit; | ||
| import java.util.function.BiFunction; | ||
| import org.openjdk.jmh.annotations.Benchmark; | ||
| import org.openjdk.jmh.annotations.BenchmarkMode; | ||
| import org.openjdk.jmh.annotations.Fork; | ||
| import org.openjdk.jmh.annotations.Measurement; | ||
| import org.openjdk.jmh.annotations.Mode; | ||
| import org.openjdk.jmh.annotations.OutputTimeUnit; | ||
| import org.openjdk.jmh.annotations.Param; | ||
| import org.openjdk.jmh.annotations.Scope; | ||
| import org.openjdk.jmh.annotations.State; | ||
| import org.openjdk.jmh.annotations.Threads; | ||
| import org.openjdk.jmh.annotations.Warmup; | ||
|
|
||
| /** | ||
| * This benchmark compares the performance of {@link StackTraceRenderer}, the custom length limit | ||
| * aware exception render, to the built-in JDK stacktrace renderer {@link | ||
| * Throwable#printStackTrace(PrintStream)}. | ||
| */ | ||
| @BenchmarkMode({Mode.AverageTime}) | ||
| @OutputTimeUnit(TimeUnit.NANOSECONDS) | ||
| @Warmup(iterations = 5, time = 100, timeUnit = TimeUnit.MILLISECONDS) | ||
| @Measurement(iterations = 5, time = 100, timeUnit = TimeUnit.MILLISECONDS) | ||
| @Fork(1) | ||
| @SuppressWarnings("StaticAssignmentOfThrowable") | ||
| public class StacktraceRenderBenchmark { | ||
|
|
||
| private static final Exception simple = new Exception("error"); | ||
| private static final Exception complex = | ||
| new Exception("error", new Exception("cause1", new Exception("cause2"))); | ||
|
|
||
| static { | ||
| complex.addSuppressed(new Exception("suppressed1")); | ||
| complex.addSuppressed(new Exception("suppressed2", new Exception("cause"))); | ||
| } | ||
|
|
||
| @State(Scope.Benchmark) | ||
| public static class BenchmarkState { | ||
|
|
||
| @Param Renderer renderer; | ||
| @Param ExceptionParam exceptionParam; | ||
|
|
||
| @Param({"10", "1000", "100000"}) | ||
| int lengthLimit; | ||
| } | ||
|
|
||
| @SuppressWarnings("ImmutableEnumChecker") | ||
| public enum Renderer { | ||
| JDK( | ||
| (throwable, limit) -> { | ||
| StringWriter stringWriter = new StringWriter(); | ||
| try (PrintWriter printWriter = new PrintWriter(stringWriter)) { | ||
| throwable.printStackTrace(printWriter); | ||
| } | ||
| String stacktrace = stringWriter.toString(); | ||
| return stacktrace.substring(0, Math.min(stacktrace.length(), limit)); | ||
| }), | ||
| CUSTOM((throwable, limit) -> new StackTraceRenderer(throwable, limit).render()); | ||
|
|
||
| private final BiFunction<Throwable, Integer, String> renderer; | ||
|
|
||
| Renderer(BiFunction<Throwable, Integer, String> renderer) { | ||
| this.renderer = renderer; | ||
| } | ||
|
|
||
| BiFunction<Throwable, Integer, String> renderer() { | ||
| return renderer; | ||
| } | ||
| } | ||
|
|
||
| @SuppressWarnings("ImmutableEnumChecker") | ||
| public enum ExceptionParam { | ||
| SIMPLE(simple), | ||
| COMPLEX(complex); | ||
|
|
||
| private final Throwable throwable; | ||
|
|
||
| ExceptionParam(Throwable throwable) { | ||
| this.throwable = throwable; | ||
| } | ||
|
|
||
| Throwable throwable() { | ||
| return throwable; | ||
| } | ||
| } | ||
|
|
||
| @Benchmark | ||
| @Threads(1) | ||
| @SuppressWarnings("ReturnValueIgnored") | ||
| public void render(BenchmarkState benchmarkState) { | ||
| BiFunction<Throwable, Integer, String> renderer = benchmarkState.renderer.renderer(); | ||
| Throwable throwable = benchmarkState.exceptionParam.throwable(); | ||
| int limit = benchmarkState.lengthLimit; | ||
|
|
||
| renderer.apply(throwable, limit); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
154 changes: 154 additions & 0 deletions
154
sdk/common/src/main/java/io/opentelemetry/sdk/internal/StackTraceRenderer.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,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; | ||
| } | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.