1. 程式人生 > >多語言業務錯誤日誌收集監控工具Sentry 安裝與使用

多語言業務錯誤日誌收集監控工具Sentry 安裝與使用

Sentry 是一個實時事件日誌記錄和彙集的平臺。其專注於錯誤監控以及提取一切事後處理所需資訊而不依賴於麻煩的使用者反饋。

Sentry是一個日誌平臺, 它分為客戶端和服務端,客戶端(目前客戶端有Python, PHP,C#, Ruby等多種語言)就嵌入在你的應用程式中間,程式出現異常就向服務端傳送訊息,服務端將訊息記錄到資料庫中並提供一個web節目方便檢視。Sentry由python編寫,原始碼開放,效能卓越,易於擴充套件,目前著名的使用者有Disqus, Path, mozilla, Pinterest等。

通過docker安裝

官方提供了 On-Premise 安裝方式

git clone https://github.com/getsentry/onpremise
cd onpremise

然後建立volume,持久化儲存

docker volume create --name=sentry-data && docker volume create --name=sentry-postgres

建立環境變數配置檔案:

cp -n .env.example .env - create env config file

docker-compose build映象,這一步會拉取官方映象和redis等依賴,耐心等待 :

docker-compose build - Build and tag the Docker services

建立secret-key,執行後得到一個key,新增到.env中的SENTRY_SECRET_KEY

docker-compose run --rm web config generate-secret-key 

建立DB和初始化使用者,等待建立資料庫和

docker-compose run --rm web upgrade
Created internal Sentry project (slug=internal, id=1)

Would you like to create a user account now? [Y/n]: y
Email: 
Password: 
Repeat for confirmation: 
Should this user be a superuser? [y/N]: y
User created: [email protected]
Added to organization: sentry
Running migrations for sentry.nodestore:
 - Migrating forwards to 0001_initial.

最後-d啟動,啟動成功後可以訪問http://server-ip:9000

docker-compose up -d 

訪問:

建立專案並整合

先建立一個project,支援多種語言,我們建立一個java的

DSN(Client Keys)

DSN格式為:

DSN="https://public:private@host:port/project_id"

可以在專案的setting-Client Keys(DSN)裡檢視。

然後根據格式拼成DSN即可。

java專案整合

有多種整合方式:

基本整合方式

新增maven引用:

    <dependency>
        <groupId>io.sentry</groupId>
        <artifactId>sentry</artifactId>
        <version>1.7.16</version>
    </dependency>

然後在全域性異常處理裡整合:

 String dsn = "http://<SENTRY_PUBLIC_KEY>:<SENTRY_PRIVATE_KEY>@host:port/<PROJECT_ID>";
 Sentry.init(dsn);

在有異常的地方:

      try {
            unsafeMethod();
        } catch (Exception e) {
            // This sends an exception event to Sentry using the statically stored instance
            // that was created in the ``main`` method.
            Sentry.capture(e);
        }

logback整合

logback配置:

<configuration>

  <appender name="Console" class="ch.qos.logback.core.ConsoleAppender">
    <encoder>
      <pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
    </encoder>
  </appender>

  <appender name="Sentry" class="io.sentry.logback.SentryAppender">
    <filter class="ch.qos.logback.classic.filter.ThresholdFilter">
      <level>WARN</level>
    </filter>
  </appender>

  <root level="DEBUG">
    <appender-ref ref="Console" />
    <appender-ref ref="Sentry"/>
  </root>

</configuration>

spring-boot整合

先新增引用:

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

在Application裡整合:

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.ServletContextInitializer;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.HandlerExceptionResolver;

@Controller
@EnableAutoConfiguration
@SpringBootApplication
public class Application {
    /*
    Register a HandlerExceptionResolver that sends all exceptions to Sentry
    and then defers all handling to the other HandlerExceptionResolvers.
    You should only register this is you are not using a logging integration,
    otherwise you may double report exceptions.
     */
    @Bean
    public HandlerExceptionResolver sentryExceptionResolver() {
        return new io.sentry.spring.SentryExceptionResolver();
    }

    /*
    Register a ServletContextInitializer that installs the SentryServletRequestListener
    so that Sentry events contain HTTP request information.
    This should only be necessary in Spring Boot applications. "Classic" Spring
    should automatically load the `io.sentry.servlet.SentryServletContainerInitializer`.
     */
    @Bean
    public ServletContextInitializer sentryServletContextInitializer() {
        return new io.sentry.spring.SentryServletContextInitializer();
    }

    @RequestMapping("/")
    @ResponseBody
    String home() {
        int x = 1 / 0;

        return "Hello World!";
    }

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}

前端專案整合

前端整合更簡單,引用sentry的js,然後init即可

<script src="https://browser.sentry-cdn.com/5.1.0/bundle.min.js" crossorigin="anonymous"></script>
Sentry.init({ dsn: 'dsn' });

作者:Jadepeng
出處:jqpeng的技術記事本--http://www.cnblogs.com/xiaoqi
您的支援是對博主最大的鼓勵,感謝您的認真閱讀。
本文版權歸作者所有,歡迎轉載,但未經作者同意必須保留此段宣告,且在文章頁面明顯位置給出原文連線,否則保留追究法律責任的權利。