搭建基于SpringBoot+sentry的日志报警系统

我们可以通过Sentry来收集线上错误日志并进行告警、监控及任务分配处理

初始化项目

通过Spring initializr初始化项目。

依赖:

  • Spring Web

集成Sentry

pom.xml中dependencies节点下添加如下配置

<dependency>
    <groupId>io.sentry</groupId>
    <artifactId>sentry-spring</artifactId>
    <version>1.7.30</version>
</dependency>

添加配置文件SentryConfig.java

@Configuration
public class SentryConfig {

    @Bean
    public HandlerExceptionResolver sentryExceptionResolver() {
        return new io.sentry.spring.SentryExceptionResolver();
    }

    @Bean
    public ServletContextInitializer sentryServletContextInitializer() {
        return new io.sentry.spring.SentryServletContextInitializer();
    }

}

添加测试的DefaultController.java

package com.testdemo.sentry;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class SentryController {

    @GetMapping
    public void index() {
        throw new NullPointerException();
    }

    @GetMapping(value = "/user")
    public void getUser() {
        throw new IndexOutOfBoundsException("user size=10,req=11");
    }
}

打包Spring-boot程序后启动

$ ./mvnw package
$ java -jar target/sentry-0.0.1-SNAPSHOT.jar
 .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::        (v2.2.6.RELEASE)

2020-04-08 09:36:53.611  INFO 55482 --- [           main] com.testdemo.sentry.SentryApplication  : Starting SentryApplication v0.0.1-SNAPSHOT on testdemo-MacBook-Pro.local with PID 55482 (/Users/bohan/Downloads/sentry/target/sentry-0.0.1-SNAPSHOT.jar started by bohan in /Users/testdemo/Downloads/sentry)
2020-04-08 09:36:53.613  INFO 55482 --- [           main] com.bohanzhang.sentry.SentryApplication  : No active profile set, falling back to default profiles: default
2020-04-08 09:36:54.350  INFO 55482 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 8080 (http)
2020-04-08 09:36:54.361  INFO 55482 --- [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2020-04-08 09:36:54.361  INFO 55482 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet engine: [Apache Tomcat/9.0.33]
2020-04-08 09:36:54.418  INFO 55482 --- [           main] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2020-04-08 09:36:54.418  INFO 55482 --- [           main] o.s.web.context.ContextLoader            : Root WebApplicationContext: initialization completed in 765 ms
2020-04-08 09:36:54.549  INFO 55482 --- [           main] o.s.s.concurrent.ThreadPoolTaskExecutor  : Initializing ExecutorService 'applicationTaskExecutor'
2020-04-08 09:36:54.680  INFO 55482 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8080 (http) with context path ''
2020-04-08 09:36:54.683  INFO 55482 --- [           main] com.testdemo.sentry.SentryApplication  : Started SentryApplication in 1.34 seconds (JVM running for 1.678)

启动后访问:http://127.0.0.1:8080报错,我们就完成了Sentry的基本集成,查看日志发现由于我们没有配置DSN,Sentry不会做任何操作

*** Couldn't find a suitable DSN, Sentry operations will do nothing! See documentation: https://docs.sentry.io/clients/java/ ***

我们在官网sentry.io中注册登录后创建我们的Sentry项目

 

创建成功后在配置中找到DNS,并复制

配置DSN:

在SpringBoot项目的resources目录新建一个sentry.properties,并添加以下配置

stacktrace.app.packages=   #关闭无关的warning
dsn=https://public:private@host:port/1  #位置setting-project(选中你sentry项目)-SDK-SETUP-Client keys(DSN),第一个DSN

 

请求DefaultController 的,会抛出异常,此时登录sentry查看,成功收集到了第一条错误

 

  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
Spring Boot 可以很方便地集成 Sentry,以下是集成步骤: 1. 在 `pom.xml` 文件中添加 Sentry 依赖: ```xml <dependency> <groupId>io.sentry</groupId> <artifactId>sentry-spring-boot-starter</artifactId> <version>3.1.0</version> </dependency> ``` 2. 在 `application.properties` 或 `application.yml` 文件中配置 Sentry: ```properties sentry.dsn=YOUR_DSN sentry.release=YOUR_RELEASE_VERSION sentry.environment=YOUR_ENVIRONMENT ``` 其中,`YOUR_DSN` 是你的 Sentry DSN(Data Source Name),`YOUR_RELEASE_VERSION` 是你的应用版本号,`YOUR_ENVIRONMENT` 是你的应用环境(如 `production`、`development` 等)。 3. 创建一个全局异常处理器类,用于捕获异常并发送到 Sentry: ```java import io.sentry.Sentry; import org.springframework.web.bind.annotation.ControllerAdvice; import org.springframework.web.bind.annotation.ExceptionHandler; import org.springframework.web.servlet.ModelAndView; @ControllerAdvice public class GlobalExceptionHandler { @ExceptionHandler(Exception.class) public ModelAndView handleException(Exception e) { // 发送异常信息到 Sentry Sentry.captureException(e); // 返回错误页面或其他处理方式 ModelAndView modelAndView = new ModelAndView(); modelAndView.setViewName("error"); modelAndView.addObject("errorMessage", "An error occurred"); return modelAndView; } } ``` 以上步骤完成后,当应用发生异常时,Sentry 将会自动捕获并发送相应的异常信息。你可以登录 Sentry 平台查看应用的异常报告和性能指标。 希望以上信息对你有帮助!如果你还有其他问题,请继续提问。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Venlenter

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值