Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
18 commits
Select commit Hold shift + click to select a range
6771b09
ref(anr): Run AnrProfilingIntegration on the HangRecorder
markushi Nov 19, 2026
e5e6734
chore(anr): Merge non-fatal-anr-3 into non-fatal-anr-4
markushi Nov 23, 2026
71602da
fix(anr): Draw the ANR profiling sample rate for each hang
markushi Nov 23, 2026
294e5fd
chore(anr): Merge non-fatal-anr-3 into non-fatal-anr-4
markushi Nov 23, 2026
bc54156
feat(anr): Skip unsampled hangs in AnrProfilingIntegration
markushi Nov 23, 2026
e65edb7
chore(anr): Merge non-fatal-anr-3 into non-fatal-anr-4
markushi Nov 23, 2026
6cf5bcc
ref(anr): Adapt AnrProfilingIntegration to nullable recording requests
markushi Nov 23, 2026
fff8376
chore(anr): Merge non-fatal-anr-3 into non-fatal-anr-4
markushi Nov 24, 2026
13beb44
chore(anr): Merge non-fatal-anr-3 into non-fatal-anr-4
markushi Nov 24, 2026
d728781
ref(anr): Adapt to the renamed hang types
markushi Nov 24, 2026
3ea4af7
chore(anr): Merge non-fatal-anr-3 into non-fatal-anr-4
markushi Nov 24, 2026
bfa05d2
chore(anr): Merge non-fatal-anr-3 into non-fatal-anr-4
markushi Nov 24, 2026
f5a1da8
chore(anr): Merge non-fatal-anr-3 into non-fatal-anr-4
markushi Nov 24, 2026
de234fa
ref(anr): Adapt to the metronome time
markushi Nov 24, 2026
bbcb524
chore(anr): Merge non-fatal-anr-3 into non-fatal-anr-4
markushi Nov 24, 2026
e8ddedb
ref(anr): Remove the options close hook
markushi Nov 24, 2026
b9cf6b7
chore(anr): Merge non-fatal-anr-3 into non-fatal-anr-4
markushi Nov 24, 2026
b4059a5
chore(anr): Merge non-fatal-anr-3 into non-fatal-anr-4
markushi Nov 24, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 17 additions & 1 deletion sentry-android-core/api/sentry-android-core.api
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ public final class io/sentry/android/core/AppStartExtension$ExtendedAppStart {
public fun <init> (Lio/sentry/ITransaction;Lio/sentry/ISpan;)V
}

