搭建基於SpringBoot+sentry的日誌報警系統

2020-08-10 10:37:27

我們可以通過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檢視,成功收集到了第一條錯誤