Skip to content

Commit

Permalink
POTEL 41 - Set InitPriority for Android, Manifest option for forceInit (
Browse files Browse the repository at this point in the history
#3675)

* Support spans across batches

* changelog

* POC use correct options for multi init

* Add settings for controlling init priority

* Set InitPriority for Android, Manifest option for forceInit

* Move auto init priority = low code

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

Co-authored-by: Markus Hintersteiner <[email protected]>

* changelog

---------

Co-authored-by: Markus Hintersteiner <[email protected]>
  • Loading branch information
adinauer and markushi authored Sep 16, 2024
1 parent c58587b commit 9b2603b
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 0 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@

- Add init priority settings ([#3674](https://github.com/getsentry/sentry-java/pull/3674))
- You may now set `forceInit=true` (`force-init` for `.properties` files) to ensure a call to Sentry.init / SentryAndroid.init takes effect
- Add force init option to Android Manifest
- Use `<meta-data android:name="io.sentry.force-init" android:value="true" />` to ensure Sentry Android auto init is not easily overwritten

### Fixes

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import android.content.pm.PackageManager;
import android.os.Bundle;
import io.sentry.ILogger;
import io.sentry.InitPriority;
import io.sentry.SentryIntegrationPackageStorage;
import io.sentry.SentryLevel;
import io.sentry.protocol.SdkVersion;
Expand Down Expand Up @@ -104,6 +105,8 @@ final class ManifestMetadataReader {

static final String ENABLE_METRICS = "io.sentry.enable-metrics";

static final String FORCE_INIT = "io.sentry.force-init";

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

Expand Down Expand Up @@ -263,6 +266,13 @@ static void applyMetadata(
options.setSendClientReports(
readBool(metadata, logger, CLIENT_REPORTS_ENABLE, options.isSendClientReports()));

final boolean isAutoInitEnabled = readBool(metadata, logger, AUTO_INIT, true);
if (isAutoInitEnabled) {
options.setInitPriority(InitPriority.LOW);
}

options.setForceInit(readBool(metadata, logger, FORCE_INIT, options.isForceInit()));

options.setCollectAdditionalContext(
readBool(
metadata,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1420,4 +1420,29 @@ class ManifestMetadataReaderTest {
// Assert
assertFalse(fixture.options.isEnableMetrics)
}

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

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

// Assert
assertTrue(fixture.options.isForceInit)
}

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

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

// Assert
assertFalse(fixture.options.isForceInit)
}
}
1 change: 1 addition & 0 deletions sentry/src/main/java/io/sentry/SentryOptions.java
Original file line number Diff line number Diff line change
Expand Up @@ -2593,6 +2593,7 @@ public SentryOptions() {
*/
private SentryOptions(final boolean empty) {
if (!empty) {
setInitPriority(InitPriority.LOWEST);
setSpanFactory(new DefaultSpanFactory());
// SentryExecutorService should be initialized before any
// SendCachedEventFireAndForgetIntegration
Expand Down

0 comments on commit 9b2603b

Please sign in to comment.