-
-
Notifications
You must be signed in to change notification settings - Fork 463
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
Changes from 2 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 |
|---|---|---|
|
|
@@ -5,6 +5,7 @@ | |
| import io.sentry.ISentryLifecycleToken; | ||
| import io.sentry.SentryLevel; | ||
| import io.sentry.Session; | ||
| import io.sentry.logger.LoggerBatchProcessor; | ||
| import io.sentry.transport.CurrentDateProvider; | ||
| import io.sentry.transport.ICurrentDateProvider; | ||
| import io.sentry.util.AutoClosableReentrantLock; | ||
|
|
@@ -29,18 +30,22 @@ final class LifecycleWatcher implements AppState.AppStateListener { | |
| private final boolean enableSessionTracking; | ||
| private final boolean enableAppLifecycleBreadcrumbs; | ||
|
|
||
| private final boolean enableLogFlushing; | ||
|
|
||
| private final @NotNull ICurrentDateProvider currentDateProvider; | ||
|
|
||
| LifecycleWatcher( | ||
| final @NotNull IScopes scopes, | ||
| final long sessionIntervalMillis, | ||
| final boolean enableSessionTracking, | ||
| final boolean enableAppLifecycleBreadcrumbs) { | ||
| final boolean enableAppLifecycleBreadcrumbs, | ||
| final boolean enableLogFlushing) { | ||
| this( | ||
| scopes, | ||
| sessionIntervalMillis, | ||
| enableSessionTracking, | ||
| enableAppLifecycleBreadcrumbs, | ||
| enableLogFlushing, | ||
| CurrentDateProvider.getInstance()); | ||
| } | ||
|
|
||
|
|
@@ -49,10 +54,12 @@ final class LifecycleWatcher implements AppState.AppStateListener { | |
| final long sessionIntervalMillis, | ||
| final boolean enableSessionTracking, | ||
| final boolean enableAppLifecycleBreadcrumbs, | ||
| final boolean enableLogFlushing, | ||
| final @NotNull ICurrentDateProvider currentDateProvider) { | ||
| this.sessionIntervalMillis = sessionIntervalMillis; | ||
| this.enableSessionTracking = enableSessionTracking; | ||
| this.enableAppLifecycleBreadcrumbs = enableAppLifecycleBreadcrumbs; | ||
| this.enableLogFlushing = enableLogFlushing; | ||
| this.scopes = scopes; | ||
| this.currentDateProvider = currentDateProvider; | ||
| } | ||
|
|
@@ -101,6 +108,29 @@ public void onBackground() { | |
| scheduleEndSession(); | ||
|
|
||
| addAppBreadcrumb("background"); | ||
|
|
||
| if (enableLogFlushing) { | ||
|
||
| try { | ||
| scopes | ||
| .getOptions() | ||
| .getExecutorService() | ||
| .submit( | ||
| new Runnable() { | ||
| @Override | ||
| public void run() { | ||
| scopes | ||
| .getGlobalScope() | ||
|
||
| .getClient() | ||
| .flushLogs(LoggerBatchProcessor.FLUSH_AFTER_MS); | ||
| } | ||
| }); | ||
| } catch (Throwable t) { | ||
| scopes | ||
| .getOptions() | ||
| .getLogger() | ||
| .log(SentryLevel.ERROR, t, "Failed to submit log flush runnable"); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| private void scheduleEndSession() { | ||
|
|
||
| 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.
Bug: Unnecessary LifecycleWatcher created when only logs enabled
The condition
|| this.options.getLogs().isEnabled()was added but theLifecycleWatcherdoesn't handle log flushing - that's done byAndroidLoggerApi. When only logs are enabled (not session tracking or breadcrumbs), this creates an unnecessaryLifecycleWatcherwith aTimerthat performs no useful work since bothenableSessionTrackingandenableAppLifecycleBreadcrumbswill be false.