Repository navigation
ref(android): Extract ANR culprit exceptions into AnrCulpritEventHelper #6593
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
Open
markushi
wants to merge
5
commits into
main
Choose a base branch
from
markushi/feat/nfanr-2-culprit-helper
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
31b563e
ref(android): Extract ANR culprit exceptions into AnrCulpritEventHelper
markushi 724e778
ref(android): Trim a redundant Javadoc sentence in AnrCulpritEventHelper
markushi e6975fe
ref(android): Rename AnrCulpritEventHelper methods and take exception…
markushi d0c188c
ref(android): Move AnrCulpritEventHelper into the anr package
markushi 32eded9
license(android): Complete the ANR-WatchDog attribution for Applicati…
markushi 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
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
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 |
|---|---|---|
| @@ -1,8 +1,12 @@ | ||
| // Adapted from ANR-WatchDog. | ||
| // Copyright (c) 2016 Salomon BRYS. | ||
| // Licensed under the MIT License. | ||
| // https://github.com/SalomonBrys/ANR-WatchDog/blob/1969075f75f5980e9000eaffbaa13b0daf282dcb/anr-watchdog/src/main/java/com/github/anrwatchdog/ANRError.java | ||
| // Based on the class above. The API unnecessary here was removed. | ||
| // The API unnecessary here was removed. | ||
| package io.sentry.android.core; | ||
|
|
||
| import io.sentry.util.Objects; | ||
| import org.jetbrains.annotations.ApiStatus; | ||
| import org.jetbrains.annotations.NotNull; | ||
| import org.jetbrains.annotations.Nullable; | ||
|
|
||
|
|
@@ -14,14 +18,15 @@ | |
| * really the cause of the exception. Each "Caused by" is the stack trace of a running thread. Note | ||
| * that the main thread always comes first. | ||
| */ | ||
| final class ApplicationNotResponding extends RuntimeException { | ||
| @ApiStatus.Internal | ||
| public final class ApplicationNotResponding extends RuntimeException { | ||
|
markushi marked this conversation as resolved.
Resolved
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. had to change the visibility, otherwise the helper could not have accessed it. Moving this class probably isn't an option, as it's used for |
||
| private static final long serialVersionUID = 252541144579117016L; | ||
|
|
||
| // temp fix for JDK 21: Never serialized. | ||
| @SuppressWarnings("serial") | ||
| private final @Nullable Thread thread; | ||
|
|
||
| ApplicationNotResponding(final @Nullable String message) { | ||
| public ApplicationNotResponding(final @Nullable String message) { | ||
| super(message); | ||
| this.thread = null; | ||
| } | ||
|
|
||
75 changes: 75 additions & 0 deletions
75
sentry-android-core/src/main/java/io/sentry/android/core/anr/AnrCulpritEventHelper.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,75 @@ | ||
| package io.sentry.android.core.anr; | ||
|
|
||
| import io.sentry.SentryExceptionFactory; | ||
| import io.sentry.android.core.ApplicationNotResponding; | ||
| import io.sentry.exception.ExceptionMechanismException; | ||
| import io.sentry.protocol.Mechanism; | ||
| import io.sentry.protocol.SentryException; | ||
| import io.sentry.protocol.SentryStackFrame; | ||
| import io.sentry.protocol.SentryStackTrace; | ||
| import java.util.List; | ||
| import org.jetbrains.annotations.ApiStatus; | ||
| import org.jetbrains.annotations.NotNull; | ||
| import org.jetbrains.annotations.Nullable; | ||
|
|
||
| @ApiStatus.Internal | ||
| public final class AnrCulpritEventHelper { | ||
|
markushi marked this conversation as resolved.
Resolved
|
||
|
|
||
| private final @NotNull SentryExceptionFactory exceptionFactory; | ||
|
|
||
| public AnrCulpritEventHelper(final @NotNull SentryExceptionFactory exceptionFactory) { | ||
| this.exceptionFactory = exceptionFactory; | ||
| } | ||
|
|
||
| /** | ||
| * The exception message is the class and method of the top frame of the culprit stack. | ||
| * | ||
| * @return the exceptions, or {@code null} if the culprit stack is empty | ||
| */ | ||
| public @Nullable List<SentryException> toSentryExceptions( | ||
| final @NotNull AggregatedStackTrace culprit, final @NotNull Mechanism mechanism) { | ||
| final @NotNull StackTraceElement[] stack = culprit.getStack(); | ||
| if (stack.length == 0) { | ||
| return null; | ||
| } | ||
| final @NotNull StackTraceElement top = stack[0]; | ||
| final @NotNull ApplicationNotResponding exception = | ||
| new ApplicationNotResponding(top.getClassName() + "." + top.getMethodName()); | ||
| exception.setStackTrace(stack); | ||
|
|
||
| return exceptionFactory.getSentryExceptions( | ||
| new ExceptionMechanismException(mechanism, exception, null, false)); | ||
| } | ||
|
|
||
| /** | ||
| * A frame is a system frame if it is not in app and its module is a Java, Kotlin, or Android | ||
| * framework package, see {@link AnrCulpritIdentifier#isSystemFrame(String)}. | ||
| * | ||
| * @return {@code true} if there is at least one exception and none of their frames is an app | ||
| * frame | ||
| */ | ||
| public static boolean hasOnlySystemFrames(final @Nullable List<SentryException> exceptions) { | ||
| if (exceptions == null || exceptions.isEmpty()) { | ||
| return false; | ||
| } | ||
|
|
||
| for (final @NotNull SentryException exception : exceptions) { | ||
| final @Nullable SentryStackTrace stacktrace = exception.getStacktrace(); | ||
| if (stacktrace != null) { | ||
| final @Nullable List<SentryStackFrame> frames = stacktrace.getFrames(); | ||
| if (frames != null && !frames.isEmpty()) { | ||
| for (final @NotNull SentryStackFrame frame : frames) { | ||
| if (frame.isInApp() != null && frame.isInApp()) { | ||
| return false; | ||
| } | ||
| final @Nullable String module = frame.getModule(); | ||
| if (module != null && !AnrCulpritIdentifier.isSystemFrame(module)) { | ||
| return false; | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
| return true; | ||
| } | ||
| } | ||
82 changes: 82 additions & 0 deletions
82
sentry-android-core/src/test/java/io/sentry/android/core/anr/AnrCulpritEventHelperTest.kt
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,82 @@ | ||
| package io.sentry.android.core.anr | ||
|
|
||
| import com.google.common.truth.Truth.assertThat | ||
| import io.sentry.SentryExceptionFactory | ||
| import io.sentry.SentryOptions | ||
| import io.sentry.SentryStackTraceFactory | ||
| import io.sentry.protocol.Mechanism | ||
| import io.sentry.protocol.SentryException | ||
| import io.sentry.protocol.SentryStackFrame | ||
| import io.sentry.protocol.SentryStackTrace | ||
| import kotlin.test.Test | ||
|
|
||
| class AnrCulpritEventHelperTest { | ||
|
|
||
| private val helper = | ||
| AnrCulpritEventHelper(SentryExceptionFactory(SentryStackTraceFactory(SentryOptions()))) | ||
|
|
||
| @Test | ||
| fun `creates exception with class and method of the top frame as message`() { | ||
| val stack = | ||
| arrayOf( | ||
| StackTraceElement("com.example.Foo", "bar", "Foo.java", 12), | ||
| StackTraceElement("android.os.Looper", "loop", "Looper.java", 200), | ||
| ) | ||
| val culprit = AggregatedStackTrace(stack, 0, stack.size - 1, 1000L, 0.5f) | ||
|
|
||
| val exceptions = helper.toSentryExceptions(culprit, Mechanism().apply { type = "ANR" }) | ||
|
|
||
| assertThat(exceptions).isNotNull() | ||
| assertThat(exceptions!!).hasSize(1) | ||
| val exception = exceptions[0] | ||
| assertThat(exception.type).isEqualTo("ApplicationNotResponding") | ||
| assertThat(exception.module).isEqualTo("io.sentry.android.core") | ||
| assertThat(exception.value).isEqualTo("com.example.Foo.bar") | ||
| assertThat(exception.mechanism!!.type).isEqualTo("ANR") | ||
| assertThat(exception.stacktrace!!.frames!!.map { it.function }).containsExactly("loop", "bar") | ||
| } | ||
|
|
||
| @Test | ||
| fun `returns null for an empty culprit stack`() { | ||
| val culprit = AggregatedStackTrace(emptyArray(), 0, -1, 1000L, 0f) | ||
|
|
||
| assertThat(helper.toSentryExceptions(culprit, Mechanism())).isNull() | ||
| } | ||
|
|
||
| @Test | ||
| fun `hasOnlySystemFrames is true for java and android frames only`() { | ||
| val exceptions = exceptionsWithFrames(frame("java.lang.Object"), frame("android.os.Looper")) | ||
|
|
||
| assertThat(AnrCulpritEventHelper.hasOnlySystemFrames(exceptions)).isTrue() | ||
| } | ||
|
|
||
| @Test | ||
| fun `hasOnlySystemFrames is false if one frame is in app`() { | ||
| val exceptions = | ||
| exceptionsWithFrames(frame("android.os.Looper"), frame("android.os.Handler", inApp = true)) | ||
|
|
||
| assertThat(AnrCulpritEventHelper.hasOnlySystemFrames(exceptions)).isFalse() | ||
| } | ||
|
|
||
| @Test | ||
| fun `hasOnlySystemFrames is false if one frame is not a system frame`() { | ||
| val exceptions = exceptionsWithFrames(frame("android.os.Looper"), frame("com.example.Foo")) | ||
|
|
||
| assertThat(AnrCulpritEventHelper.hasOnlySystemFrames(exceptions)).isFalse() | ||
| } | ||
|
|
||
| @Test | ||
| fun `hasOnlySystemFrames is false without exceptions`() { | ||
| assertThat(AnrCulpritEventHelper.hasOnlySystemFrames(null)).isFalse() | ||
| assertThat(AnrCulpritEventHelper.hasOnlySystemFrames(emptyList())).isFalse() | ||
| } | ||
|
|
||
| private fun frame(module: String, inApp: Boolean? = null) = | ||
| SentryStackFrame().apply { | ||
| this.module = module | ||
| this.isInApp = inApp | ||
| } | ||
|
|
||
| private fun exceptionsWithFrames(vararg frames: SentryStackFrame) = | ||
| listOf(SentryException().apply { stacktrace = SentryStackTrace(frames.toList()) }) | ||
| } |
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.