Skip to content

feat(anr): Profile main thread when ANR and report ANR profiles to Sentry#4899

Merged
markushi merged 45 commits intomainfrom
markushi/feat/anr-profiling
Mar 6, 2026
Merged

feat(anr): Profile main thread when ANR and report ANR profiles to Sentry#4899
markushi merged 45 commits intomainfrom
markushi/feat/anr-profiling

Conversation

@markushi
Copy link
Member

@markushi markushi commented Nov 12, 2025

📜 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:

  • New AnrProfilingIntegration to capture profiles during ANR events
  • AnrV2Integration now takes care of matching and capturing the profile on the next start
  • If the captured ANR event only contains system frames, a static fingerprint will get set, effectively changing the grouping behavior to group all noisy ANRs into a single issue

💡 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?

  • Added tests

📝 Checklist

  • I added GH Issue ID & Linear ID
  • I added tests to verify the changes.
  • No new PII added or SDK only sends newly added PII if sendDefaultPII is enabled.
  • I updated the docs if needed.
  • I updated the wizard if needed.
  • Review from the native team if needed.
  • No breaking change or entry added to the changelog.
  • No breaking change for hybrid SDKs or communicated to hybrid SDKs.

🔮 Next steps

@github-actions
Copy link
Contributor

github-actions bot commented Nov 12, 2025

Messages
📖 Do not forget to update Sentry-docs with your feature once the pull request gets approved.

Generated by 🚫 dangerJS against ab98857

@github-actions
Copy link
Contributor

github-actions bot commented Nov 12, 2025

Performance metrics 🚀

  Plain With Sentry Diff
Startup time 317.46 ms 353.67 ms 36.21 ms
Size 0 B 0 B 0 B

Baseline results on branch: main

Startup times

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

@markushi markushi marked this pull request as draft December 3, 2025 07:19
@markushi
Copy link
Member Author

@sentry review

@markushi markushi marked this pull request as ready for review December 17, 2025 09:51
Copy link

@cursor cursor bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link

@cursor cursor bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@markushi markushi requested a review from romtsn February 26, 2026 20:02
Copy link

@cursor cursor bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Member

@romtsn romtsn left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

some bugbot comments still left, but to me this looks great, hats off!

Copy link

@cursor cursor bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link

@cursor cursor bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link

@cursor cursor bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Create PR

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();
This Bugbot Autofix run was free. To enable autofix for future PRs, go to the Cursor dashboard.

* 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]>
Copy link

@cursor cursor bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link

@cursor cursor bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@markushi markushi merged commit d501a7e into main Mar 6, 2026
65 checks passed
@markushi markushi deleted the markushi/feat/anr-profiling branch March 6, 2026 10:19
markushi added a commit to getsentry/sentry-docs that referenced this pull request Mar 11, 2026
## 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]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants