|
| 1 | +package io.sentry.android.core; |
| 2 | + |
| 3 | +import static io.sentry.util.IntegrationUtils.addIntegrationToSdkVersion; |
| 4 | + |
| 5 | +import android.app.ActivityManager; |
| 6 | +import android.app.ApplicationExitInfo; |
| 7 | +import android.content.Context; |
| 8 | +import android.os.Build; |
| 9 | + |
| 10 | +import androidx.annotation.RequiresApi; |
| 11 | + |
| 12 | +import io.sentry.DateUtils; |
| 13 | +import io.sentry.IScopes; |
| 14 | +import io.sentry.Integration; |
| 15 | +import io.sentry.SentryEvent; |
| 16 | +import io.sentry.SentryLevel; |
| 17 | +import io.sentry.SentryOptions; |
| 18 | +import io.sentry.android.core.internal.tombstone.TombstoneParser; |
| 19 | +import io.sentry.cache.EnvelopeCache; |
| 20 | +import io.sentry.cache.IEnvelopeCache; |
| 21 | +import io.sentry.transport.CurrentDateProvider; |
| 22 | +import io.sentry.transport.ICurrentDateProvider; |
| 23 | +import io.sentry.util.Objects; |
| 24 | + |
| 25 | +import java.io.Closeable; |
| 26 | +import java.io.IOException; |
| 27 | +import java.util.ArrayList; |
| 28 | +import java.util.List; |
| 29 | +import java.util.concurrent.TimeUnit; |
| 30 | + |
| 31 | +import org.jetbrains.annotations.NotNull; |
| 32 | +import org.jetbrains.annotations.Nullable; |
| 33 | + |
| 34 | +public class TombstoneIntegration implements Integration, Closeable { |
| 35 | + static final long NINETY_DAYS_THRESHOLD = TimeUnit.DAYS.toMillis(91); |
| 36 | + |
| 37 | + private final @NotNull Context context; |
| 38 | + private final @NotNull ICurrentDateProvider dateProvider; |
| 39 | + private @Nullable SentryAndroidOptions options; |
| 40 | + |
| 41 | + public TombstoneIntegration(final @NotNull Context context) { |
| 42 | + // using CurrentDateProvider instead of AndroidCurrentDateProvider as AppExitInfo uses |
| 43 | + // System.currentTimeMillis |
| 44 | + this(context, CurrentDateProvider.getInstance()); |
| 45 | + } |
| 46 | + |
| 47 | + TombstoneIntegration( |
| 48 | + final @NotNull Context context, final @NotNull ICurrentDateProvider dateProvider) { |
| 49 | + this.context = ContextUtils.getApplicationContext(context); |
| 50 | + this.dateProvider = dateProvider; |
| 51 | + } |
| 52 | + |
| 53 | + @Override |
| 54 | + public void register(@NotNull IScopes scopes, @NotNull SentryOptions options) { |
| 55 | + this.options = |
| 56 | + Objects.requireNonNull( |
| 57 | + (options instanceof SentryAndroidOptions) ? (SentryAndroidOptions) options : null, |
| 58 | + "SentryAndroidOptions is required"); |
| 59 | + |
| 60 | + this.options |
| 61 | + .getLogger() |
| 62 | + .log(SentryLevel.DEBUG, "TombstoneIntegration enabled: %s", this.options.isTombstonesEnabled()); |
| 63 | + |
| 64 | + if (this.options.isTombstonesEnabled()) { |
| 65 | + if (this.options.getCacheDirPath() == null) { |
| 66 | + this.options |
| 67 | + .getLogger() |
| 68 | + .log(SentryLevel.INFO, "Cache dir is not set, unable to process Tombstones"); |
| 69 | + return; |
| 70 | + } |
| 71 | + |
| 72 | + try { |
| 73 | + options |
| 74 | + .getExecutorService() |
| 75 | + .submit( |
| 76 | + new TombstoneProcessor( |
| 77 | + context, scopes, this.options, dateProvider)); |
| 78 | + } catch (Throwable e) { |
| 79 | + options.getLogger().log(SentryLevel.DEBUG, "Failed to start TombstoneProcessor.", e); |
| 80 | + } |
| 81 | + options.getLogger().log(SentryLevel.DEBUG, "TombstoneIntegration installed."); |
| 82 | + addIntegrationToSdkVersion("Tombstone"); |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + @Override |
| 87 | + public void close() throws IOException { |
| 88 | + if (options != null) { |
| 89 | + options.getLogger().log(SentryLevel.DEBUG, "TombstoneIntegration removed."); |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | + public static class TombstoneProcessor implements Runnable { |
| 94 | + |
| 95 | + @NotNull |
| 96 | + private final Context context; |
| 97 | + @NotNull |
| 98 | + private final IScopes scopes; |
| 99 | + @NotNull |
| 100 | + private final SentryAndroidOptions options; |
| 101 | + private final long threshold; |
| 102 | + |
| 103 | + public TombstoneProcessor( |
| 104 | + @NotNull Context context, |
| 105 | + @NotNull IScopes scopes, |
| 106 | + @NotNull SentryAndroidOptions options, |
| 107 | + @NotNull ICurrentDateProvider dateProvider) { |
| 108 | + this.context = context; |
| 109 | + this.scopes = scopes; |
| 110 | + this.options = options; |
| 111 | + |
| 112 | + this.threshold = dateProvider.getCurrentTimeMillis() - NINETY_DAYS_THRESHOLD; |
| 113 | + } |
| 114 | + |
| 115 | + @Override |
| 116 | + @RequiresApi(api = Build.VERSION_CODES.R) |
| 117 | + public void run() { |
| 118 | + final ActivityManager activityManager = |
| 119 | + (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE); |
| 120 | + |
| 121 | + final List<ApplicationExitInfo> applicationExitInfoList; |
| 122 | + applicationExitInfoList = activityManager.getHistoricalProcessExitReasons(null, 0, 0); |
| 123 | + |
| 124 | + if (applicationExitInfoList.isEmpty()) { |
| 125 | + options.getLogger().log(SentryLevel.DEBUG, "No records in historical exit reasons."); |
| 126 | + return; |
| 127 | + } |
| 128 | + |
| 129 | + final IEnvelopeCache cache = options.getEnvelopeDiskCache(); |
| 130 | + if (cache instanceof EnvelopeCache) { |
| 131 | + if (options.isEnableAutoSessionTracking() |
| 132 | + && !((EnvelopeCache) cache).waitPreviousSessionFlush()) { |
| 133 | + options |
| 134 | + .getLogger() |
| 135 | + .log( |
| 136 | + SentryLevel.WARNING, |
| 137 | + "Timed out waiting to flush previous session to its own file."); |
| 138 | + |
| 139 | + // if we timed out waiting here, we can already flush the latch, because the timeout is |
| 140 | + // big |
| 141 | + // enough to wait for it only once and we don't have to wait again in |
| 142 | + // PreviousSessionFinalizer |
| 143 | + ((EnvelopeCache) cache).flushPreviousSession(); |
| 144 | + } |
| 145 | + } |
| 146 | + |
| 147 | + // making a deep copy as we're modifying the list |
| 148 | + final List<ApplicationExitInfo> exitInfos = new ArrayList<>(applicationExitInfoList); |
| 149 | + |
| 150 | + // search for the latest Tombstone to report it separately as we're gonna enrich it. The |
| 151 | + // latest |
| 152 | + // Tombstone will be first in the list, as it's filled last-to-first in order of appearance |
| 153 | + ApplicationExitInfo latestTombstone = null; |
| 154 | + for (ApplicationExitInfo applicationExitInfo : exitInfos) { |
| 155 | + if (applicationExitInfo.getReason() == ApplicationExitInfo.REASON_CRASH_NATIVE) { |
| 156 | + latestTombstone = applicationExitInfo; |
| 157 | + // remove it, so it's not reported twice |
| 158 | + // TODO: if we fail after this, we effectively lost the ApplicationExitInfo (maybe only remove after we reported it) |
| 159 | + exitInfos.remove(applicationExitInfo); |
| 160 | + break; |
| 161 | + } |
| 162 | + } |
| 163 | + |
| 164 | + if (latestTombstone == null) { |
| 165 | + options |
| 166 | + .getLogger() |
| 167 | + .log( |
| 168 | + SentryLevel.DEBUG, |
| 169 | + "No Tombstones have been found in the historical exit reasons list."); |
| 170 | + return; |
| 171 | + } |
| 172 | + |
| 173 | + if (latestTombstone.getTimestamp() < threshold) { |
| 174 | + options |
| 175 | + .getLogger() |
| 176 | + .log(SentryLevel.DEBUG, "Latest Tombstones happened too long ago, returning early."); |
| 177 | + return; |
| 178 | + } |
| 179 | + |
| 180 | + reportAsSentryEvent(latestTombstone); |
| 181 | + } |
| 182 | + |
| 183 | + @RequiresApi(api = Build.VERSION_CODES.R) |
| 184 | + private void reportAsSentryEvent(ApplicationExitInfo exitInfo) { |
| 185 | + SentryEvent event; |
| 186 | + try { |
| 187 | + TombstoneParser parser = new TombstoneParser(exitInfo.getTraceInputStream()); |
| 188 | + event = parser.parse(); |
| 189 | + event.setTimestamp(DateUtils.getDateTime(exitInfo.getTimestamp())); |
| 190 | + } catch (IOException e) { |
| 191 | + throw new RuntimeException(e); |
| 192 | + } |
| 193 | + |
| 194 | + scopes.captureEvent(event); |
| 195 | + } |
| 196 | + } |
| 197 | +} |
0 commit comments