@blueking/open-telemetry 0.0.13 → 0.0.15

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 (46) hide show
  1. package/README.md +170 -554
  2. package/dist/bk-rum.global.js +9 -9
  3. package/dist/bk-rum.global.js.map +1 -1
  4. package/dist/core/action-activity.d.ts +28 -0
  5. package/dist/core/config.d.ts +113 -93
  6. package/dist/core/page-activity.d.ts +14 -0
  7. package/dist/core/plugin.d.ts +23 -6
  8. package/dist/core/processor.d.ts +25 -4
  9. package/dist/core/request-timing.d.ts +5 -0
  10. package/dist/core/runtime-context.d.ts +51 -3
  11. package/dist/core/sampling.d.ts +5 -1
  12. package/dist/core/sdk.d.ts +29 -10
  13. package/dist/core/url.d.ts +4 -1
  14. package/dist/index.d.ts +12 -14
  15. package/dist/index.js +2184 -1175
  16. package/dist/index.js.map +1 -1
  17. package/dist/plugins/action.d.ts +6 -2
  18. package/dist/plugins/activity.d.ts +6 -0
  19. package/dist/plugins/blank-screen.d.ts +17 -0
  20. package/dist/plugins/common.d.ts +11 -3
  21. package/dist/plugins/device.d.ts +2 -0
  22. package/dist/plugins/long-task.d.ts +3 -12
  23. package/dist/plugins/page-view.d.ts +1 -0
  24. package/dist/plugins/request-body.d.ts +14 -0
  25. package/dist/plugins/resource.d.ts +17 -1
  26. package/dist/plugins/session-state.d.ts +18 -0
  27. package/dist/plugins/session.d.ts +4 -4
  28. package/dist/plugins/web-vitals.d.ts +74 -0
  29. package/dist/schema/attributes.d.ts +31 -10
  30. package/dist/schema/span.d.ts +4 -12
  31. package/package.json +3 -7
  32. package/skills/bk-rum-otele/SKILL.md +34 -33
  33. package/skills/bk-rum-otele/evals/evals.json +1 -1
  34. package/skills/bk-rum-otele/references/api.md +45 -13
  35. package/skills/bk-rum-otele/references/concepts.md +38 -112
  36. package/skills/bk-rum-otele/references/configuration.md +135 -228
  37. package/skills/bk-rum-otele/references/custom-plugin.md +8 -5
  38. package/skills/bk-rum-otele/references/data-model.md +13 -8
  39. package/skills/bk-rum-otele/references/framework-integration.md +25 -17
  40. package/skills/bk-rum-otele/references/getting-started.md +27 -87
  41. package/skills/bk-rum-otele/references/plugins.md +165 -99
  42. package/skills/bk-rum-otele/references/privacy.md +42 -33
  43. package/skills/bk-rum-otele/references/telemetry-reference.md +73 -44
  44. package/skills/bk-rum-otele/references/troubleshooting.md +72 -38
  45. package/dist/plugins/http-body.d.ts +0 -3
  46. package/dist/plugins/route-timing.d.ts +0 -2
@@ -2,317 +2,224 @@
2
2
 
3
3
  # 配置总览
4
4
 
5
- SDK 的配置入口是 `BkOTOptions`。推荐先完成 `app`、`transport`、`sampling`、`route`、`privacy` 五类基础配置,再按需打开插件。
6
-
7
- ## 完整配置示例
5
+ SDK 入口按职责分为 `application`、`transport`、`session`、`tracking`、`context`、`privacy`、`extensions`。`application.name` 与 `transport` 必填;根 `transport.endpoint` 可由所有已开启信号的独立 endpoint 取代。
8
6
 
