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 all 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
37 changes: 37 additions & 0 deletions
37
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,37 @@ | ||
| package io.sentry.core; | ||
|
|
||
| import io.sentry.core.util.Objects; | ||
|
|
||
| public class MainEventProcessor implements EventProcessor { | ||
|
|
||
| private final SentryOptions options; | ||
| private final SentryThreadFactory sentryThreadFactory = new SentryThreadFactory(); | ||
| private final SentryStackTraceFactory sentryStackTraceFactory = new SentryStackTraceFactory(); | ||
| private final SentryExceptionFactory sentryExceptionFactory = | ||
| new SentryExceptionFactory(sentryStackTraceFactory); | ||
|
|
||
| MainEventProcessor(SentryOptions options) { | ||
| this.options = Objects.requireNonNull(options, "The SentryOptions is required."); | ||
| } | ||
|
|
||
| @Override | ||
| public SentryEvent process(SentryEvent event) { | ||
| if (event.getThreads() == null) { | ||
| event.setThreads(sentryThreadFactory.getCurrentThreads()); | ||
| } | ||
|
|
||
| if (event.getRelease() == null) { | ||
| event.setRelease(options.getRelease()); | ||
| } | ||
| if (event.getEnvironment() == null) { | ||
| event.setEnvironment(options.getEnvironment()); | ||
| } | ||
|
|
||
| Throwable throwable = event.getThrowable(); | ||
| if (throwable != null) { | ||
| event.setException(sentryExceptionFactory.getSentryExceptions(throwable)); | ||
| } | ||
|
|
||
| return event; | ||
| } | ||
| } |
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
118 changes: 118 additions & 0 deletions
118
sentry-core/src/main/java/io/sentry/core/SentryExceptionFactory.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,118 @@ | ||
| package io.sentry.core; | ||
|
|
||
| import io.sentry.core.exception.ExceptionMechanismThrowable; | ||
| import io.sentry.core.protocol.Mechanism; | ||
| import io.sentry.core.protocol.SentryException; | ||
| import io.sentry.core.protocol.SentryStackTrace; | ||
| import io.sentry.core.util.Objects; | ||
| import io.sentry.core.util.VisibleForTesting; | ||
| import java.util.ArrayDeque; | ||
| import java.util.ArrayList; | ||
| import java.util.Deque; | ||
| import java.util.HashSet; | ||
| import java.util.List; | ||
| import java.util.Set; | ||
|
|
||
| /** class responsible for converting Java Throwable to SentryExceptions */ | ||
| class SentryExceptionFactory { | ||
|
|
||
| private final SentryStackTraceFactory sentryStackTraceFactory; | ||
|
|
||
| public SentryExceptionFactory(SentryStackTraceFactory sentryStackTraceFactory) { | ||
| this.sentryStackTraceFactory = | ||
| Objects.requireNonNull(sentryStackTraceFactory, "The SentryStackTraceFactory is required."); | ||
| } | ||
|
|
||
| /** | ||
| * Creates a new instance from the given {@code throwable}. | ||
| * | ||
| * @param throwable the {@link Throwable} to build this instance from | ||
| */ | ||
| List<SentryException> getSentryExceptions(final Throwable throwable) { | ||
| return getSentryExceptions(extractExceptionQueue(throwable)); | ||
| } | ||
|
|
||
| /** | ||
| * Creates a new instance from the given {@code exceptions}. | ||
| * | ||
| * @param exceptions a {@link Deque} of {@link SentryException} to build this instance from | ||
| */ | ||
| private List<SentryException> getSentryExceptions(final Deque<SentryException> exceptions) { | ||
| return new ArrayList<>(exceptions); | ||
| } | ||
|
|
||
| /** | ||
| * Creates a Sentry exception based on a Java Throwable. | ||
| * | ||
| * <p>The {@code childExceptionStackTrace} parameter is used to define the common frames with the | ||
| * child exception (Exception caused by {@code throwable}). | ||
| * | ||
| * @param throwable Java exception to send to Sentry. | ||
| * @param exceptionMechanism The optional {@link Mechanism} of the {@code throwable}. Or null if | ||
| * none exist. | ||
| */ | ||
| private SentryException getSentryException( | ||
| final Throwable throwable, final Mechanism exceptionMechanism) { | ||
|
|
||
| Package exceptionPackage = throwable.getClass().getPackage(); | ||
| String fullClassName = throwable.getClass().getName(); | ||
|
|
||
| SentryException exception = new SentryException(); | ||
|
|
||
| String exceptionMessage = throwable.getMessage(); | ||
|
|
||
| String exceptionClassName = | ||
| exceptionPackage != null | ||
| ? fullClassName.replace(exceptionPackage.getName() + ".", "") | ||
| : fullClassName; | ||
|
|
||
| String exceptionPackageName = exceptionPackage != null ? exceptionPackage.getName() : null; | ||
|
|
||
| SentryStackTrace sentryStackTrace = new SentryStackTrace(); | ||
| sentryStackTrace.setFrames(sentryStackTraceFactory.getStackFrames(throwable.getStackTrace())); | ||
|
|
||
| exception.setStacktrace(sentryStackTrace); | ||
| exception.setType(exceptionClassName); | ||
| exception.setMechanism(exceptionMechanism); | ||
| exception.setModule(exceptionPackageName); | ||
| exception.setValue(exceptionMessage); | ||
|
|
||
| return exception; | ||
| } | ||
|
|
||
| /** | ||
| * Transforms a {@link Throwable} into a Queue of {@link SentryException}. | ||
| * | ||
| * <p>Exceptions are stored in the queue from the most recent one to the oldest one. | ||
| * | ||
| * @param throwable throwable to transform in a queue of exceptions. | ||
| * @return a queue of exception with StackTrace. | ||
| */ | ||
| @VisibleForTesting | ||
| Deque<SentryException> extractExceptionQueue(final Throwable throwable) { | ||
| Deque<SentryException> exceptions = new ArrayDeque<>(); | ||
| Set<Throwable> circularityDetector = new HashSet<>(); | ||
| Mechanism exceptionMechanism; | ||
|
|
||
| Throwable currentThrowable = throwable; | ||
|
|
||
| // Stack the exceptions to send them in the reverse order | ||
| while (currentThrowable != null && circularityDetector.add(currentThrowable)) { | ||
| if (currentThrowable instanceof ExceptionMechanismThrowable) { | ||
| // this is for ANR I believe | ||
| ExceptionMechanismThrowable exceptionMechanismThrowable = | ||
| (ExceptionMechanismThrowable) currentThrowable; | ||
| exceptionMechanism = exceptionMechanismThrowable.getExceptionMechanism(); | ||
| currentThrowable = exceptionMechanismThrowable.getThrowable(); | ||
| } else { | ||
| exceptionMechanism = null; | ||
| } | ||
|
|
||
| SentryException exception = getSentryException(currentThrowable, exceptionMechanism); | ||
| exceptions.add(exception); | ||
| currentThrowable = currentThrowable.getCause(); | ||
| } | ||
|
|
||
| return exceptions; | ||
| } | ||
| } |
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
35 changes: 35 additions & 0 deletions
35
sentry-core/src/main/java/io/sentry/core/SentryStackTraceFactory.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,35 @@ | ||
| package io.sentry.core; | ||
|
|
||
| import io.sentry.core.protocol.SentryStackFrame; | ||
| import io.sentry.core.util.Nullable; | ||
| import java.util.ArrayList; | ||
| import java.util.List; | ||
|
|
||
| /** class responsible for converting Java StackTraceElements to SentryStackFrames */ | ||
| class SentryStackTraceFactory { | ||
|
|
||
| /** | ||
| * convert an Array of Java StackTraceElements to a list of SentryStackFrames | ||
| * | ||
| * @param elements Array of Java StackTraceElements | ||
| * @return list of SentryStackFrames | ||
| */ | ||
| List<SentryStackFrame> getStackFrames(@Nullable final StackTraceElement[] elements) { | ||
| List<SentryStackFrame> sentryStackFrames = new ArrayList<>(); | ||
|
|
||
| if (elements != null) { | ||
| for (StackTraceElement item : elements) { | ||
| SentryStackFrame sentryStackFrame = new SentryStackFrame(); | ||
| sentryStackFrame.setModule(item.getClassName()); | ||
| sentryStackFrame.setFunction(item.getMethodName()); | ||
| sentryStackFrame.setFilename(item.getFileName()); | ||
| sentryStackFrame.setLineno(item.getLineNumber()); | ||
| sentryStackFrame.setNative(item.isNativeMethod()); | ||
|
|
||
| sentryStackFrames.add(sentryStackFrame); | ||
| } | ||
| } | ||
|
|
||
| return sentryStackFrames; | ||
| } | ||
| } |
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
33 changes: 33 additions & 0 deletions
33
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,33 @@ | ||
| 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}. | ||
| */ | ||
| @SuppressWarnings("serial") | ||
| public final class ExceptionMechanismThrowable extends Throwable { | ||
|
|
||
| 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; | ||
| } | ||
| } |
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.