-
-
Notifications
You must be signed in to change notification settings - Fork 427
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鈥檒l occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add option to enable or disable Activity Frame Tracking #2314
Changes from 6 commits
fb45b6d
fd99cb6
e48e527
88fe044
f7368b0
afb587e
fb706a8
0ebcc82
fc2948e
21c0739
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,7 +3,6 @@ | |
import android.app.Activity; | ||
import android.util.SparseIntArray; | ||
import androidx.core.app.FrameMetricsAggregator; | ||
import io.sentry.ILogger; | ||
import io.sentry.MeasurementUnit; | ||
import io.sentry.SentryLevel; | ||
import io.sentry.android.core.internal.util.MainThreadChecker; | ||
|
@@ -16,6 +15,7 @@ | |
import org.jetbrains.annotations.NotNull; | ||
import org.jetbrains.annotations.Nullable; | ||
import org.jetbrains.annotations.TestOnly; | ||
import org.jetbrains.annotations.VisibleForTesting; | ||
|
||
/** | ||
* A class that tracks slow and frozen frames using the FrameMetricsAggregator class from | ||
|
@@ -25,48 +25,57 @@ | |
public final class ActivityFramesTracker { | ||
|
||
private @Nullable FrameMetricsAggregator frameMetricsAggregator = null; | ||
private boolean androidXAvailable = true; | ||
private final SentryAndroidOptions options; | ||
|
||
private final @NotNull Map<SentryId, Map<String, @NotNull MeasurementValue>> | ||
activityMeasurements = new ConcurrentHashMap<>(); | ||
private final @NotNull Map<Activity, FrameCounts> frameCountAtStartSnapshots = | ||
new WeakHashMap<>(); | ||
|
||
private final @Nullable ILogger logger; | ||
private final @NotNull MainLooperHandler handler; | ||
|
||
public ActivityFramesTracker( | ||
final @NotNull LoadClass loadClass, | ||
final @Nullable ILogger logger, | ||
final @NotNull SentryAndroidOptions options, | ||
final @NotNull MainLooperHandler handler) { | ||
androidXAvailable = | ||
loadClass.isClassAvailable("androidx.core.app.FrameMetricsAggregator", logger); | ||
|
||
final boolean androidXAvailable = | ||
loadClass.isClassAvailable("androidx.core.app.FrameMetricsAggregator", options.getLogger()); | ||
|
||
if (androidXAvailable) { | ||
frameMetricsAggregator = new FrameMetricsAggregator(); | ||
} | ||
this.logger = logger; | ||
this.options = options; | ||
this.handler = handler; | ||
} | ||
|
||
public ActivityFramesTracker(final @NotNull LoadClass loadClass, final @Nullable ILogger logger) { | ||
this(loadClass, logger, new MainLooperHandler()); | ||
public ActivityFramesTracker( | ||
final @NotNull LoadClass loadClass, final @NotNull SentryAndroidOptions options) { | ||
this(loadClass, options, new MainLooperHandler()); | ||
} | ||
|
||
/** | ||
* @deprecated please use {{@link ActivityFramesTracker#ActivityFramesTracker(LoadClass, | ||
* SentryAndroidOptions)}} instead. | ||
*/ | ||
public ActivityFramesTracker(final @NotNull LoadClass loadClass) { | ||
markushi marked this conversation as resolved.
Show resolved
Hide resolved
|
||
this(loadClass, null); | ||
this(loadClass, new SentryAndroidOptions()); | ||
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. Creating a new options here won't help, the feature will be always enabled and can't be disabled since 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.
exactly, that matches the existing behavior thus I thought it makes sense. But in hindsight it's actually more confusing than helpful, let me remove this again 馃憤 |
||
} | ||
|
||
@TestOnly | ||
ActivityFramesTracker( | ||
final @Nullable FrameMetricsAggregator frameMetricsAggregator, | ||
final @NotNull MainLooperHandler handler) { | ||
final @NotNull LoadClass loadClass, | ||
final @NotNull SentryAndroidOptions options, | ||
final @NotNull MainLooperHandler handler, | ||
final @Nullable FrameMetricsAggregator frameMetricsAggregator) { | ||
|
||
this(loadClass, options, handler); | ||
this.frameMetricsAggregator = frameMetricsAggregator; | ||
this.logger = null; | ||
this.handler = handler; | ||
} | ||
|
||
private boolean isFrameMetricsAggregatorAvailable() { | ||
return androidXAvailable && frameMetricsAggregator != null; | ||
@VisibleForTesting | ||
public boolean isFrameMetricsAggregatorAvailable() { | ||
return frameMetricsAggregator != null && options.isEnableFramesTracking(); | ||
} | ||
|
||
@SuppressWarnings("NullAway") | ||
|
@@ -215,15 +224,15 @@ private void runSafelyOnUiThread(final Runnable runnable, final String tag) { | |
try { | ||
runnable.run(); | ||
} catch (Throwable ignored) { | ||
if (logger != null && tag != null) { | ||
logger.log(SentryLevel.WARNING, "Failed to execute " + tag); | ||
if (tag != null) { | ||
options.getLogger().log(SentryLevel.WARNING, "Failed to execute " + tag); | ||
} | ||
} | ||
}); | ||
} | ||
} catch (Throwable ignored) { | ||
if (logger != null && tag != null) { | ||
logger.log(SentryLevel.WARNING, "Failed to execute " + tag); | ||
if (tag != null) { | ||
options.getLogger().log(SentryLevel.WARNING, "Failed to execute " + tag); | ||
} | ||
} | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Missing nullability anotations.