Skip to content

Commit 56f2a8a

Browse files
romtsnmarkushi
andauthored
Add enableScopePersistence option (#3218)
* Add enableScopePersistence option * Changelog * Add option to manifest * Update sentry/src/main/java/io/sentry/SentryOptions.java Co-authored-by: Markus Hintersteiner <markus.hintersteiner@sentry.io> --------- Co-authored-by: Markus Hintersteiner <markus.hintersteiner@sentry.io>
1 parent c34791a commit 56f2a8a

File tree

8 files changed

+64
-1
lines changed

8 files changed

+64
-1
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,11 @@
22

33
## Unreleased
44

5+
### Features
6+
7+
- 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))
8+
- When disabled, the SDK will not enrich ANRv2 events with scope data (e.g. breadcrumbs, user, tags, etc.)
9+
510
### Fixes
611

712
- Fix old profiles deletion on SDK init ([#3216](https://github.com/getsentry/sentry-java/pull/3216))

sentry-android-core/src/main/java/io/sentry/android/core/AndroidOptionsInitializer.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,9 @@ static void initializeIntegrationsAndProcessors(
223223
options.setTransactionPerformanceCollector(new DefaultTransactionPerformanceCollector(options));
224224

225225
if (options.getCacheDirPath() != null) {
226-
options.addScopeObserver(new PersistingScopeObserver(options));
226+
if (options.isEnableScopePersistence()) {
227+
options.addScopeObserver(new PersistingScopeObserver(options));
228+
}
227229
options.addOptionsObserver(new PersistingOptionsObserver(options));
228230
}
229231
}

sentry-android-core/src/main/java/io/sentry/android/core/ManifestMetadataReader.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,8 @@ final class ManifestMetadataReader {
100100

101101
static final String ENABLE_APP_START_PROFILING = "io.sentry.profiling.enable-app-start";
102102

103+
static final String ENABLE_SCOPE_PERSISTENCE = "io.sentry.enable-scope-persistence";
104+
103105
/** ManifestMetadataReader ctor */
104106
private ManifestMetadataReader() {}
105107

@@ -371,6 +373,10 @@ static void applyMetadata(
371373
options.setEnableAppStartProfiling(
372374
readBool(
373375
metadata, logger, ENABLE_APP_START_PROFILING, options.isEnableAppStartProfiling()));
376+
377+
options.setEnableScopePersistence(
378+
readBool(
379+
metadata, logger, ENABLE_SCOPE_PERSISTENCE, options.isEnableScopePersistence()));
374380
}
375381

376382
options

sentry-android-core/src/test/java/io/sentry/android/core/AndroidOptionsInitializerTest.kt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -653,4 +653,11 @@ class AndroidOptionsInitializerTest {
653653
fixture.sentryOptions.integrations.firstOrNull { it is AnrIntegration }
654654
assertNull(anrv1Integration)
655655
}
656+
657+
@Test
658+
fun `PersistingScopeObserver is not set to options, if scope persistence is disabled`() {
659+
fixture.initSut(configureOptions = { isEnableScopePersistence = false })
660+
661+
assertTrue { fixture.sentryOptions.scopeObservers.none { it is PersistingScopeObserver } }
662+
}
656663
}

sentry-android-core/src/test/java/io/sentry/android/core/ManifestMetadataReaderTest.kt

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1370,4 +1370,29 @@ class ManifestMetadataReaderTest {
13701370
// Assert
13711371
assertFalse(fixture.options.isEnableAppStartProfiling)
13721372
}
1373+
1374+
@Test
1375+
fun `applyMetadata reads enableScopePersistence flag to options`() {
1376+
// Arrange
1377+
val bundle = bundleOf(ManifestMetadataReader.ENABLE_SCOPE_PERSISTENCE to false)
1378+
val context = fixture.getContext(metaData = bundle)
1379+
1380+
// Act
1381+
ManifestMetadataReader.applyMetadata(context, fixture.options, fixture.buildInfoProvider)
1382+
1383+
// Assert
1384+
assertFalse(fixture.options.isEnableScopePersistence)
1385+
}
1386+
1387+
@Test
1388+
fun `applyMetadata reads enableScopePersistence flag to options and keeps default if not found`() {
1389+
// Arrange
1390+
val context = fixture.getContext()
1391+
1392+
// Act
1393+
ManifestMetadataReader.applyMetadata(context, fixture.options, fixture.buildInfoProvider)
1394+
1395+
// Assert
1396+
assertTrue(fixture.options.isEnableScopePersistence)
1397+
}
13731398
}

sentry/api/sentry.api

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2244,6 +2244,7 @@ public class io/sentry/SentryOptions {
22442244
public fun isEnableDeduplication ()Z
22452245
public fun isEnableExternalConfiguration ()Z
22462246
public fun isEnablePrettySerializationOutput ()Z
2247+
public fun isEnableScopePersistence ()Z
22472248
public fun isEnableShutdownHook ()Z
22482249
public fun isEnableSpotlight ()Z
22492250
public fun isEnableTimeToFullDisplayTracing ()Z
@@ -2284,6 +2285,7 @@ public class io/sentry/SentryOptions {
22842285
public fun setEnableDeduplication (Z)V
22852286
public fun setEnableExternalConfiguration (Z)V
22862287
public fun setEnablePrettySerializationOutput (Z)V
2288+
public fun setEnableScopePersistence (Z)V
22872289
public fun setEnableShutdownHook (Z)V
22882290
public fun setEnableSpotlight (Z)V
22892291
public fun setEnableTimeToFullDisplayTracing (Z)V

sentry/src/main/java/io/sentry/SentryOptions.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -448,6 +448,9 @@ public class SentryOptions {
448448

449449
private @Nullable String spotlightConnectionUrl;
450450

451+
/** Whether to enable scope persistence so the scope values are preserved if the process dies */
452+
private boolean enableScopePersistence = true;
453+
451454
/** Contains a list of monitor slugs for which check-ins should not be sent. */
452455
@ApiStatus.Experimental private @Nullable List<String> ignoredCheckIns = null;
453456

@@ -2313,6 +2316,14 @@ public void setEnableSpotlight(final boolean enableSpotlight) {
23132316
this.enableSpotlight = enableSpotlight;
23142317
}
23152318

2319+
public boolean isEnableScopePersistence() {
2320+
return enableScopePersistence;
2321+
}
2322+
2323+
public void setEnableScopePersistence(final boolean enableScopePersistence) {
2324+
this.enableScopePersistence = enableScopePersistence;
2325+
}
2326+
23162327
/** The BeforeSend callback */
23172328
public interface BeforeSendCallback {
23182329

sentry/src/test/java/io/sentry/SentryOptionsTest.kt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -603,4 +603,9 @@ class SentryOptionsTest {
603603
assertTrue(options.isEnableSpotlight)
604604
assertEquals("http://localhost:8080", options.spotlightConnectionUrl)
605605
}
606+
607+
@Test
608+
fun `when options are initialized, enableScopePersistence is set to true by default`() {
609+
assertEquals(true, SentryOptions().isEnableScopePersistence)
610+
}
606611
}

0 commit comments

Comments
 (0)