9
7
  ```ts
10
- import { BkOpenTelemetry } from '@blueking/open-telemetry';
11
-
12
8
  const bkOT = new BkOpenTelemetry({
13
- enabled: true,
14
- autoStart: true,
15
- debug: false,
16
- app: {
9
+ application: {
17
10
  name: 'bk-monitor-web',
18
11
  environment: 'production',
19
12
  version: '1.4.2',
20
13
  },
21
- user: {
22
- id: 'user-12345',
23
- },
24
14
  transport: {
25
15
  endpoint: 'https://rum-collector.example.com',
26
16
  token: 'report-token',
27
- headers: {
28
- 'x-bk-tenant-id': 'system',
17
+ signals: {
18
+ traces: { processor: 'batch', batch: { maxQueueSize: 2048 } },
19
+ metrics: { intervalMillis: 60_000 },
29
20
  },
30
21
  },
31
- sampling: {
32
- rate: 0.2,
22
+ session: {
23
+ sampleRate: 0.2,
33
24
  },
34
- route: {
35
- getPathGroup: url => new URL(url).pathname.replace(/\/\d+/g, '/:id'),
25
+ tracking: {
26
+ view: {
27
+ tracking: 'auto',
28
+ getPathGroup: url => new URL(url, location.href).pathname.replace(/\/\d+/g, '/:id'),
29
+ },
30
+ request: {
31
+ allowedTracingUrls: ['https://api.example.com'],
32
+ excludedUrls: [/\/health$/],
33
+ },
34
+ longTask: false,
35
+ websocket: false,
36
+ },
37
+ context: {
38
+ user: { id: 'user-12345' },
39
+ attributes: {
40
+ page: () => ({ 'biz.space_id': window.__BK_SPACE_ID__ }),
41
+ },
36
42
  },
37
43
  privacy: {
38
- redactUrl: url => url.replace(/([?&](token|bk_ticket)=)[^&]+/g, '$1***'),
44
+ trackingConsent: 'granted',
39
45
  redactAttributes: attributes => attributes,
40
46
  },
41
- attributes: {
42
- page: () => ({
43
- 'biz.space_id': window.__BK_SPACE_ID__,
44
- }),
45
- custom: () => ({
46
- 'biz.app_code': 'bk-monitor',
47
- }),
48
- },
49
- instrumentations: {
50
- documentLoad: true,
51
- fetch: true,
52
- xhr: true,
53
- userInteraction: true,
54
- },
55
- plugins: {
56
- pageView: true,
57
- resource: true,
58
- error: true,
59
- webVitals: true,
60
- blankScreen: {
61
- rootSelector: '#app',
62
- checkDelay: 3000,
63
- threshold: 0.8,
64
- },
65
- session: true,
66
- device: true,
67
- action: false,
68
- httpBody: false,
69
- longTask: false,
70
- routeTiming: false,
71
- cspViolation: false,
72
- websocket: false,
47
+ extensions: {
48
+ plugins: [],
49
+ instrumentations: [],
73
50
  },
74
51
  });
75
-
76
- export default bkOT;
77
52
  ```
78
53
 
79
54
  ## 顶层配置
80
55
 
81
56
  | 配置 | 默认值 | 说明 |
82
57
  | --- | --- | --- |
83
- | `enabled` | `true` | 为 `false` 时不注册 provider,也不启动插件。 |
84
- | `autoStart` | `true` | 构造后自动调用 `start()`。需要注册自定义插件时建议设为 `false`。 |
85
- | `debug` | `false` | 打印 exporter 输出和部分调试日志。 |
86
- | `app.name` | `unknown_service` | 写入 `service.name`。 |
87
- | `app.environment` | `production` | 写入 `deployment.environment.name`。 |
88
- | `app.version` | `undefined` | 写入 `service.version`。 |
89
- | `spanProcessor` | `batch` | 可选 `batch` 或 `simple`。本地调试可用 `simple`。 |
90
-
91
- ### 固定行为(当前不可配置)
92
-
93
- - **Metric 上报间隔固定 60s**(`PeriodicExportingMetricReader`),暂不开放配置。
94
- - SDK 会向 resource 注入固定属性:`rum.provider=blueking`、`telemetry.sdk.language=webjs`,以及由 `app` 派生的 `service.name`、`service.version`、`deployment.environment.name`。
95
-
96
- 示例:只在生产环境开启。
97
-
98
- ```ts
99
- new BkOpenTelemetry({
100
- enabled: import.meta.env.PROD,
101
- app: {
102
- name: 'bk-monitor-web',
103
- environment: import.meta.env.MODE,
104
- },
105
- transport: {
106
- endpoint: 'https://rum-collector.example.com',
107
- },
108
- });
109
- ```
110
-
111
- ## Instrumentation 配置
112
-
113
- | 配置 | 默认值 | 说明 |
58
+ | `application.name` | 无 | 必填,写入 `service.name` |
59
+ | `application.environment` | `production` | 写入 `deployment.environment.name` |
60
+ | `application.version` | 无 | 写入 `service.version` |
61
+ | `transport.endpoint` | 无 | 上报根地址;已开启信号没有独立 endpoint 时必填 |
62
+ | `transport.signals.traces` | `{}` | Trace 配置;设为 `false` 关闭 |
63
+ | `transport.signals.metrics` | `{}` | Metric 配置;设为 `false` 关闭 |
64
+ | `transport.signals.logs` | `{}` | Log 配置;设为 `false` 关闭 |
65
+ | `session.sampleRate` | `1` | Session 级确定性采样率 |
66
+ | `tracking.view.tracking` | `auto` | 自动或手动 View |
67
+ | `tracking.view.loadingTime` | `true` | 自动计算 View loading time |
68
+ | `privacy.trackingConsent` | `granted` | 初始化授权状态 |
69
+ | `enabled` | `true` | 是否启用 SDK |
70
+ | `autoStart` | `true` | 构造后自动启动 |
71
+ | `debug` | `false` | 输出 exporter 与插件调试信息 |
72
+
73
+ Session 固定为 15 分钟无活动过期、4 小时最长生命周期。View 和 Session 是 RUM 关联边界,不能作为普通插件关闭。
74
+
75
+ ## 内置采集开关
76
+
77
+ 内置能力全部位于 `tracking`:
78
+
79
+ | 配置 | 默认 | 说明 |
114
80
  | --- | --- | --- |
