sentry-miniapp 1.1.0 → 1.3.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 +143 -123
- package/dist/sentry-miniapp.cjs.js +295 -37
- package/dist/sentry-miniapp.cjs.js.map +1 -1
- package/dist/sentry-miniapp.esm.js +295 -37
- package/dist/sentry-miniapp.esm.js.map +1 -1
- package/dist/sentry-miniapp.umd.js +295 -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/index.d.ts +1 -0
- package/dist/types/integrations/index.d.ts.map +1 -1
- package/dist/types/integrations/rewriteframes.d.ts +35 -0
- package/dist/types/integrations/rewriteframes.d.ts.map +1 -0
- package/dist/types/integrations/trycatch.d.ts +0 -2
- package/dist/types/integrations/trycatch.d.ts.map +1 -1
- package/dist/types/sdk.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 +4 -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,208 @@
|
|
|
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
|
+
- **🗺️ 内置 SourceMap 路径抹平**:自动处理微信、支付宝、字节等多端小程序的虚拟堆栈路径,配合 sentry-cli 极简实现 SourceMap 解析。
|
|
26
|
+
- **📡 弱网离线缓存机制**:专为小程序网络环境设计,断网或发送失败时自动缓存 Event 到本地 Storage,网络恢复后静默重试上报,确保数据不丢失。
|
|
27
|
+
- **⚡ 深度性能监控**:集成小程序 Performance API,全面采集导航性能(FCP/LCP)、渲染性能、资源加载耗时及用户自定义性能标记。
|
|
28
|
+
- **�️ 智能降噪与过滤**:内置强大的错误去重和采样率控制机制,避免日志风暴。
|
|
29
|
+
- **🔧 跨端框架友好**:完美支持在 Taro、uni-app 等第三方多端编译框架中集成使用。
|
|
36
30
|
|
|
37
|
-
|
|
31
|
+
---
|
|
38
32
|
|
|
39
|
-
|
|
33
|
+
## 📦 安装
|
|
40
34
|
|
|
41
|
-
|
|
42
|
-
2. 在小程序管理后台配置 `Sentry Service` 对应的 `request` 合法域名
|
|
35
|
+
推荐使用 `npm` 进行安装。
|
|
43
36
|
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
37
|
+
```bash
|
|
38
|
+
npm install sentry-miniapp --save
|
|
39
|
+
```
|
|
47
40
|
|
|
48
|
-
|
|
41
|
+
> **注意:** `v1.1.0` 及以上版本已优化构建策略(内联依赖),**无需**再额外安装 `@sentry/core`。
|
|
49
42
|
|
|
50
|
-
|
|
51
|
-
npm install sentry-miniapp --save
|
|
52
|
-
```
|
|
43
|
+
*提示:如果您不使用 npm,也可以直接将项目仓库中 `examples/wxapp/lib/sentry-miniapp.js` 文件复制到小程序项目中引入。*
|
|
53
44
|
|
|
54
|
-
|
|
45
|
+
---
|
|
55
46
|
|
|
56
|
-
|
|
47
|
+
## 🚀 快速接入
|
|
57
48
|
|
|
58
|
-
|
|
49
|
+
### 1. 前置准备
|
|
59
50
|
|
|
60
|
-
|
|
51
|
+
1. 确保您有可用的 Sentry 平台账号(可以使用 [官方 Sentry SaaS](https://sentry.io/) 或 私有化部署服务)。
|
|
52
|
+
2. **非常重要**:在各平台的小程序管理后台,将 Sentry 的上报接口域名添加到 `request` 合法域名列表中。
|
|
61
53
|
|
|
62
|
-
###
|
|
54
|
+
### 2. 初始化 SDK
|
|
63
55
|
|
|
64
|
-
|
|
56
|
+
请在小程序入口文件(如 `app.js` 或 `app.ts`)的**最顶部**(调用 `App()` 之前)初始化 Sentry。
|
|
65
57
|
|
|
66
58
|
```javascript
|
|
67
59
|
import * as Sentry from 'sentry-miniapp';
|
|
68
60
|
|
|
69
|
-
// 在 App() 之前初始化
|
|
70
61
|
Sentry.init({
|
|
71
|
-
dsn: '
|
|
72
|
-
environment: 'production', //
|
|
73
|
-
|
|
62
|
+
dsn: 'https://<key>@sentry.io/<project>',
|
|
63
|
+
environment: 'production', // 环境变量: production / development
|
|
64
|
+
release: 'my-project-name@1.0.0', // 版本号,建议与 sourcemap 配合使用
|
|
65
|
+
|
|
66
|
+
// --- 小程序特性配置 ---
|
|
67
|
+
platform: 'wechat', // 当前平台 (wechat | alipay | bytedance | dd | swan 等)
|
|
68
|
+
enableSystemInfo: true, // 自动采集系统与设备信息
|
|
69
|
+
enableUserInteractionBreadcrumbs: true, // 自动记录用户点击行为
|
|
70
|
+
enableNavigationBreadcrumbs: true, // 自动记录页面路由跳转
|
|
74
71
|
|
|
75
|
-
//
|
|
76
|
-
|
|
77
|
-
enableSystemInfo: true, // 是否收集系统信息
|
|
78
|
-
enableUserInteractionBreadcrumbs: true, // 是否记录用户交互面包屑
|
|
79
|
-
enableConsoleBreadcrumbs: true, // 是否记录控制台日志面包屑
|
|
80
|
-
enableNavigationBreadcrumbs: true, // 是否记录导航面包屑
|
|
72
|
+
// --- 离线缓存与可靠性 ---
|
|
73
|
+
enableOfflineCache: true, // 开启断网离线缓存与重试机制 (默认开启)
|
|
81
74
|
|
|
82
|
-
//
|
|
83
|
-
|
|
75
|
+
// --- SourceMap 支持 ---
|
|
76
|
+
enableSourceMap: true, // 开启自动将堆栈的虚拟路径转为统一格式,配合上传 sourcemap 时的 --url-prefix "app:///"
|
|
84
77
|
|
|
85
|
-
//
|
|
78
|
+
// --- 性能与采样率 ---
|
|
79
|
+
sampleRate: 1.0, // 异常上报采样率 (0.0 - 1.0)
|
|
80
|
+
|
|
81
|
+
// 可选:性能监控配置
|
|
86
82
|
integrations: [
|
|
87
|
-
// 性能监控集成
|
|
88
83
|
Sentry.performanceIntegration({
|
|
89
|
-
enableNavigation: true, //
|
|
90
|
-
enableRender: true, //
|
|
91
|
-
enableResource: true, //
|
|
92
|
-
enableUserTiming: true, // 用户自定义性能标记
|
|
93
|
-
sampleRate: 1.0, // 性能数据采样率
|
|
94
|
-
reportInterval: 30000, // 数据上报间隔(毫秒)
|
|
84
|
+
enableNavigation: true, // 导航耗时监控
|
|
85
|
+
enableRender: true, // 渲染耗时监控
|
|
86
|
+
enableResource: true, // 资源加载耗时
|
|
95
87
|
}),
|
|
96
88
|
]
|
|
97
|
-
|
|
98
|
-
// 过滤配置
|
|
99
|
-
beforeSend(event) {
|
|
100
|
-
// 可以在这里过滤或修改事件
|
|
101
|
-
return event;
|
|
102
|
-
},
|
|
103
89
|
});
|
|
104
90
|
|
|
91
|
+
// 初始化完成后,再调用 App
|
|
105
92
|
App({
|
|
106
|
-
|
|
93
|
+
onLaunch() {
|
|
94
|
+
// ...
|
|
95
|
+
}
|
|
107
96
|
});
|
|
108
97
|
```
|
|
109
98
|
|
|
110
|
-
|
|
99
|
+
---
|
|
111
100
|
|
|
112
|
-
|
|
113
|
-
|
|
101
|
+
## 📚 常用进阶用法
|
|
102
|
+
|
|
103
|
+
初始化完成后,SDK 会自动在后台工作。您也可以使用以下 API 进行手动埋点或主动上报。
|
|
104
|
+
|
|
105
|
+
### 手动异常与消息上报
|
|
114
106
|
|
|
115
|
-
|
|
107
|
+
```javascript
|
|
108
|
+
// 手动捕获并上报一个 Error 对象
|
|
116
109
|
try {
|
|
117
|
-
|
|
118
|
-
throw new Error('Something went wrong!');
|
|
110
|
+
throw new Error('支付接口解析失败');
|
|
119
111
|
} catch (error) {
|
|
120
112
|
Sentry.captureException(error);
|
|
121
113
|
}
|
|
122
114
|
|
|
123
|
-
//
|
|
124
|
-
Sentry.captureMessage('
|
|
115
|
+
// 记录一条关键信息
|
|
116
|
+
Sentry.captureMessage('用户主动取消了授权', 'info');
|
|
117
|
+
```
|
|
125
118
|
|
|
126
|
-
|
|
127
|
-
Sentry.addBreadcrumb({
|
|
128
|
-
message: '用户点击了按钮',
|
|
129
|
-
category: 'ui',
|
|
130
|
-
level: 'info',
|
|
131
|
-
data: {
|
|
132
|
-
buttonId: 'submit-btn'
|
|
133
|
-
}
|
|
134
|
-
});
|
|
119
|
+
### 丰富上下文信息 (Context & Breadcrumbs)
|
|
135
120
|
|
|
136
|
-
|
|
121
|
+
```javascript
|
|
122
|
+
// 设置当前操作的用户信息
|
|
137
123
|
Sentry.setUser({
|
|
138
|
-
id: '
|
|
139
|
-
username: '
|
|
140
|
-
email: 'john@example.com'
|
|
124
|
+
id: 'user_12345',
|
|
125
|
+
username: 'John Doe'
|
|
141
126
|
});
|
|
142
127
|
|
|
143
|
-
//
|
|
144
|
-
Sentry.setTag('
|
|
128
|
+
// 设置用于过滤和统计的全局标签
|
|
129
|
+
Sentry.setTag('page_module', 'checkout_counter');
|
|
145
130
|
|
|
146
|
-
//
|
|
147
|
-
Sentry.
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
131
|
+
// 手动添加一条业务追踪面包屑
|
|
132
|
+
Sentry.addBreadcrumb({
|
|
133
|
+
message: '用户点击了[确认支付]按钮',
|
|
134
|
+
category: 'action',
|
|
135
|
+
level: 'info',
|
|
136
|
+
data: { cartId: 'c_888' }
|
|
151
137
|
});
|
|
152
138
|
```
|
|
153
139
|
|
|
154
|
-
###
|
|
140
|
+
### 自定义性能测速 (Performance)
|
|
155
141
|
|
|
156
142
|
```javascript
|
|
157
|
-
|
|
143
|
+
// 标记起始点
|
|
144
|
+
Sentry.addPerformanceMark('api-request-start');
|
|
145
|
+
// ... 执行耗时操作
|
|
146
|
+
Sentry.addPerformanceMark('api-request-end');
|
|
158
147
|
|
|
159
|
-
//
|
|
160
|
-
Sentry.
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
148
|
+
// 测量并记录该区间
|
|
149
|
+
Sentry.measurePerformance('fetch-user-data', 'api-request-start', 'api-request-end');
|
|
150
|
+
```
|
|
151
|
+
|
|
152
|
+
---
|
|
153
|
+
|
|
154
|
+
## 🗺️ Source Map 支持与配置
|
|
155
|
+
|
|
156
|
+
在小程序中,报错堆栈的路径通常是各种虚拟路径(如 `appservice/pages/index.js`),这导致直接上传的 Source Map 无法被 Sentry 正确解析。
|
|
157
|
+
|
|
158
|
+
SDK 内部已经为您解决了这个痛点:
|
|
159
|
+
只要在 `Sentry.init` 时开启了 `enableSourceMap: true`(默认开启),SDK 会在报错时自动拦截并抹平各平台的虚拟路径,统一替换为标准前缀 `app:///`。
|
|
160
|
+
|
|
161
|
+
您**只需要在打包上传 Source Map 时,确保配置的 `url-prefix` 为 `app:///` 即可**。
|
|
162
|
+
|
|
163
|
+
**使用 sentry-cli 上传示例:**
|
|
164
|
+
|
|
165
|
+
```bash
|
|
166
|
+
sentry-cli releases files "your-project-release-id" upload-sourcemaps ./dist \
|
|
167
|
+
--url-prefix "app:///" \
|
|
168
|
+
--ext .js --ext .map
|
|
177
169
|
```
|
|
178
170
|
|
|
179
|
-
|
|
171
|
+
*(注:在微信开发者工具上传代码时,请**务必关闭**工具自带的“ES6转ES5”和“代码压缩”功能,将这些工作交给 Webpack/Vite 等构建工具,以防行列号错位。)*
|
|
172
|
+
|
|
173
|
+
---
|
|
174
|
+
|
|
175
|
+
## ❓ 常见问题 (FAQ)
|
|
176
|
+
|
|
177
|
+
### 1. 初始化后无法自动上报异常,必须在 `onError` 中手动调 API 吗?
|
|
178
|
+
|
|
179
|
+
**完全不需要**。
|
|
180
|
+
`sentry-miniapp` 在初始化时会自动劫持并注册平台底层的全局错误监听(如 `wx.onError`)。只要确保 `Sentry.init` 在 `App()` 调用**之前**执行,它就能自动捕获所有未处理的 JS 异常。
|
|
181
|
+
如果发现没上报,请检查:
|
|
182
|
+
|
|
183
|
+
1. Sentry 域名是否加入了小程序后台合法域名。
|
|
184
|
+
2. `sampleRate` (采样率) 是否被意外设置得太低。
|
|
185
|
+
3. 微信开发者工具某些环境下的报错不会触发底层 `onError`,建议在**真机预览**下测试。
|
|
186
|
+
|
|
187
|
+
### 2. 这个 SDK 支持 Session Replay (屏幕操作回放) 吗?
|
|
188
|
+
|
|
189
|
+
目前 **不支持** `Sentry.replayIntegration()`。
|
|
190
|
+
Sentry 官方的 Replay 功能强依赖于浏览器标准 DOM 环境(通过 rrweb 录制)。小程序采用双线程架构且没有开放标准 DOM 接口,无法直接复用。建议通过完善**Breadcrumbs(面包屑路径)**结合**自定义日志**来还原用户操作现场。
|
|
191
|
+
|
|
192
|
+
---
|
|
193
|
+
|
|
194
|
+
## 🤝 参与贡献
|
|
195
|
+
|
|
196
|
+
我们非常欢迎开发者提交 `Pull Request` 或通过 `Issues` 提出宝贵意见!
|
|
180
197
|
|
|
181
|
-
|
|
198
|
+
要参与本地开发:
|
|
182
199
|
|
|
183
|
-
|
|
200
|
+
1. `npm install` 安装依赖
|
|
201
|
+
2. `npm run dev` 启动监听编译
|
|
202
|
+
3. `npm run test:all` 运行完整的单元测试与集成测试套件
|
|
184
203
|
|
|
185
|
-
|
|
204
|
+
---
|
|
186
205
|
|
|
187
|
-
|
|
206
|
+
## 💬 联系与交流
|
|
188
207
|
|
|
189
|
-
|
|
208
|
+
遇到问题?想探讨小程序监控方案?欢迎加入我们的交流群。
|
|
209
|
+
由于微信群二维码有 7 天时效性限制,请添加作者微信(**备注 sentry-miniapp**),由作者邀请您入群:
|
|
190
210
|
|
|
191
|
-
<img src="docs/qrcode/zhiyao.jpeg" alt="作者微信二维码" width="
|
|
211
|
+
<img src="docs/qrcode/zhiyao.jpeg" alt="作者微信二维码" width="200" style="border-radius: 8px; box-shadow: 0 4px 8px rgba(0,0,0,0.1);" />
|