@arms/rum-miniapp 0.1.5 → 0.1.7

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.
Files changed (90) hide show
  1. package/README.md +146 -9
  2. package/dist/miniapp-sdk.js +1 -1
  3. package/docs/SDK/347/211/210/346/234/254/350/257/264/346/230/216.md +71 -0
  4. package/docs/SDK/351/205/215/347/275/256/345/217/202/350/200/203.md +523 -0
  5. package/docs//346/216/245/345/205/245/345/260/217/347/250/213/345/272/217/345/272/224/347/224/250.md +272 -0
  6. package/es/collector/action/index.d.ts +14 -0
  7. package/es/collector/action/index.js +1 -0
  8. package/es/collector/application/index.d.ts +15 -0
  9. package/es/collector/application/index.js +5 -0
  10. package/es/collector/exception/index.d.ts +29 -0
  11. package/es/collector/exception/index.js +7 -0
  12. package/es/collector/longtask/render-block.d.ts +15 -0
  13. package/es/collector/longtask/render-block.js +1 -0
  14. package/es/collector/resource/api.d.ts +28 -0
  15. package/es/collector/resource/api.js +19 -0
  16. package/es/collector/resource/static-resource.d.ts +10 -0
  17. package/es/collector/resource/static-resource.js +148 -0
  18. package/es/collector/view/perf.d.ts +26 -0
  19. package/es/collector/view/perf.js +10 -0
  20. package/es/collector/view/pv.d.ts +12 -0
  21. package/es/collector/view/pv.js +2 -0
  22. package/es/configManager.d.ts +13 -0
  23. package/es/configManager.js +7 -0
  24. package/es/empty.js +1 -0
  25. package/es/index.d.ts +3 -0
  26. package/es/index.js +1 -0
  27. package/es/processor/default-processor.d.ts +5 -0
  28. package/es/processor/default-processor.js +1 -0
  29. package/es/processor/session-processor.d.ts +8 -0
  30. package/es/processor/session-processor.js +1 -0
  31. package/es/reporter/index.d.ts +10 -0
  32. package/es/reporter/index.js +3 -0
  33. package/es/shell.d.ts +42 -0
  34. package/es/shell.js +10 -0
  35. package/es/style.js +1 -0
  36. package/es/types/client.d.ts +39 -0
  37. package/es/types/client.js +1 -0
  38. package/es/utils/api.d.ts +43 -0
  39. package/es/utils/api.js +3 -0
  40. package/es/utils/base.d.ts +19 -0
  41. package/es/utils/base.js +16 -0
  42. package/es/utils/constants.d.ts +1 -0
  43. package/es/utils/constants.js +1 -0
  44. package/es/utils/hackApp.d.ts +3 -0
  45. package/es/utils/hackApp.js +2 -0
  46. package/es/utils/hackComponent.d.ts +3 -0
  47. package/es/utils/hackComponent.js +5 -0
  48. package/es/utils/hackPage.d.ts +3 -0
  49. package/es/utils/hackPage.js +2 -0
  50. package/es/utils/network.d.ts +1 -0
  51. package/es/utils/network.js +1 -0
  52. package/es/utils/platform.d.ts +41 -0
  53. package/es/utils/platform.js +6 -0
  54. package/es/utils/session.d.ts +26 -0
  55. package/es/utils/session.js +4 -0
  56. package/es/utils/sse.d.ts +23 -0
  57. package/es/utils/sse.js +14 -0
  58. package/es/utils/url.d.ts +29 -0
  59. package/es/utils/url.js +16 -0
  60. package/es/utils/view.d.ts +32 -0
  61. package/es/utils/view.js +31 -0
  62. package/lib/collector/action/index.js +1 -1
  63. package/lib/collector/application/index.js +2 -2
  64. package/lib/collector/exception/index.js +3 -3
  65. package/lib/collector/longtask/render-block.js +1 -1
  66. package/lib/collector/resource/api.d.ts +1 -0
  67. package/lib/collector/resource/api.js +13 -7
  68. package/lib/collector/resource/static-resource.js +10 -10
  69. package/lib/collector/view/perf.js +6 -5
  70. package/lib/collector/view/pv.js +2 -2
  71. package/lib/configManager.js +6 -5
  72. package/lib/empty.js +2 -0
  73. package/lib/processor/default-processor.js +1 -1
  74. package/lib/processor/session-processor.js +1 -1
  75. package/lib/reporter/index.js +2 -2
  76. package/lib/shell.js +6 -6
  77. package/lib/types/client.d.ts +21 -2
  78. package/lib/utils/api.d.ts +13 -0
  79. package/lib/utils/api.js +3 -1
  80. package/lib/utils/base.js +1 -1
  81. package/lib/utils/hackApp.js +2 -2
  82. package/lib/utils/hackComponent.js +2 -2
  83. package/lib/utils/hackPage.js +2 -2
  84. package/lib/utils/platform.js +1 -1
  85. package/lib/utils/session.js +4 -2
  86. package/lib/utils/sse.d.ts +23 -0
  87. package/lib/utils/sse.js +14 -0
  88. package/lib/utils/url.d.ts +15 -9
  89. package/lib/utils/url.js +10 -19
  90. package/package.json +12 -3
