From 81fef80c51493a8a545752690748e39fe9679203 Mon Sep 17 00:00:00 2001 From: hccake Date: Thu, 6 Aug 2020 14:14:19 +0800 Subject: [PATCH] =?UTF-8?q?:sparkles:=20=E6=8F=90=E4=BE=9B=20sentry-stater?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../mall-spring-boot-starter-sentry/pom.xml | 25 ++++++++++++ .../config/CustomSentryAutoConfiguration.java | 38 +++++++++++++++++++ .../resolver/DoNothingExceptionResolver.java | 32 ++++++++++++++++ .../main/resources/META-INF/spring.factories | 2 + common/pom.xml | 1 + mall-dependencies/pom.xml | 13 +++++++ 6 files changed, 111 insertions(+) create mode 100644 common/mall-spring-boot-starter-sentry/pom.xml create mode 100644 common/mall-spring-boot-starter-sentry/src/main/java/cn/iocoder/mall/sentry/config/CustomSentryAutoConfiguration.java create mode 100644 common/mall-spring-boot-starter-sentry/src/main/java/cn/iocoder/mall/sentry/resolver/DoNothingExceptionResolver.java create mode 100644 common/mall-spring-boot-starter-sentry/src/main/resources/META-INF/spring.factories diff --git a/common/mall-spring-boot-starter-sentry/pom.xml b/common/mall-spring-boot-starter-sentry/pom.xml new file mode 100644 index 00000000..54c0edd9 --- /dev/null +++ b/common/mall-spring-boot-starter-sentry/pom.xml @@ -0,0 +1,25 @@ + + + + common + cn.iocoder.mall + 1.0-SNAPSHOT + + 4.0.0 + + mall-spring-boot-starter-sentry + + + + io.sentry + sentry-spring-boot-starter + + + io.sentry + sentry-logback + + + + \ No newline at end of file diff --git a/common/mall-spring-boot-starter-sentry/src/main/java/cn/iocoder/mall/sentry/config/CustomSentryAutoConfiguration.java b/common/mall-spring-boot-starter-sentry/src/main/java/cn/iocoder/mall/sentry/config/CustomSentryAutoConfiguration.java new file mode 100644 index 00000000..05bb0374 --- /dev/null +++ b/common/mall-spring-boot-starter-sentry/src/main/java/cn/iocoder/mall/sentry/config/CustomSentryAutoConfiguration.java @@ -0,0 +1,38 @@ +package cn.iocoder.mall.sentry.config; + +import cn.iocoder.mall.sentry.resolver.DoNothingExceptionResolver; +import io.sentry.spring.SentryExceptionResolver; +import io.sentry.spring.autoconfigure.SentryAutoConfiguration; +import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; +import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; +import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; +import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.web.servlet.HandlerExceptionResolver; + +/** + * 自定义的 Sentry 自动配置类 + * + * @author Hccake 2020/8/6 + * @version 1.0 + */ +@ConditionalOnClass({HandlerExceptionResolver.class, SentryExceptionResolver.class}) +@ConditionalOnWebApplication +@ConditionalOnProperty(name = "sentry.enabled", havingValue = "true", matchIfMissing = true) +@Configuration(proxyBeanMethods = false) +public class CustomSentryAutoConfiguration { + + /** + * 用于覆盖原有的 SentryStarter 提供的 SentryExceptionResolver 操作 + * 解决使用 log appender 形式推送错误信息与全局异常捕获导致重复推送的情况 + * + * @return DoNothingExceptionResolver + */ + @Bean + @ConditionalOnClass(SentryAutoConfiguration.class) + @ConditionalOnMissingBean(SentryExceptionResolver.class) + public SentryExceptionResolver doNothingExceptionResolver() { + return new DoNothingExceptionResolver(); + } +} diff --git a/common/mall-spring-boot-starter-sentry/src/main/java/cn/iocoder/mall/sentry/resolver/DoNothingExceptionResolver.java b/common/mall-spring-boot-starter-sentry/src/main/java/cn/iocoder/mall/sentry/resolver/DoNothingExceptionResolver.java new file mode 100644 index 00000000..6cd0ac8e --- /dev/null +++ b/common/mall-spring-boot-starter-sentry/src/main/java/cn/iocoder/mall/sentry/resolver/DoNothingExceptionResolver.java @@ -0,0 +1,32 @@ +package cn.iocoder.mall.sentry.resolver; + +import io.sentry.spring.SentryExceptionResolver; +import org.springframework.web.servlet.ModelAndView; + +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; + +/** + * 默认什么也不做的 SentryExceptionResolver + * + * @author Hccake 2020/8/6 + * @version 1.0 + */ +public class DoNothingExceptionResolver extends SentryExceptionResolver { + + @Override + public ModelAndView resolveException(HttpServletRequest request, + HttpServletResponse response, + Object handler, + Exception ex) { + // do nothing here + + // null = run other HandlerExceptionResolvers to actually handle the exception + return null; + } + + @Override + public int getOrder() { + return Integer.MIN_VALUE; + } +} \ No newline at end of file diff --git a/common/mall-spring-boot-starter-sentry/src/main/resources/META-INF/spring.factories b/common/mall-spring-boot-starter-sentry/src/main/resources/META-INF/spring.factories new file mode 100644 index 00000000..befade37 --- /dev/null +++ b/common/mall-spring-boot-starter-sentry/src/main/resources/META-INF/spring.factories @@ -0,0 +1,2 @@ +org.springframework.boot.autoconfigure.EnableAutoConfiguration=\ +cn.iocoder.mall.sentry.config.CustomSentryAutoConfiguration diff --git a/common/pom.xml b/common/pom.xml index 6a472828..34ea1fa2 100644 --- a/common/pom.xml +++ b/common/pom.xml @@ -19,6 +19,7 @@ mall-security-annotations mall-spring-boot-starter-security-admin mall-spring-boot-starter-security-user + mall-spring-boot-starter-sentry mall-spring-boot-starter-mybatis mall-spring-boot-starter-dubbo mall-spring-boot-starter-system-error-code diff --git a/mall-dependencies/pom.xml b/mall-dependencies/pom.xml index 3bdbe443..da371860 100644 --- a/mall-dependencies/pom.xml +++ b/mall-dependencies/pom.xml @@ -55,6 +55,7 @@ 8.0.1 2.2.2 + 1.7.30 1.2.56 6.0.16.Final @@ -275,6 +276,17 @@ ${spring-boot-admin-starter-client.version} + + io.sentry + sentry-logback + ${sentry.version} + + + io.sentry + sentry-spring-boot-starter + ${sentry.version} + + @@ -350,6 +362,7 @@ hibernate-validator ${hibernate-validator.version} +