集成SpringBoot和Sentry

Sentry是一款錯誤追蹤系統,可以幫助我們發現並定位邏輯異常。 如何集成Spring Boot與Sentry?

<dependency>
    <groupId>io.sentry</groupId>
    <artifactId>sentry-spring</artifactId>
    <version>1.7.27</version>
</dependency>
package cobra.config;

import io.sentry.Sentry;
import io.sentry.SentryClient;
import io.sentry.spring.SentryExceptionResolver;
import io.sentry.spring.SentryServletContextInitializer;
import javax.annotation.PostConstruct;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.web.servlet.ServletContextInitializer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.HandlerExceptionResolver;

@Configuration
@ConditionalOnProperty(value = "sentry.enable")
public class SentryAutoConfiguration {

  @Value("${sentry.dsn}")
  String dsn;

  @Value("${sentry.environment}")
  String environment;

  @Value("${sentry.release}")
  String release;

  @PostConstruct
  void init() {
    final SentryClient sentryClient = Sentry.init(dsn);
    sentryClient.setEnvironment(environment);
    sentryClient.setRelease(release);
  }

  @Bean
  @ConditionalOnMissingBean(SentryExceptionResolver.class)
  public HandlerExceptionResolver sentryExceptionResolver() {
    return new SentryExceptionResolver();
  }

  @Bean
  @ConditionalOnMissingBean(SentryServletContextInitializer.class)
  public ServletContextInitializer sentryServletContextInitializer() {
    return new SentryServletContextInitializer();
  }
}

在yml文件中加:

# Sentry api
sentry:
  enable: true
  dsn: https://[email protected]/1338894

在.sentryclirc文件中加:

[defaults]
url = https://sentry.io/
org = XXX
project = XXX

[auth]
token = XXX

以上是後端,我們也可以在前端加上:

npm install 
"webpack-s3-plugin": "^1.0.0-rc.0",
"@sentry/browser": "4.4.0",
"@sentry/webpack-plugin": "1.6.2"

在webpack-prod.js中:

const SentryPlugin = require('@sentry/webpack-plugin');

return merge(common, {
    devtool: 'hidden-source-map',
    mode: 'XXX',
    output: {
      filename: 'assets/[name]-[chunkhash].js',
      publicPath: process.env.PUBLIC_PATH,
      sourceMapFilename: 'assets/[name]-[chunkhash].js.map',
    },
    plugins: [
        new OptimizeCSSAssetsPlugin({}),
        new SentryPlugin({
          release: `${process.env.RELEASE}`,
          include: path.resolve(rootPath, 'public/assets/'),
          urlPrefix: '~/assets',
        }),
        ...plugins
    ],
  });

在env文件中加:

SENTRY_DSN=https://[email protected]/1339298

寫一個公共函數

import * as Sentry from '@sentry/browser';

const init = () => {
  if (SENTRY_DSN) {
    Sentry.init({
      dsn: `${SENTRY_DSN}`,
      release: `${RELEASE}`,
      environment: `${ENVIRONMENT}`,
    });
  }
};

export default init;

在各個組件的源頭上加上該函數,如下:

import React from 'react';
import App from 'App/Account/Containers/App';
import { Router } from 'react-router-dom';
import { Provider } from 'react-redux';
import history from 'App/Common/history';
import initSentry from 'App/Common/Sentry';
import configureStore from './Store';

initSentry();


const store = configureStore();
window.store = store;

const Accounts = () => (
  <Provider store={store}>
    <div>
      <Router history={history}>
        <App />
      </Router>
    </div>
  </Provider>
);
export default Accounts;

參考鏈接

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章