Skip to content

Commit aefa921

Browse files
committed
Move logic to event processor
1 parent 47db30e commit aefa921

File tree

4 files changed

+343
-262
lines changed

4 files changed

+343
-262
lines changed

sentry-android-core/src/main/java/io/sentry/android/core/AnrV2Integration.java

Lines changed: 0 additions & 147 deletions
Original file line numberDiff line numberDiff line change
@@ -12,20 +12,9 @@
1212
import io.sentry.ILogger;
1313
import io.sentry.IScopes;
1414
import io.sentry.Integration;
15-
import io.sentry.ProfileChunk;
16-
import io.sentry.ProfileContext;
1715
import io.sentry.SentryEvent;
18-
import io.sentry.SentryExceptionFactory;
1916
import io.sentry.SentryLevel;
2017
import io.sentry.SentryOptions;
21-
import io.sentry.SentryStackTraceFactory;
22-
import io.sentry.android.core.anr.AggregatedStackTrace;
23-
import io.sentry.android.core.anr.AnrCulpritIdentifier;
24-
import io.sentry.android.core.anr.AnrException;
25-
import io.sentry.android.core.anr.AnrProfile;
26-
import io.sentry.android.core.anr.AnrProfileManager;
27-
import io.sentry.android.core.anr.AnrProfileRotationHelper;
28-
import io.sentry.android.core.anr.StackTraceConverter;
2918
import io.sentry.android.core.cache.AndroidEnvelopeCache;
3019
import io.sentry.android.core.internal.threaddump.Lines;
3120
import io.sentry.android.core.internal.threaddump.ThreadDumpParser;
@@ -35,12 +24,8 @@
3524
import io.sentry.protocol.DebugImage;
3625
import io.sentry.protocol.DebugMeta;
3726
import io.sentry.protocol.Message;
38-
import io.sentry.protocol.SentryException;
3927
import io.sentry.protocol.SentryId;
40-
import io.sentry.protocol.SentryStackFrame;
41-
import io.sentry.protocol.SentryStackTrace;
4228
import io.sentry.protocol.SentryThread;
43-
import io.sentry.protocol.profiling.SentryProfile;
4429
import io.sentry.transport.CurrentDateProvider;
4530
import io.sentry.transport.ICurrentDateProvider;
4631
import io.sentry.util.HintUtils;
@@ -49,12 +34,9 @@
4934
import java.io.ByteArrayInputStream;
5035
import java.io.ByteArrayOutputStream;
5136
import java.io.Closeable;
52-
import java.io.File;
5337
import java.io.IOException;
5438
import java.io.InputStream;
5539
import java.io.InputStreamReader;
56-
import java.util.Arrays;
57-
import java.util.HashMap;
5840
import java.util.List;
5941
import org.jetbrains.annotations.ApiStatus;
6042
import org.jetbrains.annotations.NotNull;
@@ -207,111 +189,9 @@ public boolean shouldReportHistorical() {
207189
}
208190
}
209191

210-
if (options.isEnableAnrProfiling()) {
211-
applyAnrProfile(isBackground, anrTimestamp, event);
212-
// TODO: maybe move to AnrV2EventProcessor instead
213-
if (hasOnlySystemFrames(event)) {
214-
// By omitting the {{ default }} fingerprint, the stacktrace will be completely ignored
215-
// and all events will be grouped
216-
// into the same issue
217-
event.setFingerprints(
218-
Arrays.asList(
219-
"{{ system-frames-only-anr }}",
220-
isBackground ? "background-anr" : "foreground-anr"));
221-
}
222-
}
223-
224192
return new ApplicationExitInfoHistoryDispatcher.Report(event, hint, anrHint);
225193
}
226194

