@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
@@ -10,7 +10,7 @@
10
10
  import { BkOpenTelemetry } from '@blueking/open-telemetry';
11
11
 
12
12
  const bkOT = new BkOpenTelemetry({
13
- app: { name: 'bk-rum-demo' },
13
+ application: { name: 'bk-rum-demo' },
14
14
  transport: { endpoint: 'https://rum-collector.example.com' },
15
15
  });
16
16
  ```
@@ -27,7 +27,10 @@ const bkOT = new BkOpenTelemetry({
27
27
  | `use(plugin)` | `(plugin) => this` | 注册自定义插件,须在 `start()` 前。 |
28
28
  | `useInstrumentation(instr)` | `(instr) => this` | 注册额外 OpenTelemetry instrumentation,须在 `start()` 前。 |
29
29
  | `setUser(user)` | `(user) => void` | 更新用户上下文(`user.id`)。 |
30
- | `setView(view)` | `(view) => void` | 更新当前 view 上下文。 |
30
+ | `setTrackingConsent(consent)` | `(consent) => Promise<void>` | 授予或撤销采集授权,并启动或停止采集。 |
31
+ | `startView(options?)` | `(options?) => void` | 结束当前 View 并创建新 View。可在 SDK 启动前调用。 |
32
+ | `setViewName(name)` | `(name) => void` | 更新当前 View 名称,不创建新 View。 |
33
+ | `setViewLoadingTime()` | `() => void` | 使用当前时刻覆盖当前 View 的自动页面稳定耗时。 |
31
34
  | `reportAction(payload)` | `(payload) => void` | 手动上报用户动作(受采样控制)。 |
32
35
  | `reportCustomEvent(payload)` | `(payload) => void` | 手动上报自定义事件(受采样控制)。 |
33
36
  | `startSpan(name, attributes?, options?)` | `(...) => Span` | 创建并返回一个 span,自动带上运行时属性与脱敏。 |
@@ -46,6 +49,7 @@ const bkOT = new BkOpenTelemetry({
46
49
 
47
50
  ```ts
48
51
  const bkOT = new BkOpenTelemetry({
52
+ application: { name: 'bk-rum-demo' },
49
53
  autoStart: false,
50
54
  transport: { endpoint: 'https://rum-collector.example.com' },
51
55
  });
@@ -105,28 +109,53 @@ bkOT.setUser({
105
109
  });
106
110
  ```
107
111
 
108
- ### setView
112
+ ### setTrackingConsent
109
113
 
110
- 更新当前 view 上下文。
114
+ 运行期更新采集授权。撤销授权会停止插件与浏览器 API patch,并刷新已允许采集的数据;再次授权会重新建立 Session/View 和采集能力。
111
115
 
112
116
  ```ts
