sentry-miniapp 1.1.0 → 1.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +118 -123
- package/dist/sentry-miniapp.cjs.js +250 -37
- package/dist/sentry-miniapp.cjs.js.map +1 -1
- package/dist/sentry-miniapp.esm.js +250 -37
- package/dist/sentry-miniapp.esm.js.map +1 -1
- package/dist/sentry-miniapp.umd.js +250 -37
- package/dist/sentry-miniapp.umd.js.map +1 -1
- package/dist/types/client.d.ts.map +1 -1
- package/dist/types/crossPlatform.d.ts +5 -1
- package/dist/types/crossPlatform.d.ts.map +1 -1
- package/dist/types/integrations/dedupe.d.ts.map +1 -1
- package/dist/types/integrations/trycatch.d.ts +0 -2
- package/dist/types/integrations/trycatch.d.ts.map +1 -1
- package/dist/types/transports/index.d.ts +1 -0
- package/dist/types/transports/index.d.ts.map +1 -1
- package/dist/types/transports/offlineStore.d.ts +6 -0
- package/dist/types/transports/offlineStore.d.ts.map +1 -0
- package/dist/types/transports/xhr.d.ts.map +1 -1
- package/dist/types/types.d.ts +2 -0
- package/dist/types/types.d.ts.map +1 -1
- package/dist/types/version.d.ts +1 -1
- package/dist/types/version.d.ts.map +1 -1
- package/package.json +15 -6
package/README.md
CHANGED
|
@@ -4,188 +4,183 @@
|
|
|
4
4
|

|
|
5
5
|

|
|
6
6
|

|
|
7
|
-

|
|
7
|
+

|
|
8
|
+

