Skip to content

Commit

Permalink
Add enableScopePersistence option (#3218)
Browse files Browse the repository at this point in the history
* 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>
  • Loading branch information
romtsn and markushi authored Feb 22, 2024
1 parent c34791a commit 56f2a8a
Show file tree
Hide file tree
Showing 8 changed files with 64 additions and 1 deletion.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,8 @@ final class ManifestMetadataReader {

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

static final String ENABLE_SCOPE_PERSISTENCE = "io.sentry.enable-scope-persistence";

/** ManifestMetadataReader ctor */
private ManifestMetadataReader() {}

Expand Down Expand Up @@ -371,6 +373,10 @@ static void applyMetadata(
options.setEnableAppStartProfiling(
readBool(
metadata, logger, ENABLE_APP_START_PROFILING, options.isEnableAppStartProfiling()));

options.setEnableScopePersistence(
readBool(
metadata, logger, ENABLE_SCOPE_PERSISTENCE, options.isEnableScopePersistence()));
}

options
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 } }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -1370,4 +1370,29 @@ class ManifestMetadataReaderTest {
// Assert
assertFalse(fixture.options.isEnableAppStartProfiling)
}

@Test
fun `applyMetadata reads enableScopePersistence flag to options`() {
// Arrange
val bundle = bundleOf(ManifestMetadataReader.ENABLE_SCOPE_PERSISTENCE to false)
val context = fixture.getContext(metaData = bundle)

// Act
ManifestMetadataReader.applyMetadata(context, fixture.options, fixture.buildInfoProvider)

// Assert
assertFalse(fixture.options.isEnableScopePersistence)
}

@Test
fun `applyMetadata reads enableScopePersistence flag to options and keeps default if not found`() {
// Arrange
val context = fixture.getContext()

// Act
ManifestMetadataReader.applyMetadata(context, fixture.options, fixture.buildInfoProvider)

// Assert
assertTrue(fixture.options.isEnableScopePersistence)
}
}
2 changes: 2 additions & 0 deletions sentry/api/sentry.api
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
11 changes: 11 additions & 0 deletions sentry/src/main/java/io/sentry/SentryOptions.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<String> ignoredCheckIns = null;

Expand Down Expand Up @@ -2313,6 +2316,14 @@ public void setEnableSpotlight(final boolean enableSpotlight) {
this.enableSpotlight = enableSpotlight;
}

public boolean isEnableScopePersistence() {
return enableScopePersistence;
}

public void setEnableScopePersistence(final boolean enableScopePersistence) {
this.enableScopePersistence = enableScopePersistence;
}

/** The BeforeSend callback */
public interface BeforeSendCallback {

Expand Down
5 changes: 5 additions & 0 deletions sentry/src/test/java/io/sentry/SentryOptionsTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}

0 comments on commit 56f2a8a

Please sign in to comment.