From e465b5702f0ea0add5dbb4ca1267fa649b9bdc40 Mon Sep 17 00:00:00 2001 From: Roman Zavarnitsyn Date: Thu, 22 Feb 2024 14:02:25 +0100 Subject: [PATCH 1/2] Add enableScopePersistence option --- .../android/core/AndroidOptionsInitializer.java | 4 +++- .../android/core/AndroidOptionsInitializerTest.kt | 7 +++++++ sentry/api/sentry.api | 2 ++ sentry/src/main/java/io/sentry/SentryOptions.java | 11 +++++++++++ sentry/src/test/java/io/sentry/SentryOptionsTest.kt | 5 +++++ 5 files changed, 28 insertions(+), 1 deletion(-) diff --git a/sentry-android-core/src/main/java/io/sentry/android/core/AndroidOptionsInitializer.java b/sentry-android-core/src/main/java/io/sentry/android/core/AndroidOptionsInitializer.java index e777db4add7..372448b8e71 100644 --- a/sentry-android-core/src/main/java/io/sentry/android/core/AndroidOptionsInitializer.java +++ b/sentry-android-core/src/main/java/io/sentry/android/core/AndroidOptionsInitializer.java @@ -223,7 +223,9 @@ static void initializeIntegrationsAndProcessors( options.setTransactionPerformanceCollector(new DefaultTransactionPerformanceCollector(options)); if (options.getCacheDirPath() != null) { - options.addScopeObserver(new PersistingScopeObserver(options)); + if (options.isEnableScopePersistence()) { + options.addScopeObserver(new PersistingScopeObserver(options)); + } options.addOptionsObserver(new PersistingOptionsObserver(options)); } } diff --git a/sentry-android-core/src/test/java/io/sentry/android/core/AndroidOptionsInitializerTest.kt b/sentry-android-core/src/test/java/io/sentry/android/core/AndroidOptionsInitializerTest.kt index 6353e9dde89..7800063b352 100644 --- a/sentry-android-core/src/test/java/io/sentry/android/core/AndroidOptionsInitializerTest.kt +++ b/sentry-android-core/src/test/java/io/sentry/android/core/AndroidOptionsInitializerTest.kt @@ -653,4 +653,11 @@ class AndroidOptionsInitializerTest { fixture.sentryOptions.integrations.firstOrNull { it is AnrIntegration } assertNull(anrv1Integration) } + + @Test + fun `PersistingScopeObserver is not set to options, if scope persistence is disabled`() { + fixture.initSut(configureOptions = { isEnableScopePersistence = false }) + + assertTrue { fixture.sentryOptions.scopeObservers.none { it is PersistingScopeObserver } } + } } diff --git a/sentry/api/sentry.api b/sentry/api/sentry.api index b46483457d1..7bd5904be9b 100644 --- a/sentry/api/sentry.api +++ b/sentry/api/sentry.api @@ -2244,6 +2244,7 @@ public class io/sentry/SentryOptions { public fun isEnableDeduplication ()Z public fun isEnableExternalConfiguration ()Z public fun isEnablePrettySerializationOutput ()Z + public fun isEnableScopePersistence ()Z public fun isEnableShutdownHook ()Z public fun isEnableSpotlight ()Z public fun isEnableTimeToFullDisplayTracing ()Z @@ -2284,6 +2285,7 @@ public class io/sentry/SentryOptions { public fun setEnableDeduplication (Z)V public fun setEnableExternalConfiguration (Z)V public fun setEnablePrettySerializationOutput (Z)V + public fun setEnableScopePersistence (Z)V public fun setEnableShutdownHook (Z)V public fun setEnableSpotlight (Z)V public fun setEnableTimeToFullDisplayTracing (Z)V diff --git a/sentry/src/main/java/io/sentry/SentryOptions.java b/sentry/src/main/java/io/sentry/SentryOptions.java index bfc51d92185..648068fcd8c 100644 --- a/sentry/src/main/java/io/sentry/SentryOptions.java +++ b/sentry/src/main/java/io/sentry/SentryOptions.java @@ -448,6 +448,9 @@ public class SentryOptions { private @Nullable String spotlightConnectionUrl; + /** Whether to enable scope persistence so the scope values are preserved if the process dies */ + private boolean enableScopePersistence = true; + /** Contains a list of monitor slugs for which check-ins should not be sent. */ @ApiStatus.Experimental private @Nullable List ignoredCheckIns = null; @@ -2313,6 +2316,14 @@ public void setEnableSpotlight(final boolean enableSpotlight) { this.enableSpotlight = enableSpotlight; } + public boolean isEnableScopePersistence() { + return enableScopePersistence; + } + + public void setEnableScopePersistence(boolean enableScopePersistence) { + this.enableScopePersistence = enableScopePersistence; + } + /** The BeforeSend callback */ public interface BeforeSendCallback { diff --git a/sentry/src/test/java/io/sentry/SentryOptionsTest.kt b/sentry/src/test/java/io/sentry/SentryOptionsTest.kt index 213ce69b565..b446813421f 100644 --- a/sentry/src/test/java/io/sentry/SentryOptionsTest.kt +++ b/sentry/src/test/java/io/sentry/SentryOptionsTest.kt @@ -603,4 +603,9 @@ class SentryOptionsTest { assertTrue(options.isEnableSpotlight) assertEquals("http://localhost:8080", options.spotlightConnectionUrl) } + + @Test + fun `when options are initialized, enableScopePersistence is set to true by default`() { + assertEquals(true, SentryOptions().isEnableScopePersistence) + } } From b1f59ebf3c5c296f78242d303334f6e62e82d957 Mon Sep 17 00:00:00 2001 From: Roman Zavarnitsyn Date: Thu, 22 Feb 2024 14:23:22 +0100 Subject: [PATCH 2/2] Changelog --- CHANGELOG.md | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index d557f71dd6b..7196f12a7cd 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,11 @@ ## Unreleased +### Features + +- Add `enableScopePersistence` option to disable `PersistingScopeObserver` used for ANR reporting which may increase performance overhead. Defaults to `true` ([#3218](https://github.com/getsentry/sentry-java/pull/3218)) + - When disabled, the SDK will not enrich ANRv2 events with scope data (e.g. breadcrumbs, user, tags, etc.) + ### Fixes - Fix old profiles deletion on SDK init ([#3216](https://github.com/getsentry/sentry-java/pull/3216))