feat(anr): Profile main thread when ANR and report ANR profiles to Sentry#4899
feat(anr): Profile main thread when ANR and report ANR profiles to Sentry#4899
Conversation
|
🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <[email protected]>
sentry-android-core/src/main/java/io/sentry/android/core/AnrV2Integration.java
Outdated
Show resolved
Hide resolved
sentry-android-core/src/main/java/io/sentry/android/core/anr/AnrProfileManager.java
Show resolved
Hide resolved
sentry-android-core/src/main/java/io/sentry/android/core/anr/AnrCulpritIdentifier.java
Show resolved
Hide resolved
sentry-android-core/src/main/java/io/sentry/android/core/anr/AggregatedStackTrace.java
Outdated
Show resolved
Hide resolved
Performance metrics 🚀
|
| Revision | Plain | With Sentry | Diff |
|---|---|---|---|
| ee747ae | 554.98 ms | 611.50 ms | 56.52 ms |
| 22f4345 | 325.23 ms | 454.66 ms | 129.43 ms |
| dba088c | 320.59 ms | 361.29 ms | 40.70 ms |
| 9fbb112 | 359.71 ms | 421.85 ms | 62.14 ms |
| d15471f | 302.62 ms | 353.84 ms | 51.22 ms |
| d15471f | 310.26 ms | 377.04 ms | 66.78 ms |
| b750b96 | 421.25 ms | 444.09 ms | 22.84 ms |
| ad8da22 | 314.52 ms | 352.47 ms | 37.95 ms |
| 27d7cf8 | 306.76 ms | 366.66 ms | 59.90 ms |
| 9fbb112 | 401.87 ms | 515.87 ms | 114.00 ms |
App size
| Revision | Plain | With Sentry | Diff |
|---|---|---|---|
| ee747ae | 1.58 MiB | 2.10 MiB | 530.95 KiB |
| 22f4345 | 1.58 MiB | 2.29 MiB | 719.83 KiB |
| dba088c | 1.58 MiB | 2.13 MiB | 558.99 KiB |
| 9fbb112 | 1.58 MiB | 2.11 MiB | 539.18 KiB |
| d15471f | 1.58 MiB | 2.13 MiB | 559.54 KiB |
| d15471f | 1.58 MiB | 2.13 MiB | 559.54 KiB |
| b750b96 | 1.58 MiB | 2.10 MiB | 533.20 KiB |
| ad8da22 | 1.58 MiB | 2.29 MiB | 719.83 KiB |
| 27d7cf8 | 1.58 MiB | 2.12 MiB | 549.42 KiB |
| 9fbb112 | 1.58 MiB | 2.11 MiB | 539.18 KiB |
Previous results on branch: markushi/feat/anr-profiling
Startup times
| Revision | Plain | With Sentry | Diff |
|---|---|---|---|
| 091320b | 312.00 ms | 346.63 ms | 34.63 ms |
| eb02e45 | 362.67 ms | 431.71 ms | 69.04 ms |
| 8c4e7d6 | 309.15 ms | 359.27 ms | 50.12 ms |
| c10e603 | 367.92 ms | 393.50 ms | 25.58 ms |
| eb7143a | 347.66 ms | 408.54 ms | 60.88 ms |
| 00299fd | 359.87 ms | 424.85 ms | 64.98 ms |
| fca8df8 | 326.79 ms | 379.69 ms | 52.90 ms |
| 8d7c3bb | 304.55 ms | 377.88 ms | 73.33 ms |
| 931fa1a | 300.38 ms | 354.90 ms | 54.52 ms |
| fa76e86 | 274.32 ms | 349.63 ms | 75.31 ms |
App size
| Revision | Plain | With Sentry | Diff |
|---|---|---|---|
| 091320b | 1.58 MiB | 2.29 MiB | 728.60 KiB |
| eb02e45 | 1.58 MiB | 2.29 MiB | 725.26 KiB |
| 8c4e7d6 | 1.58 MiB | 2.29 MiB | 727.03 KiB |
| c10e603 | 1.58 MiB | 2.29 MiB | 723.72 KiB |
| eb7143a | 1.58 MiB | 2.29 MiB | 724.12 KiB |
| 00299fd | 1.58 MiB | 2.29 MiB | 723.50 KiB |
| fca8df8 | 1.58 MiB | 2.29 MiB | 723.68 KiB |
| 8d7c3bb | 1.58 MiB | 2.29 MiB | 727.01 KiB |
| 931fa1a | 0 B | 0 B | 0 B |
| fa76e86 | 1.58 MiB | 2.29 MiB | 724.06 KiB |
sentry-android-core/src/main/java/io/sentry/android/core/anr/AnrCulpritIdentifier.java
Show resolved
Hide resolved
…ntry-java into markushi/feat/anr-profiling
sentry-android-core/src/main/java/io/sentry/android/core/AnrV2Integration.java
Outdated
Show resolved
Hide resolved
sentry-android-core/src/main/java/io/sentry/android/core/AnrV2Integration.java
Outdated
Show resolved
Hide resolved
sentry-android-core/src/main/java/io/sentry/android/core/AnrV2Integration.java
Outdated
Show resolved
Hide resolved
sentry-android-core/src/main/java/io/sentry/android/core/anr/AnrStackTrace.java
Outdated
Show resolved
Hide resolved
sentry-android-core/src/main/java/io/sentry/android/core/anr/AnrProfilingIntegration.java
Show resolved
Hide resolved
|
@sentry review |
There was a problem hiding this comment.
Cursor Bugbot has reviewed your changes and found 1 potential issue.
Bugbot Autofix is OFF. To automatically fix reported issues with Cloud Agents, enable autofix in the Cursor dashboard.
sentry-android-core/src/main/java/io/sentry/android/core/ApplicationExitInfoEventProcessor.java
Show resolved
Hide resolved
There was a problem hiding this comment.
Cursor Bugbot has reviewed your changes and found 1 potential issue.
Bugbot Autofix is OFF. To automatically fix reported issues with Cloud Agents, enable autofix in the Cursor dashboard.
sentry-android-core/src/main/java/io/sentry/android/core/AnrIntegration.java
Outdated
Show resolved
Hide resolved
sentry-android-core/src/main/java/io/sentry/android/core/ApplicationExitInfoEventProcessor.java
Show resolved
Hide resolved
There was a problem hiding this comment.
Cursor Bugbot has reviewed your changes and found 2 potential issues.
Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, enable autofix in the Cursor dashboard.
sentry-android-core/src/main/java/io/sentry/android/core/anr/AnrCulpritIdentifier.java
Outdated
Show resolved
Hide resolved
sentry-android-core/src/main/java/io/sentry/android/core/ApplicationExitInfoEventProcessor.java
Show resolved
Hide resolved
romtsn
left a comment
There was a problem hiding this comment.
some bugbot comments still left, but to me this looks great, hats off!
sentry-android-core/src/main/java/io/sentry/android/core/anr/AnrProfileManager.java
Show resolved
Hide resolved
There was a problem hiding this comment.
Cursor Bugbot has reviewed your changes and found 1 potential issue.
Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, enable autofix in the Cursor dashboard.
sentry-android-core/src/main/java/io/sentry/android/core/anr/AnrProfilingIntegration.java
Show resolved
Hide resolved
There was a problem hiding this comment.
Cursor Bugbot has reviewed your changes and found 1 potential issue.
Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, enable autofix in the Cursor dashboard.
sentry-android-core/src/main/java/io/sentry/android/core/ApplicationExitInfoEventProcessor.java
Show resolved
Hide resolved
sentry-android-core/src/main/java/io/sentry/android/core/ApplicationExitInfoEventProcessor.java
Outdated
Show resolved
Hide resolved
There was a problem hiding this comment.
Cursor Bugbot has reviewed your changes and found 2 potential issues.
Bugbot Autofix prepared fixes for both issues found in the latest run.
- ✅ Fixed: Mismatched stack collection limit and queue capacity
- Changed MAX_NUM_STACKS to use the same formula as MAX_NUM_STACKTRACES, reducing it from 151 to 120 to match the queue capacity and prevent stack eviction.
- ✅ Fixed: toStream closes underlying sink via try-with-resources
- Removed try-with-resources block to avoid closing the caller-owned OutputStream sink and eliminated redundant flush call.
Or push these changes by commenting:
@cursor push 408aaa27a1
Preview (408aaa27a1)
diff --git a/sentry-android-core/src/main/java/io/sentry/android/core/anr/AnrProfileManager.java b/sentry-android-core/src/main/java/io/sentry/android/core/anr/AnrProfileManager.java
--- a/sentry-android-core/src/main/java/io/sentry/android/core/anr/AnrProfileManager.java
+++ b/sentry-android-core/src/main/java/io/sentry/android/core/anr/AnrProfileManager.java
@@ -73,11 +73,9 @@
public void toStream(
final @NotNull AnrStackTrace value, final @NotNull OutputStream sink)
throws IOException {
- try (final @NotNull DataOutputStream dos = new DataOutputStream(sink)) {
- value.serialize(dos);
- dos.flush();
- sink.flush();
- }
+ final @NotNull DataOutputStream dos = new DataOutputStream(sink);
+ value.serialize(dos);
+ dos.flush();
}
});
}
diff --git a/sentry-android-core/src/main/java/io/sentry/android/core/anr/AnrProfilingIntegration.java b/sentry-android-core/src/main/java/io/sentry/android/core/anr/AnrProfilingIntegration.java
--- a/sentry-android-core/src/main/java/io/sentry/android/core/anr/AnrProfilingIntegration.java
+++ b/sentry-android-core/src/main/java/io/sentry/android/core/anr/AnrProfilingIntegration.java
@@ -33,7 +33,7 @@
public static final long POLLING_INTERVAL_MS = 66;
private static final long THRESHOLD_SUSPICION_MS = 1000;
public static final long THRESHOLD_ANR_MS = 4000;
- static final int MAX_NUM_STACKS = (int) (10_000 / POLLING_INTERVAL_MS);
+ static final int MAX_NUM_STACKS = (int) ((THRESHOLD_ANR_MS / POLLING_INTERVAL_MS) * 2);
private final AtomicBoolean enabled = new AtomicBoolean(true);
private final Runnable updater = () -> lastMainThreadExecutionTime = SystemClock.uptimeMillis();
sentry-android-core/src/main/java/io/sentry/android/core/anr/AnrProfilingIntegration.java
Show resolved
Hide resolved
sentry-android-core/src/main/java/io/sentry/android/core/anr/AnrProfileManager.java
Show resolved
Hide resolved
* feat(android): Add enableAnrFingerprinting option Decouple ANR fingerprinting from ANR profiling into a standalone opt-in option. This allows static fingerprinting of system-frame-only ANRs regardless of whether profiling is enabled. Co-Authored-By: Claude Opus 4.6 <[email protected]> * feat(android): Mark enableAnrFingerprinting as experimental Also enable the option in the sample app. Co-Authored-By: Claude Opus 4.6 <[email protected]> * feat(android): Default enableAnrFingerprinting to true Co-Authored-By: Claude Opus 4.6 <[email protected]> * refactor(android): Remove experimental flag from enableAnrFingerprinting Co-Authored-By: Claude Opus 4.6 <[email protected]> * Fix api dump * Address PR feedback --------- Co-authored-by: Claude Opus 4.6 <[email protected]>
sentry-android-core/src/main/java/io/sentry/android/core/ApplicationExitInfoEventProcessor.java
Show resolved
Hide resolved
There was a problem hiding this comment.
Cursor Bugbot has reviewed your changes and found 2 potential issues.
Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, enable autofix in the Cursor dashboard.
sentry-android-core/src/main/java/io/sentry/android/core/ApplicationExitInfoEventProcessor.java
Show resolved
Hide resolved
sentry-android-core/src/main/java/io/sentry/android/core/ApplicationExitInfoEventProcessor.java
Show resolved
Hide resolved
…ntry-java into markushi/feat/anr-profiling
There was a problem hiding this comment.
Cursor Bugbot has reviewed your changes and found 2 potential issues.
Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, enable autofix in the Cursor dashboard.
sentry-android-core/src/main/java/io/sentry/android/core/anr/AnrProfileManager.java
Show resolved
Hide resolved
sentry-android-core/src/main/java/io/sentry/android/core/SentryAndroidOptions.java
Show resolved
Hide resolved
## DESCRIBE YOUR PR Document two new Android SDK features available in version 8.35.0: - **ANR Profiling (Experimental)**: Captures main thread stack profiles during ANR detection, attached as flamegraphs on the issue details page. Configured via `anrProfilingSampleRate` (disabled by default). - **ANR Fingerprinting**: Groups system-frame-only ANRs into a single issue to reduce noise. Enabled by default, configurable via `enableAnrFingerprinting`. Changes across three files: - `docs/platforms/android/configuration/app-not-respond.mdx` — New sections with configuration examples (AndroidManifest.xml, Kotlin, Java) - `docs/platforms/android/configuration/options.mdx` — New `SdkOption` entries for both features - `docs/platforms/android/index.mdx` — Links to new features in Next Steps Related SDK PRs: - getsentry/sentry-java#4899 - getsentry/sentry-java#5168 ## IS YOUR CHANGE URGENT? Help us prioritize incoming PRs by letting us know when the change needs to go live. - [ ] Urgent deadline (GA date, etc.): - [ ] Other deadline: - [x] None: Not urgent, can wait up to 1 week+ ## SLA - Teamwork makes the dream work, so please add a reviewer to your PRs. - Please give the docs team up to 1 week to review your PR unless you've added an urgent due date to it. Thanks in advance for your help! ## PRE-MERGE CHECKLIST *Make sure you've checked the following before merging your changes:* - [ ] Checked Vercel preview for correctness, including links - [ ] PR was reviewed and approved by any necessary SMEs (subject matter experts) - [ ] PR was reviewed and approved by a member of the [Sentry docs team](https://github.com/orgs/getsentry/teams/docs) --------- Co-authored-by: Claude Opus 4.6 <[email protected]>

📜 Description
Adds ANR (Application Not Responding) profiling integration that profiles the main thread when an ANR is detected and reports the captured profiles to Sentry.
Key Changes:
AnrProfilingIntegrationto capture profiles during ANR eventsAnrV2Integrationnow takes care of matching and capturing the profile on the next start💡 Motivation and Context
This feature enables better ANR diagnostics by capturing profiling data at the time of ANR detection, allowing developers to identify performance bottlenecks and problematic code paths causing application hangs.
Example event: https://sentry-sdks.sentry.io/issues/7229210096/events/4598ff6fcc0f402d8ecca615005e7f64/
💚 How did you test it?
📝 Checklist
sendDefaultPIIis enabled.🔮 Next steps