115
- | `documentLoad` | `true` | 采集文档加载和导航性能 span。 |
116
- | `fetch` | `true` | patch `fetch`,采集 HTTP span,并支持同源 traceparent。 |
117
- | `xhr` | `true` | patch `XMLHttpRequest`,采集 HTTP span。 |
118
- | `userInteraction` | `true` | 使用 OpenTelemetry 官方用户交互 instrumentation。 |
119
- | `propagateTraceHeaderUrls` | `undefined`(仅同源) | 把跨域后台 API 纳入 `traceparent`(含 OpenTelemetry traceId)注入范围。 |
120
-
121
- `propagateTraceHeaderUrls` 用于让 fetch/xhr 请求在调用后台 API 时带上 W3C `traceparent` 请求头,实现前后端 trace 串联。同源请求始终注入,该配置只用于在此基础上扩展跨域目标,取值支持:
122
-
123
- - `'all'`:所有请求都注入(含跨域,使用前确认隐私/CORS 影响)。
124
- - `string`:URL 包含该子串即注入,例如 `'api.example.com'`。
125
- - `RegExp`:URL 匹配该正则即注入。
126
- - 上述字符串/正则组成的数组:命中任意一个即注入。
127
- - `(url: string) => boolean`:完全自定义判断。
128
-
129
- ```ts
130
- new BkOpenTelemetry({
131
- instrumentations: {
132
- // 给所有调用 api.example.com / api.internal.com 的后台请求注入 traceparent
133
- propagateTraceHeaderUrls: [/https:\/\/api\.example\.com/, 'api.internal.com'],
134
- },
135
- });
136
- ```
137
-
138
- > 注意:向跨域服务注入自定义头会触发 CORS 预检,目标服务需在 `Access-Control-Allow-Headers` 中放行 `traceparent`(以及按需的 `tracestate`),否则请求会被浏览器拦截。
139
-
140
- 示例:关闭接口自动采集。
81
+ | `view` | 开 | 初始加载、SPA、BFCache、手动 View |
82
+ | `request` | 开 | Fetch/XHR 请求 span、Trace Header、body |
83
+ | `resource` | 开 | 静态资源 span;PerformanceObserver 始终为 View 活动和请求 timing 服务 |
84
+ | `error` | 开 | JS、Promise 与资源错误 |
85
+ | `webVitals` | 开 | FCP/LCP/TTFB 及 View 级 CLS/INP |
86
+ | `blankScreen` | 开 | 白屏检测 |
87
+ | `device` | 开 | 浏览器、OS、屏幕与网络信息 |
88
+ | `action` | 开 | 点击 Action 与活动聚合 |
89
+ | `longTask` | 关 | LoAF/Long Task |
90
+ | `websocket` | 关 | WebSocket 指标 |
91
+ | `cspViolation` | 关 | CSP 违规 |
141
92
 
142
93
  ```ts
143
94
  new BkOpenTelemetry({
144
- instrumentations: {
145
- fetch: false,
146
- xhr: false,
95
+ application: { name: 'demo' },
96
+ transport: { endpoint: 'https://rum-collector.example.com' },
97
+ tracking: {
98
+ request: false,
99
+ blankScreen: false,
100
+ longTask: { threshold: 80 },
147
101
  },
148
102
  });
149
103
  ```
150
104
 
151
- ## 插件默认开关
105
+ ## 请求配置
152
106
 
