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://XXX@sentry.io/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://XXX@sentry.io/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;