Skip to content

Commit 48ed5e1

Browse files
committed
Adapt AndroidEnvelopeCache to also write Tombstone timestamp markers
1 parent 920221a commit 48ed5e1

File tree

2 files changed

+62
-10
lines changed

2 files changed

+62
-10
lines changed

sentry-android-core/api/sentry-android-core.api

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -499,10 +499,12 @@ public final class io/sentry/android/core/ViewHierarchyEventProcessor : io/sentr
499499

500500
public final class io/sentry/android/core/cache/AndroidEnvelopeCache : io/sentry/cache/EnvelopeCache {
501501
public static final field LAST_ANR_REPORT Ljava/lang/String;
502+
public static final field LAST_TOMBSTONE_REPORT Ljava/lang/String;
502503
public fun <init> (Lio/sentry/android/core/SentryAndroidOptions;)V
503504
public fun getDirectory ()Ljava/io/File;
504505
public static fun hasStartupCrashMarker (Lio/sentry/SentryOptions;)Z
505506
public static fun lastReportedAnr (Lio/sentry/SentryOptions;)Ljava/lang/Long;
507+
public static fun lastReportedTombstone (Lio/sentry/SentryOptions;)Ljava/lang/Long;
506508
public fun store (Lio/sentry/SentryEnvelope;Lio/sentry/Hint;)V
507509
public fun storeEnvelope (Lio/sentry/SentryEnvelope;Lio/sentry/Hint;)Z
508510
}

sentry-android-core/src/main/java/io/sentry/android/core/cache/AndroidEnvelopeCache.java

Lines changed: 60 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import io.sentry.UncaughtExceptionHandlerIntegration;
1111
import io.sentry.android.core.AnrV2Integration;
1212
import io.sentry.android.core.SentryAndroidOptions;
13+
import io.sentry.android.core.TombstoneIntegration;
1314
import io.sentry.android.core.internal.util.AndroidCurrentDateProvider;
1415
import io.sentry.android.core.performance.AppStartMetrics;
1516
import io.sentry.android.core.performance.TimeSpan;
@@ -31,6 +32,7 @@
3132
public final class AndroidEnvelopeCache extends EnvelopeCache {
3233

3334
public static final String LAST_ANR_REPORT = "last_anr_report";
35+
public static final String LAST_TOMBSTONE_REPORT = "last_tombstone_report";
3436

3537
private final @NotNull ICurrentDateProvider currentDateProvider;
3638

@@ -94,6 +96,22 @@ private boolean storeInternalAndroid(@NotNull SentryEnvelope envelope, @NotNull
9496

9597
writeLastReportedAnrMarker(timestamp);
9698
});
99+
100+
HintUtils.runIfHasType(
101+
hint,
102+
TombstoneIntegration.TombstoneHint.class,
103+
(tombstoneHint) -> {
104+
final @Nullable Long timestamp = tombstoneHint.timestamp();
105+
options
106+
.getLogger()
107+
.log(
108+
SentryLevel.DEBUG,
109+
"Writing last reported Tombstone marker with timestamp %d",
110+
timestamp);
111+
112+
writeLastReportedTombstoneMarker(timestamp);
113+
});
114+
97115
return didStore;
98116
}
99117

@@ -152,42 +170,74 @@ public static boolean hasStartupCrashMarker(final @NotNull SentryOptions options
152170
return false;
153171
}
154172

155-
public static @Nullable Long lastReportedAnr(final @NotNull SentryOptions options) {
173+
private static @Nullable Long lastReportedMarker(
174+
final @NotNull SentryOptions options,
175+
@NotNull String reportFilename,
176+
@NotNull String markerCategory) {
156177
final String cacheDirPath =
157178
Objects.requireNonNull(
158-
options.getCacheDirPath(), "Cache dir path should be set for getting ANRs reported");
179+
options.getCacheDirPath(),
180+
"Cache dir path should be set for getting " + markerCategory + "s reported");
159181

160-
final File lastAnrMarker = new File(cacheDirPath, LAST_ANR_REPORT);
182+
final File lastMarker = new File(cacheDirPath, reportFilename);
161183
try {
162-
final String content = FileUtils.readText(lastAnrMarker);
184+
final String content = FileUtils.readText(lastMarker);
163185
// we wrapped into try-catch already
164186
//noinspection ConstantConditions
165187
return content.equals("null") ? null : Long.parseLong(content.trim());
166188
} catch (Throwable e) {
167189
if (e instanceof FileNotFoundException) {
168190
options
169191
.getLogger()
170-
.log(DEBUG, "Last ANR marker does not exist. %s.", lastAnrMarker.getAbsolutePath());
192+
.log(
193+
DEBUG,
194+
"Last " + markerCategory + " marker does not exist. %s.",
195+
lastMarker.getAbsolutePath());
171196
} else {
172-
options.getLogger().log(ERROR, "Error reading last ANR marker", e);
197+
options.getLogger().log(ERROR, "Error reading last " + markerCategory + " marker", e);
173198
}
174199
}
175200
return null;
176201
}
177202

178-
private void writeLastReportedAnrMarker(final @Nullable Long timestamp) {
203+
private void writeLastReportedMarker(
204+
final @Nullable Long timestamp,
205+
@NotNull String reportFilename,
206+
@NotNull String markerCategory) {
179207
final String cacheDirPath = options.getCacheDirPath();
180208
if (cacheDirPath == null) {
181-
options.getLogger().log(DEBUG, "Cache dir path is null, the ANR marker will not be written");
209+
options
210+
.getLogger()
211+
.log(
212+
DEBUG,
213+
"Cache dir path is null, the " + markerCategory + " marker will not be written");
182214
return;
183215
}
184216

185-
final File anrMarker = new File(cacheDirPath, LAST_ANR_REPORT);
217+
final File anrMarker = new File(cacheDirPath, reportFilename);
186218
try (final OutputStream outputStream = new FileOutputStream(anrMarker)) {
187219
outputStream.write(String.valueOf(timestamp).getBytes(UTF_8));
188220
outputStream.flush();
189221
} catch (Throwable e) {
190-
options.getLogger().log(ERROR, "Error writing the ANR marker to the disk", e);
222+
options
223+
.getLogger()
224+
.log(ERROR, "Error writing the " + markerCategory + " marker to the disk", e);
191225
}
192226
}
227+
228+
public static @Nullable Long lastReportedAnr(final @NotNull SentryOptions options) {
229+
return lastReportedMarker(options, LAST_ANR_REPORT, "ANR");
230+
}
231+
232+
private void writeLastReportedAnrMarker(final @Nullable Long timestamp) {
233+
this.writeLastReportedMarker(timestamp, LAST_ANR_REPORT, "ANR");
234+
}
235+
236+
public static @Nullable Long lastReportedTombstone(final @NotNull SentryOptions options) {
237+
return lastReportedMarker(options, LAST_TOMBSTONE_REPORT, "Tombstone");
238+
}
239+
240+
private void writeLastReportedTombstoneMarker(final @Nullable Long timestamp) {
241+
this.writeLastReportedMarker(timestamp, LAST_TOMBSTONE_REPORT, "Tombstone");
242+
}
193243
}

0 commit comments

Comments
 (0)