153
- | 插件 | 默认状态 | 说明 |
154
- | --- | --- | --- |
155
- | `pageView` | 开 | 初始页面和 SPA 路由 view。 |
156
- | `resource` | 开 | 静态资源 PerformanceObserver。 |
157
- | `error` | 开 | JS error、unhandledrejection、资源错误。 |
158
- | `webVitals` | 开 | CLS、FCP、INP、LCP、TTFB。 |
159
- | `blankScreen` | 开 | 延迟检测页面空白比例。 |
160
- | `device` | 开 | 设备、浏览器、OS、viewport、网络属性。 |
161
- | `session` | 开 | session.id 生成、轮换和跨 tab 同步。 |
162
- | `action` | 关 | 自动采集 click、submit、keydown 等动作。 |
163
- | `httpBody` | 关 | 错误路径采集请求/响应 body。 |
164
- | `longTask` | 关 | Long Task API。 |
165
- | `routeTiming` | 关 | SPA 路由切换渲染耗时估算。 |
166
- | `cspViolation` | 关 | CSP 违规 error span。 |
167
- | `websocket` | 关 | WebSocket 连接、消息、错误、关闭。 |
168
-
169
- 示例:开启高成本插件。
107
+ `tracking.request.allowedTracingUrls` 是严格白名单,同源请求也必须命中后才注入 `traceparent`。关闭 Trace 时不会注入。`excludedUrls` 命中后完全不采集。SDK 自身已开启 signal endpoint 始终排除。
170
108
 
171
109
  ```ts
172
110
  new BkOpenTelemetry({
173
- plugins: {
174
- longTask: {
175
- threshold: 80,
176
- },
177
- httpBody: {
178
- maxBodySize: 4096,
179
- redact: ({ body, type }) => (type === 'request' ? '[redacted request body]' : body),
111
+ application: { name: 'request-demo' },
112
+ transport: { endpoint: 'https://rum-collector.example.com' },
113
+ tracking: {
114
+ request: {
115
+ allowedTracingUrls: [/https:\/\/api\.example\.com/],
116
+ excludedUrls: [/\/health(?:\?|$)/],
117
+ beforeSend: (event, context) => {
118
+ event.attributes['request.transport'] = context.type;
119
+ return true;
120
+ },
121
+ body: {
122
+ capture: 'errors',
123
+ redact: ({ body }) => body.replace(/token=[^&]+/g, 'token=***'),
124
+ },
180
125
  },
181
- websocket: true,
182
126
  },
183
127
  });
184
128
  ```
185
129
 
186
- # 上报与采样
130
+ 跨域注入 Trace Header 可能触发 CORS 预检,服务端需要放行 `traceparent`。
187
131
 
188
- SDK 使用 OTLP HTTP exporter 分别上报 traces、metrics、logs。默认 endpoint 是 `http://localhost:4318`,会自动拼接 `/rum`、`/metrics`、`/logs`。
132
+ ## 全局 beforeSend
189
133
 
190
- ## 统一 endpoint
134
+ `tracking.beforeSend` 在 span/log 进入 exporter 前执行,可修改属性或返回 `false` 丢弃事件。内部 `bk.rum.sampled` 标记会在此之前移除。
191
135
 
192
136
  ```ts
193
- new BkOpenTelemetry({
194
- transport: {
195
- endpoint: 'https://rum-collector.example.com/v1',
196
- token: 'report-token',
137
+ tracking: {
138
+ beforeSend(event) {
139
+ delete event.attributes['debug.payload'];
140
+ return event.name !== 'custom.ignore-me';
197
141
  },
198
- });
142
+ }
199
143
  ```
200
144
 
201
- 实际使用的上报地址为:
145
+ ## 不再支持的旧入口
146
+
147
+ `app`、`sampling`、`route`、顶层 `view`、`plugins`、`instrumentations`、`attributes`、`spanProcessor`、`spanBatch` 已删除,不做兼容映射。
202
148
 
203
- - traces:`https://rum-collector.example.com/v1/rum`
204
- - metrics:`https://rum-collector.example.com/v1/metrics`
205
- - logs:`https://rum-collector.example.com/v1/logs`
149
+ # 传输与采样
206
150
 
207
- ## 分信号 endpoint
151
+ ## Endpoint
208
152
 
209
- 如果 traces、metrics、logs 由不同网关接收,可以使用 `transport.signals`。
153
+ `transport.endpoint` 是可选根地址。未配置独立地址的已开启信号会基于它生成 `/v1/rum`、`/v1/metrics`、`/v1/logs`;单信号 `endpoint` 是完整 URL,不再拼接路径。
210
154
 
