This repository was archived by the owner on Aug 30, 2023. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 30
feat stacktraces and exceptions #61
Merged
Merged
Changes from 7 commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
ccb80f6
created MainEventProcessor
a98da12
WIP
ee4f571
WIP
7733648
fix merge conflict
199de95
fixing protocol names
affa3cb
parsing stacktraces
13d48b7
WIP
9f09714
removed frame cache
ea0cebe
cleaning up
9a62459
fix merge conflict
69aea15
ref stacktraces and exceptions
b0ef08a
code review
marandaneto 224a099
Merge branch 'master' into feat/stacktraces
marandaneto a7ff3aa
fix assert
marandaneto 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
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
40 changes: 40 additions & 0 deletions
40
sentry-core/src/main/java/io/sentry/core/MainEventProcessor.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,40 @@ | ||
| package io.sentry.core; | ||
|
|
||
| import io.sentry.core.exception.SentryExceptionReader; | ||
| import io.sentry.core.protocol.Message; | ||
| import io.sentry.core.protocol.SentryException; | ||
| import io.sentry.core.protocol.SentryStackFrame; | ||
| import io.sentry.core.protocol.SentryStackTrace; | ||
| import io.sentry.core.util.Objects; | ||
| import java.util.ArrayList; | ||
| import java.util.List; | ||
|
|
||
| public class MainEventProcessor implements EventProcessor { | ||
|
|
||
| private final SentryOptions options; | ||
|
|
||
| public MainEventProcessor(SentryOptions options) { | ||
| this.options = Objects.requireNonNull(options, "The SentryOptions is required."); | ||
| } | ||
|
|
||
| @Override | ||
| public SentryEvent process(SentryEvent event) { | ||
| Throwable throwable = event.getThrowable(); | ||
| if (throwable != null) { | ||
|
|
||
| if (event.getMessage() == null) { | ||
| event.setMessage(getMessage(throwable)); | ||
| } | ||
|
|
||
| event.setException(SentryExceptionReader.sentryExceptionReader(throwable)); | ||
| } | ||
|
|
||
| return event; | ||
| } | ||
|
|
||
| private Message getMessage(Throwable throwable) { | ||
| Message message = new Message(); | ||
| message.setFormatted(throwable.getMessage()); | ||
| return message; | ||
| } | ||
| } |
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
31 changes: 31 additions & 0 deletions
31
sentry-core/src/main/java/io/sentry/core/exception/ExceptionMechanismThrowable.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,31 @@ | ||
| package io.sentry.core.exception; | ||
|
|
||
| import io.sentry.core.protocol.Mechanism; | ||
|
|
||
| /** | ||
| * A throwable decorator that holds an {@link io.sentry.core.protocol.Mechanism} related to the decorated {@link Throwable}. | ||
| */ | ||
| public final class ExceptionMechanismThrowable extends Throwable { | ||
| private static final long serialVersionUID = 100L; | ||
|
|
||
| private final Mechanism exceptionMechanism; | ||
| private final Throwable throwable; | ||
|
|
||
| /** | ||
| * A {@link Throwable} that decorates another with a Sentry {@link Mechanism}. | ||
| * @param mechanism The {@link Mechanism}. | ||
| * @param throwable The {@link java.lang.Throwable}. | ||
| */ | ||
| public ExceptionMechanismThrowable(Mechanism mechanism, Throwable throwable) { | ||
| this.exceptionMechanism = mechanism; | ||
| this.throwable = throwable; | ||
| } | ||
|
|
||
| public Mechanism getExceptionMechanism() { | ||
| return exceptionMechanism; | ||
| } | ||
|
|
||
| public Throwable getThrowable() { | ||
| return throwable; | ||
| } | ||
| } |
111 changes: 111 additions & 0 deletions
111
sentry-core/src/main/java/io/sentry/core/exception/FrameCache.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,111 @@ | ||
| package io.sentry.core.exception; | ||
|
|
||
| import java.util.HashSet; | ||
| import java.util.Map; | ||
| import java.util.Set; | ||
| import java.util.WeakHashMap; | ||
|
|
||
| /** | ||
| * Utility class used by the Sentry Java Agent to store per-frame local variable | ||
| * information for the last thrown exception. | ||
| */ | ||
| public final class FrameCache { | ||
| private static Set<String> appPackages = new HashSet<>(); | ||
|
|
||
| private static ThreadLocal<WeakHashMap<Throwable, SentryFrame[]>> cache = | ||
| new ThreadLocal<WeakHashMap<Throwable, SentryFrame[]>>() { | ||
| @Override | ||
| protected WeakHashMap<Throwable, SentryFrame[]> initialValue() { | ||
| return new WeakHashMap<>(); | ||
| } | ||
| }; | ||
|
|
||
| /** | ||
| * Utility class, no public ctor. | ||
| */ | ||
| private FrameCache() { | ||
|
|
||
| } | ||
|
|
||
| /** | ||
| * Store the per-frame local variable information for the last exception thrown on this thread. | ||
| * | ||
| * @param throwable Throwable that the provided {@link SentryFrame}s represent. | ||
| * @param frames Array of {@link SentryFrame}s to store | ||
| */ | ||
| public static void add(Throwable throwable, SentryFrame[] frames) { | ||
| Map<Throwable, SentryFrame[]> weakMap = cache.get(); | ||
| weakMap.put(throwable, frames); | ||
| } | ||
|
|
||
| /** | ||
| * Retrieve the per-frame local variable information for the last exception thrown on this thread. | ||
| * | ||
| * @param throwable Throwable to look up cached {@link SentryFrame}s for. | ||
| * @return Array of {@link SentryFrame}s | ||
| */ | ||
| public static SentryFrame[] get(Throwable throwable) { | ||
| Map<Throwable, SentryFrame[]> weakMap = cache.get(); | ||
| return weakMap.get(throwable); | ||
| } | ||
|
|
||
| /** | ||
| * Check whether the provided {@link Throwable} should be cached or not. Called by | ||
| * the native agent code so that the Java side (this code) can check the existing | ||
| * cache and user configuration, such as which packages are "in app". | ||
| * | ||
| * @param throwable Throwable to be checked | ||
| * @param numFrames Number of frames in the Throwable's stacktrace | ||
| * @return true if the Throwable should be processed and cached | ||
| */ | ||
| public static boolean shouldCacheThrowable(Throwable throwable, int numFrames) { | ||
| // only cache frames when 'in app' packages are provided | ||
| if (appPackages.isEmpty()) { | ||
| return false; | ||
| } | ||
|
|
||
| // many libraries/frameworks seem to rethrow the same object with trimmed | ||
| // stacktraces, which means later ("smaller") throws would overwrite the existing | ||
| // object in cache. for this reason we prefer the throw with the greatest stack | ||
| // length... | ||
| Map<Throwable, SentryFrame[]> weakMap = cache.get(); | ||
| SentryFrame[] existing = weakMap.get(throwable); | ||
| if (existing != null && numFrames <= existing.length) { | ||
| return false; | ||
| } | ||
|
|
||
| // check each frame against all "in app" package prefixes | ||
| for (StackTraceElement stackTraceElement : throwable.getStackTrace()) { | ||
| for (String appFrame : appPackages) { | ||
| if (stackTraceElement.getClassName().startsWith(appFrame)) { | ||
| return true; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| return false; | ||
| } | ||
|
|
||
| /** | ||
| * Add an "in app" package prefix to the set of packages for which exception | ||
| * local variables will be cached. | ||
| * | ||
| * When an exception is thrown it must contain at least one frame that originates | ||
| * from a package in this set, otherwise local variable information will not be | ||
| * cached. See {@link FrameCache#shouldCacheThrowable(Throwable, int)}. | ||
| * | ||
| * @param newAppPackage package prefix to add to the set | ||
| */ | ||
| public static void addAppPackage(String newAppPackage) { | ||
| appPackages.add(newAppPackage); | ||
| } | ||
|
|
||
| /** | ||
| * This method is meant for cleaner testing. Don't attempt to use it in production. | ||
| */ | ||
| // visible for testing | ||
| static void reset() { | ||
| cache.get().clear(); | ||
| appPackages.clear(); | ||
| } | ||
| } |
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.