113
- bkOT.setView({
114
- id: 'view-home',
117
+ await bkOT.setTrackingConsent('granted');
118
+ await bkOT.setTrackingConsent('not-granted');
119
+ ```
120
+
121
+ ### startView
122
+
123
+ 创建新的 View,并结束前一个 View。`view.id` 由 SDK 生成,业务侧不能覆盖。
124
+
125
+ ```ts
126
+ bkOT.startView({
127
+ name: '首页',
115
128
  url: window.location.href,
116
- urlPathGroup: '/home',
117
- loadingType: 'initial_load',
129
+ attributes: {
130
+ 'biz.owner': 'portal-team',
131
+ },
118
132
  });
119
133
  ```
120
134
 
135
+ ### setViewName
136
+
137
+ 只修改当前 View 的 `view.name`,后续子事件使用新名称。
138
+
139
+ ```ts
140
+ bkOT.setViewName('首页-新版');
141
+ ```
142
+
143
+ ### setViewLoadingTime
144
+
145
+ 当自动页面活动无法表达业务真正的渲染完成点时,手动覆盖当前 View 的 `view.loading_time`。每次调用都会替换前一次结果。
146
+
147
+ ```ts
148
+ await renderAsyncPage();
149
+ bkOT.setViewLoadingTime();
150
+ ```
151
+
121
152
  ### reportAction
122
153
 
123
154
  手动上报用户动作。
124
155
 
125
156
  ```ts
126
157
  bkOT.reportAction({
127
- type: 'click',
128
- targetTag: 'button',
129
- targetText: '保存',
158
+ name: '保存',
130
159
  attributes: {
131
160
  'biz.form': 'alarm-strategy',
132
161
  },
@@ -218,11 +247,14 @@ span.end();
218
247
  import { resolveConfig } from '@blueking/open-telemetry';
219
248
 
220
249
  const config = resolveConfig({
221
- app: {
250
+ application: {
222
251
  name: 'bk-rum-demo',
223
252
  },
253
+ transport: {
254
+ endpoint: 'https://rum-collector.example.com',
255
+ },
224
256
  });
225
257
 
226
- console.log(config.app.name);
258
+ console.log(config.application.name);
227
259
  console.log(config.transport.traces.endpoint);
228
260
  ```
@@ -10,7 +10,7 @@
10
10
 
11
11
  - Trace:用于描述一次有起止时间的行为,例如页面访问、接口请求、资源加载、用户点击、长任务。
12
12
  - Metric:用于描述可聚合的数值,例如 Web Vitals、长任务数量、WebSocket 消息大小、空白屏结果。
13
- - Log:用于描述离散事件或诊断信息,例如错误、CSP 违规、Session 生命周期、HTTP body 错误路径。
13
+ - Log:用于描述离散事件或诊断信息,例如 CSP 违规、Session 生命周期和 WebSocket 状态。
14
14
 
15
15
  示例:手动创建一个业务 span。
16
16
 
@@ -18,7 +18,7 @@
18
18
  import { BkOpenTelemetry } from '@blueking/open-telemetry';
19
19
 
20
20
  const bkOT = new BkOpenTelemetry({
21
- app: { name: 'checkout-web' },
21
+ application: { name: 'checkout-web' },
22
22
  transport: { endpoint: 'https://rum-collector.example.com' },
23
23
  });
24
24
 
@@ -36,7 +36,7 @@ SDK 会维护运行时公共上下文,并自动挂到后续 span/log 上:
36
36
 
37
37
  - `user.id`:由接入方提供,SDK 原样透传,不 hash、不脱敏、不改写。
38
38
  - `session.id`:由 session 插件生成和轮换,用于关联同一次访问会话。
39
- - `view.id` / `view.url` / `view.url_path_group`:由 pageView 插件维护,用于关联同一个页面或路由实例。
39
+ - `view.id` / `view.name` / `view.url` / `view.url_path_group`:由 View 管理器维护,用于关联同一个页面或路由实例。
40
40
 
41
41
  示例:登录后更新用户上下文。
42
42
 
@@ -46,34 +46,33 @@ bkOT.setUser({
46
46
  });
47
47
  ```
48
48
 
49
- 示例:业务路由系统主动设置 view 上下文。
49
+ 示例:手动模式由业务路由系统创建 View。
50
50
 
51
51
  ```ts
52
- bkOT.setView({
53
- id: 'view-product-detail',
52
+ bkOT.startView({
53
+ name: '商品详情',
54
54
  url: window.location.href,
55
- urlPathGroup: '/product/:id',
56
- loadingType: 'route_change',
55
+ attributes: { 'biz.owner': 'product-team' },
57
56
  });
58
57
  ```
59
58
 
60
- ## Instrumentation 与插件
59
+ ## 内置采集与扩展
61
60
 
62
61
  SDK 将能力分为两类:
63
62
 
64
- 1. 官方 instrumentation:`documentLoad`、`fetch`、`xhr`、`userInteraction`,主要负责 patch 浏览器 API 或读取标准浏览器性能数据。
65
- 2. RUM 插件:`pageView`、`resource`、`error`、`webVitals`、`blankScreen`、`device`、`session` 等,负责主动产生 RUM 归一数据。
63
+ 1. 内置采集:`tracking.request` 使用统一的 Fetch/XHR 生命周期实现,负责 patch 浏览器 API、创建 CLIENT Span、Trace Header 和请求 Performance Timing 匹配。
64
+ 2. RUM 插件:View、Session、`resource`、`error`、`webVitals`、`blankScreen`、`device` 等,负责主动产生 RUM 归一数据。
66
65
 
67
- 示例:关闭官方用户交互 instrumentation,仅使用 SDK 的 `action` 插件。
66
+ 用户交互统一由 SDK 的 `action` 插件采集,避免与官方 user-interaction instrumentation 重复上报。
68
67
 
69
68
  ```ts
70
69
  new BkOpenTelemetry({
71
- instrumentations: {
72
- userInteraction: false,
73
- },
74
- plugins: {
70
+ application: { name: 'demo' },
71
+ transport: { endpoint: 'https://rum-collector.example.com' },
72
+ tracking: {
75
73
  action: {
76
- eventNames: ['click', 'submit', 'keydown'],
74
+ actionNameAttribute: 'data-bk-action-name',
75
+ maxDuration: 10000,
77
76
  },
78
77
  },
79
78
  });
@@ -81,14 +80,15 @@ new BkOpenTelemetry({
81
80
 
82
81
  ## 采样
83
82
 
84
- `sampling.rate` 是会话级 sticky 采样。一次会话命中后,RUM 插件和手动事件都会上报;未命中时,主动产生数据的插件不会启动,`reportAction()` 和 `reportCustomEvent()` 会丢弃。
83
+ `session.sampleRate` 是会话级确定性采样。决定由 `application.name + session.id` 的稳定 hash 得出,同一 Session 的三类信号保持一致。插件会维护必要的页面生命周期,但未命中时 sampler/processor 不导出其数据,`reportAction()` 和 `reportCustomEvent()` 直接丢弃。
85
84
 
86
85
  示例:生产环境按 10% 会话采样。
87
86
 
88
87
  ```ts
89
88
  new BkOpenTelemetry({
90
- sampling: {
91
- rate: 0.1,
89
+ application: { name: 'demo' },
90
+ session: {
91
+ sampleRate: 0.1,
92
92
  },
93
93
  transport: {
94
94
  endpoint: 'https://rum-collector.example.com',
@@ -100,7 +100,7 @@ new BkOpenTelemetry({
100
100
 
101
101
  SDK 会为 span 补充 RUM 归一字段,例如:
102
102
 
103
- - `span_type`:`document`、`http`、`resource`、`vital`、`error`、`longtask`、`action`、`route`、`custom`
103
+ - `span_type`:`document`、`resource`、`vital`、`error`、`longtask`、`action`、`route`、`custom`
104
104
  - `result`:`success`、`warning`、`error`、`timeout`、`abort`
105
105
  - `duration_bucket`:`<100ms`、`100~500ms`、`500ms~2s`、`>2s`
106
106
  - `target_label`:跨类型的主标签,用于统一检索
@@ -122,106 +122,32 @@ const span = bkOT.startSpan('custom.cache_warmup', {
122
122
  span.end();
123
123
  ```
124
124
 
125
- # Plugin 与 Instrumentation 的区别
126
-
127
- `@blueking/open-telemetry` 把采集能力拆成两类:**官方 Instrumentation** 和 **RUM 插件(Plugin)**。两者在配置入口、数据来源、生命周期和采样行为上都不同,理解差异有助于按需裁剪采集能力。
128
-
129
- ## 一句话区分
125
+ # 内置采集与扩展
130
126
 
131
- - **Instrumentation**:OpenTelemetry 官方/第三方探针,负责 patch 浏览器原生 API 或读取标准性能数据,是「数据从哪来」的底层来源。
132
- - **Plugin**:bk-ot 自研的能力单元,基于 runtime context 主动产生 RUM 归一数据,是「我们要采什么、怎么归一」的业务层。
133
-
134
- ## 配置入口
135
-
136
- 两类能力在配置上完全对称,分别挂在顶层的 `instrumentations` 和 `plugins` 下:
127
+ `tracking` 只配置 SDK 内置采集能力;`extensions` 只注册接入方提供的扩展实例。
137
128
 
138
129
  ```ts
139
130
  new BkOpenTelemetry({
140
- // 官方探针:开关 + 自定义实例
141
- instrumentations: {
142
- documentLoad: true,
143
- fetch: true,
144
- xhr: true,
145
- userInteraction: true,
146
- custom: [new MyCustomInstrumentation()],
147
- },
148
- // bk-ot 自研插件:开关 + 自定义插件
149
- plugins: {
150
- pageView: true,
131
+ application: { name: 'demo' },
132
+ transport: { endpoint: 'https://collector.example.com' },
133
+ tracking: {
134
+ request: true,
151
135
  error: true,
152
- webVitals: true,
153
- custom: [myBizPlugin],
136
+ longTask: false,
154
137
  },
155
- });
156
- ```
157
-
158
- ## 内置能力清单
159
-
160
- | 类型 | 内置项 |
161
- | --------------- | ---------------------------------------------------------------------------------------------------------------------------- |
162
- | Instrumentation | `documentLoad`、`fetch`、`xhr`、`userInteraction` |
163
- | Plugin | `pageView`、`resource`、`error`、`webVitals`、`blankScreen`、`device`、`session`、`action`、`httpBody`、`longTask`、`routeTiming`、`cspViolation`、`websocket` |
164
-
165
- ## 核心差异对比
166
-
167
- | 维度 | Instrumentation | Plugin |
168
- | ------------ | ------------------------------------------------ | --------------------------------------------------- |
169
- | 来源 | OpenTelemetry 官方包 / 第三方 | bk-ot 自研 |
170
- | 职责 | patch 原生 API、读取标准性能数据 | 基于 runtime context 主动产生 RUM 归一数据 |
171
- | 配置位置 | `instrumentations: {}` | `plugins: {}` |
172
- | 自定义扩展 | `instrumentations.custom` / `useInstrumentation()` | `plugins.custom` / `use()` / `createPlugin()` |
173
- | 启动条件 | **SDK 启用(`enabled`)即装载** | 官方探针适配插件随 SDK 启用;产生 RUM 数据的插件**仅采样命中时启动** |
174
- | 受采样影响 | 否 | 是(RUM 数据插件未命中采样时不启动) |
175
-
176
- ## 生命周期与采样
177
-
178
- 这是两者最容易混淆、也最关键的差异:
179
-
180
- - **Instrumentation 不受采样影响**。只要 `enabled !== false`,官方探针与 `instrumentations.custom` 中的实例都会在 `start()` 时注册。它们 patch 的是底层 API,需要在采样判定之前就位。
181
-
182
- > 注意:这里的"不受采样影响"指的是**是否装载/patch**。未命中采样的会话,trace sampler 的比例为 `0`,探针产生的 span 仍会在采样阶段被丢弃、不会上报。也就是说 patch 照做,但数据最终不落库。
183
- - **RUM 插件受采样影响**。`sampling.rate` 是会话级 sticky 采样,未命中的会话不会启动主动产生数据的插件,`reportAction()`、`reportCustomEvent()` 也会丢弃。
184
-
185
- > 说明:SDK 内部把官方 instrumentation 封装成一个核心适配插件(`official-instrumentations`),它与自研 RUM 插件走同一套插件生命周期,但归属于「SDK 启用即装载」的核心插件,不参与采样开关。
186
-
187
- ## 如何扩展
188
-
189
- ### 接入第三方 Instrumentation
190
-
191
- 两种等价写法,都必须在 `start()` 之前:
192
-
193
- ```ts
194
- // 写法一:配置式
195
- new BkOpenTelemetry({
196
- autoStart: false,
197
- instrumentations: {
198
- custom: [new MyCustomInstrumentation()],
138
+ extensions: {
139
+ plugins: [myRumPlugin],
140
+ instrumentations: [myOtelInstrumentation],
199
141
  },
200
142
  });
201
-
202
- // 写法二:链式(适合运行时动态追加)
203
- bkOT.useInstrumentation(new MyCustomInstrumentation());
204
- await bkOT.start();
205
143
  ```
206
144
 
207
- ### 编写自研 Plugin
208
-
209
- 通过 `createPlugin()` 访问 `tracer`、`meter`、`logger`、`startSpan()`、`emitLog()` 等 runtime context 能力,详见[自定义插件](./custom-plugin.md)。
210
-
211
- ```ts
212
- import { createPlugin } from '@blueking/open-telemetry';
213
-
214
- const bizPlugin = createPlugin({
215
- name: 'biz-plugin',
216
- init(context) {
217
- const span = context.startSpan('custom.biz_ready');
218
- span.end();
219
- },
220
- });
221
- ```
145
+ | 类型 | 配置位置 | 适用场景 |
146
+ | --- | --- | --- |
147
+ | 内置采集 | `tracking` | View、请求、资源、错误、Web Vitals、Action 等 |
148
+ | RUM 插件 | `extensions.plugins` / `use()` | 需要 Session/View、日志、指标和活动总线的浏览器语义 |
149
+ | OTel instrumentation | `extensions.instrumentations` / `useInstrumentation()` | 已有标准 instrumentation,直接使用 SDK 私有 provider |
222
150
 
223
- ## 选择建议
151
+ 内置 Fetch/XHR 不再基于官方 instrumentation 叠加适配,而是由单一请求生命周期实现,避免重复 patch、重复活动计数及私有 `_addHeaders/_patchSend` 依赖。
224
152
 
225
- - 只想 patch 一个浏览器/网络 API、拿标准遥测数据 → 用 **Instrumentation**(优先看官方是否已有现成包)。
226
- - 想产生带 RUM 归一字段(`span_type`、`result`、`target_label` 等)的业务事件、需要访问运行时上下文 → 用 **Plugin**。
227
- - 想让能力跟随采样开关、只对命中会话生效 → 必须做成 **Plugin**(RUM 数据类)。
153
+ 扩展必须在 `start()` 前注册;动态 Consent 重新授权时,插件可能再次执行 `init()`,所以插件的 `shutdown()` 应完整释放副作用。