public final class io/sentry/android/core/AppState : java/io/Closeable {
public final class io/sentry/android/core/AppState : io/sentry/android/core/AppStateProvider, java/io/Closeable {
public fun addAppStateListener (Lio/sentry/android/core/AppState$AppStateListener;)V
public fun close ()V
public static fun getInstance ()Lio/sentry/android/core/AppState;
Expand Down Expand Up @@ -444,6 +444,7 @@ public final class io/sentry/android/core/SentryAndroidOptions : io/sentry/Sentr
public fun getBeforeViewHierarchyCaptureCallback ()Lio/sentry/android/core/SentryAndroidOptions$BeforeCaptureCallback;
public fun getDebugImagesLoader ()Lio/sentry/android/core/IDebugImagesLoader;
public fun getFrameMetricsCollector ()Lio/sentry/android/core/internal/util/SentryFrameMetricsCollector;
public fun getHangManager ()Lio/sentry/android/core/hang/HangManager;
public fun getMonotonicTicker ()Lio/sentry/time/MonotonicTicker;
public fun getNativeSdkName ()Ljava/lang/String;
public fun getNdkAppHangTimeoutIntervalMillis ()J
Expand Down Expand Up @@ -514,6 +515,7 @@ public final class io/sentry/android/core/SentryAndroidOptions : io/sentry/Sentr
public fun setEnableSystemEventBreadcrumbs (Z)V
public fun setEnableSystemEventBreadcrumbsExtras (Z)V
public fun setFrameMetricsCollector (Lio/sentry/android/core/internal/util/SentryFrameMetricsCollector;)V
public fun setHangManager (Lio/sentry/android/core/hang/HangManager;)V
public fun setMemoryLimiterEnabled (Z)V
public fun setNativeHandlerStrategy (Lio/sentry/android/core/NdkHandlerStrategy;)V
public fun setNativeSdkName (Ljava/lang/String;)V
Expand Down Expand Up @@ -763,6 +765,14 @@ public final class io/sentry/android/core/anr/AnrProfileRotationHelper {
public static fun rotate ()V
}

public final class io/sentry/android/core/anr/AnrProfileStore : io/sentry/android/core/hang/HangSampleStore {
public fun <init> (Lio/sentry/SentryOptions;)V
public fun add (Lio/sentry/android/core/anr/AnrStackTrace;)V
public synthetic fun add (Ljava/lang/Object;)V
public fun clear ()V
public fun close ()V
}

public final class io/sentry/android/core/anr/AnrProfilingIntegration : io/sentry/Integration, io/sentry/android/core/AppState$AppStateListener, java/io/Closeable, java/lang/Runnable {
public static final field POLLING_INTERVAL_MS J
public static final field THRESHOLD_ANR_MS J
Expand Down Expand Up @@ -811,6 +821,12 @@ public final class io/sentry/android/core/cache/AndroidEnvelopeCache : io/sentry
public fun storeEnvelope (Lio/sentry/SentryEnvelope;Lio/sentry/Hint;)Z
}

public final class io/sentry/android/core/hang/HandlerThreadPoster : io/sentry/android/core/hang/ThreadPoster {
public fun <init> (Landroid/os/Handler;)V
public fun post (Ljava/lang/Runnable;)V
public fun remove (Ljava/lang/Runnable;)V
}

public final class io/sentry/android/core/hang/HangCollectorSetup {
public fun <init> (Lio/sentry/android/core/hang/HangDataCollector;Lio/sentry/android/core/hang/HangRecordWriter;Lio/sentry/android/core/hang/HangSampleStore;)V
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,14 @@
import android.content.Context;
import android.content.pm.PackageInfo;
import android.os.Build;
import android.os.Handler;
import android.os.Looper;
import io.sentry.CompositePerformanceCollector;
import io.sentry.DeduplicateMultithreadedEventProcessor;
import io.sentry.DefaultCompositePerformanceCollector;
import io.sentry.DefaultVersionDetector;
import io.sentry.IContinuousProfiler;
import io.sentry.IDebugManager;
import io.sentry.ILogger;
import io.sentry.ISentryLifecycleToken;
import io.sentry.ITransactionProfiler;
Expand All @@ -29,9 +32,17 @@
import io.sentry.SendFireAndForgetOutboxSender;
import io.sentry.SentryLevel;
import io.sentry.SentryOpenTelemetryMode;
import io.sentry.android.core.anr.AnrProfileManager;
import io.sentry.android.core.anr.AnrProfileRotationHelper;
import io.sentry.android.core.anr.AnrProfileStore;
import io.sentry.android.core.anr.AnrProfilingIntegration;
import io.sentry.android.core.cache.AndroidEnvelopeCache;
import io.sentry.android.core.hang.HandlerThreadPoster;
import io.sentry.android.core.hang.HangCollectorSetup;
import io.sentry.android.core.hang.HangManager;
import io.sentry.android.core.hang.ThreadHangSignal;
import io.sentry.android.core.hang.ThreadPoster;
import io.sentry.android.core.hang.ThreadSampleCollector;
import io.sentry.android.core.internal.debugmeta.AssetsDebugMetaLoader;
import io.sentry.android.core.internal.gestures.AndroidViewGestureTargetLocator;
import io.sentry.android.core.internal.modules.AssetsModulesLoader;
Expand All @@ -53,6 +64,9 @@
import io.sentry.internal.modules.NoOpModulesLoader;
import io.sentry.internal.viewhierarchy.ViewHierarchyExporter;
import io.sentry.protocol.SentryId;
import io.sentry.time.EpochClock;
import io.sentry.time.JavaMonotonicTicker;
import io.sentry.time.MonotonicTicker;
import io.sentry.transport.CurrentDateProvider;
import io.sentry.transport.NoOpEnvelopeCache;
import io.sentry.transport.NoOpTransportGate;
Expand All @@ -61,6 +75,7 @@
import io.sentry.util.thread.NoOpThreadChecker;
import java.io.File;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
Expand Down Expand Up @@ -258,9 +273,12 @@ static void initializeIntegrationsAndProcessors(
if (options.getThreadChecker() instanceof NoOpThreadChecker) {
options.setThreadChecker(AndroidThreadChecker.getInstance());
}
if (options.getDebugManager() == null) {
options.setDebugManager(new AndroidDebugManager());
@Nullable IDebugManager debugManager = options.getDebugManager();
if (debugManager == null) {
debugManager = new AndroidDebugManager();
options.setDebugManager(debugManager);
}
setupHangManager(options, debugManager);
if (options.getSocketTagger() instanceof NoOpSocketTagger) {
options.setSocketTagger(AndroidSocketTagger.getInstance());
}
Expand Down Expand Up @@ -516,6 +534,41 @@ static void installDefaultIntegrations(
.setFormHandler(new SentryAndroidOptions.AndroidUserFeedbackFormHandler());
}

private static void setupHangManager(
final @NotNull SentryAndroidOptions options, final @NotNull IDebugManager debugManager) {
if (options.getCacheDirPath() == null || options.getHangManager() != null) {
return;
}
final @NotNull Looper mainLooper = Looper.getMainLooper();
final @NotNull ThreadPoster mainThreadPoster = new HandlerThreadPoster(new Handler(mainLooper));
// options.getMonotonicTicker() counts device sleep, which would read as a hang.
final @NotNull MonotonicTicker ticker = JavaMonotonicTicker.getInstance();
final @NotNull EpochClock epochClock = options.getEpochClock();
final @NotNull HangManager manager =
new HangManager(
options,
AppState.getInstance(),
ticker,
epochClock,
HangManager.DEFAULT_POLLING_INTERVAL_MS,
HangManager.DEFAULT_MAX_RECORDING_TIME_MS,
AnrProfileManager.MAX_NUM_STACKTRACES,
HangManager.DEFAULT_EXECUTOR_FACTORY,
metronome ->
Collections.singletonList(
new ThreadHangSignal(
metronome,
debugManager,
mainThreadPoster,
HangManager.DEFAULT_SUSPICIOUS_THRESHOLD_MS)),
Collections.singletonList(
new HangCollectorSetup<>(
new ThreadSampleCollector(epochClock, mainLooper.getThread()),
(stackTrace, record) -> record.addStackTrace(stackTrace),
new AnrProfileStore(options))));
options.setHangManager(manager);
}

/**
* Reads and sets default option values that are Android specific like release and inApp
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@

/** AppState holds the state of the App, e.g. whether the app is in background/foreground, etc. */
@ApiStatus.Internal
public final class AppState implements Closeable {
public final class AppState implements Closeable, AppStateProvider {
private static @NotNull AppState instance = new AppState();
private final @NotNull AutoClosableReentrantLock lock = new AutoClosableReentrantLock();
private volatile @Nullable LifecycleObserver lifecycleObserver;
Expand Down Expand Up @@ -61,6 +61,7 @@ void setInBackground(final boolean inBackground) {
this.inBackground = inBackground;
}

@Override
public void addAppStateListener(final @NotNull AppStateListener listener) {
try (final @NotNull ISentryLifecycleToken ignored = lock.acquire()) {
ensureLifecycleObserver(NoOpLogger.getInstance());
Expand All @@ -71,6 +72,7 @@ public void addAppStateListener(final @NotNull AppStateListener listener) {
}
}

@Override
public void removeAppStateListener(final @NotNull AppStateListener listener) {
try (final @NotNull ISentryLifecycleToken ignored = lock.acquire()) {
if (lifecycleObserver != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import io.sentry.SentryLevel;
import io.sentry.SentryOptions;
import io.sentry.SpanStatus;
import io.sentry.android.core.hang.HangManager;
import io.sentry.android.core.internal.time.AndroidMonotonicTicker;
import io.sentry.android.core.internal.util.RootChecker;
import io.sentry.android.core.internal.util.SentryFrameMetricsCollector;
Expand Down Expand Up @@ -333,6 +334,8 @@ public interface BeforeCaptureCallback {

private boolean enableAnrFingerprinting = true;

private volatile @Nullable HangManager hangManager;

public SentryAndroidOptions() {
setSentryClientName(BuildConfig.SENTRY_ANDROID_SDK_NAME + "/" + BuildConfig.VERSION_NAME);
setSdkVersion(createSdkVersion());
Expand Down Expand Up @@ -977,6 +980,10 @@ public void setEnableSystemEventBreadcrumbsExtras(
* Sets the sample rate for capturing main-thread stack profiles when a foreground ANR is
* detected. The profile is attached to the ANR event on the next app start.
*
* <p>The sampling decision is made at each hang. Stack sampling starts after the main thread was
* blocked for 300 ms. An unsampled hang captures no stacks, unless another feature, such as
* non-fatal ANR reporting, needs them.
*
* <p>Use {@code null} to disable ANR profiling (default). Values must be between {@code 0.0} and
* {@code 1.0}.
*
Expand All @@ -1002,6 +1009,20 @@ public boolean isAnrProfilingEnabled() {
return anrProfilingSampleRate != null && anrProfilingSampleRate > 0;
}

/**
* The hang manager that the ANR integrations share. Null if {@link #getCacheDirPath()} was null
* at init.
*/
@ApiStatus.Internal
public @Nullable HangManager getHangManager() {
return hangManager;
}

@ApiStatus.Internal
public void setHangManager(final @Nullable HangManager hangManager) {
this.hangManager = hangManager;
}

/**
* Returns whether ANR fingerprinting is enabled. When enabled, the SDK assigns static
* fingerprints to ANR events that would otherwise produce noisy grouping. Currently, this applies
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
import org.jetbrains.annotations.NotNull;

/**
* Coordinates file rotation between AnrProfilingIntegration and AnrV2Integration to prevent
* concurrent access to the same QueueFile.
* Coordinates file rotation between AnrProfileStore and AnrV2Integration to prevent concurrent
* access to the same QueueFile.
*/
@ApiStatus.Internal
public final class AnrProfileRotationHelper {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package io.sentry.android.core.anr;

import io.sentry.SentryOptions;
import io.sentry.android.core.hang.HangSampleStore;
import java.io.File;
import java.io.IOException;
import org.jetbrains.annotations.ApiStatus;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

/**
* Writes the main-thread samples of a hang to the ANR profile file, which the fatal ANR path reads
* at the next app start. Opens the file at the first write. Does nothing without a {@link
* SentryOptions#getCacheDirPath()}.
*/
@ApiStatus.Internal
public final class AnrProfileStore implements HangSampleStore<AnrStackTrace> {

private final @NotNull SentryOptions options;
private @Nullable AnrProfileManager profileManager;

public AnrProfileStore(final @NotNull SentryOptions options) {
this.options = options;
}

@Override
public void clear() throws IOException {
final @Nullable AnrProfileManager manager = openProfileManager();
if (manager != null) {
manager.clear();
}
}

@Override
public void add(final @NotNull AnrStackTrace sample) throws IOException {
final @Nullable AnrProfileManager manager = openProfileManager();
if (manager != null) {
manager.add(sample);
}
}

@Override
public void close() throws IOException {
final @Nullable AnrProfileManager manager = profileManager;
profileManager = null;
if (manager != null) {
manager.close();
}
}

private @Nullable AnrProfileManager openProfileManager() {
if (profileManager == null) {
final @Nullable String cacheDirPath = options.getCacheDirPath();
if (cacheDirPath != null) {
profileManager =
new AnrProfileManager(
options, AnrProfileRotationHelper.getFileForRecording(new File(cacheDirPath)));
}
}
return profileManager;
}
}
Loading
Loading