211
155
  ```ts
212
156
  new BkOpenTelemetry({
157
+ application: { name: 'demo' },
213
158
  transport: {
214
- endpoint: 'https://rum-collector.example.com',
215
- headers: {
216
- 'x-bk-app-code': 'bk-monitor',
217
- },
159
+ endpoint: 'https://collector.example.com',
160
+ token: 'report-token',
218
161
  signals: {
219
- endpoints: {
220
- traces: 'https://trace-gateway.example.com/v1/rum',
221
- metrics: 'https://metric-gateway.example.com/v1/metrics',
222
- logs: 'https://log-gateway.example.com/v1/logs',
223
- },
224
162
  traces: {
225
- timeoutMillis: 5000,
226
- concurrencyLimit: 4,
227
- },
228
- logs: {
229
- headers: {
230
- 'x-bk-log-source': 'browser',
163
+ endpoint: 'https://trace.example.com/v1/rum',
164
+ processor: 'batch',
165
+ batch: {
166
+ maxQueueSize: 2048,
167
+ maxExportBatchSize: 512,
168
+ scheduledDelayMillis: 5000,
169
+ exportTimeoutMillis: 30000,
231
170
  },
232
171
  },
172
+ metrics: { endpoint: 'https://metric.example.com/v1/metrics', intervalMillis: 60_000 },
173
+ logs: { endpoint: 'https://log.example.com/v1/logs' },
233
174
  },
234
175
  },
235
176
  });
236
177
  ```
237
178
 
238
- `transport.token` 会生成默认 `Authorization: Bearer <token>`,显式配置的 `headers` 会继续合并进去。
239
-
240
- ## 避免自循环采集
241
-
242
- SDK 内置 `FilteringSpanProcessor`,会过滤指向自身 OTLP endpoint 的 span,避免上报请求本身再次被 fetch/xhr instrumentation 采集。
179
+ ## 独立信号开关
243
180
 
244
- 示例:自定义 Collector 地址时无需手动忽略自身上报。
181
+ 三类信号默认全部开启。设为 `false` 时,不创建对应 exporter、processor 或 metric reader:
245
182
 
246
183
  ```ts
247
184
  new BkOpenTelemetry({
185
+ application: { name: 'trace-only-demo' },
248
186
  transport: {
249
- endpoint: 'https://rum-collector.example.com/otlp',
250
- },
251
- instrumentations: {
252
- fetch: true,
253
- xhr: true,
187
+ signals: {
188
+ traces: { endpoint: 'https://trace.example.com/v1/rum' },
189
+ metrics: false,
190
+ logs: false,
191
+ },
254
192
  },
255
193
  });
256
194
  ```
257
195
 
258
- ## 同源 traceparent 传播
259
-
260
- fetch/xhr instrumentation 会给符合条件的同源请求注入 W3C `traceparent`,用于打通前后端 trace。跨域请求是否能传播还取决于服务端 CORS 是否允许该 header。
261
-
262
- 示例:前端请求同源 API。
263
-
264
- ```ts
265
- await fetch('/api/orders/202605270001', {
266
- method: 'GET',
267
- });
268
- ```
269
-
270
- ## 会话级 sticky 采样
196
+ 校验规则:
271
197
 
272
- `sampling.rate` 范围是 `0` 到 `1`。SDK 会将采样结果保存在 session 维度,保证同一会话内行为一致。
198
+ - 至少开启一种信号,三类不能全部为 `false`。
199
+ - 每个开启信号必须有独立 endpoint,或能回退到根 `transport.endpoint`。
200
+ - 关闭 Trace 后不记录 Span,也不会向 `allowedTracingUrls` 注入 `traceparent`。
201
+ - 关闭 Metric 后不启动周期导出定时器。
202
+ - 关闭 Log 不影响 Session/View 上下文维护,但不会产生 Session、View Snapshot、HTTP body 等 Log。
273
203
 
274
- ```ts
275
- new BkOpenTelemetry({
276
- sampling: {
277
- rate: 0.05,
278
- },
279
- transport: {
280
- endpoint: 'https://rum-collector.example.com',
281
- },
282
- });
283
- ```
284
-
285
- 需要注意:
204
+ 只开启 Metric 时没有精确 `view.id` 下钻链路;需要从指标定位具体 View 时,至少开启 Trace。
286
205
 
