Skip to content
Original file line number Diff line number Diff line change
Expand Up @@ -471,14 +471,6 @@ private static void readDefaultOptionValues(
options.addInAppInclude(packageName);
}
}

if (options.getDistinctId() == null) {
try {
options.setDistinctId(Installation.id(context));
} catch (RuntimeException e) {
options.getLogger().log(SentryLevel.ERROR, "Could not generate distinct Id.", e);
}
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,14 @@ public static void init(
"Error in the 'OptionsConfiguration.configure' callback.",
t);
}
if (options.getDistinctId() == null

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Moving this here no longer allows customers to override it in the options configuration callback passed to init since it used to run before but now is set afterwards. It can still be removed via beforeSend. Is that OK?

&& options.getDataCollectionResolver().isUserInfoWithLegacyAlways()) {
try {
options.setDistinctId(Installation.id(context));
} catch (RuntimeException e) {
options.getLogger().log(SentryLevel.ERROR, "Could not generate distinct Id.", e);
}
}

// if SentryPerformanceProvider was disabled or removed,
// we set the app start / sdk init time here instead
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,12 @@ class AndroidOptionsInitializerTest {
)

sentryOptions.configureOptions()
if (
sentryOptions.distinctId == null &&
sentryOptions.dataCollectionResolver.isUserInfoWithLegacyAlways
) {
sentryOptions.distinctId = Installation.id(if (useRealContext) context else mockContext)
}
AndroidOptionsInitializer.initializeIntegrationsAndProcessors(
sentryOptions,
if (useRealContext) context else mockContext,
Expand Down Expand Up @@ -349,6 +355,44 @@ class AndroidOptionsInitializerTest {
installation.deleteOnExit()
}

@Test
fun `init should not set generated distinct id when user info is disabled`() {
fixture.initSut(configureOptions = { dataCollection.setUserInfo(false) })

assertNull(fixture.sentryOptions.distinctId)
}

@Test
fun `init should set generated distinct id when user info is enabled`() {
fixture.initSut(configureOptions = { dataCollection.setUserInfo(true) })

assertNotNull(fixture.sentryOptions.distinctId)
}

@Test
fun `init should preserve explicit distinct id when user info is disabled`() {
fixture.initSut(
configureOptions = {
dataCollection.setUserInfo(false)
distinctId = "custom-id"
}
)

assertEquals("custom-id", fixture.sentryOptions.distinctId)
}

@Test
fun `init should set generated distinct id when explicit value is null`() {
fixture.initSut(
configureOptions = {
dataCollection.setUserInfo(true)
distinctId = null
}
)

assertNotNull(fixture.sentryOptions.distinctId)
}

@Test
fun `init should set proguard uuid id on start`() {
fixture.initSut(
Expand Down
Loading