编程开源技术交流,分享技术与知识

网站首页 > 开源技术 正文

Sentry与SpringBoot整合(springboot整合servlet)

wxchong 2024-08-31 04:10:55 开源技术 9 ℃ 0 评论

背景

对于任何一个复杂项目,在测试环境上测试肯定做不到100%的测试,我们也不能保证用户能按照我们的预期进行操作。

所以用户才是最好的测试者,但是我们不能奢求每个用户遇到问题时候都会主动向我们反馈,即使反馈了,我们也很难及时的找到相关原因。

因此,我们需要在项目出现异常时主动对其进行收集上报,分析原因和影响后制定下一步解决方案。

Sentry 介绍

Sentry是一个集中式日志管理系统。它具备以下优点:

  1. 多项目,多用户
  2. 界面友好
  3. 可以配置异常出发规则,例如发送邮件
  4. 支持主流语言接口

Sentry不但有多种语言的客户端,还直接支持大量的日志框架,比如java的log4j,log4j2,logback。而且我们之前的代码几乎可以不用做任何修改,而仅仅加一点配置即可。

在Sentry的项目 Dashboard 你可以浏览到更详细的报告,比如按照异常信息的类别进行分类和过滤,也可以统计近期异常的状态和频率,非常方便。

另外你还可以针对异常问题进行分配、跟踪、关键字查找,例如指派团队的某个成员去处理某一类问题,对于长时间没有再发生的问题自动标记为解决等等

构建sentry-spring-boot-starter

在Sentry-log4j2 和 Sentry-Spring中 官方推荐使用 Sentry-log4j2 来实现issue event的发送,Sentry-log4j2不仅仅可以发送exception,还可以发送指定配置的log level

@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@Import({SentryAutoConfiguration.class})
public @interface EnableSentryPlus {
}

@Configuration
@ConditionalOnClass(Sentry.class)
@EnableConfigurationProperties(SentryProperties.class)
public class SentryAutoConfiguration {
 @Resource
 private Environment environment;
 @Bean
 public SentryClient sentry(SentryProperties properties) {
 String activeEnvironment = Arrays.stream(environment.getActiveProfiles()).collect(Collectors.joining(","));
 SentryClient sentryClient = Sentry.init(properties.getDsn());
 sentryClient.setEnvironment(activeEnvironment);
 sentryClient.setServerName(environment.getProperty("spring.application.name"));
 sentryClient.setRelease(properties.getRelease());
 return sentryClient;
 }
}

Sentry 默认是异步发送issue事件,所以不会对性能造成影响

与springboot整合

<dependency>
 <groupId>com.kinsey.sentry</groupId>
 <artifactId>sentry-spring-boot-starter</artifactId>
 <version>1.0.0.RELEASE</version>
 </dependency>

在application.yml中配置 dsn就可以了

sentry:
 dsn: https://fb41f383128b4f9099739855e09a22d3@sentry.io/1354170
 release: 1.0

在Application启动类上加上 @EnableSentryPlus即可

@EnableSentryPlus
@SpringBootApplication
public class SpringBootSentryDemoApplication {
 public static void main(String[] args) {
 SpringApplication.run(SpringBootSentryDemoApplication.class, args);
 }
}

sentry效果图

获取DSN地址方式

监听界面

详细错误信息

谢谢大家观看,这是我的第一篇文章,接下来将详细介绍SpringCloud使用

Tags:

本文暂时没有评论,来添加一个吧(●'◡'●)

欢迎 发表评论:

最近发表
标签列表