287
- - `rate: 1` 表示全量上报。
288
- - `rate: 0` 表示 RUM 插件和手动事件都不上报。
289
- - 未命中采样时,主动产生数据的 RUM 插件不会启动。
290
- - 调试时建议临时设置 `rate: 1` 和 `debug: true`。
206
+ 请求头优先级:`token` 生成的 Authorization < `transport.headers` < 单 signal `headers`。
291
207
 
292
- ## Batch 与 Simple Processor
208
+ 浏览器 exporter 使用 `fetch keepalive`,SDK 在 `pagehide` 和页面进入 hidden 时触发 flush。`shutdown()` 会先结束 View 和插件,再 flush final Snapshot,最后关闭 provider。
293
209
 
294
- 默认使用 batch 处理器,适合生产环境。调试时可以用 simple 处理器减少等待。
210
+ ## Session 级确定性采样
295
211
 
296
212
  ```ts
297
213
  new BkOpenTelemetry({
298
- debug: true,
299
- spanProcessor: 'simple',
300
- transport: {
301
- endpoint: 'http://localhost:4318',
302
- },
214
+ application: { name: 'demo' },
215
+ transport: { endpoint: 'https://collector.example.com' },
216
+ session: { sampleRate: 0.05 },
303
217
  });
304
218
  ```
305
219
 
306
- 批量参数可以通过 `spanBatch` 调整。
220
+ - `1` 表示全量,`0` 表示全部丢弃。
221
+ - 决定由 `application.name + session.id` 的稳定 hash 得出。
222
+ - 不写额外 sessionStorage;同一 Session 内 span/log/metric 决定一致。
223
+ - Session 续期后使用新 ID 重新计算。
307
224
 
308
- ```ts
309
- new BkOpenTelemetry({
310
- spanProcessor: 'batch',
311
- spanBatch: {
312
- maxQueueSize: 2048,
313
- maxExportBatchSize: 256,
314
- scheduledDelayMillis: 5000,
315
- exportTimeoutMillis: 30000,
316
- },
317
- });
318
- ```
225
+ Metric exporter 只接受带内部采样标记的数据点,并在传输前移除该标记。Metric 只保留低基数 `view.url_path_group`,精确 View 关联由 span/log/View Snapshot 的 `view.id` 提供。
@@ -38,6 +38,7 @@ import { BkOpenTelemetry } from '@blueking/open-telemetry';
38
38
  import { bizPlugin } from './biz-plugin';
39
39
 
