-
-
Notifications
You must be signed in to change notification settings - Fork 461
SentryCheckIn annotation and advice config for Spring #2946
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
adinauer
merged 28 commits into
feat/crons-lib-support
from
feat/crons-spring-scheduled
Sep 27, 2023
Merged
Changes from all commits
Commits
Show all changes
28 commits
Select commit
Hold shift + click to select a range
3b2debe
checkin
adinauer 1de0ba7
Add trace context, release and env; factory methods; fix serializatio…
adinauer 3dd4fce
add tests
adinauer 41cd16d
remove serial version uid
adinauer 97b8e63
Merge branch 'main' into feat/crons
adinauer 083e021
add back serial version uid
adinauer 4828fa3
quartz support for spring jakarta
adinauer ef54e76
changelog
adinauer cc23d3c
code review changes; add sample code to spring scheduled job
adinauer 6038dc5
Merge branch 'feat/crons' into feat/crons-quartz
adinauer 44a3c75
add checkin example to scheduled job
adinauer b558570
Merge branch 'feat/crons' into feat/crons-quartz
adinauer 0c29f12
Move quartz job listener into a separate module
adinauer 24215e5
add quartz auto config to spring boot 2
adinauer 68931af
Merge branch 'main' into feat/crons-quartz
adinauer a709a40
fix comment
adinauer 372a2f0
Add enableAutomaticCheckIns and ignoreCheckIns options
adinauer 66cb754
move __ slug logic
adinauer f4fb1dd
fix sample by adding quartz lib
adinauer 2e7fb74
Merge branch 'feat/crons-quartz' into feat/crons-options
adinauer 86e5ded
changelog
adinauer 409827e
mark crons features experimental
adinauer 49adb9e
SentryCheckIn annotation and advice config for jakarta
adinauer e522b75
SentryCheckIn annotation and advice config for non jakarta
adinauer 8b721cb
Merge branch 'feat/crons-lib-support' into feat/crons-spring-scheduled
adinauer b5e1646
changelog
adinauer e37edb4
code review changes
adinauer 8179966
Apply suggestions from code review
adinauer 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
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
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
41 changes: 41 additions & 0 deletions
41
sentry-spring-jakarta/src/main/java/io/sentry/spring/jakarta/checkin/SentryCheckIn.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,41 @@ | ||
| package io.sentry.spring.jakarta.checkin; | ||
|
|
||
| import java.lang.annotation.ElementType; | ||
| import java.lang.annotation.Retention; | ||
| import java.lang.annotation.RetentionPolicy; | ||
| import java.lang.annotation.Target; | ||
| import org.jetbrains.annotations.ApiStatus; | ||
| import org.springframework.core.annotation.AliasFor; | ||
|
|
||
| /** Sends a {@link io.sentry.CheckIn} for the annotated method. */ | ||
| @Retention(RetentionPolicy.RUNTIME) | ||
| @Target({ElementType.METHOD}) | ||
| @ApiStatus.Experimental | ||
| public @interface SentryCheckIn { | ||
|
|
||
| /** | ||
| * Monitor slug. If not set, no check-in will be sent. | ||
| * | ||
| * @return monitor slug | ||
| */ | ||
| @AliasFor("value") | ||
| String monitorSlug() default ""; | ||
|
|
||
| /** | ||
| * Whether to send only send heartbeat events. | ||
| * | ||
| * <p>A hearbeat check-in means there's no separate IN_PROGRESS check-in at the start of the jobs | ||
| * execution. Only the check-in after finishing the job will be sent. | ||
| * | ||
| * @return true if only heartbeat check-ins should be sent. | ||
| */ | ||
| boolean heartbeat() default false; | ||
|
|
||
| /** | ||
| * Monitor slug. If not set, no check-in will be sent. | ||
| * | ||
| * @return monitor slug | ||
| */ | ||
| @AliasFor("monitorSlug") | ||
| String value() default ""; | ||
| } |
78 changes: 78 additions & 0 deletions
78
...ry-spring-jakarta/src/main/java/io/sentry/spring/jakarta/checkin/SentryCheckInAdvice.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,78 @@ | ||
| package io.sentry.spring.jakarta.checkin; | ||
|
|
||
| import com.jakewharton.nopen.annotation.Open; | ||
| import io.sentry.CheckIn; | ||
| import io.sentry.CheckInStatus; | ||
| import io.sentry.DateUtils; | ||
| import io.sentry.IHub; | ||
| import io.sentry.SentryLevel; | ||
| import io.sentry.protocol.SentryId; | ||
| import io.sentry.util.Objects; | ||
| import java.lang.reflect.Method; | ||
| import org.aopalliance.intercept.MethodInterceptor; | ||
| import org.aopalliance.intercept.MethodInvocation; | ||
| import org.jetbrains.annotations.ApiStatus; | ||
| import org.jetbrains.annotations.NotNull; | ||
| import org.jetbrains.annotations.Nullable; | ||
| import org.springframework.aop.support.AopUtils; | ||
| import org.springframework.core.annotation.AnnotationUtils; | ||
| import org.springframework.util.ObjectUtils; | ||
|
|
||
| /** | ||
| * Reports execution of every bean method annotated with {@link SentryCheckIn} as a monitor | ||
| * check-in. | ||
| */ | ||
| @ApiStatus.Internal | ||
| @ApiStatus.Experimental | ||
| @Open | ||
| public class SentryCheckInAdvice implements MethodInterceptor { | ||
| private final @NotNull IHub hub; | ||
|
|
||
| public SentryCheckInAdvice(final @NotNull IHub hub) { | ||
| this.hub = Objects.requireNonNull(hub, "hub is required"); | ||
| } | ||
|
|
||
| @Override | ||
| public Object invoke(final @NotNull MethodInvocation invocation) throws Throwable { | ||
| final Method mostSpecificMethod = | ||
| AopUtils.getMostSpecificMethod(invocation.getMethod(), invocation.getThis().getClass()); | ||
|
|
||
| @Nullable | ||
| SentryCheckIn checkInAnnotation = | ||
| AnnotationUtils.findAnnotation(mostSpecificMethod, SentryCheckIn.class); | ||
| if (checkInAnnotation == null) { | ||
| return invocation.proceed(); | ||
| } | ||
|
|
||
| final boolean isHeartbeatOnly = checkInAnnotation.heartbeat(); | ||
| final @Nullable String monitorSlug = checkInAnnotation.value(); | ||
|
|
||
| if (ObjectUtils.isEmpty(monitorSlug)) { | ||
| hub.getOptions() | ||
| .getLogger() | ||
| .log( | ||
| SentryLevel.WARNING, | ||
| "Not capturing check-in for method annotated with @SentryCheckIn because it does not specify a monitor slug."); | ||
| return invocation.proceed(); | ||
| } | ||
|
|
||
| @Nullable SentryId checkInId = null; | ||
| final long startTime = System.currentTimeMillis(); | ||
| boolean didError = false; | ||
|
|
||
| try { | ||
| if (!isHeartbeatOnly) { | ||
| checkInId = hub.captureCheckIn(new CheckIn(monitorSlug, CheckInStatus.IN_PROGRESS)); | ||
| } | ||
| return invocation.proceed(); | ||
| } catch (Throwable e) { | ||
| didError = true; | ||
| throw e; | ||
| } finally { | ||
| final @NotNull CheckInStatus status = didError ? CheckInStatus.ERROR : CheckInStatus.OK; | ||
| CheckIn checkIn = new CheckIn(checkInId, monitorSlug, status); | ||
| checkIn.setDuration(DateUtils.millisToSeconds(System.currentTimeMillis() - startTime)); | ||
| hub.captureCheckIn(checkIn); | ||
| } | ||
| } | ||
| } | ||
31 changes: 31 additions & 0 deletions
31
...arta/src/main/java/io/sentry/spring/jakarta/checkin/SentryCheckInAdviceConfiguration.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.spring.jakarta.checkin; | ||
|
|
||
| import com.jakewharton.nopen.annotation.Open; | ||
| import io.sentry.IHub; | ||
| import org.aopalliance.aop.Advice; | ||
| import org.jetbrains.annotations.ApiStatus; | ||
| import org.jetbrains.annotations.NotNull; | ||
| import org.springframework.aop.Advisor; | ||
| import org.springframework.aop.Pointcut; | ||
| import org.springframework.aop.support.DefaultPointcutAdvisor; | ||
| import org.springframework.beans.factory.annotation.Qualifier; | ||
| import org.springframework.context.annotation.Bean; | ||
| import org.springframework.context.annotation.Configuration; | ||
|
|
||
| @Configuration(proxyBeanMethods = false) | ||
| @Open | ||
| @ApiStatus.Experimental | ||
| public class SentryCheckInAdviceConfiguration { | ||
|
|
||
| @Bean | ||
| public @NotNull Advice sentryCheckInAdvice(final @NotNull IHub hub) { | ||
| return new SentryCheckInAdvice(hub); | ||
| } | ||
|
|
||
| @Bean | ||
| public @NotNull Advisor sentryCheckInAdvisor( | ||
| final @NotNull @Qualifier("sentryCheckInPointcut") Pointcut sentryCheckInPointcut, | ||
| final @NotNull @Qualifier("sentryCheckInAdvice") Advice sentryCheckInAdvice) { | ||
| return new DefaultPointcutAdvisor(sentryCheckInPointcut, sentryCheckInAdvice); | ||
| } | ||
| } |
29 changes: 29 additions & 0 deletions
29
...ta/src/main/java/io/sentry/spring/jakarta/checkin/SentryCheckInPointcutConfiguration.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,29 @@ | ||
| package io.sentry.spring.jakarta.checkin; | ||
|
|
||
| import com.jakewharton.nopen.annotation.Open; | ||
| import org.jetbrains.annotations.ApiStatus; | ||
| import org.jetbrains.annotations.NotNull; | ||
| import org.springframework.aop.Pointcut; | ||
| import org.springframework.aop.support.ComposablePointcut; | ||
| import org.springframework.aop.support.annotation.AnnotationClassFilter; | ||
| import org.springframework.aop.support.annotation.AnnotationMatchingPointcut; | ||
| import org.springframework.context.annotation.Bean; | ||
| import org.springframework.context.annotation.Configuration; | ||
|
|
||
| /** AOP pointcut configuration for {@link SentryCheckIn}. */ | ||
| @Configuration(proxyBeanMethods = false) | ||
| @Open | ||
| @ApiStatus.Experimental | ||
| public class SentryCheckInPointcutConfiguration { | ||
|
|
||
| /** | ||
| * Pointcut around which check-ins are created. | ||
| * | ||
| * @return pointcut used by {@link SentryCheckInAdvice}. | ||
| */ | ||
| @Bean | ||
| public @NotNull Pointcut sentryCheckInPointcut() { | ||
| return new ComposablePointcut(new AnnotationClassFilter(SentryCheckIn.class, true)) | ||
| .union(new AnnotationMatchingPointcut(null, SentryCheckIn.class)); | ||
| } | ||
| } |
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.