Skip to content

Add enableScopePersistence option #3218

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’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Feb 22, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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
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(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)
}
}