227-
private void applyAnrProfile(
228-
final boolean isBackground, final long anrTimestamp, final @NotNull SentryEvent event) {
229-
230-
// as of now AnrProfilingIntegration only generates profiles in foreground
231-
if (isBackground) {
232-
return;
233-
}
234-
235-
final @Nullable String cacheDirPath = options.getCacheDirPath();
236-
if (cacheDirPath == null) {
237-
return;
238-
}
239-
final @NotNull File cacheDir = new File(cacheDirPath);
240-
241-
@Nullable AnrProfile anrProfile = null;
242-
243-
try {
244-
final File lastFile = AnrProfileRotationHelper.getLastFile(cacheDir);
245-
246-
if (lastFile.exists()) {
247-
options.getLogger().log(SentryLevel.DEBUG, "Reading ANR profile");
248-
try (final AnrProfileManager provider = new AnrProfileManager(options, lastFile)) {
249-
anrProfile = provider.load();
250-
}
251-
} else {
252-
options.getLogger().log(SentryLevel.DEBUG, "No ANR profile file found");
253-
}
254-
} catch (Throwable t) {
255-
options.getLogger().log(SentryLevel.INFO, "Could not retrieve ANR profile", t);
256-
} finally {
257-
if (!AnrProfileRotationHelper.deleteLastFile(cacheDir)) {
258-
options.getLogger().log(SentryLevel.INFO, "Could not delete ANR profile file");
259-
}
260-
}
261-
262-
if (anrProfile != null) {
263-
options.getLogger().log(SentryLevel.INFO, "ANR profile found");
264-
if (anrTimestamp >= anrProfile.startTimeMs && anrTimestamp <= anrProfile.endtimeMs) {
265-
final @Nullable AggregatedStackTrace culprit =
266-
AnrCulpritIdentifier.identify(anrProfile.stacks);
267-
if (culprit != null) {
268-
final @Nullable SentryId profilerId = captureAnrProfile(anrTimestamp, anrProfile);
269-
270-
final @NotNull StackTraceElement[] stack = culprit.getStack();
271-
if (stack.length > 0) {
272-
final StackTraceElement stackTraceElement = culprit.getStack()[0];
273-
final String message =
274-
stackTraceElement.getClassName() + "." + stackTraceElement.getMethodName();
275-
final AnrException exception = new AnrException(message);
276-
exception.setStackTrace(stack);
277-
278-
// TODO should this be re-used from somewhere else?
279-
final SentryExceptionFactory factory =
280-
new SentryExceptionFactory(new SentryStackTraceFactory(options));
281-
event.setExceptions(factory.getSentryExceptions(exception));
282-
if (profilerId != null) {
283-
event.getContexts().setProfile(new ProfileContext(profilerId));
284-
}
285-
}
286-
}
287-
} else {
288-
options.getLogger().log(SentryLevel.DEBUG, "ANR profile found, but doesn't match");
289-
}
290-
}
291-
}
292-
293-
@Nullable
294-
private SentryId captureAnrProfile(
295-
final long anrTimestamp, final @NotNull AnrProfile anrProfile) {
296-
final SentryProfile profile = StackTraceConverter.convert(anrProfile);
297-
final ProfileChunk chunk =
298-
new ProfileChunk(
299-
new SentryId(),
300-
new SentryId(),
301-
null,
302-
new HashMap<>(0),
303-
anrTimestamp / 1000.0d,
304-
ProfileChunk.PLATFORM_JAVA,
305-
options);
306-
chunk.setSentryProfile(profile);
307-
final SentryId profilerId = scopes.captureProfileChunk(chunk);
308-
if (SentryId.EMPTY_ID.equals(profilerId)) {
309-
return null;
310-
} else {
311-
return chunk.getProfilerId();
312-
}
313-
}
314-
315195
private @NotNull ParseResult parseThreadDump(
316196
final @NotNull ApplicationExitInfo exitInfo, final boolean isBackground) {
317197
final byte[] dump;
@@ -365,33 +245,6 @@ private byte[] getDumpBytes(final @NotNull InputStream trace) throws IOException
365245
}
366246
}
367247

368-
private static boolean hasOnlySystemFrames(final @NotNull SentryEvent event) {
369-
final List<SentryException> exceptions = event.getExceptions();
370-
if (exceptions == null || exceptions.isEmpty()) {
371-
// No exceptions means we haven't verified frames - don't apply special fingerprinting
372-
return false;
373-
}
374-
375-
for (final SentryException exception : exceptions) {
376-
final @Nullable SentryStackTrace stacktrace = exception.getStacktrace();
377-
if (stacktrace != null) {
378-
final @Nullable List<SentryStackFrame> frames = stacktrace.getFrames();
379-
if (frames != null && !frames.isEmpty()) {
380-
for (final SentryStackFrame frame : frames) {
381-
if (frame.isInApp() != null && frame.isInApp()) {
382-
return false;
383-
}
384-
final @Nullable String module = frame.getModule();
385-
if (module != null && !AnrCulpritIdentifier.isSystemFrame(frame.getModule())) {
386-
return false;
387-
}
388-
}
389-
}
390-
}
391-
}
392-
return true;
393-
}
394-
395248
@ApiStatus.Internal
396249
public static final class AnrV2Hint extends BlockingFlushHint
397250
implements Backfillable, AbnormalExit {

0 commit comments

Comments
 (0)