@blueking/open-telemetry 0.0.22-beta.1 → 0.0.23

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 (45) hide show
  1. package/README.md +258 -70
  2. package/dist/bk-rum.global.js +2 -2
  3. package/dist/bk-rum.global.js.map +1 -1
  4. package/dist/core/config.d.ts +10 -10
  5. package/dist/core/runtime-context.d.ts +1 -1
  6. package/dist/core/sdk.d.ts +1 -1
  7. package/dist/index.d.ts +2 -2
  8. package/dist/index.js +608 -662
  9. package/dist/index.js.map +1 -1
  10. package/dist/plugins/blank-screen.d.ts +1 -1
  11. package/dist/plugins/common.d.ts +2 -2
  12. package/dist/plugins/device.d.ts +2 -2
  13. package/dist/plugins/resource.d.ts +1 -0
  14. package/dist/plugins/web-vitals.d.ts +1 -3
  15. package/dist/schema/attributes.d.ts +6 -7
  16. package/dist/schema/span.d.ts +24 -15
  17. package/package.json +1 -1
  18. package/skills/bk-rum-otele/SKILL.md +31 -30
  19. package/skills/bk-rum-otele/references/actions.md +166 -0
  20. package/skills/bk-rum-otele/references/api.md +248 -215
  21. package/skills/bk-rum-otele/references/architecture.md +139 -37
  22. package/skills/bk-rum-otele/references/blank-screen.md +190 -0
  23. package/skills/bk-rum-otele/references/capabilities.md +175 -83
  24. package/skills/bk-rum-otele/references/configuration.md +226 -151
  25. package/skills/bk-rum-otele/references/data-model.md +157 -24
  26. package/skills/bk-rum-otele/references/errors-security.md +168 -0
  27. package/skills/bk-rum-otele/references/extensions.md +237 -0
  28. package/skills/bk-rum-otele/references/framework-integration.md +318 -75
  29. package/skills/bk-rum-otele/references/getting-started.md +186 -70
  30. package/skills/bk-rum-otele/references/network.md +258 -0
  31. package/skills/bk-rum-otele/references/performance.md +198 -0
  32. package/skills/bk-rum-otele/references/privacy.md +236 -67
  33. package/skills/bk-rum-otele/references/production.md +266 -0
  34. package/skills/bk-rum-otele/references/session-context.md +185 -0
  35. package/skills/bk-rum-otele/references/telemetry-reference.md +500 -51
  36. package/skills/bk-rum-otele/references/transport-sampling.md +321 -32
  37. package/skills/bk-rum-otele/references/troubleshooting.md +341 -151
  38. package/skills/bk-rum-otele/references/views-routing.md +211 -0
  39. package/skills/bk-rum-otele/references/concepts.md +0 -60
  40. package/skills/bk-rum-otele/references/custom-plugin.md +0 -170
  41. package/skills/bk-rum-otele/references/plugins-basic.md +0 -135
  42. package/skills/bk-rum-otele/references/plugins-error-security.md +0 -86
  43. package/skills/bk-rum-otele/references/plugins-experience.md +0 -124
  44. package/skills/bk-rum-otele/references/plugins-network.md +0 -32
  45. package/skills/bk-rum-otele/references/plugins.md +0 -62
@@ -2,226 +2,301 @@
2
2
 
3
3
  # 配置参考
4
4
 
5
- `BkOpenTelemetry` 只接受当前 `BkOTOptions` 结构。`application.name` 与 `transport.endpoint` 必填。
5
+ 本文按 `BkOTOptions` 的当前 TypeScript 定义列出全部公开配置、默认值和约束。源码只校验真正必填的字段;浏览器兼容、隐私与基数治理仍需要接入方负责。
6
6
 
