|
| 1 | +package io.sentry.android.core; |
| 2 | + |
| 3 | +import io.sentry.IAppStartExtender; |
| 4 | +import io.sentry.ILogger; |
| 5 | +import io.sentry.ISentryLifecycleToken; |
| 6 | +import io.sentry.ISpan; |
| 7 | +import io.sentry.ITransaction; |
| 8 | +import io.sentry.NoOpSpan; |
| 9 | +import io.sentry.Sentry; |
| 10 | +import io.sentry.SentryDate; |
| 11 | +import io.sentry.SentryLevel; |
| 12 | +import io.sentry.SpanStatus; |
| 13 | +import io.sentry.android.core.performance.AppStartMetrics; |
| 14 | +import io.sentry.util.AutoClosableReentrantLock; |
| 15 | +import org.jetbrains.annotations.ApiStatus; |
| 16 | +import org.jetbrains.annotations.NotNull; |
| 17 | +import org.jetbrains.annotations.Nullable; |
| 18 | + |
| 19 | +/** |
| 20 | + * Owns the lifecycle of an extended app start. Created and held by {@link AppStartMetrics}, it |
| 21 | + * keeps the new "extend app start" concern out of that already-large class. |
| 22 | + * |
| 23 | + * <p>Both the eager standalone App Start {@link ITransaction} and its extended child {@link ISpan} |
| 24 | + * are created by the integration (which has access to scopes) and handed back here via {@link |
| 25 | + * #onExtended(ITransaction, ISpan)}. This component owns them from then on: it never stores them in |
| 26 | + * the integration's shared transaction field, so the per-activity cleanup can never cancel an |
| 27 | + * eagerly-created extension. |
| 28 | + */ |
| 29 | +@ApiStatus.Internal |
| 30 | +public final class AppStartExtension implements IAppStartExtender { |
| 31 | + |
| 32 | + /** |
| 33 | + * Notifies the integration that an extension was requested. The integration creates the |
| 34 | + * standalone App Start transaction + extended child span (it has scopes) and hands them back via |
| 35 | + * {@link #onExtended(ITransaction, ISpan)}. When no listener is registered (e.g. standalone |
| 36 | + * tracing is disabled), {@link #extendAppStart()} is inert and the whole API stays a no-op. |
| 37 | + */ |
| 38 | + public interface ExtendAppStartListener { |
| 39 | + void onExtendAppStartRequested(); |
| 40 | + } |
| 41 | + |
| 42 | + private final @NotNull AppStartMetrics metrics; |
| 43 | + private final @NotNull AutoClosableReentrantLock lock = new AutoClosableReentrantLock(); |
| 44 | + |
| 45 | + private @Nullable ExtendAppStartListener extendAppStartListener; |
| 46 | + private @Nullable ISpan extendedSpan; |
| 47 | + private @Nullable ITransaction extendedTransaction; |
| 48 | + |
| 49 | + public AppStartExtension(final @NotNull AppStartMetrics metrics) { |
| 50 | + this.metrics = metrics; |
| 51 | + } |
| 52 | + |
| 53 | + public void setExtendAppStartListener(final @Nullable ExtendAppStartListener listener) { |
| 54 | + this.extendAppStartListener = listener; |
| 55 | + } |
| 56 | + |
| 57 | + @Override |
| 58 | + public void extendAppStart() { |
| 59 | + try (final @NotNull ISentryLifecycleToken ignored = lock.acquire()) { |
| 60 | + if (extendedSpan != null) { |
| 61 | + getLogger().log(SentryLevel.WARNING, "App start is already being extended."); |
| 62 | + return; |
| 63 | + } |
| 64 | + // Ignore the foreground check: headless app starts (broadcast/service) run in a |
| 65 | + // non-foreground process but can still be extended. The window gate still rejects an |
| 66 | + // extension once an activity was created, the first frame was drawn, or measurements were |
| 67 | + // already sent. |
| 68 | + if (!metrics.isAppStartWindowOpen()) { |
| 69 | + getLogger() |
| 70 | + .log( |
| 71 | + SentryLevel.WARNING, |
| 72 | + "Cannot extend app start: the app start window has already passed."); |
| 73 | + return; |
| 74 | + } |
| 75 | + final @Nullable ExtendAppStartListener listener = extendAppStartListener; |
| 76 | + if (listener != null) { |
| 77 | + listener.onExtendAppStartRequested(); |
| 78 | + } |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + /** |
| 83 | + * Hands the eagerly-created standalone App Start transaction and its extended child span over to |
| 84 | + * this component, which owns them from now on. Called synchronously by the integration while |
| 85 | + * handling {@link ExtendAppStartListener#onExtendAppStartRequested()}. |
| 86 | + */ |
| 87 | + public void onExtended( |
| 88 | + final @NotNull ITransaction transaction, final @NotNull ISpan extendedSpan) { |
| 89 | + try (final @NotNull ISentryLifecycleToken ignored = lock.acquire()) { |
| 90 | + this.extendedTransaction = transaction; |
| 91 | + this.extendedSpan = extendedSpan; |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + @Override |
| 96 | + public void finishAppStart() { |
| 97 | + try (final @NotNull ISentryLifecycleToken ignored = lock.acquire()) { |
| 98 | + final @Nullable ISpan span = extendedSpan; |
| 99 | + if (span != null && !span.isFinished()) { |
| 100 | + span.finish(SpanStatus.OK); |
| 101 | + } |
| 102 | + } |
| 103 | + } |
| 104 | + |
| 105 | + @Override |
| 106 | + public @NotNull ISpan getExtendedAppStartSpan() { |
| 107 | + try (final @NotNull ISentryLifecycleToken ignored = lock.acquire()) { |
| 108 | + final @Nullable ISpan span = extendedSpan; |
| 109 | + if (span != null && !span.isFinished()) { |
| 110 | + return span; |
| 111 | + } |
| 112 | + return NoOpSpan.getInstance(); |
| 113 | + } |
| 114 | + } |
| 115 | + |
| 116 | + /** Whether an eagerly-created extension transaction exists and has not finished yet. */ |
| 117 | + public boolean isActive() { |
| 118 | + try (final @NotNull ISentryLifecycleToken ignored = lock.acquire()) { |
| 119 | + return extendedTransaction != null && !extendedTransaction.isFinished(); |
| 120 | + } |
| 121 | + } |
| 122 | + |
| 123 | + /** |
| 124 | + * Finishes the owned transaction at the natural app start end (first frame, or the headless stop |
| 125 | + * time). {@code waitForChildren} holds the transaction open until the extended span finishes, so |
| 126 | + * the app start vital is never captured before this point. Idempotent. |
| 127 | + */ |
| 128 | + public void finishTransaction(final @NotNull SentryDate endTimestamp) { |
| 129 | + try (final @NotNull ISentryLifecycleToken ignored = lock.acquire()) { |
| 130 | + final @Nullable ITransaction transaction = extendedTransaction; |
| 131 | + if (transaction != null && !transaction.isFinished()) { |
| 132 | + transaction.finish(SpanStatus.OK, endTimestamp); |
| 133 | + } |
| 134 | + } |
| 135 | + } |
| 136 | + |
| 137 | + /** |
| 138 | + * The effective end of the extended app start, used to extend the app start vital. Returns {@code |
| 139 | + * null} when no extension finished, or when it finished via the deadline timeout - in the latter |
| 140 | + * case the vital is suppressed instead of reporting an artificially inflated duration. |
| 141 | + */ |
| 142 | + public @Nullable SentryDate getExtendedEndTime() { |
| 143 | + try (final @NotNull ISentryLifecycleToken ignored = lock.acquire()) { |
| 144 | + final @Nullable ISpan span = extendedSpan; |
| 145 | + if (span == null || !span.isFinished()) { |
| 146 | + return null; |
| 147 | + } |
| 148 | + if (span.getStatus() == SpanStatus.DEADLINE_EXCEEDED) { |
| 149 | + return null; |
| 150 | + } |
| 151 | + return span.getFinishDate(); |
| 152 | + } |
| 153 | + } |
| 154 | + |
| 155 | + /** |
| 156 | + * Resets the per-start state so a stale extension can't affect a later (e.g. warm) app start. The |
| 157 | + * registered listener is intentionally kept: it is registered once at SDK init and must survive |
| 158 | + * across app starts. |
| 159 | + */ |
| 160 | + public void reset() { |
| 161 | + try (final @NotNull ISentryLifecycleToken ignored = lock.acquire()) { |
| 162 | + extendedSpan = null; |
| 163 | + extendedTransaction = null; |
| 164 | + } |
| 165 | + } |
| 166 | + |
| 167 | + private static @NotNull ILogger getLogger() { |
| 168 | + return Sentry.getCurrentScopes().getOptions().getLogger(); |
| 169 | + } |
| 170 | +} |
0 commit comments