-
-
Notifications
You must be signed in to change notification settings - Fork 464
feat(android): Add log flushing on app backgrounding #4873
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
base: main
Are you sure you want to change the base?
Changes from all commits
415bd82
5f14ca0
04d209c
7448805
2f205b3
0bd817c
2fa3dc3
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,49 @@ | ||
| package io.sentry.android.core; | ||
|
|
||
| import io.sentry.Scopes; | ||
| import io.sentry.SentryLevel; | ||
| import io.sentry.logger.LoggerApi; | ||
| import io.sentry.logger.LoggerBatchProcessor; | ||
| import java.io.Closeable; | ||
| import java.io.IOException; | ||
| import org.jetbrains.annotations.ApiStatus; | ||
| import org.jetbrains.annotations.NotNull; | ||
|
|
||
| @ApiStatus.Internal | ||
| public class AndroidLoggerApi extends LoggerApi implements Closeable, AppState.AppStateListener { | ||
|
|
||
| public AndroidLoggerApi(final @NotNull Scopes scopes) { | ||
| super(scopes); | ||
| AppState.getInstance().addAppStateListener(this); | ||
| } | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Bug: Forked scopes accumulate listeners causing memory leakEvery Additional Locations (1)
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah that's a valid point, although not a concern in globalhub mode. @romtsn I'm wondering if we should completely decouple the flush-triggering logic from logger(api), as the changes blew up quite a bit. We could move it to |
||
|
|
||
| @Override | ||
| @ApiStatus.Internal | ||
| public void close() throws IOException { | ||
| AppState.getInstance().removeAppStateListener(this); | ||
| } | ||
|
|
||
| @Override | ||
| public void onForeground() {} | ||
|
|
||
| @Override | ||
| public void onBackground() { | ||
| try { | ||
| scopes | ||
| .getOptions() | ||
| .getExecutorService() | ||
| .submit( | ||
| new Runnable() { | ||
| @Override | ||
| public void run() { | ||
| scopes.getClient().flushLogs(LoggerBatchProcessor.FLUSH_AFTER_MS); | ||
| } | ||
| }); | ||
| } catch (Throwable t) { | ||
| scopes | ||
| .getOptions() | ||
| .getLogger() | ||
| .log(SentryLevel.ERROR, t, "Failed to submit log flush runnable"); | ||
| } | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| package io.sentry.android.core; | ||
|
|
||
| import io.sentry.Scopes; | ||
| import io.sentry.logger.ILoggerApiFactory; | ||
| import io.sentry.logger.LoggerApi; | ||
| import org.jetbrains.annotations.ApiStatus; | ||
| import org.jetbrains.annotations.NotNull; | ||
|
|
||
| @ApiStatus.Internal | ||
| public final class AndroidLoggerApiFactory implements ILoggerApiFactory { | ||
|
|
||
| @Override | ||
| @NotNull | ||
| public LoggerApi create(@NotNull Scopes scopes) { | ||
| return new AndroidLoggerApi(scopes); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -45,7 +45,8 @@ public void register(final @NotNull IScopes scopes, final @NotNull SentryOptions | |
| this.options.isEnableAppLifecycleBreadcrumbs()); | ||
|
|
||
| if (this.options.isEnableAutoSessionTracking() | ||
| || this.options.isEnableAppLifecycleBreadcrumbs()) { | ||
| || this.options.isEnableAppLifecycleBreadcrumbs() | ||
| || this.options.getLogs().isEnabled()) { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Bug: Unnecessary LifecycleWatcher created when only logs enabledThe condition |
||
| try (final ISentryLifecycleToken ignored = lock.acquire()) { | ||
| if (watcher != null) { | ||
| return; | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| package io.sentry.android.core | ||
|
|
||
| import androidx.test.ext.junit.runners.AndroidJUnit4 | ||
| import io.sentry.Scopes | ||
| import kotlin.test.assertIs | ||
| import org.junit.Test | ||
| import org.junit.runner.RunWith | ||
| import org.mockito.kotlin.mock | ||
|
|
||
| @RunWith(AndroidJUnit4::class) | ||
| class AndroidLoggerApiFactoryTest { | ||
|
|
||
| @Test | ||
| fun `factory creates AndroidLogger`() { | ||
| val factory = AndroidLoggerApiFactory() | ||
| val logger = factory.create(mock<Scopes>()) | ||
| assertIs<AndroidLoggerApi>(logger) | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,51 @@ | ||
| package io.sentry.android.core | ||
|
|
||
| import androidx.test.ext.junit.runners.AndroidJUnit4 | ||
| import io.sentry.Scopes | ||
| import io.sentry.SentryClient | ||
| import io.sentry.SentryOptions | ||
| import io.sentry.test.ImmediateExecutorService | ||
| import kotlin.test.assertTrue | ||
| import org.junit.Before | ||
| import org.junit.Test | ||
| import org.junit.runner.RunWith | ||
| import org.mockito.kotlin.any | ||
| import org.mockito.kotlin.mock | ||
| import org.mockito.kotlin.verify | ||
| import org.mockito.kotlin.whenever | ||
|
|
||
| @RunWith(AndroidJUnit4::class) | ||
| class AndroidLoggerApiTest { | ||
|
|
||
| @Before | ||
| fun setup() { | ||
| AppState.getInstance().resetInstance() | ||
| } | ||
|
|
||
| @Test | ||
| fun `AndroidLogger registers and unregisters app state listener`() { | ||
| val scopes = mock<Scopes>() | ||
| val logger = AndroidLoggerApi(scopes) | ||
| assertTrue(AppState.getInstance().lifecycleObserver.listeners.isNotEmpty()) | ||
|
|
||
| logger.close() | ||
| assertTrue(AppState.getInstance().lifecycleObserver.listeners.isEmpty()) | ||
| } | ||
|
|
||
| @Test | ||
| fun `AndroidLogger triggers flushing if app goes in background`() { | ||
| val scopes = mock<Scopes>() | ||
|
|
||
| val client = mock<SentryClient>() | ||
| whenever(scopes.client).thenReturn(client) | ||
|
|
||
| val options = SentryOptions() | ||
| options.executorService = ImmediateExecutorService() | ||
| whenever(scopes.options).thenReturn(options) | ||
|
|
||
| val logger = AndroidLoggerApi(scopes) | ||
| logger.onBackground() | ||
|
|
||
| verify(client).flushLogs(any()) | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -47,6 +47,8 @@ public interface ISentryClient { | |
| */ | ||
| void flush(long timeoutMillis); | ||
|
|
||
| void flushLogs(long timeoutMillis); | ||
|
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not the biggest fan of adding a new API here, an alternative would be to move this directly into
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. if we're not going for https://github.com/getsentry/sentry-java/pull/4873/files#r2581483713, then I think this makes sense to make it cleaner and bake it into ILoggerApi instead |
||
|
|
||
| /** | ||
| * Captures the event. | ||
| * | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
maybe this should go above the previous entry, because it doesn't look right when rendered:
