Skip to content

fix(init): get Application from Context to register integrations #4355

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

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@
- Set `-Dio.opentelemetry.context.contextStorageProvider=io.sentry.opentelemetry.SentryContextStorageProvider` on your `java` command
- Sentry will then wrap the other `ContextStorageProvider` that has been configured by loading it through SPI
- If no other `ContextStorageProvider` is available or there are problems loading it, we fall back to using `SentryOtelThreadLocalStorage`

- Fallback to `context.applicationContext` if `Sentry.init(context)` is not instance of Application ([#4355](https://github.com/getsentry/sentry-java/pull/4355))
Copy link
Contributor

Choose a reason for hiding this comment

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

  • 🚫 The changelog entry seems to be part of an already released section ## 8.10.0.
    Consider moving the entry to the ## Unreleased section, please.


### Fixes

- Update profile chunk rate limit and client report ([#4353](https://github.com/getsentry/sentry-java/pull/4353))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -357,23 +357,23 @@ static void installDefaultIntegrations(
// it to set the replayId in case of an ANR
options.addIntegration(AnrIntegrationFactory.create(context, buildInfoProvider));

// registerActivityLifecycleCallbacks is only available if Context is an AppContext
if (context instanceof Application) {
// registerActivityLifecycleCallbacks is only available on AppContext
if (ContextUtils.getApplicationContext(context) instanceof Application) {
final Application application = (Application) ContextUtils.getApplicationContext(context);
options.addIntegration(
new ActivityLifecycleIntegration(
(Application) context, buildInfoProvider, activityFramesTracker));
options.addIntegration(new ActivityBreadcrumbsIntegration((Application) context));
options.addIntegration(new CurrentActivityIntegration((Application) context));
options.addIntegration(new UserInteractionIntegration((Application) context, loadClass));
new ActivityLifecycleIntegration(application, buildInfoProvider, activityFramesTracker));
options.addIntegration(new ActivityBreadcrumbsIntegration(application));
options.addIntegration(new CurrentActivityIntegration(application));
options.addIntegration(new UserInteractionIntegration(application, loadClass));
if (isFragmentAvailable) {
options.addIntegration(new FragmentLifecycleIntegration((Application) context, true, true));
options.addIntegration(new FragmentLifecycleIntegration(application, true, true));
}
} else {
options
.getLogger()
.log(
SentryLevel.WARNING,
"ActivityLifecycle, FragmentLifecycle and UserInteraction Integrations need an Application class to be installed.");
"ActivityLifecycle, FragmentLifecycle and UserInteraction Integrations need context or getApplicationContext() to be an Application class to be installed.");
}

if (isTimberAvailable) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,18 +70,21 @@ class AndroidOptionsInitializerTest {
useRealContext: Boolean = false,
configureOptions: SentryAndroidOptions.() -> Unit = {},
configureContext: Context.() -> Unit = {},
assets: AssetManager? = null
assets: AssetManager? = null,
customMockContext: Context? = null,
isFragmentAvailable: Boolean = false
) {
sentryOptions.executorService = ImmediateExecutorService()
mockContext = if (metadata != null) {
ContextUtilsTestHelper.mockMetaData(
mockContext = ContextUtilsTestHelper.createMockContext(hasAppContext),
metaData = metadata,
assets = assets
)
} else {
ContextUtilsTestHelper.createMockContext(hasAppContext)
}
mockContext = customMockContext
?: if (metadata != null) {
ContextUtilsTestHelper.mockMetaData(
mockContext = ContextUtilsTestHelper.createMockContext(hasAppContext),
metaData = metadata,
assets = assets
)
} else {
ContextUtilsTestHelper.createMockContext(hasAppContext)
}
whenever(mockContext.cacheDir).thenReturn(file)
if (mockContext.applicationContext != null) {
whenever(mockContext.applicationContext.cacheDir).thenReturn(file)
Expand All @@ -101,7 +104,7 @@ class AndroidOptionsInitializerTest {
BuildInfoProvider(AndroidLogger()),
loadClass,
activityFramesTracker,
false,
isFragmentAvailable,
false,
false
)
Expand Down Expand Up @@ -880,4 +883,24 @@ class AndroidOptionsInitializerTest {
assertFalse { fixture.sentryOptions.socketTagger is AndroidSocketTagger }
assertFalse { fixture.sentryOptions.compositePerformanceCollector is DefaultCompositePerformanceCollector }
}

@Test
fun `When given context which is not instance of Application use applicationContext to initialize activityLifecycleCallbacks integrations`() {
fixture.initSut(isFragmentAvailable = true)

assertNotNull(fixture.sentryOptions.integrations.firstOrNull { it is ActivityBreadcrumbsIntegration })
assertNotNull(fixture.sentryOptions.integrations.firstOrNull { it is CurrentActivityIntegration })
assertNotNull(fixture.sentryOptions.integrations.firstOrNull { it is UserInteractionIntegration })
assertNotNull(fixture.sentryOptions.integrations.firstOrNull { it is FragmentLifecycleIntegration }) // Only when fragment is available
}

@Test
fun `When given context applicationContext is not App do not initialize activityLifecycleCallbacks integrations`() {
fixture.initSut(customMockContext = mock<Context>())

assertNull(fixture.sentryOptions.integrations.firstOrNull { it is ActivityBreadcrumbsIntegration })
assertNull(fixture.sentryOptions.integrations.firstOrNull { it is CurrentActivityIntegration })
assertNull(fixture.sentryOptions.integrations.firstOrNull { it is UserInteractionIntegration })
assertNull(fixture.sentryOptions.integrations.firstOrNull { it is FragmentLifecycleIntegration }) // Only when fragment is available
}
}
Loading