7
- ```ts
8
- import { BkOpenTelemetry, type BkOTOptions } from '@blueking/open-telemetry';
9
-
10
- const options: BkOTOptions = {
11
- application: { name: 'bk-monitor-web' },
12
- transport: { endpoint: 'https://rum-collector.example.com' },
13
- };
7
+ ## 完整结构
14
8
 
15
- const bkOT = new BkOpenTelemetry(options);
9
+ ```ts
10
+ new BkOpenTelemetry({
11
+ application: { name, environment?, version? },
12
+ transport: {
13
+ endpoint,
14
+ token?, headers?, processor?, batch?, timeoutMillis?, concurrencyLimit?,
15
+ },
16
+ autoStart?,
17
+ enabled?,
18
+ debug?,
19
+ session: { sampleRate?, storageKey? },
20
+ context: { user?, attributes? },
21
+ privacy: { trackingConsent?, redactUrl?, redactAttributes? },
22
+ tracking: {
23
+ request?, resource?, error?, webVitals?, action?, blankScreen?, device?,
24
+ longTask?, websocket?, cspViolation?, view?, beforeSend?,
25
+ },
26
+ extensions: { plugins?, instrumentations? },
27
+ });
16
28
  ```
17
29
 
18
30
  ## 顶层配置
19
31
 
20
- | 字段 | 类型 | 默认 | 说明 |
32
+ | 配置 | 类型 | 默认 | 说明 |
21
33
  | --- | --- | --- | --- |
22
- | `application` | `BkOTApplicationConfig` | 必填 | 应用 Resource 标识 |
23
- | `transport` | `BkOTTransportConfig` | 必填 | 单一 RUM OTLP HTTP 导出链路 |
24
- | `session` | `BkOTSessionConfig` | `{ sampleRate: 1 }` | Session 存储和确定性采样 |
25
- | `tracking` | `BkOTTrackingConfig` | `{}` | 内置采集、View 与事件 Hook |
26
- | `context` | `BkOTContextConfig` | 无 | 初始用户与公共属性 Hook |
27
- | `privacy` | `BkOTPrivacyConfig` | Consent 已授权、默认 URL 脱敏 | 授权、URL 和属性脱敏 |
28
- | `extensions` | `BkOTExtensionsConfig` | 空数组 | 自定义 RUM 插件与 OTel instrumentation |
29
- | `enabled` | `boolean` | `true` | `false` 时 `start()` 不安装采集能力 |
30
- | `autoStart` | `boolean` | `true` | 构造后异步启动 |
31
- | `debug` | `boolean` | `false` | 输出 exporter 数据和可诊断 warning |
34
+ | `application` | `BkOTApplicationConfig` | 必填 | 应用 Resource 信息 |
35
+ | `transport` | `BkOTTransportConfig` | 必填 | 唯一 Trace 上报通道 |
36
+ | `autoStart` | `boolean` | `true` | 构造后异步调用 `start()` |
37
+ | `enabled` | `boolean` | `true` | `false` 时不启动采集;Provider 仍会构造,便于公开 API 形态稳定 |
38
+ | `debug` | `boolean` | `false` | 打印待导出 Span,并显示部分插件初始化/丢弃警告 |
39
+ | `session` | `BkOTSessionConfig` | `{ sampleRate: 1 }` | 会话采样和存储 key |
40
+ | `context` | `BkOTContextConfig` | 无 | 初始用户及按事件读取的自定义属性 |
41
+ | `privacy` | `BkOTPrivacyConfig` | 已授权、默认 URL 清洗 | Consent 与脱敏 Hook |
42
+ | `tracking` | `BkOTTrackingConfig` | 见能力矩阵 | 内置采集开关和参数 |
43
+ | `extensions` | `BkOTExtensionsConfig` | 空数组 | 自定义 RUM 插件和 OTel instrumentation |
44
+
45
+ 如果要调用 `use()` 或 `useInstrumentation()`,必须设置 `autoStart: false`,先注册再 `await start()`。
46
+
47
+ `enabled: false` 和初始 `trackingConsent: 'not-granted'` 都不阻止构造阶段创建 Provider/Resource。默认 Device 开启时,稳定 Device Resource 快照也会在构造时读取;它们只阻止后续插件采集和 Span 发送。对“授权前不能读取浏览器设备信息”有严格要求时,应延后整个构造调用。
32
48
 
33
49
  ## application
34
50
 
35
- | 字段 | 类型 | 默认 | Resource Attribute |
51
+ | 配置 | 类型 | 默认 | 上报位置 |
36
52
  | --- | --- | --- | --- |
37
- | `name` | `string` | 必填且非空 | `service.name` |
38
- | `environment` | `string` | `production` | `deployment.environment.name` |
39
- | `version` | `string` | 无 | `service.version` |
53
+ | `name` | `string` | 必填、非空 | Resource `service.name`,也参与 Session 稳定采样 |
54
+ | `environment` | `string` | `'production'` | Resource `deployment.environment.name` |
55
+ | `version` | `string` | 不发送 | Resource `service.version` |
40
56
 
41
- SDK 还会固定写入 `rum.schema.version=2`、`telemetry.sdk.name=@blueking/open-telemetry`、`telemetry.sdk.language=webjs` 和当前包版本 `telemetry.sdk.version`。默认启用 `tracking.device` 时,浏览器、OS 和稳定的硬件能力也会写入 Resource。
57
+ `name` 应稳定且低基数,不要包含用户、构建时间或路由。`version` 推荐使用可以定位前端构建产物的版本号或 commit 标识。
42
58
 
43
59
  ## transport
44
60
 
61
+ | 配置 | 类型 | 默认 | 说明 |
62
+ | --- | --- | ---: | --- |
63
+ | `endpoint` | `string` | 必填、非空 | 上报根地址;去除末尾 `/` 后追加 `/v1/rum` |
64
+ | `token` | `string` | 无 | 生成默认 `Authorization: Bearer <token>` |
65
+ | `headers` | `Record<string, string>` | 无 | 自定义头;同名时覆盖 token 生成的头 |
66
+ | `processor` | `'batch' \| 'simple'` | `'batch'` | 批量或逐 Span 导出 |
67
+ | `batch` | `BkOTBatchConfig` | OTel 默认值 | 仅 batch 模式生效 |
68
+ | `timeoutMillis` | `number` | `10000` | exporter 请求及重试的总期限,必须大于 0 |
69
+ | `concurrencyLimit` | `number` | `30` | exporter 并发 Promise 上限 |
70
+
71
+ `batch` 子项:
72
+
73
+ | 配置 | 默认 | 说明 |
74
+ | --- | ---: | --- |
75
+ | `maxQueueSize` | `2048` | 队列满后丢弃新的已结束 Span |
76
+ | `maxExportBatchSize` | `512` | 单批 Span 上限,不能有效超过 queue size |
77
+ | `scheduledDelayMillis` | `5000` | 批量调度等待 |
78
+ | `exportTimeoutMillis` | `30000` | processor 等待 export 的上限 |
79
+
80
+ 完整协议与重试语义见[上报协议、批处理与采样](./transport-sampling.md)。
81
+
82
+ ## session
83
+
84
+ | 配置 | 类型 | 默认 | 说明 |
85
+ | --- | --- | --- | --- |
86
+ | `sampleRate` | `number` | `1` | 0–1 的 Session 级确定性采样率;越界会截断 |
87
+ | `storageKey` | `string` | 按应用和 endpoint host 生成 | 跨 Tab 共享 Session 状态的 localStorage key |
88
+
89
+ 15 分钟无活动和 4 小时最大生命周期是当前固定行为,不是公开配置项。localStorage 不可用时退化为页面进程内存,无法跨页面/Tab 共享。详见[Session、用户与设备上下文](./session-context.md)。
90
+
91
+ ## context
92
+
45
93
  ```ts
