-
-
Notifications
You must be signed in to change notification settings - Fork 469
feat: merge tombstone and native sdk events #5037
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
Merged
Merged
Changes from 2 commits
Commits
Show all changes
29 commits
Select commit
Hold shift + click to select a range
028f17b
feat: merge tombstone and native sdk events
supervacuus 6befa43
add preliminary change log
supervacuus aa8f65f
Merge branch 'main' into feat/tombstone_native_sdk_merge
supervacuus bf14914
Merge branch 'main' into feat/tombstone_native_sdk_merge
supervacuus d7d5447
add preliminary change log
supervacuus ba1bfc7
apply review+sync feedback
supervacuus 7d73c41
Merge branch 'main' into feat/tombstone_native_sdk_merge
supervacuus cd970cb
add tombstone manifest flag
supervacuus b832e7c
remove tombstone-native correlation via processStateSummary
supervacuus df83d57
2-phase streaming NativeEventCollector (#5065)
supervacuus e6f6788
Merge branch 'main' into feat/tombstone_native_sdk_merge
supervacuus 0ba1f6a
Merge branch 'main' into feat/tombstone_native_sdk_merge
supervacuus 00c5839
Merge branch 'main' into feat/tombstone_native_sdk_merge
supervacuus 72d5644
extract inner inApp check into a reusable static method
supervacuus 2fd6b28
reduce I/O in the collect() method of the NativeEventCollector.
supervacuus c3be389
add native attachments to TombstoneHint.
supervacuus c8dafdd
introduce VMA -> module coalescing via ModuleAccumulator
supervacuus f20587a
ensure native crash survives the merge
supervacuus 011008d
handle null nativeLibraryDir in TombstoneParser
supervacuus 423145d
clarify inApp vs nativeLibraryDir usage in code comment
supervacuus 03ac8a6
Merge branch 'main' into feat/tombstone_native_sdk_merge
supervacuus f028319
ignore stack frames from anonymous VMAs that don't resolve to a funct…
supervacuus 0ec2068
use the right nativeLibraryDir for the tombstone test fixture
supervacuus aeeacd2
add proguard rule for protobuf-lite
supervacuus 017ac5f
make BoundedInputStream safer wrt double closes
supervacuus 67aee7b
override empty function name behavior from SentryStackTraceFactory.is…
supervacuus 555a2d3
Merge branch 'main' into feat/tombstone_native_sdk_merge
markushi 3a78d01
Merge branch 'refs/heads/main' into feat/tombstone_native_sdk_merge
supervacuus 3e3fc73
pre merge preps.
supervacuus 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
Some comments aren't visible on the classic Files Changed page.
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
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
276 changes: 276 additions & 0 deletions
276
sentry-android-core/src/main/java/io/sentry/android/core/NativeEventCollector.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,276 @@ | ||
| package io.sentry.android.core; | ||
|
|
||
| import static io.sentry.cache.EnvelopeCache.PREFIX_CURRENT_SESSION_FILE; | ||
| import static io.sentry.cache.EnvelopeCache.PREFIX_PREVIOUS_SESSION_FILE; | ||
| import static io.sentry.cache.EnvelopeCache.STARTUP_CRASH_MARKER_FILE; | ||
|
|
||
| import io.sentry.SentryEnvelope; | ||
| import io.sentry.SentryEnvelopeItem; | ||
| import io.sentry.SentryEvent; | ||
| import io.sentry.SentryItemType; | ||
| import io.sentry.SentryLevel; | ||
| import java.io.BufferedInputStream; | ||
| import java.io.BufferedReader; | ||
| import java.io.ByteArrayInputStream; | ||
| import java.io.File; | ||
| import java.io.FileInputStream; | ||
| import java.io.InputStream; | ||
| import java.io.InputStreamReader; | ||
| import java.io.Reader; | ||
| import java.nio.charset.StandardCharsets; | ||
| import java.util.ArrayList; | ||
| import java.util.Date; | ||
| import java.util.List; | ||
| import org.jetbrains.annotations.ApiStatus; | ||
| import org.jetbrains.annotations.NotNull; | ||
| import org.jetbrains.annotations.Nullable; | ||
|
|
||
| /** | ||
| * Collects native crash events from the outbox directory. These events can be correlated with | ||
| * tombstone events from ApplicationExitInfo to avoid sending duplicate crash reports. | ||
| */ | ||
| @ApiStatus.Internal | ||
| public final class NativeEventCollector { | ||
|
|
||
| private static final String NATIVE_PLATFORM = "native"; | ||
|
|
||
| // TODO: will be replaced with the correlationId once the Native SDK supports it | ||
| private static final long TIMESTAMP_TOLERANCE_MS = 5000; | ||
|
|
||
| private final @NotNull SentryAndroidOptions options; | ||
| private final @NotNull List<NativeEventData> nativeEvents = new ArrayList<>(); | ||
| private boolean collected = false; | ||
|
|
||
| public NativeEventCollector(final @NotNull SentryAndroidOptions options) { | ||
| this.options = options; | ||
| } | ||
|
|
||
| /** Holds a native event along with its source file for later deletion. */ | ||
| public static final class NativeEventData { | ||
| private final @NotNull SentryEvent event; | ||
| private final @NotNull File file; | ||
| private final @NotNull SentryEnvelope envelope; | ||
| private final long timestampMs; | ||
|
|
||
| NativeEventData( | ||
| final @NotNull SentryEvent event, | ||
| final @NotNull File file, | ||
| final @NotNull SentryEnvelope envelope, | ||
| final long timestampMs) { | ||
| this.event = event; | ||
| this.file = file; | ||
| this.envelope = envelope; | ||
| this.timestampMs = timestampMs; | ||
| } | ||
|
|
||
| public @NotNull SentryEvent getEvent() { | ||
| return event; | ||
| } | ||
|
|
||
| public @NotNull File getFile() { | ||
| return file; | ||
| } | ||
|
|
||
| public @NotNull SentryEnvelope getEnvelope() { | ||
| return envelope; | ||
| } | ||
|
|
||
| public long getTimestampMs() { | ||
| return timestampMs; | ||
| } | ||
|
|
||
| /** | ||
| * Extracts the correlation ID from the event's extra data. | ||
| * | ||
| * @return the correlation ID, or null if not present | ||
| */ | ||
| public @Nullable String getCorrelationId() { | ||
| final @Nullable Object correlationId = event.getExtra("sentry.native.correlation_id"); | ||
| if (correlationId instanceof String) { | ||
| return (String) correlationId; | ||
| } | ||
| return null; | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Scans the outbox directory and collects all native crash events. This method should be called | ||
| * once before processing tombstones. Subsequent calls are no-ops. | ||
| */ | ||
| public void collect() { | ||
| if (collected) { | ||
| return; | ||
| } | ||
| collected = true; | ||
|
|
||
| final @Nullable String outboxPath = options.getOutboxPath(); | ||
| if (outboxPath == null) { | ||
| options | ||
| .getLogger() | ||
| .log(SentryLevel.DEBUG, "Outbox path is null, skipping native event collection."); | ||
| return; | ||
| } | ||
|
|
||
| final File outboxDir = new File(outboxPath); | ||
| if (!outboxDir.isDirectory()) { | ||
|
supervacuus marked this conversation as resolved.
Outdated
|
||
| options.getLogger().log(SentryLevel.DEBUG, "Outbox path is not a directory: %s", outboxPath); | ||
| return; | ||
| } | ||
|
|
||
| final File[] files = outboxDir.listFiles((d, name) -> isRelevantFileName(name)); | ||
| if (files == null || files.length == 0) { | ||
| options.getLogger().log(SentryLevel.DEBUG, "No envelope files found in outbox."); | ||
| return; | ||
| } | ||
|
|
||
| options | ||
| .getLogger() | ||
| .log(SentryLevel.DEBUG, "Scanning %d files in outbox for native events.", files.length); | ||
|
|
||
| for (final File file : files) { | ||
|
supervacuus marked this conversation as resolved.
|
||
| if (!file.isFile()) { | ||
| continue; | ||
| } | ||
|
|
||
| final @Nullable NativeEventData nativeEventData = extractNativeEventFromFile(file); | ||
| if (nativeEventData != null) { | ||
| nativeEvents.add(nativeEventData); | ||
| options | ||
| .getLogger() | ||
| .log( | ||
| SentryLevel.DEBUG, | ||
| "Found native event in outbox: %s (timestamp: %d)", | ||
| file.getName(), | ||
| nativeEventData.getTimestampMs()); | ||
| } | ||
| } | ||
|
|
||
| options | ||
| .getLogger() | ||
| .log(SentryLevel.DEBUG, "Collected %d native events from outbox.", nativeEvents.size()); | ||
| } | ||
|
|
||
| /** | ||
| * Finds a native event that matches the given tombstone timestamp or correlation ID. If a match | ||
| * is found, it is removed from the internal list so it won't be matched again. | ||
| * | ||
| * <p>This method will lazily collect native events from the outbox on first call. | ||
| * | ||
| * @param tombstoneTimestampMs the timestamp from ApplicationExitInfo | ||
| * @param correlationId the correlation ID from processStateSummary, or null | ||
| * @return the matching native event data, or null if no match found | ||
| */ | ||
| public @Nullable NativeEventData findAndRemoveMatchingNativeEvent( | ||
| final long tombstoneTimestampMs, final @Nullable String correlationId) { | ||
|
|
||
| // Lazily collect on first use (runs on executor thread, not main thread) | ||
| collect(); | ||
|
|
||
| // First, try to match by correlation ID (when sentry-native supports it) | ||
| if (correlationId != null) { | ||
| for (final NativeEventData nativeEvent : nativeEvents) { | ||
| final @Nullable String nativeCorrelationId = nativeEvent.getCorrelationId(); | ||
| if (correlationId.equals(nativeCorrelationId)) { | ||
| options | ||
| .getLogger() | ||
| .log(SentryLevel.DEBUG, "Matched native event by correlation ID: %s", correlationId); | ||
| nativeEvents.remove(nativeEvent); | ||
| return nativeEvent; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| // Fall back to timestamp-based matching | ||
| for (final NativeEventData nativeEvent : nativeEvents) { | ||
| final long timeDiff = Math.abs(tombstoneTimestampMs - nativeEvent.getTimestampMs()); | ||
| if (timeDiff <= TIMESTAMP_TOLERANCE_MS) { | ||
| options | ||
| .getLogger() | ||
| .log(SentryLevel.DEBUG, "Matched native event by timestamp (diff: %d ms)", timeDiff); | ||
| nativeEvents.remove(nativeEvent); | ||
| return nativeEvent; | ||
| } | ||
| } | ||
|
|
||
| return null; | ||
| } | ||
|
|
||
| /** | ||
| * Deletes a native event file from the outbox. | ||
| * | ||
| * @param nativeEventData the native event data containing the file reference | ||
| * @return true if the file was deleted successfully | ||
| */ | ||
| public boolean deleteNativeEventFile(final @NotNull NativeEventData nativeEventData) { | ||
| final File file = nativeEventData.getFile(); | ||
| try { | ||
| if (file.delete()) { | ||
| options | ||
| .getLogger() | ||
| .log(SentryLevel.DEBUG, "Deleted native event file from outbox: %s", file.getName()); | ||
| return true; | ||
| } else { | ||
| options | ||
| .getLogger() | ||
| .log( | ||
| SentryLevel.WARNING, | ||
| "Failed to delete native event file: %s", | ||
| file.getAbsolutePath()); | ||
| return false; | ||
| } | ||
| } catch (Throwable e) { | ||
| options | ||
| .getLogger() | ||
| .log( | ||
| SentryLevel.ERROR, e, "Error deleting native event file: %s", file.getAbsolutePath()); | ||
| return false; | ||
| } | ||
| } | ||
|
|
||
| private @Nullable NativeEventData extractNativeEventFromFile(final @NotNull File file) { | ||
| try (final InputStream stream = new BufferedInputStream(new FileInputStream(file))) { | ||
| final SentryEnvelope envelope = options.getEnvelopeReader().read(stream); | ||
| if (envelope == null) { | ||
| return null; | ||
| } | ||
|
|
||
| for (final SentryEnvelopeItem item : envelope.getItems()) { | ||
| if (!SentryItemType.Event.equals(item.getHeader().getType())) { | ||
| continue; | ||
| } | ||
|
|
||
| try (final Reader eventReader = | ||
| new BufferedReader( | ||
| new InputStreamReader( | ||
| new ByteArrayInputStream(item.getData()), StandardCharsets.UTF_8))) { | ||
| final SentryEvent event = | ||
| options.getSerializer().deserialize(eventReader, SentryEvent.class); | ||
| if (event != null && NATIVE_PLATFORM.equals(event.getPlatform())) { | ||
| final long timestampMs = extractTimestampMs(event); | ||
| return new NativeEventData(event, file, envelope, timestampMs); | ||
| } | ||
| } | ||
| } | ||
| } catch (Throwable e) { | ||
| options | ||
| .getLogger() | ||
| .log(SentryLevel.DEBUG, e, "Error reading envelope file: %s", file.getAbsolutePath()); | ||
| } | ||
| return null; | ||
| } | ||
|
|
||
| private long extractTimestampMs(final @NotNull SentryEvent event) { | ||
| final @Nullable Date timestamp = event.getTimestamp(); | ||
| if (timestamp != null) { | ||
| return timestamp.getTime(); | ||
| } | ||
| return 0; | ||
| } | ||
|
|
||
| private boolean isRelevantFileName(final @Nullable String fileName) { | ||
| return fileName != null | ||
| && !fileName.startsWith(PREFIX_CURRENT_SESSION_FILE) | ||
| && !fileName.startsWith(PREFIX_PREVIOUS_SESSION_FILE) | ||
| && !fileName.startsWith(STARTUP_CRASH_MARKER_FILE); | ||
| } | ||
| } | ||
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.