-
-
Notifications
You must be signed in to change notification settings - Fork 472
feat(extend-app-start): [2/4] Extract AppStartExtension component #5606
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
buenaflor
wants to merge
13
commits into
feat/app-start-extension-core
Choose a base branch
from
feat/app-start-extension-android
base: feat/app-start-extension-core
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
7dfe617
feat(extend-app-start): Extract AppStartExtension component for the Aโฆ
buenaflor 4d0c97d
refactor(extend-app-start): Inject logger into AppStartExtension instโฆ
buenaflor b5ac7f5
refactor(extend-app-start): Return ExtendedAppStart from the listenerโฆ
buenaflor 81efded
chore(extend-app-start): Remove redundant comments and rename reset tโฆ
buenaflor 3cc743e
refactor(extend-app-start): Inline the logger lookup instead of injecโฆ
buenaflor 1dafb9b
chore(extend-app-start): Drop the class and listener javadocs on AppSโฆ
buenaflor 8548b4d
chore(extend-app-start): Trim finishTransaction/getExtendedEndTime coโฆ
buenaflor 01b1dc4
refactor(extend-app-start): Rename AppStartExtension.finishAppStart tโฆ
buenaflor 05a9df2
chore(extend-app-start): Drop the redundant foreground-check comment โฆ
buenaflor 5830303
test(extend-app-start): Drop the no-value getAppStartExtension test aโฆ
buenaflor ec4da6c
test(extend-app-start): Drop the no-op finishExtendedAppStart test
buenaflor d2c16a2
fix(extend-app-start): Read the extended span finish date, not its fiโฆ
buenaflor b6dd76e
fix(extend-app-start): End the extended transaction at the extended sโฆ
buenaflor File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
149 changes: 149 additions & 0 deletions
149
sentry-android-core/src/main/java/io/sentry/android/core/AppStartExtension.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,149 @@ | ||
| package io.sentry.android.core; | ||
|
|
||
| import io.sentry.IAppStartExtender; | ||
| import io.sentry.ISentryLifecycleToken; | ||
| import io.sentry.ISpan; | ||
| import io.sentry.ITransaction; | ||
| import io.sentry.NoOpSpan; | ||
| import io.sentry.Sentry; | ||
| import io.sentry.SentryDate; | ||
| import io.sentry.SentryLevel; | ||
| import io.sentry.SpanStatus; | ||
| import io.sentry.android.core.performance.AppStartMetrics; | ||
| import io.sentry.util.AutoClosableReentrantLock; | ||
| import org.jetbrains.annotations.ApiStatus; | ||
| import org.jetbrains.annotations.NotNull; | ||
| import org.jetbrains.annotations.Nullable; | ||
|
|
||
| @ApiStatus.Internal | ||
| public final class AppStartExtension implements IAppStartExtender { | ||
|
|
||
| public static final class ExtendedAppStart { | ||
| public final @NotNull ITransaction transaction; | ||
| public final @NotNull ISpan span; | ||
|
|
||
| public ExtendedAppStart(final @NotNull ITransaction transaction, final @NotNull ISpan span) { | ||
| this.transaction = transaction; | ||
| this.span = span; | ||
| } | ||
| } | ||
|
|
||
| public interface ExtendAppStartListener { | ||
| @Nullable | ||
| ExtendedAppStart onExtendAppStartRequested(); | ||
| } | ||
|
|
||
| private final @NotNull AppStartMetrics metrics; | ||
| private final @NotNull AutoClosableReentrantLock lock = new AutoClosableReentrantLock(); | ||
|
|
||
| private @Nullable ExtendAppStartListener extendAppStartListener; | ||
| private @Nullable ISpan extendedSpan; | ||
| private @Nullable ITransaction extendedTransaction; | ||
|
|
||
| public AppStartExtension(final @NotNull AppStartMetrics metrics) { | ||
| this.metrics = metrics; | ||
| } | ||
|
|
||
| public void setExtendAppStartListener(final @Nullable ExtendAppStartListener listener) { | ||
| this.extendAppStartListener = listener; | ||
| } | ||
|
Comment on lines
+47
to
+49
|
||
|
|
||
| @Override | ||
| public void extendAppStart() { | ||
| try (final @NotNull ISentryLifecycleToken ignored = lock.acquire()) { | ||
| if (extendedSpan != null) { | ||
| Sentry.getCurrentScopes() | ||
| .getOptions() | ||
| .getLogger() | ||
| .log(SentryLevel.WARNING, "App start is already being extended."); | ||
| return; | ||
| } | ||
| if (!metrics.isAppStartWindowOpen()) { | ||
| Sentry.getCurrentScopes() | ||
| .getOptions() | ||
| .getLogger() | ||
| .log( | ||
| SentryLevel.WARNING, | ||
| "Cannot extend app start: the app start window has already passed."); | ||
| return; | ||
| } | ||
| final @Nullable ExtendAppStartListener listener = extendAppStartListener; | ||
| if (listener != null) { | ||
| final @Nullable ExtendedAppStart extended = listener.onExtendAppStartRequested(); | ||
| if (extended != null) { | ||
| this.extendedTransaction = extended.transaction; | ||
| this.extendedSpan = extended.span; | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| public void finishExtendedAppStart() { | ||
| try (final @NotNull ISentryLifecycleToken ignored = lock.acquire()) { | ||
| final @Nullable ISpan span = extendedSpan; | ||
| if (span != null && !span.isFinished()) { | ||
| span.finish(SpanStatus.OK); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| public @NotNull ISpan getExtendedAppStartSpan() { | ||
| try (final @NotNull ISentryLifecycleToken ignored = lock.acquire()) { | ||
| final @Nullable ISpan span = extendedSpan; | ||
| if (span != null && !span.isFinished()) { | ||
| return span; | ||
| } | ||
| return NoOpSpan.getInstance(); | ||
| } | ||
| } | ||
|
|
||
| public boolean isActive() { | ||
| try (final @NotNull ISentryLifecycleToken ignored = lock.acquire()) { | ||
| return extendedTransaction != null && !extendedTransaction.isFinished(); | ||
| } | ||
| } | ||
|
|
||
| public void finishTransaction(final @NotNull SentryDate endTimestamp) { | ||
|
Contributor
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. this will be used in the next PR 3/4 |
||
| try (final @NotNull ISentryLifecycleToken ignored = lock.acquire()) { | ||
| final @Nullable ITransaction transaction = extendedTransaction; | ||
| if (transaction != null && !transaction.isFinished()) { | ||
| // If the extended span already finished after endTimestamp, end the transaction there so it | ||
| // contains the extended span and its duration matches the reported app start vital. When | ||
| // the | ||
| // span is still open, waitForChildren keeps the transaction open until it finishes. | ||
| final @Nullable ISpan span = extendedSpan; | ||
| final @Nullable SentryDate spanEnd = span == null ? null : span.getFinishDate(); | ||
| final @NotNull SentryDate end = | ||
| spanEnd != null && spanEnd.isAfter(endTimestamp) ? spanEnd : endTimestamp; | ||
| transaction.finish(SpanStatus.OK, end); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| public @Nullable SentryDate getExtendedEndTime() { | ||
| try (final @NotNull ISentryLifecycleToken ignored = lock.acquire()) { | ||
| final @Nullable ISpan span = extendedSpan; | ||
| if (span == null) { | ||
| return null; | ||
| } | ||
| // A deadline timeout would report an artificially inflated duration; suppress the vital | ||
| // instead. | ||
| if (span.getStatus() == SpanStatus.DEADLINE_EXCEEDED) { | ||
| return null; | ||
| } | ||
| // Read the finish date, not isFinished(): finishing the extended span completes the | ||
| // waitForChildren transaction and runs the event processor re-entrantly before the span's | ||
| // finished flag is set, but the finish timestamp is already in place. Null until finished. | ||
| return span.getFinishDate(); | ||
| } | ||
| } | ||
|
|
||
| public void clear() { | ||
| try (final @NotNull ISentryLifecycleToken ignored = lock.acquire()) { | ||
| extendedSpan = null; | ||
| extendedTransaction = null; | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.