|
|
10
9
|
|
|
11
|
-
|
|
10
|
+
一个基于 `@sentry/core` (v10.45.0) 核心构建的**多端小程序异常与性能监控 SDK**。旨在为小程序开发者提供与 Web 端一致的、强大且现代的 Sentry 监控体验。
|
|
12
11
|
|
|
13
|
-
>
|
|
14
|
-
|
|
15
|
-
|
|
12
|
+
> **💡 版本说明**
|
|
13
|
+
>
|
|
14
|
+
> - `v1.x.x`:全新架构,基于 Sentry V10 核心,全面支持微信、支付宝、字节跳动、百度、QQ、钉钉、快手等多端小程序及主流跨端框架(Taro / uni-app 等)。
|
|
15
|
+
> - `v0.x.x`:旧版本,已停止维护。
|
|
16
16
|
|
|
17
|
-
|
|
17
|
+
---
|
|
18
18
|
|
|
19
|
-
|
|
20
|
-
- 🎨 遵守 Sentry 官方统一的 API 设计文档,使用方式和官方保持一致
|
|
21
|
-
- 📍 默认上报异常发生时的路由栈
|
|
22
|
-
- 🎯 自动捕获小程序生命周期异常(onError、onUnhandledRejection、onPageNotFound、onMemoryWarning)
|
|
23
|
-
- 🍞 自动记录面包屑(设备、用户操作、网络请求、页面导航等)
|
|
24
|
-
- 🛡️ 智能错误去重和过滤机制
|
|
25
|
-
- ⚡ 全面的性能监控(导航性能、渲染性能、资源加载、用户自定义性能标记)
|
|
26
|
-
- 📈 智能性能阈值检查和自动警告
|
|
27
|
-
- 🔧 支持在 Taro 等第三方小程序框架中使用
|
|
28
|
-
- 📱 支持微信小程序和微信小游戏
|
|
29
|
-
- 🔧 TypeScript 编写,提供完整的类型定义
|
|
30
|
-
- 📦 支持 ES6 和 CommonJS 两种模块系统
|
|
31
|
-
- 📊 完善的测试覆盖率(286 测试用例,覆盖核心功能模块)
|
|
32
|
-
- 🔍 完整的集成测试套件
|
|
19
|
+
## ✨ 核心特性
|
|
33
20
|
|
|
34
|
-
|
|
35
|
-
|
|
21
|
+
- **🚀 现代架构**:基于最新的 Sentry JavaScript V10 SDK 核心模块构建。
|
|
22
|
+
- **📱 真正的多端支持**:内置 API 抹平引擎,一套代码无缝兼容**微信、支付宝、字节、百度、QQ、钉钉、快手**等主流小程序平台。
|
|
23
|
+
- **🎯 全自动异常捕获**:无需侵入业务代码,自动监听并上报生命周期异常(`onError`、`onUnhandledRejection`、`onPageNotFound`、`onMemoryWarning`)。
|
|
24
|
+
- **🍞 丰富的上下文面包屑**:自动记录设备信息、用户点击/触摸操作、网络请求(XHR/Fetch)、以及页面路由导航路径。
|
|
25
|
+
- **📡 弱网离线缓存机制**:专为小程序网络环境设计,断网或发送失败时自动缓存 Event 到本地 Storage,网络恢复后静默重试上报,确保数据不丢失。
|
|
26
|
+
- **⚡ 深度性能监控**:集成小程序 Performance API,全面采集导航性能(FCP/LCP)、渲染性能、资源加载耗时及用户自定义性能标记。
|
|
27
|
+
- **�️ 智能降噪与过滤**:内置强大的错误去重和采样率控制机制,避免日志风暴。
|
|
28
|
+
- **🔧 跨端框架友好**:完美支持在 Taro、uni-app 等第三方多端编译框架中集成使用。
|
|
36
29
|
|
|
37
|
-
|
|
30
|
+
---
|
|
38
31
|
|
|
39
|
-
|
|
32
|
+
## 📦 安装
|
|
40
33
|
|
|
41
|
-
|
|
42
|
-
2. 在小程序管理后台配置 `Sentry Service` 对应的 `request` 合法域名
|
|
34
|
+
推荐使用 `npm` 进行安装。
|
|
43
35
|
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
36
|
+
```bash
|
|
37
|
+
npm install sentry-miniapp --save
|
|
38
|
+
```
|
|
47
39
|
|
|
48
|
-
|
|
40
|
+
> **注意:** `v1.1.0` 及以上版本已优化构建策略(内联依赖),**无需**再额外安装 `@sentry/core`。
|
|
49
41
|
|
|
50
|
-
|
|
51
|
-
npm install sentry-miniapp --save
|
|
52
|
-
```
|
|
42
|
+
*提示:如果您不使用 npm,也可以直接将项目仓库中 `examples/wxapp/lib/sentry-miniapp.js` 文件复制到小程序项目中引入。*
|
|
53
43
|
|
|
54
|
-
|
|
44
|
+
---
|
|
55
45
|
|
|
56
|
-
|
|
46
|
+
## 🚀 快速接入
|
|
57
47
|
|
|
58
|
-
|
|
48
|
+
### 1. 前置准备
|
|
59
49
|
|
|
60
|
-
|
|
50
|
+
1. 确保您有可用的 Sentry 平台账号(可以使用 [官方 Sentry SaaS](https://sentry.io/) 或 私有化部署服务)。
|
|
51
|
+
2. **非常重要**:在各平台的小程序管理后台,将 Sentry 的上报接口域名添加到 `request` 合法域名列表中。
|
|
61
52
|
|
|
62
|
-
###
|
|
53
|
+
### 2. 初始化 SDK
|
|
63
54
|
|
|
64
|
-
|
|
55
|
+
请在小程序入口文件(如 `app.js` 或 `app.ts`)的**最顶部**(调用 `App()` 之前)初始化 Sentry。
|
|
65
56
|
|
|
66
57
|
```javascript
|
|
67
58
|
import * as Sentry from 'sentry-miniapp';
|
|
68
59
|
|
|
69
|
-
// 在 App() 之前初始化
|
|
70
60
|
Sentry.init({
|
|
71
|
-
dsn: '
|
|
72
|
-
environment: 'production', //
|
|
73
|
-
|
|
61
|
+
dsn: 'https://<key>@sentry.io/<project>',
|
|
62
|
+
environment: 'production', // 环境变量: production / development
|
|
63
|
+
release: 'my-project-name@1.0.0', // 版本号,建议与 sourcemap 配合使用
|
|
74
64
|
|
|
75
|
-
//
|
|
76
|
-
platform: 'wechat',
|
|
77
|
-
enableSystemInfo: true, //
|
|
78
|
-
enableUserInteractionBreadcrumbs: true, //
|
|
79
|
-
|
|
80
|
-
enableNavigationBreadcrumbs: true, // 是否记录导航面包屑
|
|
65
|
+
// --- 小程序特性配置 ---
|
|
66
|
+
platform: 'wechat', // 当前平台 (wechat | alipay | bytedance | dd | swan 等)
|
|
67
|
+
enableSystemInfo: true, // 自动采集系统与设备信息
|
|
68
|
+
enableUserInteractionBreadcrumbs: true, // 自动记录用户点击行为
|
|
69
|
+
enableNavigationBreadcrumbs: true, // 自动记录页面路由跳转
|
|
81
70
|
|
|
82
|
-
//
|
|
83
|
-
|
|
71
|
+
// --- 离线缓存与可靠性 ---
|
|
72
|
+
enableOfflineCache: true, // 开启断网离线缓存与重试机制 (默认开启)
|
|
84
73
|
|
|
85
|
-
//
|
|
74
|
+
// --- 性能与采样率 ---
|
|
75
|
+
sampleRate: 1.0, // 异常上报采样率 (0.0 - 1.0)
|
|
76
|
+
|
|
77
|
+
// 可选:性能监控配置
|
|
86
78
|
integrations: [
|
|
87
|
-
// 性能监控集成
|
|
88
79
|
Sentry.performanceIntegration({
|
|
89
|
-
enableNavigation: true, //
|
|
90
|
-
enableRender: true, //
|
|
91
|
-
enableResource: true, //
|
|
92
|
-
enableUserTiming: true, // 用户自定义性能标记
|
|
93
|
-
sampleRate: 1.0, // 性能数据采样率
|
|
94
|
-
reportInterval: 30000, // 数据上报间隔(毫秒)
|
|
80
|
+
enableNavigation: true, // 导航耗时监控
|
|
81
|
+
enableRender: true, // 渲染耗时监控
|
|
82
|
+
enableResource: true, // 资源加载耗时
|
|
95
83
|
}),
|
|
96
84
|
]
|
|
97
|
-
|
|
98
|
-
// 过滤配置
|
|
99
|
-
beforeSend(event) {
|
|
100
|
-
// 可以在这里过滤或修改事件
|
|
101
|
-
return event;
|
|
102
|
-
},
|
|
103
85
|
});
|
|
104
86
|
|
|
87
|
+
// 初始化完成后,再调用 App
|
|
105
88
|
App({
|
|
106
|
-
|
|
89
|
+
onLaunch() {
|
|
90
|
+
// ...
|
|
91
|
+
}
|
|
107
92
|
});
|
|
108
93
|
```
|
|
109
94
|
|
|
110
|
-
|
|
95
|
+
---
|
|
111
96
|
|
|
112
|
-
|
|
113
|
-
|
|
97
|
+
## 📚 常用进阶用法
|
|
98
|
+
|
|
99
|
+
初始化完成后,SDK 会自动在后台工作。您也可以使用以下 API 进行手动埋点或主动上报。
|
|
114
100
|
|
|
115
|
-
|
|
101
|
+
### 手动异常与消息上报
|
|
102
|
+
|
|
103
|
+
```javascript
|
|
104
|
+
// 手动捕获并上报一个 Error 对象
|
|
116
105
|
try {
|
|
117
|
-
|
|
118
|
-
throw new Error('Something went wrong!');
|
|
106
|
+
throw new Error('支付接口解析失败');
|
|
119
107
|
} catch (error) {
|
|
120
108
|
Sentry.captureException(error);
|
|
121
109
|
}
|
|
122
110
|
|
|
123
|
-
//
|
|
124
|
-
Sentry.captureMessage('
|
|
111
|
+
// 记录一条关键信息
|
|
112
|
+
Sentry.captureMessage('用户主动取消了授权', 'info');
|
|
113
|
+
```
|
|
125
114
|
|
|
126
|
-
|
|
127
|
-
Sentry.addBreadcrumb({
|
|
128
|
-
message: '用户点击了按钮',
|
|
129
|
-
category: 'ui',
|
|
130
|
-
level: 'info',
|
|
131
|
-
data: {
|
|
132
|
-
buttonId: 'submit-btn'
|
|
133
|
-
}
|
|
134
|
-
});
|
|
115
|
+
### 丰富上下文信息 (Context & Breadcrumbs)
|
|
135
116
|
|
|
136
|
-
|
|
117
|
+
```javascript
|
|
118
|
+
// 设置当前操作的用户信息
|
|
137
119
|
Sentry.setUser({
|
|
138
|
-
id: '
|
|
139
|
-
username: '
|
|
140
|
-
email: 'john@example.com'
|
|
120
|
+
id: 'user_12345',
|
|
121
|
+
username: 'John Doe'
|
|
141
122
|
});
|
|
142
123
|
|
|
143
|
-
//
|
|
144
|
-
Sentry.setTag('
|
|
124
|
+
// 设置用于过滤和统计的全局标签
|
|
125
|
+
Sentry.setTag('page_module', 'checkout_counter');
|
|
145
126
|
|
|
146
|
-
//
|
|
147
|
-
Sentry.
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
127
|
+
// 手动添加一条业务追踪面包屑
|
|
128
|
+
Sentry.addBreadcrumb({
|
|
129
|
+
message: '用户点击了[确认支付]按钮',
|
|
130
|
+
category: 'action',
|
|
131
|
+
level: 'info',
|
|
132
|
+
data: { cartId: 'c_888' }
|
|
151
133
|
});
|
|
152
134
|
```
|
|
153
135
|
|
|
154
|
-
###
|
|
136
|
+
### 自定义性能测速 (Performance)
|
|
155
137
|
|
|
156
138
|
```javascript
|
|
157
|
-
|
|
139
|
+
// 标记起始点
|
|
140
|
+
Sentry.addPerformanceMark('api-request-start');
|
|
141
|
+
// ... 执行耗时操作
|
|
142
|
+
Sentry.addPerformanceMark('api-request-end');
|
|
158
143
|
|
|
159
|
-
//
|
|
160
|
-
Sentry.
|
|
161
|
-
// ... 页面加载逻辑
|
|
162
|
-
Sentry.addPerformanceMark('page-load-end');
|
|
163
|
-
|
|
164
|
-
// 测量性能区间
|
|
165
|
-
Sentry.measurePerformance('page-load', 'page-load-start', 'page-load-end');
|
|
166
|
-
|
|
167
|
-
// 记录自定义性能数据
|
|
168
|
-
Sentry.recordPerformance({
|
|
169
|
-
name: 'api-request',
|
|
170
|
-
value: 1200, // 毫秒
|
|
171
|
-
unit: 'millisecond',
|
|
172
|
-
tags: {
|
|
173
|
-
endpoint: '/api/user',
|
|
174
|
-
method: 'GET'
|
|
175
|
-
}
|
|
176
|
-
});
|
|
144
|
+
// 测量并记录该区间
|
|
145
|
+
Sentry.measurePerformance('fetch-user-data', 'api-request-start', 'api-request-end');
|
|
177
146
|
```
|
|
178
147
|
|
|
179
|
-
|
|
148
|
+
---
|
|
149
|
+
|
|
150
|
+
## ❓ 常见问题 (FAQ)
|
|
151
|
+
|
|
152
|
+
### 1. 初始化后无法自动上报异常,必须在 `onError` 中手动调 API 吗?
|
|
153
|
+
|
|
154
|
+
**完全不需要**。
|
|
155
|
+
`sentry-miniapp` 在初始化时会自动劫持并注册平台底层的全局错误监听(如 `wx.onError`)。只要确保 `Sentry.init` 在 `App()` 调用**之前**执行,它就能自动捕获所有未处理的 JS 异常。
|
|
156
|
+
如果发现没上报,请检查:
|
|
157
|
+
|
|
158
|
+
1. Sentry 域名是否加入了小程序后台合法域名。
|
|
159
|
+
2. `sampleRate` (采样率) 是否被意外设置得太低。
|
|
160
|
+
3. 微信开发者工具某些环境下的报错不会触发底层 `onError`,建议在**真机预览**下测试。
|
|
161
|
+
|
|
162
|
+
### 2. 这个 SDK 支持 Session Replay (屏幕操作回放) 吗?
|
|
163
|
+
|
|
164
|
+
目前 **不支持** `Sentry.replayIntegration()`。
|
|
165
|
+
Sentry 官方的 Replay 功能强依赖于浏览器标准 DOM 环境(通过 rrweb 录制)。小程序采用双线程架构且没有开放标准 DOM 接口,无法直接复用。建议通过完善**Breadcrumbs(面包屑路径)**结合**自定义日志**来还原用户操作现场。
|
|
166
|
+
|
|
167
|
+
---
|
|
168
|
+
|
|
169
|
+
## 🤝 参与贡献
|
|
170
|
+
|
|
171
|
+
我们非常欢迎开发者提交 `Pull Request` 或通过 `Issues` 提出宝贵意见!
|
|
180
172
|
|
|
181
|
-
|
|
173
|
+
要参与本地开发:
|
|
182
174
|
|
|
183
|
-
|
|
175
|
+
1. `npm install` 安装依赖
|
|
176
|
+
2. `npm run dev` 启动监听编译
|
|
177
|
+
3. `npm run test:all` 运行完整的单元测试与集成测试套件
|
|
184
178
|
|
|
185
|
-
|
|
179
|
+
---
|
|
186
180
|
|
|
187
|
-
|
|
181
|
+
## 💬 联系与交流
|
|
188
182
|
|
|
189
|
-
|
|
183
|
+
遇到问题?想探讨小程序监控方案?欢迎加入我们的交流群。
|
|
184
|
+
由于微信群二维码有 7 天时效性限制,请添加作者微信(**备注 sentry-miniapp**),由作者邀请您入群:
|
|
190
185
|
|
|
191
|
-
<img src="docs/qrcode/zhiyao.jpeg" alt="作者微信二维码" width="
|
|
186
|
+
<img src="docs/qrcode/zhiyao.jpeg" alt="作者微信二维码" width="200" style="border-radius: 8px; box-shadow: 0 4px 8px rgba(0,0,0,0.1);" />
|