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 32
Add Spring Integration. #539
Merged
Parent:
sentry-java v-next
Merged
Changes from all commits
Commits
Show all changes
21 commits
Select commit
Hold shift + click to select a range
a0ea203
Add Spring Integration.
maciejwalkowiak 0bee84f
Polish.
maciejwalkowiak c3f1a7c
Add custom EventProcessor to Spring Boot sample.
maciejwalkowiak cd37677
Clear breadcrumbs added during application startup.
maciejwalkowiak 3fcb157
Polish.
maciejwalkowiak 3d03787
Polish.
maciejwalkowiak ad53250
Polish
maciejwalkowiak f04bbd8
Merge remote-tracking branch 'origin/feat/sentry-java' into spring-in…
maciejwalkowiak b98827f
Set Java version and vendor in custom event processor in sample app.
maciejwalkowiak 8a63bf9
Autoconfigure multiple event processors and integrations if exist.
maciejwalkowiak 507a08d
Add convenient method to create HTTP breadcrumb.
maciejwalkowiak 7a65de2
Add convenient method to create HTTP breadcrumb.
maciejwalkowiak cc01bb9
Polish.
maciejwalkowiak 33c2d03
Set DSN using annotation attribute on `@EnableSentry`.
maciejwalkowiak ea83f4c
Deduplicate events based on the throwable.
maciejwalkowiak 7f317f2
Fix linter issue.
maciejwalkowiak 24c4465
Set Java runtime on events in sample event processor.
maciejwalkowiak b31f2d2
Polish.
maciejwalkowiak 250b299
Run tests on random port.
maciejwalkowiak 1672487
Do not clear breadcrumbs on http request.
maciejwalkowiak 97bf1a4
Polish.
maciejwalkowiak 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
75 changes: 75 additions & 0 deletions
75
sentry-core/src/main/java/io/sentry/core/DuplicateEventDetectionEventProcessor.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,75 @@ | ||
| package io.sentry.core; | ||
|
|
||
| import io.sentry.core.exception.ExceptionMechanismException; | ||
| import io.sentry.core.util.Objects; | ||
| import java.util.ArrayList; | ||
| import java.util.List; | ||
| import java.util.Map; | ||
| import java.util.WeakHashMap; | ||
| import org.jetbrains.annotations.NotNull; | ||
| import org.jetbrains.annotations.Nullable; | ||
|
|
||
| /** Deduplicates events containing throwable that has been already processed. */ | ||
| public final class DuplicateEventDetectionEventProcessor implements EventProcessor { | ||
| private final WeakHashMap<Throwable, Object> capturedObjects = new WeakHashMap<>(); | ||
| private final SentryOptions options; | ||
|
|
||
| public DuplicateEventDetectionEventProcessor(final @NotNull SentryOptions options) { | ||
| this.options = Objects.requireNonNull(options, "options are required"); | ||
| } | ||
|
|
||
| @Override | ||
| public SentryEvent process(final @NotNull SentryEvent event, final @Nullable Object hint) { | ||
| final Throwable throwable = event.getThrowable(); | ||
| if (throwable != null) { | ||
| if (throwable instanceof ExceptionMechanismException) { | ||
| final ExceptionMechanismException ex = (ExceptionMechanismException) throwable; | ||
| if (capturedObjects.containsKey(ex.getThrowable())) { | ||
| options | ||
| .getLogger() | ||
| .log( | ||
| SentryLevel.DEBUG, | ||
| "Duplicate Exception detected. Event %s will be discarded.", | ||
| event.getEventId()); | ||
| return null; | ||
| } else { | ||
| capturedObjects.put(ex.getThrowable(), null); | ||
| } | ||
| } else { | ||
| if (capturedObjects.containsKey(throwable) | ||
| || containsAnyKey(capturedObjects, allCauses(throwable))) { | ||
| options | ||
| .getLogger() | ||
| .log( | ||
| SentryLevel.DEBUG, | ||
| "Duplicate Exception detected. Event %s will be discarded.", | ||
| event.getEventId()); | ||
| return null; | ||
| } else { | ||
| capturedObjects.put(throwable, null); | ||
| } | ||
| } | ||
| } | ||
| return event; | ||
| } | ||
|
|
||
| private static <T> boolean containsAnyKey( | ||
| final @NotNull Map<T, Object> map, final @NotNull List<T> list) { | ||
| for (T entry : list) { | ||
| if (map.containsKey(entry)) { | ||
| return true; | ||
| } | ||
| } | ||
| return false; | ||
| } | ||
|
|
||
| private static @NotNull List<Throwable> allCauses(final @NotNull Throwable throwable) { | ||
| final List<Throwable> causes = new ArrayList<>(); | ||
| Throwable ex = throwable; | ||
| while (ex.getCause() != null) { | ||
| causes.add(ex.getCause()); | ||
| ex = ex.getCause(); | ||
| } | ||
| return causes; | ||
| } | ||
| } |
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
70 changes: 70 additions & 0 deletions
70
sentry-core/src/test/java/io/sentry/core/DuplicateEventDetectionEventProcessorTest.kt
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,70 @@ | ||
| package io.sentry.core | ||
|
|
||
| import io.sentry.core.exception.ExceptionMechanismException | ||
| import io.sentry.core.protocol.Mechanism | ||
| import java.lang.RuntimeException | ||
| import kotlin.test.Test | ||
| import kotlin.test.assertNotNull | ||
| import kotlin.test.assertNull | ||
|
|
||
| class DuplicateEventDetectionEventProcessorTest { | ||
|
|
||
| val processor = DuplicateEventDetectionEventProcessor(SentryOptions()) | ||
|
|
||
| @Test | ||
| fun `does not drop event if no previous event with same exception was processed`() { | ||
| processor.process(SentryEvent(), null) | ||
|
|
||
| val result = processor.process(SentryEvent(RuntimeException()), null) | ||
|
|
||
| assertNotNull(result) | ||
| } | ||
|
|
||
| @Test | ||
| fun `drops event with the same exception`() { | ||
| val event = SentryEvent(RuntimeException()) | ||
| processor.process(event, null) | ||
|
|
||
| val result = processor.process(event, null) | ||
| assertNull(result) | ||
| } | ||
|
|
||
| @Test | ||
| fun `drops event with mechanism exception having an exception that has already been processed`() { | ||
| val event = SentryEvent(RuntimeException()) | ||
| processor.process(event, null) | ||
|
|
||
| val result = processor.process(SentryEvent(ExceptionMechanismException(Mechanism(), event.throwable, null)), null) | ||
| assertNull(result) | ||
| } | ||
|
|
||
| @Test | ||
| fun `drops event with exception that has already been processed with event with mechanism exception`() { | ||
| val sentryEvent = SentryEvent(ExceptionMechanismException(Mechanism(), RuntimeException(), null)) | ||
| processor.process(sentryEvent, null) | ||
|
|
||
| val result = processor.process(SentryEvent((sentryEvent.throwable as ExceptionMechanismException).throwable), null) | ||
|
|
||
| assertNull(result) | ||
| } | ||
|
|
||
| @Test | ||
| fun `drops event with the cause equal to exception in already processed event`() { | ||
| val event = SentryEvent(RuntimeException()) | ||
| processor.process(event, null) | ||
|
|
||
| val result = processor.process(SentryEvent(RuntimeException(event.throwable)), null) | ||
|
|
||
| assertNull(result) | ||
| } | ||
|
|
||
| @Test | ||
| fun `drops event with any of the causes has been already processed`() { | ||
| val event = SentryEvent(RuntimeException()) | ||
| processor.process(event, null) | ||
|
|
||
| val result = processor.process(SentryEvent(RuntimeException(RuntimeException(event.throwable))), null) | ||
|
|
||
| assertNull(result) | ||
| } | ||
| } |
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
...samples-spring-boot/src/main/java/io/sentry/samples/spring/boot/CustomEventProcessor.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.samples.spring.boot; | ||
|
|
||
| import io.sentry.core.EventProcessor; | ||
| import io.sentry.core.SentryEvent; | ||
| import io.sentry.core.protocol.SentryRuntime; | ||
| import org.jetbrains.annotations.Nullable; | ||
| import org.springframework.stereotype.Component; | ||
|
|
||
| /** | ||
| * Custom {@link EventProcessor} implementation lets modifying {@link SentryEvent}s before they are | ||
| * sent to Sentry. | ||
| */ | ||
| @Component | ||
| public class CustomEventProcessor implements EventProcessor { | ||
| private final String javaVersion; | ||
| private final String javaVendor; | ||
|
|
||
| public CustomEventProcessor(String javaVersion, String javaVendor) { | ||
| this.javaVersion = javaVersion; | ||
| this.javaVendor = javaVendor; | ||
| } | ||
|
|
||
| public CustomEventProcessor() { | ||
| this(System.getProperty("java.version"), System.getProperty("java.vendor")); | ||
| } | ||
|
|
||
| @Override | ||
| public SentryEvent process(SentryEvent event, @Nullable Object hint) { | ||
| final SentryRuntime runtime = new SentryRuntime(); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Would be nice to do this to all events (put it in the main event processor) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yeah it won't, we could have the processor on sentry-core but either it gets added to the processor list only if its non-Android or we do a conditional flag. |
||
| runtime.setVersion(javaVersion); | ||
| runtime.setName(javaVendor); | ||
| event.getContexts().setRuntime(runtime); | ||
| return event; | ||
| } | ||
| } | ||
24 changes: 24 additions & 0 deletions
24
...amples/sentry-samples-spring-boot/src/main/java/io/sentry/samples/spring/boot/Person.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,24 @@ | ||
| package io.sentry.samples.spring.boot; | ||
|
|
||
| public class Person { | ||
| private final String firstName; | ||
| private final String lastName; | ||
|
|
||
| public Person(String firstName, String lastName) { | ||
| this.firstName = firstName; | ||
| this.lastName = lastName; | ||
| } | ||
|
|
||
| public String getFirstName() { | ||
| return firstName; | ||
| } | ||
|
|
||
| public String getLastName() { | ||
| return lastName; | ||
| } | ||
|
|
||
| @Override | ||
| public String toString() { | ||
| return "Person{" + "firstName='" + firstName + '\'' + ", lastName='" + lastName + '\'' + '}'; | ||
| } | ||
| } |
28 changes: 28 additions & 0 deletions
28
...try-samples-spring-boot/src/main/java/io/sentry/samples/spring/boot/PersonController.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,28 @@ | ||
| package io.sentry.samples.spring.boot; | ||
|
|
||
| import org.slf4j.Logger; | ||
| import org.slf4j.LoggerFactory; | ||
| import org.springframework.web.bind.annotation.GetMapping; | ||
| import org.springframework.web.bind.annotation.PathVariable; | ||
| import org.springframework.web.bind.annotation.PostMapping; | ||
| import org.springframework.web.bind.annotation.RequestBody; | ||
| import org.springframework.web.bind.annotation.RequestMapping; | ||
| import org.springframework.web.bind.annotation.RestController; | ||
|
|
||
| @RestController | ||
| @RequestMapping("/person/") | ||
| public class PersonController { | ||
| private static final Logger LOGGER = LoggerFactory.getLogger(PersonController.class); | ||
|
|
||
| @GetMapping("{id}") | ||
| Person person(@PathVariable Long id) { | ||
| LOGGER.info("Loading person with id={}", id); | ||
| throw new IllegalArgumentException("Something went wrong [id=" + id + "]"); | ||
| } | ||
|
|
||
| @PostMapping | ||
| Person create(@RequestBody Person person) { | ||
| LOGGER.warn("Creating person: {}", person); | ||
| return person; | ||
| } | ||
| } |
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.