@blueking/open-telemetry 0.0.4 → 0.0.6
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 +388 -0
- package/dist/bk-rum.global.js +8 -8
- package/dist/bk-rum.global.js.map +1 -1
- package/dist/browser.d.ts +2 -0
- package/dist/core/config.d.ts +138 -0
- package/dist/core/id.d.ts +8 -0
- package/dist/core/plugin.d.ts +46 -0
- package/dist/core/processor.d.ts +16 -0
- package/dist/core/route-observer.d.ts +9 -0
- package/dist/core/sampling.d.ts +2 -0
- package/dist/core/sdk.d.ts +44 -0
- package/dist/core/throttle.d.ts +20 -0
- package/dist/core/url.d.ts +4 -0
- package/dist/index.d.ts +16 -0
- package/dist/index.js +865 -601
- package/dist/index.js.map +1 -1
- package/dist/plugins/blank-screen.d.ts +3 -0
- package/dist/plugins/common.d.ts +3 -0
- package/dist/plugins/csp-violation.d.ts +19 -0
- package/dist/plugins/device.d.ts +23 -0
- package/dist/plugins/error.d.ts +3 -0
- package/dist/plugins/http-body.d.ts +3 -0
- package/dist/plugins/long-task.d.ts +15 -0
- package/dist/plugins/page-view.d.ts +3 -0
- package/dist/plugins/route-timing.d.ts +3 -0
- package/dist/plugins/session.d.ts +3 -0
- package/dist/plugins/web-vitals.d.ts +3 -0
- package/dist/plugins/websocket.d.ts +3 -0
- package/package.json +5 -3
package/README.md
CHANGED
|
@@ -0,0 +1,388 @@
|
|
|
1
|
+
# @blueking/open-telemetry
|
|
2
|
+
|
|
3
|
+
面向浏览器 RUM 场景的 OpenTelemetry SDK 封装,默认会采集页面访问、错误、Web Vitals、设备、会话、fetch/xhr 等常用 RUM 数据,并通过 OTLP HTTP 上报。
|
|
4
|
+
|
|
5
|
+
## 安装使用
|
|
6
|
+
|
|
7
|
+
### npm 引入
|
|
8
|
+
|
|
9
|
+
```javascript
|
|
10
|
+
import { BkOpenTelemetry } from '@blueking/open-telemetry';
|
|
11
|
+
|
|
12
|
+
const bkOT = new BkOpenTelemetry({
|
|
13
|
+
endpoint: 'https://your-otlp-collector',
|
|
14
|
+
token: 'your-report-token',
|
|
15
|
+
environment: 'production',
|
|
16
|
+
});
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
### CDN 引入
|
|
20
|
+
|
|
21
|
+
```html
|
|
22
|
+
<script src="https://unpkg.com/@blueking/open-telemetry/dist/bk-rum.global.js"></script>
|
|
23
|
+
<script>
|
|
24
|
+
const bkOT = new BkOpenTelemetry({
|
|
25
|
+
endpoint: 'https://your-otlp-collector',
|
|
26
|
+
token: 'your-report-token',
|
|
27
|
+
environment: 'production',
|
|
28
|
+
});
|
|
29
|
+
</script>
|
|
30
|
+
```
|
|
31
|
+
|
|
32
|
+
## 最小配置
|
|
33
|
+
|
|
34
|
+
```javascript
|
|
35
|
+
new BkOpenTelemetry({
|
|
36
|
+
endpoint: 'https://your-otlp-collector',
|
|
37
|
+
token: 'your-report-token',
|
|
38
|
+
});
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
`endpoint` 是 OTLP collector 地址,SDK 会自动拼出三类信号地址:
|
|
42
|
+
|
|
43
|
+
- traces: `endpoint/v1/traces`
|
|
44
|
+
- metrics: `endpoint/v1/metrics`
|
|
45
|
+
- logs: `endpoint/v1/logs`
|
|
46
|
+
|
|
47
|
+
如果三类信号需要发到不同地址,可以分别配置:
|
|
48
|
+
|
|
49
|
+
```javascript
|
|
50
|
+
new BkOpenTelemetry({
|
|
51
|
+
endpoint: 'https://your-otlp-collector',
|
|
52
|
+
token: 'your-report-token',
|
|
53
|
+
signalEndpoints: {
|
|
54
|
+
traces: 'https://your-trace-collector/rum',
|
|
55
|
+
metrics: 'https://your-metric-collector/v1/metrics',
|
|
56
|
+
logs: 'https://your-log-collector/v1/logs',
|
|
57
|
+
},
|
|
58
|
+
});
|
|
59
|
+
```
|
|
60
|
+
|
|
61
|
+
`signalEndpoints.traces`、`signalEndpoints.metrics`、`signalEndpoints.logs` 是完整上报地址,不会自动拼 `/v1/*`;未配置时会回退到对应的 `traces.endpoint`、`metrics.endpoint`、`logs.endpoint`,再回退到统一 `endpoint`。
|
|
62
|
+
|
|
63
|
+
`token` 会转换为上报请求头:
|
|
64
|
+
|
|
65
|
+
```txt
|
|
66
|
+
Authorization: Bearer your-report-token
|
|
67
|
+
```
|
|
68
|
+
|
|
69
|
+
## 基础配置
|
|
70
|
+
|
|
71
|
+
```javascript
|
|
72
|
+
new BkOpenTelemetry({
|
|
73
|
+
token: 'your-report-token',
|
|
74
|
+
environment: 'production',
|
|
75
|
+
enabled: true,
|
|
76
|
+
debug: false,
|
|
77
|
+
sampleRate: 1,
|
|
78
|
+
spanProcessor: 'batch',
|
|
79
|
+
headers: {
|
|
80
|
+
'X-Custom-Header': 'value',
|
|
81
|
+
},
|
|
82
|
+
signalEndpoints: {
|
|
83
|
+
traces: 'https://your-trace-collector/rum',
|
|
84
|
+
metrics: 'https://your-metric-collector/v1/metrics',
|
|
85
|
+
logs: 'https://your-log-collector/v1/logs',
|
|
86
|
+
},
|
|
87
|
+
});
|
|
88
|
+
```
|
|
89
|
+
|
|
90
|
+
- `endpoint`: 上报地址BaseURL,默认 `http://localhost:4318`。
|
|
91
|
+
- `token`: 上报 token,会写入 `Authorization: Bearer xxx`。
|
|
92
|
+
- `environment`: 环境名,默认 `production`。
|
|
93
|
+
- `enabled`: 是否启用 SDK,默认 `true`。
|
|
94
|
+
- `debug`: 是否在控制台打印上报数据,默认 `false`。
|
|
95
|
+
- `sampleRate`: 采样率,范围 `0 - 1`,默认 `1`。
|
|
96
|
+
- `spanProcessor`: trace 处理模式,默认 `batch`;可选 `simple` 用于本地调试。
|
|
97
|
+
- `headers`: 附加上报请求头,显式传入的 header 可覆盖 token 生成的同名 header。
|
|
98
|
+
- `signalEndpoints`: 分别配置 trace、metric、log 的完整上报地址,优先级高于统一 `endpoint`。
|
|
99
|
+
- `traces` / `metrics` / `logs`: 分别配置 trace、metric、log 的高级请求参数;其中 `endpoint` 优先级低于 `signalEndpoints`。
|
|
100
|
+
|
|
101
|
+
`spanProcessor: 'batch'` 会批量缓存并定时上报 trace,适合生产环境;`spanProcessor: 'simple'` 会在 span 结束后立即触发导出,适合本地调试,不建议生产环境使用。
|
|
102
|
+
|
|
103
|
+
## RUM 采集配置
|
|
104
|
+
|
|
105
|
+
所有采集能力统一放在 `rum` 下配置:
|
|
106
|
+
|
|
107
|
+
```javascript
|
|
108
|
+
new BkOpenTelemetry({
|
|
109
|
+
endpoint: 'https://your-otlp-collector',
|
|
110
|
+
token: 'your-report-token',
|
|
111
|
+
rum: {
|
|
112
|
+
documentLoad: true,
|
|
113
|
+
fetch: true,
|
|
114
|
+
xhr: true,
|
|
115
|
+
userInteraction: true,
|
|
116
|
+
pageView: true,
|
|
117
|
+
error: true,
|
|
118
|
+
webVitals: true,
|
|
119
|
+
blankScreen: true,
|
|
120
|
+
device: true,
|
|
121
|
+
session: true,
|
|
122
|
+
websocket: true,
|
|
123
|
+
longTask: { threshold: 50 },
|
|
124
|
+
routeTiming: true,
|
|
125
|
+
cspViolation: true,
|
|
126
|
+
httpBody: {
|
|
127
|
+
maxBodySize: 4 * 1024,
|
|
128
|
+
redact: ({ body, type }) => (type === 'request' ? body.replace(/password/g, '***') : body),
|
|
129
|
+
},
|
|
130
|
+
},
|
|
131
|
+
});
|
|
132
|
+
```
|
|
133
|
+
|
|
134
|
+
默认开启:`documentLoad`、`fetch`、`xhr`、`userInteraction`、`pageView`、`error`、`webVitals`、`blankScreen`、`device`、`session`、`websocket`。
|
|
135
|
+
|
|
136
|
+
默认关闭:`httpBody`、`longTask`、`routeTiming`、`cspViolation`。
|
|
137
|
+
|
|
138
|
+
常用说明:
|
|
139
|
+
|
|
140
|
+
- `documentLoad`: 采集页面加载性能。
|
|
141
|
+
- `fetch` / `xhr`: 采集 HTTP 请求,并给同源请求注入 `traceparent`。
|
|
142
|
+
- `userInteraction`: 采集用户交互。
|
|
143
|
+
- `pageView`: 采集页面访问和 SPA 路由变化。
|
|
144
|
+
- `error`: 采集 JS 错误、未处理 Promise rejection、资源加载失败。
|
|
145
|
+
- `webVitals`: 采集 LCP、CLS、INP、FCP、TTFB。
|
|
146
|
+
- `blankScreen`: 检测白屏,SPA 项目建议显式配置 `rootSelector` 为应用挂载点(详见下文「白屏检测」)。
|
|
147
|
+
- `device`: 采集浏览器、系统、网络等设备信息。
|
|
148
|
+
- `session`: 维护匿名会话。
|
|
149
|
+
- `websocket`: 采集 WebSocket 连接和消息事件。
|
|
150
|
+
- `longTask`: 采集长任务,`threshold` 表示最小时长阈值。
|
|
151
|
+
- `routeTiming`: 采集 SPA 路由切换耗时。
|
|
152
|
+
- `cspViolation`: 采集 CSP violation。
|
|
153
|
+
- `httpBody`: 采集请求/响应 body,建议配合 `redact` 做脱敏。
|
|
154
|
+
|
|
155
|
+
### 白屏检测
|
|
156
|
+
|
|
157
|
+
页面就绪后延迟一段时间,在视口的 5 个采样点用 `document.elementFromPoint` 判断这些点是否落在"根元素"或裸 body 上,命中比例超过阈值即视为白屏。命中后会同时上报:
|
|
158
|
+
|
|
159
|
+
- metric: `browser.blank_screen.count`(仅带低基数维度 `bk.rum.blank_screen.root`)
|
|
160
|
+
- log: `browser.blank_screen`(携带评分、命中元素、DOM 节点数等高基数信息)
|
|
161
|
+
|
|
162
|
+
完整配置示例:
|
|
163
|
+
|
|
164
|
+
```javascript
|
|
165
|
+
new BkOpenTelemetry({
|
|
166
|
+
endpoint: 'https://your-otlp-collector',
|
|
167
|
+
token: 'your-report-token',
|
|
168
|
+
rum: {
|
|
169
|
+
blankScreen: {
|
|
170
|
+
// SPA 项目强烈建议设置为应用挂载点,例如 '#app' / '#root'
|
|
171
|
+
rootSelector: '#app',
|
|
172
|
+
// 页面 ready 后延迟多少毫秒开始检测,默认 3000
|
|
173
|
+
checkDelay: 3000,
|
|
174
|
+
// 命中比例阈值,范围 0 - 1,默认 0.8
|
|
175
|
+
threshold: 0.8,
|
|
176
|
+
// 自定义"加载中"占位选择器,命中样本会从分母里剔除,避免骨架屏被误判为白屏
|
|
177
|
+
ignoreSelectors: ['.app-loading', '.page-skeleton'],
|
|
178
|
+
},
|
|
179
|
+
},
|
|
180
|
+
});
|
|
181
|
+
```
|
|
182
|
+
|
|
183
|
+
配置项说明:
|
|
184
|
+
|
|
185
|
+
- `rootSelector`: 默认 `body`。**SPA 项目(React/Vue 等)必须显式配置为应用挂载点**,否则一旦应用成功 mount,采样点会落在挂载根(如 `#app`)的子节点上,永远不会判定为白屏;反之挂载失败时挂载根本身仍存在,配置成 `#app` 后才能正确命中。
|
|
186
|
+
- `checkDelay`: 从 `DOMContentLoaded` 起算的延迟(毫秒),默认 `3000`。SPA 首屏渲染较慢的项目可以适当调大。
|
|
187
|
+
- `threshold`: 命中比例阈值(0 - 1),默认 `0.8`。
|
|
188
|
+
- `ignoreSelectors`: 项目自定义的"加载中"占位选择器。SDK 内置了一组通用兜底(`[data-loading="true"]`、`[aria-busy="true"]`、`.loading`、`.is-loading`、`.spinner`、`.skeleton`、`.bk-loading`),但**强烈建议项目按自己的骨架屏/loading 约定显式追加**,命中此选择器的样本既不算"空白"也不算"非空白",会从分母里剔除。
|
|
189
|
+
|
|
190
|
+
判定算法(伪代码):
|
|
191
|
+
|
|
192
|
+
```text
|
|
193
|
+
score = empty_samples / (total_samples - loading_samples)
|
|
194
|
+
isBlank = score >= threshold
|
|
195
|
+
```
|
|
196
|
+
|
|
197
|
+
如果所有采样点都命中 loading(说明页面整体还在加载),本次检测会被视为待定,不上报。
|
|
198
|
+
|
|
199
|
+
## 公共属性
|
|
200
|
+
|
|
201
|
+
通过 `attributes` 给不同类型数据补充公共属性:
|
|
202
|
+
|
|
203
|
+
```javascript
|
|
204
|
+
new BkOpenTelemetry({
|
|
205
|
+
endpoint: 'https://your-otlp-collector',
|
|
206
|
+
token: 'your-report-token',
|
|
207
|
+
attributes: {
|
|
208
|
+
page: () => ({
|
|
209
|
+
'rum.page.host': window.location.host,
|
|
210
|
+
'rum.page.path': window.location.pathname,
|
|
211
|
+
}),
|
|
212
|
+
custom: () => ({
|
|
213
|
+
'biz.team': 'demo',
|
|
214
|
+
}),
|
|
215
|
+
error: () => ({
|
|
216
|
+
'biz.error_source': 'browser',
|
|
217
|
+
}),
|
|
218
|
+
metric: () => ({
|
|
219
|
+
'biz.metric_source': 'rum',
|
|
220
|
+
}),
|
|
221
|
+
},
|
|
222
|
+
});
|
|
223
|
+
```
|
|
224
|
+
|
|
225
|
+
- `page`: 页面、错误、指标、自定义事件等都会常用的页面属性。
|
|
226
|
+
- `custom`: 自定义事件公共属性。
|
|
227
|
+
- `error`: 错误事件公共属性。
|
|
228
|
+
- `metric`: 指标类数据公共属性。
|
|
229
|
+
|
|
230
|
+
## 脱敏配置
|
|
231
|
+
|
|
232
|
+
通过 `redact` 统一处理敏感数据:
|
|
233
|
+
|
|
234
|
+
```javascript
|
|
235
|
+
new BkOpenTelemetry({
|
|
236
|
+
endpoint: 'https://your-otlp-collector',
|
|
237
|
+
token: 'your-report-token',
|
|
238
|
+
redact: {
|
|
239
|
+
attributes: attributes => {
|
|
240
|
+
const result = { ...attributes };
|
|
241
|
+
for (const key of Object.keys(result)) {
|
|
242
|
+
const value = result[key];
|
|
243
|
+
if (typeof value === 'string' && /\b\d{15}|\d{18}\b/.test(value)) {
|
|
244
|
+
result[key] = value.replace(/\d/g, '*');
|
|
245
|
+
}
|
|
246
|
+
}
|
|
247
|
+
return result;
|
|
248
|
+
},
|
|
249
|
+
url: url => url.replace(/token=[^&]+/g, 'token=***'),
|
|
250
|
+
},
|
|
251
|
+
});
|
|
252
|
+
```
|
|
253
|
+
|
|
254
|
+
- `redact.attributes`: 过滤写入 span/log 的属性。
|
|
255
|
+
- `redact.url`: 过滤 URL 类属性,例如 `url.full`、`document.referrer`。
|
|
256
|
+
|
|
257
|
+
`rum.httpBody.redact` 只处理请求/响应 body,适合对 body 里的密码、token 等字段做单独脱敏。
|
|
258
|
+
|
|
259
|
+
## 自定义事件
|
|
260
|
+
|
|
261
|
+
`new BkOpenTelemetry()` 会返回 SDK 实例,可以通过 `reportCustomEvent` 上报业务自定义事件:
|
|
262
|
+
|
|
263
|
+
```javascript
|
|
264
|
+
const bkOT = new BkOpenTelemetry({
|
|
265
|
+
endpoint: 'https://your-otlp-collector',
|
|
266
|
+
token: 'your-report-token',
|
|
267
|
+
});
|
|
268
|
+
|
|
269
|
+
bkOT.reportCustomEvent({
|
|
270
|
+
name: 'order.checkout',
|
|
271
|
+
attributes: {
|
|
272
|
+
'biz.order_id': 'O20260509001',
|
|
273
|
+
'biz.order_amount': 199,
|
|
274
|
+
},
|
|
275
|
+
});
|
|
276
|
+
```
|
|
277
|
+
|
|
278
|
+
如果事件失败,可以带上 `error`:
|
|
279
|
+
|
|
280
|
+
```javascript
|
|
281
|
+
bkOT.reportCustomEvent({
|
|
282
|
+
name: 'order.checkout',
|
|
283
|
+
attributes: {
|
|
284
|
+
'biz.order_id': 'O20260509001',
|
|
285
|
+
},
|
|
286
|
+
error: new Error('库存不足'),
|
|
287
|
+
});
|
|
288
|
+
```
|
|
289
|
+
|
|
290
|
+
## 自定义插件
|
|
291
|
+
|
|
292
|
+
通过 `createPlugin` 扩展 SDK 能力:
|
|
293
|
+
|
|
294
|
+
```javascript
|
|
295
|
+
import { BkOpenTelemetry, createPlugin } from '@blueking/open-telemetry';
|
|
296
|
+
|
|
297
|
+
const plugin = createPlugin({
|
|
298
|
+
name: 'demo-plugin',
|
|
299
|
+
init(context) {
|
|
300
|
+
context.setRuntimeAttributes({
|
|
301
|
+
'biz.plugin_enabled': true,
|
|
302
|
+
});
|
|
303
|
+
|
|
304
|
+
const span = context.startSpan('demo.plugin_ready');
|
|
305
|
+
span.end();
|
|
306
|
+
},
|
|
307
|
+
});
|
|
308
|
+
|
|
309
|
+
const bkOT = new BkOpenTelemetry({
|
|
310
|
+
autoStart: false,
|
|
311
|
+
endpoint: 'https://your-otlp-collector',
|
|
312
|
+
token: 'your-report-token',
|
|
313
|
+
plugins: [plugin],
|
|
314
|
+
});
|
|
315
|
+
|
|
316
|
+
await bkOT.start();
|
|
317
|
+
```
|
|
318
|
+
|
|
319
|
+
也可以通过 `use(plugin)` 链式注册,`use(plugin)` 只允许在 `start()` 前调用;自定义插件场景建议显式设置 `autoStart: false`。
|
|
320
|
+
|
|
321
|
+
## 自定义 Instrumentation
|
|
322
|
+
|
|
323
|
+
如需接入 OpenTelemetry 原生 instrumentation,可通过 `useInstrumentation` 注册:
|
|
324
|
+
|
|
325
|
+
```javascript
|
|
326
|
+
import { BkOpenTelemetry } from '@blueking/open-telemetry';
|
|
327
|
+
import { MyCustomInstrumentation } from './my-custom-instrumentation';
|
|
328
|
+
|
|
329
|
+
const bkOT = new BkOpenTelemetry({
|
|
330
|
+
autoStart: false,
|
|
331
|
+
endpoint: 'https://your-otlp-collector',
|
|
332
|
+
token: 'your-report-token',
|
|
333
|
+
instrumentations: [new MyCustomInstrumentation()],
|
|
334
|
+
});
|
|
335
|
+
|
|
336
|
+
await bkOT.start();
|
|
337
|
+
```
|
|
338
|
+
|
|
339
|
+
也可以通过 `useInstrumentation(instrumentation)` 在 `start()` 前注册。
|
|
340
|
+
|
|
341
|
+
## 生命周期
|
|
342
|
+
|
|
343
|
+
```javascript
|
|
344
|
+
const bkOT = new BkOpenTelemetry({
|
|
345
|
+
endpoint: 'https://your-otlp-collector',
|
|
346
|
+
token: 'your-report-token',
|
|
347
|
+
});
|
|
348
|
+
|
|
349
|
+
await bkOT.flush();
|
|
350
|
+
await bkOT.shutdown();
|
|
351
|
+
```
|
|
352
|
+
|
|
353
|
+
- `flush`: 立即刷新插件、trace、metric、log。
|
|
354
|
+
- `shutdown`: 刷新后关闭 SDK,并卸载事件监听。
|
|
355
|
+
|
|
356
|
+
## 完整示例
|
|
357
|
+
|
|
358
|
+
```javascript
|
|
359
|
+
import { BkOpenTelemetry } from '@blueking/open-telemetry';
|
|
360
|
+
|
|
361
|
+
export const bkOT = new BkOpenTelemetry({
|
|
362
|
+
endpoint: 'https://your-otlp-collector',
|
|
363
|
+
token: 'your-report-token',
|
|
364
|
+
environment: 'production',
|
|
365
|
+
debug: false,
|
|
366
|
+
sampleRate: 1,
|
|
367
|
+
rum: {
|
|
368
|
+
fetch: true,
|
|
369
|
+
xhr: true,
|
|
370
|
+
documentLoad: true,
|
|
371
|
+
userInteraction: true,
|
|
372
|
+
longTask: { threshold: 50 },
|
|
373
|
+
routeTiming: true,
|
|
374
|
+
httpBody: {
|
|
375
|
+
maxBodySize: 4 * 1024,
|
|
376
|
+
redact: ({ body }) => body.replace(/("password"\s*:\s*)"[^"]*"/g, '$1"***"'),
|
|
377
|
+
},
|
|
378
|
+
},
|
|
379
|
+
attributes: {
|
|
380
|
+
custom: () => ({
|
|
381
|
+
'biz.team': 'rum-demo',
|
|
382
|
+
}),
|
|
383
|
+
},
|
|
384
|
+
redact: {
|
|
385
|
+
url: url => url.replace(/token=[^&]+/g, 'token=***'),
|
|
386
|
+
},
|
|
387
|
+
});
|
|
388
|
+
```
|