什么是Sentry?
为什么用Sentry?
- 实时错误跟踪:Sentry能够实时捕获应用程序中的错误,并立即通知开发团队。这大大减少了从用户报告问题到开发团队发现并解决问题的时间。
- 提高应用质量:通过持续监控和分析错误数据,开发团队可以及时发现并解决潜在的问题,从而提高应用程序的质量和稳定性。
- 详细的错误报告:Sentry提供的错误报告非常详细,包括完整的堆栈跟踪和上下文信息。这使得开发人员能够迅速定位问题的根源,并有效地进行调试和修复。
- 跨平台和语言支持:Sentry支持多种编程语言和平台,包括JavaScript、Python、Java、Ruby、PHP等。这意味着无论你的应用程序是用什么技术栈构建的,Sentry都能提供有效的错误跟踪服务。
- 可扩展性和集成性:Sentry具有强大的可扩展性,可以与各种第三方服务和工具集成,如Slack、GitHub、JIRA等。这使得开发团队能够根据自己的需求定制工作流程,并与其他团队协作工具无缝衔接。
- 节省时间和资源:通过自动化错误跟踪和报告,Sentry帮助开发团队节省了大量手动排查问题的时间。这不仅可以提高开发效率,还可以降低维护成本。
- 用户满意度提升:及时发现和解决应用程序中的问题可以显著提高用户体验和满意度。当用户遇到问题时,能够快速得到响应和解决,这将增强他们对应用程序的信任和忠诚度。
接入方式
准备工作-项目接入sentry
-
通过域名登录sentry
-
进来后没有看到team信息,加入到组。
-
加入后,进入Projects,查看自己在组内的项目,如果没有的话,需要创建。
-
创建项目需要权限,如果自己没有权限,可以查看下Members,找有权限的人员帮忙创建
-
如下图,直接按照文档说明加入到项目里即可
-
如果是小程序uni-app项目,直接使用vue3的sdk无法上报,那么可以使用第三方插件sentry-miniapp
1 import * as Sentry from "sentry-miniapp"; 2 Sentry.init({ 3 dsn: "https://xxxxxxxxx", 4 // Set tracesSampleRate to 1.0 to capture 100% 5 // of transactions for performance monitoring. 6 // We recommend adjusting this value in production 7 tracesSampleRate: 1.0 8 });
VUE项目的sentry设置
sentryInit 伪代码
1 // 调用sentryInit函数进行Sentry的初始化配置 2 sentryInit({ 3 // 将sentry的init方法和app实例传递给sentryInit 4 sentry: { init }, 5 app, 6 7 // 指定Sentry项目的DSN(数据源名称),用于将错误数据发送到正确的Sentry项目 8 dsn: 'https://xxxxx', 9 10 // 配置集成,这里添加了两个集成:BrowserTracing和Replay 11 integrations: [ 12 // BrowserTracing集成用于跟踪浏览器中的性能问题,如页面加载时间等 13 new BrowserTracing({ 14 // 通过vueRouterInstrumentation对Vue路由进行追踪 15 routingInstrumentation: vueRouterInstrumentation(router), 16 }), 17 // Replay集成用于记录和回放用户的操作,帮助开发者更好地理解错误发生的上下文 18 new Replay({ 19 // 配置允许采集的接口,如果有非同源的接口域名需要在这里配置 20 networkDetailAllowUrls: [window.location.origin], 21 22 // 配置请求头中需要采集的字段 默认只采集 Content-Type、Content-Length、Accept, Authorization 需要确认需不需要 23 networkRequestHeaders: ['traceparent', '__refreshid__', 'Cookie', 'Authorization'], 24 25 // 配置响应头中需要采集的字段 26 networkResponseHeaders: ['traceparent', 'set-cookie'], 27 28 // 是否对所有文本进行脱敏处理,这里设置为false 29 maskAllText: false, 30 31 // 是否对所有输入进行脱敏处理,这里设置为false 32 maskAllInputs: false, 33 34 // 是否阻止所有媒体加载,这里设置为false 35 blockAllMedia: false, 36 }), 37 ], 38 39 // 设置追踪的采样率,1.0表示100%的追踪事件都会被采集 值介于0 至1.0, 事件是随机挑选的 40 tracesSampleRate: 1.0, 41 42 // 设置环境变量,用于区分不同的环境(如开发、测试、生产等) 43 environment: import.meta.env.MODE, 44 45 // 是否开启错误上报, 46 enabled: true, 47 48 // 设置版本号,可以用来过滤和定位特定版本的错误 49 release: import.meta.env.BUILD_TIME as string, 50 51 // 配置需要忽略的错误类型或错误消息,这些错误将不会被上报到Sentry 52 ignoreErrors: [ 53 'ResizeObserver loop limit exceeded', 54 // ...(其他需要忽略的错误) 55 // 'ResizeObserver loop completed with undelivered notifications', 56 // /Failed to fetch dynamically imported module/, 57 // /Failed to load module script/, 58 // /Importing a module script failed/, 59 // /promise rejection captured with keys: code, error, msg/, 60 // /exception captured with keys: code, data, msg/, 61 // /Unable to preload CSS for/, 62 // /exception captured with keys: errorFields, outOfDate, values/, 63 ], 64 65 initialScope: { 66 tags: { "my-tag": "my value" }, 67 user: { id: 4222xxx, email: "xxxx.com" }, 68 }, 69 70 // 设置用户名 71 username: xxx.userName, 72 73 // 是否将错误记录到控制台,这里设置为true 74 logErrors: true, 75 }) 76 77 78 79 export function configSentryPlugin() { 80 return vitePluginSentry({ 81 // 指定 Sentry 服务的 URL 82 url: 'https://sentry.xxxx.cn', 83 84 // 指定 Sentry 的授权令牌,这是连接 Sentry 服务并进行错误追踪的凭证 85 authToken: '', //sentry授权令牌 86 87 // 指定 Sentry 中的组织名称 88 org: 'sentry', 89 90 // 指定 Sentry 中的项目名称 91 project: 'xxxx', 92 93 // 指定发布的版本,这里使用了环境变量 BUILD_TIME 的值作为版本信息 94 release: process.env.BUILD_TIME as string, 95 96 // 配置 source map 的相关设置,用于在 Sentry 中更准确地定位错误位置 97 sourceMaps: { 98 // 指定需要包含进 source map 的文件或文件夹,这里包含了 './dist/assets' 文件夹 99 include: ['./dist/assets'], 100 101 // 指定需要忽略的文件或文件夹,这里忽略了 'node_modules' 文件夹 102 ignore: ['node_modules'], 103 104 // 设置 source map 的 URL 前缀,用于在 Sentry 中构建正确的 source map URL 105 urlPrefix: '~/cloudConfigAssets', 106 }, 107 108 // 设置跳过环境检查,即使在非生产环境中也上传 source map 和错误信息到 Sentry 109 skipEnvironmentCheck: true, 110 }) 111 }
方法: sentryLog (主动上传错误.'info')
对项目设置企业机器人提醒
-
准备企业微信机器人。
-
使用sentry的webhook插件,通知企业微信。
-
点击settings,projects
对项目配置alert规则
-
进入警报页面
-
进入后,选择issues,错误等级,环境等变量,然后可以根据自己的实际情况设置条件和过滤信息。
设置项目的sourceMap
定位错误位置
实现思路
-
生成上传的token
-
上传方式
使用插件上传
webpack
1 // vue.config.js 2 const SentryCliPlugin = require('@sentry/webpack-plugin') 3 4 ... 5 6 configureWebpack(config) { 7 if (process.env.NODE_ENV === 'production') { 8 config.plugin('sentry').use(SentryCliPlugin, [{ 9 include: './dist/assets', // 指定上传目录 10 ignoreFile: '.gitignore', // 指定忽略文件配置 11 release: process.env.VUE_APP_BASE_API, // 指定发布版本 12 ignore: ['node_modules', 'webpack.config.js'], 13 configFile: './.sentryclirc', // 指定sentry上传配置 14 urlPrefix: '~/assets/' // 保持与publicpath相符 15 }]) 16 } 17 }
vite
1 import vitePluginSentry from 'vite-plugin-sentry'; 2 3 vitePluginSentry({ 4 url: 'https://sentry.xxx.cn', 5 authToken: 'xxx', // sentry授权令牌 6 org: 'sentry', 7 project: 'xxx', 8 skipEnvironmentCheck: true, 9 release: process.env.BUILD_TIME as string, 10 sourceMaps: { 11 include: ['./dist/assets'], 12 ignore: ['node_modules'], 13 urlPrefix: `${process.env.VITE_PUBLIC_PATH}/assets`, 14 }, 15 }), 16 17 build: { 18 target: 'es2015', 19 sourcemap: true, 20 },
上报方法封装
伪代码
1 /** 2 * sentry 初始化函数 3 * 4 * @param options - sentry 对象 5 * 6 * @returns 返回当前的刷新 id 7 * @public 8 */ 9 export declare const sentryInit: ({ sentry, app, environment, enabled, release, tracesSampleRate, tags, username, dsn, isMiniapp, integrations, normalizeDepth, beforeSend, ...otherOptions }: SentryOptions) => { 10 __refreshId__: string; 11 }; 12 /** 13 * 基础 sentry 上报函数 14 * 15 * @param sentry - sentry 对象 16 * @param error - 错误信息 17 * @param options - 额外的信息 18 * 19 * @returns type and meaning of return value 20 * @public 21 */ 22 export declare const sentryLog: (sentry: any, error: Error | string, options: SentryLogOptions) => void; 23 /** 24 * 25 * 记录历史 api 请求信息 26 * 27 * @param apiLog - api 数据 28 * @param maxSaveLength - 最大保存长度 29 * 30 * @returns 无 31 * @public 32 */ 33 export declare const saveApiLog: (apiLog: ApiLog, maxSaveLength?: number) => void; 34 /** 35 * sentry api 接口错误上报函数 36 * 37 * @param sentry - sentry 对象 38 * @param error - 错误信息 39 * @param options - 额外的信息 40 * 41 * @returns 无 42 * @public 43 */ 44 export declare const sentryApiLog: (sentry: any, error: Error | string, options: SentryApiLogOptions) => void;
扩展
录屏精准复现
1 <span data-sentry-mask>{{ xxx }}</span>
使用情况
- 默认过滤所有,不需要脱敏的数据增加sentry-unmask、sentry-unblock的类或者增加data-sentry-unmask、data-sentry-unblock的属性取消,适合敏感信息较多的场景
- 默认所有不过滤,对于需要脱敏的数据增加sentry-mask、sentry-block的类或者增加data-sentry-mask、data-sentry-block的属性,适合敏感信息较少的场景
1 // 默认所有数据不脱敏 2 new Replay({ 3 networkDetailAllowUrls: [window.location.origin], 4 networkRequestHeaders: ['traceparent', '__refreshid__', 'Cookie'], 5 networkResponseHeaders: ['traceparent', 'set-cookie'], 6 maskAllText: false, 7 maskAllInputs: false, 8 blockAllMedia: false, 9 }),
- 某些场景如dom由第三方依赖生成,无法增加class或者属性,可以统一在全局增加
1 new Replay({ 2 networkDetailAllowUrls: [window.location.origin], 3 networkRequestHeaders:['traceparent', '_refreshid_', 'set-cookie'], 4 networkResponseHeaders:['traceparent', 'set-cookie'], 5 block:['.mce-content-body'], 6 })
使用分析
-
Console
-
Network
header
和 response
, header
支持默认的 Content-Type
、 Content-LengthAccept
,以及在 networkRequestHeaders
和 networkResponseHeaders
中配置的字段
剩下的就需要自己坚持主动 或者 团队坚持每双周过一次所有上报的问题,专人持续轮流跟进,常态化治理。为自己及团队的项目保驾护航。
所有评论(0)