40
40
  const bkOT = new BkOpenTelemetry({
41
+ application: { name: 'plugin-demo' },
41
42
  autoStart: false,
42
43
  transport: {
43
44
  endpoint: 'https://rum-collector.example.com',
@@ -125,18 +126,19 @@ const createResizePlugin = () => {
125
126
 
126
127
  ## 注册额外 instrumentation
127
128
 
128
- 第三方 OpenTelemetry instrumentation 属于 instrumentation 而非 bk-ot 插件,因此挂在顶层 `instrumentations.custom` 下,或通过 `useInstrumentation()` 链式注册。两者区别详见 [Plugin 与 Instrumentation 的区别](./concepts.md)。
129
+ 第三方 OpenTelemetry instrumentation 放在 `extensions.instrumentations`,或通过 `useInstrumentation()` 链式注册。两者区别详见 [内置采集与扩展](./concepts.md)。
129
130
 
130
131
  配置式:
131
132
 
132
133
  ```ts
133
134
  const bkOT = new BkOpenTelemetry({
134
- instrumentations: {
135
- custom: [new SomeWebInstrumentation()],
136
- },
135
+ application: { name: 'extension-demo' },
137
136
  transport: {
138
137
  endpoint: 'https://rum-collector.example.com',
139
138
  },
139
+ extensions: {
140
+ instrumentations: [new SomeWebInstrumentation()],
141
+ },
140
142
  });
141
143
  ```
142
144
 
@@ -144,6 +146,7 @@ const bkOT = new BkOpenTelemetry({
144
146
 
145
147
  ```ts
146
148
  const bkOT = new BkOpenTelemetry({
149
+ application: { name: 'extension-demo' },
147
150
  autoStart: false,
148
151
  transport: {
149
152
  endpoint: 'https://rum-collector.example.com',
@@ -155,4 +158,4 @@ bkOT.useInstrumentation(new SomeWebInstrumentation());
155
158
  await bkOT.start();
156
159
  ```
157
160
 
158
- > 与受采样影响的 RUM 插件不同,instrumentation 只要 SDK 启用(`enabled !== false`)就会注册,不受 `sampling.rate` 影响。
161
+ > Extension instrumentation 与采集生命周期一致:未授权或 SDK 停止时不会注册;授权后注册。它产生的 RUM 数据仍受 Session sampler 和导出过滤约束。
@@ -15,17 +15,16 @@ SDK 的数据模型遵循“浏览器 SDK 负责低成本原始数据和单事
15
15
 
16
16
  | 类型 | 说明 | 常见来源 |
17
17
  | --- | --- | --- |
18
- | `document` | 文档加载 | documentLoad instrumentation、pageView 插件(初始加载) |
19
- | `http` | 接口请求 | fetch/xhr instrumentation、httpBody 插件 |
20
- | `resource` | 静态资源 | resource 插件、documentLoad instrumentation |
18
+ | `document` | 文档加载 | View 管理器(初始加载) |
19
+ | `resource` | Fetch/XHR 请求与静态资源 | request、resource 采集 |
21
20
  | `vital` | Web Vitals | webVitals 插件 |
22
21
  | `error` | 错误 | error 插件、blankScreen 插件、cspViolation 插件、自定义事件错误 |
23
22
  | `longtask` | 长任务 | longTask 插件 |
24
23
  | `action` | 用户动作 | action 插件、reportAction |
25
- | `route` | 路由切换 | pageView 插件(SPA 路由)、routeTiming 插件 |
24
+ | `route` | 路由切换 | View 管理器(SPA/手动 View) |
26
25
  | `custom` | 自定义事件 | reportCustomEvent、自定义插件、websocket 插件 |
27
26
 
28
- > 每种 span 的具体 span 名(如 `browser.web_vital`、`browser.route_change`)、metric 名、log 事件名和完整属性字典见[遥测参考](./telemetry-reference.md)。
27
+ > 每种 span 的具体 span 名、metric 名、log 事件名和完整属性字典见[遥测参考](./telemetry-reference.md)。
29
28
 
30
29
  示例:自定义业务事件归一为 `custom`。
31
30
 
@@ -55,7 +54,7 @@ span.end();
55
54
  - `timeout`
56
55
  - `abort`
57
56
 
58
- HTTP span 会根据状态码推导结果:`408`、`504` 为 timeout,`>= 400` 为 error,其余为 success。
57
+ 请求 Resource Span 会根据状态码推导结果:`408`、`504` 为 timeout,`>= 400` 为 error,其余为 success。
59
58
 
60
59
  ```ts
61
60
  bkOT.reportCustomEvent({
@@ -89,8 +88,8 @@ console.log(getDurationBucket(1200)); // 500ms~2s
89
88
 
90
89
  - 应用与环境:`service.name`、`deployment.environment.name`、`service.version`
91
90
  - 用户与会话:`user.id`、`session.id`
92
- - View:`view.id`、`view.url`、`view.url_path_group`、`view.loading_type`
93
- - 终端信息:`device_type`、`browser_name`、`browser_version`、`os_name`、`network.effective_type`
91
+ - View:`view.id`、`view.name`、`view.url`、`view.url_path_group`、`view.loading_type`
92
+ - 终端信息:`device.type`、`browser.name`、`browser.version`、`os.name`、`network.effective_type`
94
93
  - HTTP / Resource:`http.request.method`、`http.response.status_code`、`url.full`、`transfer_size`
95
94
  - Web Vitals:`vital.metric`、`vital.value`、`vital.rating`
96
95
  - Error:`exception.type`、`exception.message`、`exception.stacktrace`、`exception.fingerprint`
@@ -120,3 +119,9 @@ bkOT.reportCustomEvent({
120
119
  ```
121
120
 
122
121
  上例中 `biz.order_id` 适合放在 span/log 中定位单次问题,不适合作为 metric 维度做长期聚合。
122
+
123
+ ## Metric 与 View 的关联
124
+
125
+ Metric 不携带唯一的 `view.id`、`session.id` 或 `action.id`,只保留 `view.url_path_group`、`view.loading_type` 等低基数维度。这样可以按页面类型聚合,又不会因为每次访问生成新的时序而造成基数爆炸。
126
+
127
+ 需要定位某一次 View 时,使用同一指标对应的 span(例如 `browser.web_vital`)或 View Snapshot Log;它们携带精确的 `session.id` 和 `view.id`。后端需要从聚合指标跳转到样本时,应通过 exemplar 或 span/log 派生聚合建立关联,而不是把 `view.id` 放进 metric labels。
@@ -12,14 +12,17 @@ import { BkOpenTelemetry } from '@blueking/open-telemetry';
12
12
 
13
13
  export const bkOT = new BkOpenTelemetry({
14
14
  autoStart: false,
15
- app: {
15
+ application: {
16
16
  name: 'vue-rum-demo',
17
17
  environment: import.meta.env.MODE,
18
18
  },
19
19
  transport: {
20
20
  endpoint: 'https://rum-collector.example.com',
21
21
  },
22
- plugins: {
22
+ tracking: {
23
+ view: {
24
+ tracking: 'manual',
25
+ },
23
26
  blankScreen: {
24
27
  rootSelector: '#app',
25
28
  },
@@ -35,10 +38,9 @@ import App from './App.vue';
35
38
  import router from './router';
36
39
 
37
40
  router.afterEach(to => {
38
- bkOT.setView({
41
+ bkOT.startView({
42
+ name: to.matched.at(-1)?.path || to.path,
39
43
  url: window.location.href,
40
- urlPathGroup: to.matched.at(-1)?.path || to.path,
41
- loadingType: 'route_change',
42
44
  });
43
45
  });
44
46
 
@@ -49,7 +51,7 @@ createApp(App).use(router).mount('#app');
49
51
 
50
52
  ## Vue Router 路由模板
51
53
 
52
- 如果不想在 `afterEach` 中主动 `setView()`,可以通过 `route.getPathGroup` 统一处理。
54
+ 自动模式不需要 `afterEach`,通过 `tracking.view.getPathGroup` 统一生成低基数路径。
53
55
 
54
56
  ```ts
55
57
  const routeGroups = [
@@ -58,10 +60,14 @@ const routeGroups = [
58
60
  ];
59
61
 
60
62
  new BkOpenTelemetry({
61
- route: {
62
- getPathGroup: url => {
63
- const pathname = new URL(url, window.location.href).pathname;
64
- return routeGroups.find(route => route.pattern.test(pathname))?.group || pathname.replace(/\/\d+/g, '/:id');
63
+ application: { name: 'vue-rum-demo' },
64
+ transport: { endpoint: 'https://rum-collector.example.com' },
65
+ tracking: {
66
+ view: {
67
+ getPathGroup: url => {
68
+ const pathname = new URL(url, window.location.href).pathname;
69
+ return routeGroups.find(route => route.pattern.test(pathname))?.group || pathname.replace(/\/\d+/g, '/:id');
70
+ },
65
71
  },
66
72
  },
67
73
  });
@@ -75,9 +81,10 @@ import { useEffect } from 'react';
75
81
  import { useLocation, useMatches } from 'react-router-dom';
76
82
 
77
83
  export const bkOT = new BkOpenTelemetry({
78
- app: { name: 'react-rum-demo' },
84
+ application: { name: 'react-rum-demo' },
79
85
  transport: { endpoint: 'https://rum-collector.example.com' },
80
- plugins: {
86
+ tracking: {
87
+ view: { tracking: 'manual' },
81
88
  blankScreen: {
82
89
  rootSelector: '#root',
83
90
  },
@@ -89,10 +96,9 @@ export const RumRouteSync = () => {
89
96
  const matches = useMatches();
90
97
 
91
98
  useEffect(() => {
92
- bkOT.setView({
99
+ bkOT.startView({
100
+ name: matches.at(-1)?.pathname || location.pathname,
93
101
  url: window.location.href,
94
- urlPathGroup: matches.at(-1)?.pathname || location.pathname,
95
- loadingType: 'route_change',
96
102
  });
97
103
  }, [location.pathname, matches]);
98
104
 
@@ -127,14 +133,16 @@ export const onLogout = () => {
127
133
  import { BkOpenTelemetry } from '@blueking/open-telemetry';
128
134
 
129
135
  const bkOT = new BkOpenTelemetry({
130
- app: { name: 'micro-frontend-host' },
136
+ application: { name: 'micro-frontend-host' },
131
137
  transport: { endpoint: 'https://rum-collector.example.com' },
132
138
  });
133
139
 
134
140
  window.__BK_RUM__ = {
135
141
  reportAction: bkOT.reportAction,
136
142
  reportCustomEvent: bkOT.reportCustomEvent,
137
- setView: bkOT.setView,
143
+ setViewName: bkOT.setViewName,
144
+ setViewLoadingTime: bkOT.setViewLoadingTime,
145
+ startView: bkOT.startView,
138
146
  };
139
147
  ```
140
148