46
- transport: {
47
- endpoint: 'https://collector.example.com',
48
- token: 'report-token',
49
- headers: { 'X-Tenant': 'blueking' },
50
- processor: 'batch',
51
- batch: {
52
- maxQueueSize: 2048,
53
- maxExportBatchSize: 512,
54
- scheduledDelayMillis: 5000,
55
- exportTimeoutMillis: 30000,
94
+ context: {
95
+ user: { id: 'user-001' },
96
+ attributes: {
97
+ page: () => ({ 'biz.module': 'order' }),
98
+ error: () => ({ 'biz.release_channel': 'gray' }),
99
+ custom: () => ({ 'biz.tenant': 'tenant-a' }),
56
100
  },
57
- timeoutMillis: 10_000,
58
- concurrencyLimit: 10,
59
101
  }
60
102
  ```
61
103
 
62
- ### 根字段
104
+ | 配置 | 调用时机 | 合并到 |
105
+ | --- | --- | --- |
106
+ | `user.id` | 构造时,之后可用 `setUser()` 更新 | 运行时 `user.id` |
107
+ | `attributes.page()` | 插件创建相关 Span、过滤层补属性时 | 页面/请求/事件 Span,具体取决于调用插件 |
108
+ | `attributes.error()` | 创建错误 Span 时 | Error Span |
109
+ | `attributes.custom()` | `reportCustomEvent()` 时 | Custom Span |
63
110
 
64
- | 字段 | 类型 | 默认 | 说明 |
65
- | --- | --- | --- | --- |
66
- | `endpoint` | `string` | 无 | 根地址,去掉末尾 `/` 后拼 `/v1/rum` |
67
- | `token` | `string` | 无 | 生成 `Authorization: Bearer <token>` |
68
- | `headers` | `Record<string,string>` | 无 | RUM 请求 Header |
69
- | `timeoutMillis` | `number` | OTel 默认 | Exporter 超时 |
70
- | `concurrencyLimit` | `number` | OTel 默认 | Exporter 并发数 |
71
- | `processor` | `'batch' \| 'simple'` | `batch` | Span Processor 模式 |
72
- | `batch` | `BkOTBatchConfig` | OTel 默认 | BatchSpanProcessor 参数 |
111
+ Hook 每次调用都应返回新的普通对象,且不能抛异常。SDK 不 hash、不改写 `user.id`。如果 ID 敏感,接入方必须在传入前完成不可逆处理。
73
112
 
74
- `batch` 支持 `maxQueueSize`、`maxExportBatchSize`、`scheduledDelayMillis`、`exportTimeoutMillis`。未提供时使用 OpenTelemetry `BatchSpanProcessor` 默认值。
113
+ 未配置 `attributes.page` 时默认返回原始 `location.host` 和 `location.pathname` 对应的 `rum.page.host/path`。自定义 Hook 会完全替换默认值;`rum.page.path` 不经过 `redactUrl` 或 `getPathGroup`,动态路径应在该 Hook 或 `redactAttributes` 中治理。
75
114
 
76
- SDK 不提供 Logs/Metrics 开关或 Provider,所有 RUM 原始事件只走 `/v1/rum`。
115
+ 属性合并优先级不是全局统一的:各插件会先组合公共字段、Hook 字段和事件字段,`startSpan()` 再用运行时上下文作为底层。精确字段请以对应能力页和[RUM 数据模型](./data-model.md)为准。
77
116
 
78
- 更多行为见[传输与采样](./transport-sampling.md)。
117
+ ## privacy
79
118
 
80
- ## session
119
+ | 配置 | 类型 | 默认 | 说明 |
120
+ | --- | --- | --- | --- |
121
+ | `trackingConsent` | `'granted' \| 'not-granted'` | `'granted'` | 初始化时是否安装采集插件 |
122
+ | `redactUrl` | `(url: string) => string` | 去除认证信息、query、fragment | 所有按规范写入的 URL 在入 Span 前清洗 |
123
+ | `redactAttributes` | `(attributes) => attributes` | 原样返回 | 插件 emit 和 Resource 构造时的统一属性过滤 |
81
124
 
82
- | 字段 | 类型 | 默认 | 说明 |
125
+ `redactAttributes` 必须返回合法 OpenTelemetry `Attributes`。删除字段时返回新对象最安全。Consent 与各 Hook 的覆盖边界见[隐私、Consent 与字段治理](./privacy.md)。
126
+
127
+ ## tracking 默认开关
128
+
129
+ | 配置 | 默认 | 类型/参数 | 作用 |
83
130
  | --- | --- | --- | --- |
84
- | `sampleRate` | `number` | `1` | Session 确定性采样;有限数值限制到 `0..1`,非有限值回退 `1` |
85
- | `storageKey` | `string` | 按应用名和 Endpoint host hash | Session localStorage key |
131
+ | `request` | 开 | `boolean \| BkOTRequestConfig` | Fetch/XHR、可选 traceparent |
132
+ | `resource` | 开 | `boolean` | 静态资源 PerformanceResourceTiming |
133
+ | `error` | 开 | `boolean` | runtime、Promise、资源加载错误 |
134
+ | `webVitals` | 开 | `boolean` | CLS/FCP/INP/LCP/TTFB |
135
+ | `action` | 开 | `boolean \| BkOTActionPluginOptions` | 点击 Action 与挫败信号 |
136
+ | `blankScreen` | 开 | `boolean \| BkOTBlankScreenPluginOptions` | 白屏检测 |
137
+ | `device` | 开 | `boolean \| BkOTDevicePluginOptions` | Resource 与运行时设备属性 |
138
+ | `longTask` | 关 | `boolean \| BkOTLongTaskPluginOptions` | LoAF/Long Task |
139
+ | `websocket` | 关 | `boolean` | WebSocket 连接生命周期 |
140
+ | `cspViolation` | 关 | `boolean` | CSP violation |
141
+ | `view` | 自动、loading 开 | `BkOTViewConfig` | View 边界、命名和 loading time |
142
+ | `beforeSend` | 无 | `(event) => boolean \| void` | 所有结束 Span 的最终业务过滤 |
143
+
144
+ Session、Activity 和 Page View 上下文是关联模型的基础,不提供统一的 `tracking.session: false` 或 `tracking.activity: false` 开关。
145
+
146
+ ## request
86
147
 
87
- Session 固定为 15 分钟无活动过期、4 小时最长生命周期,不开放超时配置。localStorage 不可用时退回进程内存。
148
+ ```ts
149
+ tracking: {
150
+ request: {
151
+ excludedUrls: [/\/health(?:\?|$)/],
152
+ allowedTracingUrls: [url => new URL(url, location.href).origin === 'https://api.example.com'],
153
+ beforeSend(event, context) {
154
+ if (event.resource.outcome.type === 'response' && event.resource.outcome.statusCode === 404) return false;
155
+ event.attributes['biz.api_group'] = 'order';
156
+ },
157
+ },
158
+ }
159
+ ```
160
+
161
+ | 配置 | 默认 | 说明 |
162
+ | --- | --- | --- |
163
+ | `excludedUrls` | 无 | 匹配后不建请求 Span、不注入 trace header,也不计入 View/Action 活动 |
164
+ | `allowedTracingUrls` | 无 | 只有明确匹配的 URL 才注入 W3C `traceparent`,同源也不例外 |
165
+ | `beforeSend` | 无 | 只作用于 Fetch/XHR 请求事件;`event.resource.outcome` 区分 response/abort/timeout/network_error,返回 `false` 丢弃请求 Span |
166
+
167
+ URL matcher 支持 string、RegExp 或函数:
168
+
169
+ - string:URL 包含该字符串即匹配;
170
+ - RegExp:对完整 URL 执行 `test`,SDK 会重置 `lastIndex`;
171
+ - function:返回 true 即匹配;
172
+ - 单个 matcher 或数组均可。
88
173
 
89
- ## tracking.view
174
+ 请求插件不采集请求体、响应体和常规业务 header。详见[网络请求、资源与链路传播](./network.md)。
90
175
 
91
- | 字段 | 类型 | 默认 | 说明 |
176
+ ## view
177
+
178
+ | 配置 | 类型 | 默认 | 说明 |
92
179
  | --- | --- | --- | --- |
93
- | `tracking` | `'auto' \| 'manual'` | `auto` | 自动观察 History/hash,或由业务调用 `startView()` |
94
- | `loadingTime` | `boolean` | `true` | 计算 View 页面活动稳定耗时 |
95
- | `getPathGroup` | `(url) => string \| undefined` | URL pathname | 返回低基数 `view.url_path_group` |
96
- | `excludedActivityUrls` | matcher 或 matcher[] | 无 | 命中请求/资源不延长 View/Action 活动窗口 |
180
+ | `tracking` | `'auto' \| 'manual'` | `'auto'` | 自动监听 History/hash,或只接受 `startView()` |
181
+ | `loadingTime` | `boolean` | `true` | 自动计算初始加载和路由稳定耗时 |
182
+ | `getPathGroup` | `(url) => string \| undefined` | URL pathname | 生成低基数 `view.url_path_group` |
183
+ | `excludedActivityUrls` | URL matcher | 无 | 不让长轮询/流式/埋点请求延长 View loading time |
184
+
185
+ `getPathGroup` 返回 `undefined` 时回退到 pathname。自动路由不会因 query-only 变化创建新 View;hash 变化会创建,但纯锚点语义由业务自行决定是否改用 manual。详见[View、路由与页面加载](./views-routing.md)。
97
186
 
98
- URL matcher 支持字符串包含、`RegExp` 或 `(url) => boolean`。字符串使用 `url.includes(matcher)`,不等于 origin 精确匹配;安全白名单应使用 URL-aware 函数或带边界的正则。自动 View 只对 pathname 和非页内锚点 hash path 的变化切换,query-only 变化不切换。
187
+ ## action
188
+
189
+ | 配置 | 类型 | 默认 | 说明 |
190
+ | --- | --- | --- | --- |
191
+ | `actionNameAttribute` | `string` | `'data-bk-action-name'` | 从目标或祖先读取显式名称 |
192
+ | `maxDuration` | `number` | `10000` | Action 最长持续时间,非法或非正数回退默认 |
193
+ | `nameCollection` | `'explicit' \| 'text'` | `'explicit'` | 是否允许从可访问名称/DOM 文本推导名称 |
99
194
 
100
- ## tracking.request
195
+ `text` 可能采集用户界面文本,开启前必须做隐私评审。详见[用户操作与挫败信号](./actions.md)。
101
196
 
102
- `request` 可设为 `false` 完全关闭 Fetch/XHR patch;`true` 或 `{}` 使用默认配置。
197
+ ## blankScreen
103
198
 
104
- | 字段 | 类型 | 默认 | 说明 |
199
+ | 配置 | 类型 | 默认 | 说明 |
105
200
  | --- | --- | --- | --- |
106
- | `excludedUrls` | matcher 或 matcher[] | 无 | 命中后不采集、不注入、不计入页面活动 |
107
- | `allowedTracingUrls` | matcher 或 matcher[] | 无 | W3C Trace Context 显式白名单;同源请求也必须命中,推荐 URL-aware 函数 |
108
- | `beforeSend` | `(event, context) => boolean \| void` | 无 | 修改请求属性;返回 `false` 丢弃 Span |
201
+ | `rootSelector` | `string` | `'body'` | 应用根节点;不存在会成为白屏候选 |
202
+ | `threshold` | `number` | `0.8` | 九点采样中的空白比例阈值,截断至 0–1 |
203
+ | `checkDelay` | `number` | `1000` | 页面活动稳定后的首检延迟 |
204
+ | `confirmationDelay` | `number` | `200` | 两次白屏结果的确认间隔 |
205
+ | `ignoreSelectors` | `string[]` | 内置 loading/skeleton 列表 | 追加忽略元素,不替换内置列表 |
109
206
 
110
- SDK 不读取或上报请求/响应 Body。
207
+ SPA 应将 `rootSelector` 指向真实挂载节点。详见[白屏检测](./blank-screen.md)。
111
208
 
112
- ## tracking 内置能力
209
+ ## device
113
210
 
114
- | 字段 | 类型 | 默认 | 参数 |
211
+ | 配置 | 类型 | 默认 | 说明 |
115
212
  | --- | --- | --- | --- |
116
- | `resource` | `boolean` | `true` | 无;`false` 只关闭静态资源 Span,Resource Observer 仍服务请求 timing/活动 |
117
- | `error` | `boolean \| BkOTErrorPluginOptions` | `true` | `windowMs=60_000`、`maxPerWindow=5` |
118
- | `webVitals` | `boolean` | `true` | 无 |
119
- | `blankScreen` | `boolean \| BkOTBlankScreenPluginOptions` | `true` | 见下表 |
120
- | `device` | `boolean \| BkOTDevicePluginOptions` | `true` | 见下表 |
121
- | `action` | `boolean \| BkOTActionPluginOptions` | `true` | 见下表 |
122
- | `longTask` | `boolean \| BkOTLongTaskPluginOptions` | `false` | `threshold=50` ms |
123
- | `websocket` | `boolean` | `false` | 无 |
124
- | `cspViolation` | `boolean` | `false` | 无,内置同指纹 60 秒最多 5 条 |
125
-
126
- Blank Screen:
127
-
128
- | 字段 | 默认 | 说明 |
129
- | --- | --- | --- |
130
- | `rootSelector` | `body` | 应用挂载根节点 |
131
- | `threshold` | `0.8` | 九点采样的空白比例,限制到 `0..1` |
132
- | `checkDelay` | `1000` ms | 页面活动稳定后的首次检查延迟 |
133
- | `confirmationDelay` | `200` ms | 两次候选白屏的确认间隔 |
134
- | `ignoreSelectors` | 内置 loading/skeleton 选择器 | 自定义数组会替换内置数组 |
213
+ | `persistentId` | `boolean` | `false` | 是否把匿名设备 ID 持久化到 localStorage |
214
+ | `storageKey` | `string` | `BK_RUM_DEVICE_ID_<应用 hash>` | 持久设备 ID 的 key |
135
215
 
136
- Action:
216
+ `persistentId: false` 时,每次 Device 插件初始化都会创建新的设备 ID,只在当前采集周期内稳定;撤销再授予 Consent 会重新初始化插件并换 ID。`tracking.device: false` 还会阻止浏览器/OS/硬件 Resource 属性生成。详见[Session、用户与设备上下文](./session-context.md)。
137
217
 
138
- | 字段 | 默认 | 说明 |
139
- | --- | --- | --- |
140
- | `actionNameAttribute` | `data-bk-action-name` | 向上查找的显式动作名属性 |
141
- | `maxDuration` | `10_000` ms | 单次 Action 最大活动窗口 |
142
- | `nameCollection` | `explicit` | 默认只取显式属性,否则退回标签名;`text` 才读取可访问名称/DOM 文本 |
218
+ ## longTask
143
219
 
144
- Device:
220
+ | 配置 | 类型 | 默认 | 说明 |
221
+ | --- | --- | --- | --- |
222
+ | `threshold` | `number` | `50` | 最低 duration,非法或负数回退默认 |
145
223
 
146
- | 字段 | 默认 | 说明 |
147
- | --- | --- | --- |
148
- | `persistentId` | `false` | `false` 时 ID 只在当前实例稳定;`true` 写 localStorage |
149
- | `storageKey` | 按应用名 hash | 仅持久 ID 使用 |
224
+ 优先使用 Long Animation Frame API,不支持时回退 Long Tasks API;两者的可用字段不同。详见[性能体验与长任务](./performance.md)。
150
225
 
151
226
  ## tracking.beforeSend
152
227
 
153
228
  ```ts
154
229
  tracking: {
155
230
  beforeSend(event) {
156
- delete event.attributes['debug.payload'];
157
- return event.name !== 'custom.ignore-me';
231
+ if (event.name === 'browser.resource' && event.attributes['url.full']?.includes('/noise')) {
232
+ return false;
233
+ }
234
+ event.attributes['biz.reviewed'] = true;
158
235
  },
159
236
  }
160
237
  ```
161
238
 
162
- 该 Hook 在 Span 进入底层 processor 前执行,可原地修改属性或返回 `false` 丢弃。异常会被吞掉,`debug: true` 时输出 warning;请求专属逻辑优先使用 `tracking.request.beforeSend`。
163
-
164
- ## context
239
+ 事件结构:
165
240
 
166
- | 字段 | 类型 | 默认 | 作用 |
167
- | --- | --- | --- | --- |
168
- | `user.id` | `string` | 无 | SDK 原样写入 `user.id` |
169
- | `attributes.page` | `() => Attributes` | host/path | 页面、请求、错误、Action 等事件属性 |
170
- | `attributes.error` | `() => Attributes` | `{}` | Error Span 附加属性 |
171
- | `attributes.custom` | `() => Attributes` | `{}` | `reportCustomEvent()` 附加属性 |
172
-
173
- 自定义 `page` Hook 会替换默认 host/path Hook,不是合并默认实现。
174
-
175
- ## privacy
241
+ ```ts
242
+ interface BkOTBeforeSendEvent {
243
+ signal: 'span';
244
+ name: string;
245
+ attributes: Record<string, unknown>;
246
+ }
247
+ ```
176
248
 
177
- | 字段 | 类型 | 默认 | 说明 |
178
- | --- | --- | --- | --- |
179
- | `trackingConsent` | `'granted' \| 'not-granted'` | `granted` | 初始化授权状态 |
180
- | `redactUrl` | `(url) => string` | 删除 username/password/query/hash | SDK URL 属性脱敏 |
181
- | `redactAttributes` | `(attributes) => Attributes` | 原样返回 | Resource、SDK 创建的 Span 和内置插件属性脱敏 |
249
+ 该 Hook 在 Span 结束、导出过滤阶段执行:
182
250
 
183
- 第三方 instrumentation 的自有 Span 属性不保证经过 `redactAttributes`;需要在 `tracking.beforeSend` 做最终兜底。详见[隐私与公共属性](./privacy.md)。
251
+ - 返回 `false` 丢弃;返回 `void` 保留;
252
+ - 可原地修改 `event.attributes`;
253
+ - 只暴露名字和属性,不暴露 Span event、时间、status、trace ID;
254
+ - 在 Span 已完成常规脱敏之后执行;该 Hook 新增或改写的字段不会再自动经过 `redactAttributes`;
255
+ - Hook 抛异常时 SDK 保留 Span 并继续发送,`debug: true` 时打印 warning;业务仍应在 Hook 内避免可能失败或高开销的逻辑。
184
256
 
185
257
  ## extensions
186
258
 
187
- | 字段 | 类型 | 默认 | 说明 |
259
+ | 配置 | 类型 | 默认 | 说明 |
188
260
  | --- | --- | --- | --- |
189
- | `plugins` | `BkOTPlugin[]` | `[]` | 在内置插件后初始化 |
190
- | `instrumentations` | `Instrumentation[]` | `[]` | 插件完成后注册到 SDK provider |
261
+ | `plugins` | `BkOTPlugin[]` | `[]` | 使用 SDK RUM runtime context 的插件 |
262
+ | `instrumentations` | `Instrumentation[]` | `[]` | 绑定 SDK 私有 WebTracerProvider 的 OTel instrumentation |
191
263
 
192
- 也可以在 `start()` 前调用 `use()` / `useInstrumentation()`。需要动态注册时设置 `autoStart: false`;启动后调用会抛错。
264
+ 两类扩展都在 SDK 获得 Consent 后启动,并在撤销 Consent/shutdown 时关闭。使用方法见[插件与 OpenTelemetry 扩展](./extensions.md)。
193
265
 
194
- ## 完整配置骨架
266
+ ## 可复制的生产模板
195
267
 
196
268
  ```ts
197
- new BkOpenTelemetry({
198
- application: { name: 'demo', environment: 'production', version: '1.0.0' },
269
+ const rum = new BkOpenTelemetry({
270
+ application: {
271
+ name: 'order-center',
272
+ environment: 'production',
273
+ version: '2026.07.21',
274
+ },
199
275
  transport: {
200
- endpoint: 'https://collector.example.com',
201
- token: 'token',
202
- headers: {},
276
+ endpoint: 'https://rum.example.com',
277
+ token: 'limited-browser-write-token',
203
278
  processor: 'batch',
204
- batch: {},
205
279
  },
206
- session: { sampleRate: 1 },
280
+ session: { sampleRate: 0.2 },
207
281
  tracking: {
208
- view: { tracking: 'auto', loadingTime: true },
209
- request: true,
210
- resource: true,
211
- error: true,
212
- webVitals: true,
213
- blankScreen: true,
214
- device: true,
215
- action: true,
216
- longTask: false,
217
- websocket: false,
218
- cspViolation: false,
282
+ view: {
283
+ getPathGroup(url) {
284
+ return new URL(url, location.href).pathname.replace(/\/orders\/[^/]+/, '/orders/:id');
285
+ },
286
+ excludedActivityUrls: [/\/events\/stream(?:\?|$)/],
287
+ },
288
+ request: {
289
+ excludedUrls: [/\/health(?:\?|$)/],
290
+ allowedTracingUrls: [url => new URL(url, location.href).origin === 'https://api.example.com'],
291
+ },
292
+ blankScreen: { rootSelector: '#app' },
293
+ },
294
+ privacy: {
295
+ redactAttributes(attributes) {
296
+ const next = { ...attributes };
297
+ delete next['biz.secret'];
298
+ return next;
299
+ },
219
300
  },
220
- context: { attributes: {} },
221
- privacy: { trackingConsent: 'granted' },
222
- extensions: { plugins: [], instrumentations: [] },
223
- enabled: true,
224
- autoStart: true,
225
- debug: false,
226
301
  });
227
302
  ```