Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
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
11 changes: 10 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 @@ -437,13 +437,15 @@ public final class io/sentry/android/core/SentryAndroidDateProvider : io/sentry/

public final class io/sentry/android/core/SentryAndroidOptions : io/sentry/SentryOptions {
public fun <init> ()V
public fun close ()V
public fun enableAllAutoBreadcrumbs (Z)V
public fun getAnrProfilingSampleRate ()Ljava/lang/Double;
public fun getAnrTimeoutIntervalMillis ()J
public fun getBeforeScreenshotCaptureCallback ()Lio/sentry/android/core/SentryAndroidOptions$BeforeCaptureCallback;
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 getHangRecorder ()Lio/sentry/android/core/hang/HangRecorder;
public fun getMonotonicTicker ()Lio/sentry/time/MonotonicTicker;
public fun getNativeSdkName ()Ljava/lang/String;
public fun getNdkAppHangTimeoutIntervalMillis ()J
Expand Down Expand Up @@ -514,6 +516,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 setHangRecorder (Lio/sentry/android/core/hang/HangRecorder;)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 @@ -811,6 +814,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 abstract interface class io/sentry/android/core/hang/HangDataCollector : java/io/Closeable {
public abstract fun close ()V
public abstract fun onRecordingCanceled ()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 @@ -32,6 +35,11 @@
import io.sentry.android.core.anr.AnrProfileRotationHelper;
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.HangRecorder;
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 +61,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 +72,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 +270,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);
}
setupHangRecorder(options, debugManager);
if (options.getSocketTagger() instanceof NoOpSocketTagger) {
options.setSocketTagger(AndroidSocketTagger.getInstance());
}
Expand Down Expand Up @@ -516,6 +531,38 @@ static void installDefaultIntegrations(
.setFormHandler(new SentryAndroidOptions.AndroidUserFeedbackFormHandler());
}

private static void setupHangRecorder(
final @NotNull SentryAndroidOptions options, final @NotNull IDebugManager debugManager) {
if (options.getCacheDirPath() == null || options.getHangRecorder() != 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 HangRecorder recorder =
new HangRecorder(
options,
AppState.getInstance(),
ticker,
epochClock,
HangRecorder.DEFAULT_POLLING_INTERVAL_MS,
HangRecorder.DEFAULT_MAX_RECORDING_TIME_MS,
HangRecorder.DEFAULT_EXECUTOR_FACTORY,
metronome ->
Collections.singletonList(
new ThreadHangSignal(
metronome,
ticker,
debugManager,
mainThreadPoster,
HangRecorder.DEFAULT_SUSPICIOUS_THRESHOLD_MS)),
Collections.singletonList(
new ThreadSampleCollector(options, epochClock, ticker, mainLooper.getThread())));
options.setHangRecorder(recorder);
}

/**
* 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.HangRecorder;
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 HangRecorder hangRecorder;

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 once per SDK initialization, so a process records the stacks
* of all its hangs, or of none. Stack sampling starts after the main thread was blocked for 300
* ms.
*
* <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,30 @@ public boolean isAnrProfilingEnabled() {
return anrProfilingSampleRate != null && anrProfilingSampleRate > 0;
}

/**
* The recorder that the ANR integrations share. Null if {@link #getCacheDirPath()} was null at
* init.
*/
@ApiStatus.Internal
public @Nullable HangRecorder getHangRecorder() {
return hangRecorder;
}

@ApiStatus.Internal
public void setHangRecorder(final @Nullable HangRecorder hangRecorder) {
this.hangRecorder = hangRecorder;
}

@ApiStatus.Internal
@Override
public void close() {
super.close();
final @Nullable HangRecorder recorder = hangRecorder;
if (recorder != null) {
recorder.close();
}
}

/**
* 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,7 +6,7 @@
import org.jetbrains.annotations.NotNull;

/**
* Coordinates file rotation between AnrProfilingIntegration and AnrV2Integration to prevent
* Coordinates file rotation between ThreadSampleCollector and AnrV2Integration to prevent
* concurrent access to the same QueueFile.
*/
@ApiStatus.Internal
Expand Down
Loading
Loading