@@ -0,0 +1,71 @@
1
+ # 小程序 SDK 版本说明
2
+
3
+ 本文档介绍用户体验监控小程序 SDK(`@arms/rum-miniapp`)的版本发布历史。
4
+
5
+ > **运行环境** SDK 支持微信 / 支付宝 / 百度 / 字节 / 快手 / 钉钉 / 京东等主流小程序平台,兼容 Taro / Uniapp 跨端框架。SSE 流式监控仅微信小程序(基础库 >= 2.20.2)支持。
6
+
7
+ ## 版本历史
8
+
9
+ ### v0.1.7(2026年06月)
10
+
11
+ - 新增 **SSE 流式接口监控**(微信小程序):自动产出 `time_to_first_token` / `inter_token_latency_avg` / `inter_token_latency_max` / `message_count` 等指标
12
+ - 升级核心依赖 `@arms/rum-core` 至 0.1.5
13
+ - 优化包体积:117KB → 58KB(降幅 50%),通过排除不必要的 Babel 转译、启用 tree-shaking 实现
14
+ - 移除 `@babel/runtime` peerDependencies 依赖
15
+
16
+ ### v0.1.4(2026年03月)
17
+
18
+ - 升级核心依赖 `@arms/rum-core`,同步会话存储模型重构
19
+ - 优化 API 采集性能
20
+
21
+ ### v0.1.2(2025年12月)
22
+
23
+ - 增强新老版本远程配置兼容性
24
+ - 支持 v2 远程配置协议(`agentconfig` 接口)
25
+
26
+ ### v0.1.1(2025年11月)
27
+
28
+ - 远端配置升级,支持 **启动优先**(`launch-first`)和 **配置优先**(`remote-first`)模式
29
+ - 增加 SDK 透出 **collector API**(`ArmsRum.getCollector(name)`)
30
+
31
+ ### v0.1.0(2025年09月)
32
+
33
+ - 新增 **卡顿监控**(`longTask`):通过 `setUpdatePerformanceListener` 监听 setData 渲染阻塞
34
+ - 新增 **动态配置下发**(`remoteConfig`)
35
+ - 优化性能指标采集
36
+
37
+ ### v0.0.42(2025年09月)
38
+
39
+ - 新增支持 **快手小程序**
40
+ - 增强 **支付宝小程序** 异常采集能力
41
+ - 增强 **Uniapp** 的异常监控兼容性
42
+ - 优化 trace 请求头插入兼容性
43
+
44
+ ### v0.0.37(2025年04月)
45
+
46
+ - 新增 **卡顿监控** 支持
47
+ - 新增 **动态配置下发**
48
+ - 优化性能指标采集
49
+
50
+ ### v0.0.29(2024年09月)
51
+
52
+ - 增加打包版本号区分 npm 和 CDN
53
+
54
+ ### v0.0.28(2024年09月)
55
+
56
+ - 新增兼容 **百度** 和 **京东** 小程序
57
+ - 微信、字节、支付宝小程序增加 **3 个启动性能指标** 和 **4 个 view 性能指标**
58
+ - 支持 **Session 采样** 和配置(`sessionConfig`)
59
+ - 优化 App、Page、Component 的劫持方式
60
+ - 增加自定义属性 **`properties`**,对所有类型的事件生效
61
+ - 增加对小程序 Exception 和 API 的 **Filter**
62
+
63
+ ### v0.0.25(2024年07月)
64
+
65
+ - 增加 **`evaluateApi`** 钩子,用于对 API 进行自定义解析,支持 `snapshots`
66
+ - 提供 device、os、geo、isp 等公共属性的自定义配置
67
+ - 增加自定义异常上报
68
+ - API 无法获取对应 performance 时,补充模拟耗时
69
+ - 重构 EventId 生成规范
70
+
71
+ > **历史版本(v0.0.x 早期)** 早期版本聚焦基础能力建设:PV / 性能 / 异常 / API / 用户行为 / 远程配置 / 分布式链路追踪。当前已不建议使用,请升级至 v0.1.x。
@@ -0,0 +1,523 @@
1
+ # 小程序 SDK 配置参考
2
+
3
+ 本文档介绍用户体验监控小程序 SDK 的全部配置项与 API。通过 `ArmsRum.init(config)` 传入 `IMiniappConfig` 对象完成 SDK 初始化。
4
+
5
+ 下面按用途分组说明各配置项。
6
+
7
+ ---
8
+
9
+ ## SDK 基础配置
10
+
11
+ | 参数 | 类型 | 描述 | 是否必填 | 默认值 |
12
+ |------|------|------|----------|--------|
13
+ | `pid` | `string` | 应用 ID | 是 | — |
14
+ | `endpoint` | `string` | 数据上报地址 | 是 | — |
15
+ | `enable` | `boolean` | 是否启用 SDK,关闭后所有采集器与上报均不工作 | 否 | `true` |
16
+ | `env` | `'prod' \| 'gray' \| 'pre' \| 'daily' \| 'local'` | 应用环境标识 | 否 | `'prod'` |
17
+ | `version` | `string` | 应用版本号 | 否 | — |
18
+
19
+ > **说明** `endpoint` 为完整上报地址 URL,可在阿里云云监控控制台「用户体验监控 > 应用列表」创建应用后获取。
20
+
21
+ ```typescript
22
+ import ArmsRum from '@arms/rum-miniapp';
23
+
24
+ ArmsRum.init({
25
+ pid: '<your-pid>',
26
+ endpoint: '<your-endpoint>',
27
+ enable: true,
28
+ env: 'prod',
29
+ version: '1.0.0',
30
+ });
31
+ ```
32
+
33
+ ---
34
+
35
+ ## user 配置
36
+
37
+ 用户信息,便于在控制台按用户维度排查问题。
38
+
39
+ | 参数 | 类型 | 描述 | 默认值 |
40
+ |------|------|------|--------|
41
+ | `user.id` | `string` | 用户 ID(由 SDK 默认生成,不建议覆盖) | 自动生成 |
42
+ | `user.name` | `string` | 用户名称 | — |
43
+ | `user.tags` | `string` | 用户标签(逗号分隔) | — |
44
+
45
+ ```typescript
46
+ ArmsRum.init({
47
+ pid: '<your-pid>',
48
+ endpoint: '<your-endpoint>',
49
+ user: { name: '张三', tags: 'vip,enterprise' },
50
+ });
51
+ ```
52
+
53
+ > **说明** 如需关联业务自有账号体系,建议使用 `user.name` 或 `user.tags`,强制覆盖 `user.id` 会影响 UV 计算。
54
+
55
+ ---
56
+
57
+ ## sessionConfig 配置
58
+
59
+ 会话(Session)的采样与生命周期策略。
60
+
61
+ | 参数 | 类型 | 描述 | 默认值 |
62
+ |------|------|------|--------|
63
+ | `sampling` | `number` | 会话采样率(0~100,百分比) | `100` |
64
+ | `sampleRate` | `number` | **已废弃**:旧的会话采样率字段(0~1);保留兼容,运行时自动转换。新接入请用 `sampling` | — |
65
+ | `maxDuration` | `number` | 会话最大持续时间(ms) | `86400000`(24h) |
66
+ | `overtime` | `number` | 会话无活动超时时间(ms) | `1800000`(30min) |
67
+ | `storage` | `'auto' \| 'localStorage' \| 'memory'` | 会话存储介质 | `'auto'` |
68
+
69
+ ```typescript
70
+ ArmsRum.init({
71
+ pid: '<your-pid>',
72
+ endpoint: '<your-endpoint>',
73
+ sessionConfig: {
74
+ sampling: 50, // 50% 采样
75
+ maxDuration: 86400000, // 最长 24 小时
76
+ overtime: 1800000, // 30 分钟无活动超时
77
+ },
78
+ });
79
+ ```
80
+
81
+ > 小程序本地缓存中存储了 `_arms_uid`(用户 ID)和 `_arms_session`(Session 信息:sessionId、sampled、startTime、lastTime)。
82
+
83
+ ---
84
+
85
+ ## reportConfig 配置
86
+
87
+ 上报节奏配置。
88
+
89
+ | 参数 | 类型 | 描述 | 默认值 |
90
+ |------|------|------|--------|
91
+ | `flushTime` | `number` | 上报时间间隔(ms),取值范围 [0, 10000] | `3000` |
92
+ | `maxEventCount` | `number` | 单次上报最大事件数,取值范围 [1, 100] | `20` |
93
+
94
+ ```typescript
95
+ ArmsRum.init({
96
+ pid: '<your-pid>',
97
+ endpoint: '<your-endpoint>',
98
+ reportConfig: {
99
+ flushTime: 0, // 立即上报
100
+ maxEventCount: 50, // 一次最多上报 50 条
101
+ },
102
+ });
103
+ ```
104
+
105
+ ---
106
+
107
+ ## remoteConfig 配置
108
+
109
+ 远程配置动态管控。也可直接传 `remoteConfig: true` 开启默认远程配置。
110
+
111
+ | 参数 | 类型 | 描述 | 默认值 |
112
+ |------|------|------|--------|
113
+ | `enable` | `boolean` | 是否启用远程配置 | `false` |
114
+ | `url` | `string` | 配置服务器 URL;未指定时 SDK 从 `endpoint` / `pid` 自动推导 | — |
115
+ | `mode` | `'launch-first' \| 'remote-first'` | `launch-first` 先用本地配置启动,异步拉取远端;`remote-first` 阻塞等待云端配置 | `'launch-first'` |
116
+ | `cacheTimeout` | `number` | 本地配置缓存有效期(ms) | `3600000`(1h) |
117
+ | `region` | `string` | 远程配置拉取地域 | `'cn-hangzhou'` |
118
+
119
+ ```typescript
120
+ ArmsRum.init({
121
+ pid: '<your-pid>',
122
+ endpoint: '<your-endpoint>',
123
+ remoteConfig: {
124
+ enable: true,
125
+ mode: 'launch-first',
126
+ region: 'cn-hangzhou',
127
+ },
128
+ });
129
+ ```
130
+
131
+ > SDK 获取远程配置后会缓存到本地存储,下次启动时优先使用本地缓存配置。需 SDK 版本 >= 0.0.37。
132
+
133
+ ---
134
+
135
+ ## collectors 配置(采集器)
136
+
137
+ 各采集器的启用/禁用开关。每个采集器支持 `boolean` 或对象细化配置。
138
+
139
+ | 采集器 | 类型 | 描述 | 默认值 |
140
+ |--------|------|------|--------|
141
+ | `api` | `boolean \| IApiCollectorConfig` | API 请求监控(含 SSE) | `true` |
142
+ | `jsError` | `boolean \| ICollectorConfig` | 未捕获异常 + 未处理 Promise 拒绝 | `true` |
143
+ | `consoleError` | `boolean \| ICollectorConfig` | `console.error` 拦截上报 | `true` |
144
+ | `action` | `boolean \| ICollectorConfig` | 用户行为(tap 事件) | `true` |
145
+ | `longTask` | `boolean \| ICollectorConfig` | 页面卡顿(setData 渲染阻塞) | `true` |
146
+ | `perf` | `boolean \| ICollectorConfig` | 页面性能指标 | `true` |
147
+
148
+ ```typescript
149
+ ArmsRum.init({
150
+ pid: '<your-pid>',
151
+ endpoint: '<your-endpoint>',
152
+ collectors: {
153
+ action: false, // 关闭用户行为采集
154
+ longTask: false, // 关闭卡顿监控
155
+ consoleError: false, // 关闭 console.error 拦截
156
+ api: {
157
+ enable: true,
158
+ filters: [/\.internal\.example\.com/],
159
+ sse: { timeout: 30000 },
160
+ },
161
+ },
162
+ });
163
+ ```
164
+
165
+ ### api 采集器细化配置
166
+
167
+ `api` 采集器统一管理小程序 `request` / `httpRequest` 的所有网络请求。除通用字段外,还支持 SSE 子配置:
168
+
169
+ | 字段 | 类型 | 描述 | 默认值 |
170
+ |------|------|------|--------|
171
+ | `enable` | `boolean` | 是否启用 API 采集 | `true` |
172
+ | `filters` | `MatchOption[]` | 过滤规则;URL 命中即跳过采集 | — |
173
+ | `sse` | `ISseCollectorConfig` | SSE 流式接口配置(仅微信小程序支持) | — |
174
+ | `sse.enabled` | `boolean` | 是否启用 SSE 监控 | `true` |
175
+ | `sse.timeout` | `number` | SSE 流活动超时时间(ms),每次收到数据重置 | `60000` |
176
+
177
+ > **SSE 平台支持**:仅微信小程序(基础库 >= 2.20.2)通过 `enableChunked` 原生支持 SSE 流式监控。其他平台自动静默降级为普通 API 监控。
178
+
179
+ ---
180
+
181
+ ## longTaskConfig 配置(卡顿监控)
182
+
183
+ 卡顿监控的上报限制与判定阈值。
184
+
185
+ | 参数 | 类型 | 描述 | 默认值 |
186
+ |------|------|------|--------|
187
+ | `maxEventCount` | `number` | 单次 PV 内最大上报卡顿次数,取值范围 [1, 5] | `5` |
188
+ | `renderThreshold` | `number` | setData 卡顿判定阈值(ms),最小值 50 | `50` |
189
+
190
+ ```typescript
191
+ ArmsRum.init({
192
+ pid: '<your-pid>',
193
+ endpoint: '<your-endpoint>',
194
+ longTaskConfig: {
195
+ maxEventCount: 4,
196
+ renderThreshold: 100, // setData 耗时超过 100ms 视作卡顿
197
+ },
198
+ });
199
+ ```
200
+
201
+ > 卡顿监控依赖 `setUpdatePerformanceListener` API,部分低版本基础库不支持时 SDK 自动跳过。
202
+
203
+ ---
204
+
205
+ ## tracing 配置
206
+
207
+ 分布式链路追踪。支持 `boolean` 快速开关或 `ITracingOption` 对象。SDK 在 outbound 请求上自动注入追踪头。
208
+
209
+ | 参数 | 类型 | 描述 | 默认值 |
210
+ |------|------|------|--------|
211
+ | `enable` | `boolean` | 是否启用链路追踪 | `false` |
212
+ | `sample` | `number` | 采样率(0~100,百分比) | `100` |
213
+ | `propagatorTypes` | `Array<'tracecontext' \| 'b3' \| 'b3multi' \| 'jaeger' \| 'sw8'>` | 传播协议 | `['tracecontext']` |
214
+ | `allowedUrls` | `Array<MatchOption \| TraceOption>` | 允许注入追踪头的 URL/path 规则;未配置时不会注入 | `[]` |
215
+ | `tracestate` | `boolean` | 是否携带 W3C tracestate | `true` |
216
+ | `baggage` | `boolean` | 是否携带 W3C baggage | `false` |
217
+
218
+ `TraceOption`:`{ match: MatchOption; sampling?: number; propagatorTypes?: PropagatorType[]; tracestate?: boolean; baggage?: boolean }`,可对单条 URL 覆盖全局策略。
219
+
220
+ ```typescript
221
+ ArmsRum.init({
222
+ pid: '<your-pid>',
223
+ endpoint: '<your-endpoint>',
224
+ tracing: {
225
+ enable: true,
226
+ sample: 50,
227
+ propagatorTypes: ['tracecontext', 'b3'],
228
+ allowedUrls: [
229
+ 'https://api.example.com',
230
+ /\/api\/v\d+\//,
231
+ { match: 'https://payment.example.com', sampling: 100 },
232
+ ],
233
+ tracestate: true,
234
+ },
235
+ });
236
+ ```
237
+
238
+ > **`sw8` 排他性** `propagatorTypes` 包含 `'sw8'` 时只使用 sw8 单一协议(SkyWalking 标准要求)。
239
+
240
+ ---
241
+
242
+ ## filters 配置(事件级过滤)
243
+
244
+ 事件级过滤规则,命中后该事件不上报。与 `collectors.api.filters`(采集器级,命中即不采)互补。
245
+
246
+ | 参数 | 类型 | 描述 |
247
+ |------|------|------|
248
+ | `resource` | `MatchOption \| MatchOption[]` | 资源事件(API / 静态资源),按 URL 匹配 |
249
+ | `exception` | `MatchOption \| MatchOption[]` | 异常事件,按 error.name / message / stack 匹配 |
250
+
251
+ `MatchOption` 支持三种形式:
252
+ - `string`:URL/path 前缀匹配
253
+ - `RegExp`:正则匹配
254
+ - `(value: string) => boolean`:自定义函数
255
+
256
+ ```typescript
257
+ ArmsRum.init({
258
+ pid: '<your-pid>',
259
+ endpoint: '<your-endpoint>',
260
+ filters: {
261
+ exception: [
262
+ 'Test error',
263
+ /^Script error\.?$/,
264
+ (msg) => msg.includes('example-error'),
265
+ ],
266
+ resource: [
267
+ 'https://example.com/',
268
+ /localhost/i,
269
+ ],
270
+ },
271
+ });
272
+ ```
273
+
274
+ ---
275
+
276
+ ## 小程序专属配置
277
+
278
+ | 参数 | 类型 | 描述 | 默认值 |
279
+ |------|------|------|--------|
280
+ | `parseViewName` | `(url: string) => string` | 自定义页面 name 解析(入参为当前页面路由) | — |
281
+ | `parseResourceName` | `(url: string) => string` | 自定义资源 name 解析(入参为请求 URL) | — |
282
+ | `evaluateApi` | `(options, response, error?) => Promise<IApiBaseAttr>` | 自定义 API 事件解析回调 | — |
283
+ | `beforeReport` | `(bundle) => any` | 上报前回调,可修改或阻止数据上报 | — |
284
+ | `properties` | `Record<string, number \| string>` | 全局自定义属性,附加到所有上报事件 | — |
285
+
286
+ ### evaluateApi:自定义 payload 写入 snapshots
287
+
288
+ `evaluateApi` 用于把请求/响应内容写入事件。**SDK 不自动采集 payload**——业务侧按需提取并通过返回的 `snapshots` 字段上报,SDK 自动裁剪至 5KB。
289
+
290
+ 回调入参:
291
+
292
+ | 参数 | 类型 | 描述 |
293
+ |------|------|------|
294
+ | `options` | `Object` | 请求参数(url、headers、data 等) |
295
+ | `response` | `Object` | 请求响应体 |
296
+ | `error` | `Error \| undefined` | 仅请求失败时传入 |
297
+
298
+ 返回值结构(`IApiBaseAttr`):
299
+
300
+ | 字段 | 类型 | 说明 |
301
+ |------|------|------|
302
+ | `name` | `string` | API 名称(优先级高于 `parseResourceName`),最大 1000 字符 |
303
+ | `message` | `string` | API 信息描述,最大 1000 字符 |
304
+ | `success` | `0 \| 1` | 业务成功/失败标记 |
305
+ | `duration` | `number` | 覆盖 SDK 推导的耗时(ms) |
306
+ | `status_code` | `number \| string` | 覆盖 SDK 推导的状态码 |
307
+ | `snapshots` | `string` | 写入 `event.snapshots`;自动经 5KB 裁剪 |
308
+ | `properties` | `Record<string, number \| string>` | 写入 `event.properties`;保留给用户业务标签 |
309
+
310
+ ```typescript
311
+ ArmsRum.init({
312
+ pid: '<your-pid>',
313
+ endpoint: '<your-endpoint>',
314
+ evaluateApi: async (options, response, error) => {
315
+ const respText = JSON.stringify(response);
316
+ return {
317
+ success: error ? 0 : 1,
318
+ snapshots: JSON.stringify({
319
+ params: options.data,
320
+ response: respText.substring(0, 2000),
321
+ }),
322
+ };
323
+ },
324
+ });
325
+ ```
326
+
327
+ > **超时与错误处理** 回调抛错时 SDK 静默捕获,回退到原始事件(不会阻塞上报)。
328
+
329
+ ---
330
+
331
+ ## 其他配置
332
+
333
+ RUM SDK 支持配置基于设备和网络等的公共属性,主动配置优先级高于自动解析。
334
+
335
+ | 参数 | 类型 | 描述 |
336
+ |------|------|------|
337
+ | `device` | `Object` | 设备信息 |
338
+ | `os` | `Object` | 系统、容器信息 |
339
+ | `geo` | `Object` | 行政地理信息 |
340
+ | `isp` | `Object` | 运营商信息 |
341
+ | `net` | `Object` | 网络信息(SDK 自动监听网络类型变化) |
342
+
343
+ ```typescript
344
+ ArmsRum.init({
345
+ pid: '<your-pid>',
346
+ endpoint: '<your-endpoint>',
347
+ geo: {
348
+ country: 'China',
349
+ city: 'Hangzhou',
350
+ },
351
+ });
352
+ ```
353
+
354
+ ---
355
+
356
+ ## properties 配置
357
+
358
+ 全局自定义属性,对所有上报事件生效。
359
+
360
+ | 参数 | 类型 | 描述 |
361
+ |------|------|------|
362
+ | `[key: string]` | `string \| number` | key 最大 50 字符;value 为 string 时最大 2000 字符。不符合要求的键值对会被移除 |
363
+
364
+ - 全局 `properties` 与事件级 `properties` 存储时会合并,事件级优先
365
+ - 合并后键值对数量不可超过 20 对
366
+
367
+ ```typescript
368
+ ArmsRum.init({
369
+ pid: '<your-pid>',
370
+ endpoint: '<your-endpoint>',
371
+ properties: {
372
+ department: 'engineering',
373
+ region: 'cn-hangzhou',
374
+ },
375
+ });
376
+ ```
377
+
378
+ ---
379
+
380
+ ## SDK API
381
+
382
+ ### `ArmsRum.init(config)`
383
+
384
+ 初始化 SDK。返回 `Promise`,可链式处理初始化完成事件。
385
+
386
+ ```typescript
387
+ import ArmsRum from '@arms/rum-miniapp';
388
+
389
+ ArmsRum.init({
390
+ pid: '<your-pid>',
391
+ endpoint: '<your-endpoint>',
392
+ env: 'prod',
393
+ version: '1.0.0',
394
+ });
395
+ ```
396
+
397
+ ---
398
+
399
+ ### `ArmsRum.getConfig()`
400
+
401
+ 获取当前 SDK 配置(合并远程配置后的最终态)。
402
+
403
+ ```typescript
404
+ const config = ArmsRum.getConfig();
405
+ console.log(config?.endpoint, config?.env);
406
+ ```
407
+
408
+ ---
409
+
410
+ ### `ArmsRum.setConfig()`
411
+
412
+ 动态修改 SDK 配置,支持两种调用方式。
413
+
414
+ ```typescript
415
+ // 指定 key 设置
416
+ ArmsRum.setConfig('env', 'pre');
417
+
418
+ // 覆盖设置
419
+ const config = ArmsRum.getConfig();
420
+ ArmsRum.setConfig({
421
+ ...config,
422
+ version: '2.0.0',
423
+ env: 'pre',
424
+ });
425
+ ```
426
+
427
+ > 修改后采集器立即按新配置工作;已上报的事件不受影响。
428
+
429
+ ---
430
+
431
+ ### `ArmsRum.getCollector(name)`
432
+
433
+ 获取某个内置采集器实例,用于扩展或调试。
434
+
435
+ 可选 `name` 值:
436
+
437
+ | 采集器名 | 说明 |
438
+ |----------|------|
439
+ | `'pv-collector'` | 页面 PV / 路由切换 |
440
+ | `'perf-collector'` | 页面性能 |
441
+ | `'exception-collector'` | JS 异常 |
442
+ | `'api-collector'` | API 请求(含 SSE) |
443
+ | `'action-collector'` | 用户行为 |
444
+ | `'render-block-collector'` | 卡顿监控 |
445
+
446
+ ---
447
+
448
+ ### `ArmsRum.sendCustom(event)`
449
+
450
+ 上报自定义数据,必须包含 `type` 和 `name` 两个属性。
451
+
452
+ | 参数 | 类型 | 描述 | 是否必填 |
453
+ |------|------|------|----------|
454
+ | `type` | `string` | 类型 | 是 |
455
+ | `name` | `string` | 名称 | 是 |
456
+ | `group` | `string` | 分组 | 否 |
457
+ | `value` | `number` | 值 | 否 |
458
+ | `properties` | `object` | 自定义属性 | 否 |
459
+
460
+ ```typescript
461
+ ArmsRum.sendCustom({
462
+ type: 'CustomEventType',
463
+ name: 'order-submit',
464
+ group: 'business',
465
+ value: 99.9,
466
+ properties: { orderId: '12345' },
467
+ });
468
+ ```
469
+
470
+ ---
471
+
472
+ ### `ArmsRum.sendException(event)`
473
+
474
+ 上报自定义异常数据,必须包含 `name` 和 `message` 两个属性。
475
+
476
+ | 参数 | 类型 | 描述 | 是否必填 |
477
+ |------|------|------|----------|
478
+ | `name` | `string` | 异常名称 | 是 |
479
+ | `message` | `string` | 异常信息 | 是 |
480
+ | `file` | `string` | 异常发生文件 | 否 |
481
+ | `stack` | `string` | 异常堆栈信息 | 否 |
482
+ | `line` | `number` | 异常发生行数 | 否 |
483
+ | `column` | `number` | 异常发生列数 | 否 |
484
+ | `properties` | `object` | 自定义属性 | 否 |
485
+
486
+ ```typescript
487
+ ArmsRum.sendException({
488
+ name: 'BusinessError',
489
+ message: 'Payment failed',
490
+ stack: 'Error: Payment failed\n at ...',
491
+ properties: { userId: 'u_123' },
492
+ });
493
+ ```
494
+
495
+ ---
496
+
497
+ ### `ArmsRum.sendResource(event)`
498
+
499
+ 上报自定义资源,必须包含 `name`、`type` 和 `duration` 三个属性。
500
+
501
+ | 参数 | 类型 | 描述 | 是否必填 |
502
+ |------|------|------|----------|
503
+ | `name` | `string` | 资源名 | 是 |
504
+ | `type` | `string` | 资源类型(如 `api`、`script`、`image`) | 是 |
505
+ | `duration` | `number` | 请求耗时(ms) | 是 |
506
+ | `success` | `number` | 请求状态:1 成功 / 0 失败 / -1 未知 | 否 |
507
+ | `method` | `string` | 请求方法 | 否 |
508
+ | `status_code` | `number \| string` | 请求状态码 | 否 |
509
+ | `message` | `string` | 请求消息 | 否 |
510
+ | `url` | `string` | 请求地址 | 否 |
511
+ | `trace_id` | `string` | 链路追踪 ID | 否 |
512
+ | `properties` | `object` | 自定义属性 | 否 |
513
+
514
+ ```typescript
515
+ ArmsRum.sendResource({
516
+ name: 'getListByPage',
517
+ type: 'api',
518
+ duration: 800,
519
+ url: 'https://api.example.com/list',
520
+ success: 1,
521
+ properties: { page: '1' },
522
+ });
523
+ ```