@blueking/open-telemetry 0.0.6 → 0.0.8
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 +247 -239
- package/dist/bk-rum.global.js +9 -8
- package/dist/bk-rum.global.js.map +1 -1
- package/dist/core/config.d.ts +131 -37
- package/dist/core/plugin.d.ts +9 -4
- package/dist/core/processor.d.ts +2 -2
- package/dist/core/runtime-context.d.ts +30 -0
- package/dist/core/sampling.d.ts +2 -2
- package/dist/core/sdk.d.ts +12 -7
- package/dist/core/url.d.ts +4 -4
- package/dist/index.d.ts +6 -1
- package/dist/index.js +1004 -535
- package/dist/index.js.map +1 -1
- package/dist/plugins/action.d.ts +3 -0
- package/dist/plugins/blank-screen.d.ts +2 -2
- package/dist/plugins/csp-violation.d.ts +3 -3
- package/dist/plugins/device.d.ts +2 -2
- package/dist/plugins/error.d.ts +2 -2
- package/dist/plugins/http-body.d.ts +2 -2
- package/dist/plugins/long-task.d.ts +2 -2
- package/dist/plugins/page-view.d.ts +2 -2
- package/dist/plugins/resource.d.ts +3 -0
- package/dist/plugins/route-timing.d.ts +2 -2
- package/dist/plugins/session.d.ts +2 -2
- package/dist/plugins/web-vitals.d.ts +2 -2
- package/dist/plugins/websocket.d.ts +2 -2
- package/dist/schema/attributes.d.ts +50 -0
- package/dist/schema/span.d.ts +62 -0
- package/package.json +6 -2
package/README.md
CHANGED
|
@@ -1,213 +1,197 @@
|
|
|
1
1
|
# @blueking/open-telemetry
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
蓝鲸监控 RUM OT SDK,使用 OpenTelemetry 采集页面访问、接口请求、静态资源、错误、Web Vitals、设备和会话等数据,并通过 OTLP HTTP 上报。
|
|
4
4
|
|
|
5
|
-
##
|
|
5
|
+
## 安装和使用
|
|
6
6
|
|
|
7
|
-
|
|
7
|
+
```bash
|
|
8
|
+
# npm
|
|
9
|
+
npm install @blueking/open-telemetry
|
|
10
|
+
|
|
11
|
+
# pnpm
|
|
12
|
+
pnpm add @blueking/open-telemetry
|
|
13
|
+
|
|
14
|
+
# yarn
|
|
15
|
+
yarn add @blueking/open-telemetry
|
|
16
|
+
```
|
|
17
|
+
|
|
18
|
+
### npm 包使用
|
|
8
19
|
|
|
9
20
|
```javascript
|
|
10
21
|
import { BkOpenTelemetry } from '@blueking/open-telemetry';
|
|
11
22
|
|
|
12
23
|
const bkOT = new BkOpenTelemetry({
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
24
|
+
app: {
|
|
25
|
+
name: 'demo-app',
|
|
26
|
+
environment: 'production',
|
|
27
|
+
version: '1.0.0',
|
|
28
|
+
},
|
|
29
|
+
transport: {
|
|
30
|
+
endpoint: 'https://your-otlp-collector',
|
|
31
|
+
token: 'your-report-token',
|
|
32
|
+
},
|
|
33
|
+
user: {
|
|
34
|
+
id: window.USER_ID,
|
|
35
|
+
},
|
|
16
36
|
});
|
|
17
37
|
```
|
|
18
38
|
|
|
19
|
-
### CDN
|
|
39
|
+
### CDN 使用
|
|
20
40
|
|
|
21
41
|
```html
|
|
22
42
|
<script src="https://unpkg.com/@blueking/open-telemetry/dist/bk-rum.global.js"></script>
|
|
23
43
|
<script>
|
|
24
|
-
const bkOT = new BkOpenTelemetry({
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
44
|
+
const bkOT = new window.BkOpenTelemetry({
|
|
45
|
+
app: {
|
|
46
|
+
name: 'demo-app',
|
|
47
|
+
environment: 'production',
|
|
48
|
+
version: '1.0.0',
|
|
49
|
+
},
|
|
50
|
+
transport: {
|
|
51
|
+
endpoint: 'https://your-otlp-collector',
|
|
52
|
+
token: 'your-report-token',
|
|
53
|
+
},
|
|
54
|
+
user: {
|
|
55
|
+
id: window.USER_ID,
|
|
56
|
+
},
|
|
28
57
|
});
|
|
29
58
|
</script>
|
|
30
59
|
```
|
|
31
60
|
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
```javascript
|
|
35
|
-
new BkOpenTelemetry({
|
|
36
|
-
endpoint: 'https://your-otlp-collector',
|
|
37
|
-
token: 'your-report-token',
|
|
38
|
-
});
|
|
39
|
-
```
|
|
61
|
+
默认 `autoStart: true`,所以 `new BkOpenTelemetry()` 后 SDK 会自动启动。
|
|
40
62
|
|
|
41
|
-
`endpoint`
|
|
63
|
+
`transport.endpoint` 填 OTLP collector 根地址即可,SDK 会自动生成:
|
|
42
64
|
|
|
43
65
|
- traces: `endpoint/v1/traces`
|
|
44
66
|
- metrics: `endpoint/v1/metrics`
|
|
45
67
|
- logs: `endpoint/v1/logs`
|
|
46
68
|
|
|
47
|
-
|
|
69
|
+
`transport.token` 会变成请求头:`Authorization: Bearer <token>`。
|
|
48
70
|
|
|
49
|
-
|
|
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
|
-
```
|
|
71
|
+
## 2. 先理解默认采集
|
|
60
72
|
|
|
61
|
-
`
|
|
73
|
+
不写 `rum` 配置时,SDK 已经默认开启常用 RUM 能力:
|
|
62
74
|
|
|
63
|
-
`
|
|
75
|
+
- 页面加载:`documentLoad`
|
|
76
|
+
- 页面访问:`pageView`
|
|
77
|
+
- 接口请求:`fetch`、`xhr`
|
|
78
|
+
- 静态资源:`resource`
|
|
79
|
+
- JS 错误:`error`
|
|
80
|
+
- 页面体验:`webVitals`、`blankScreen`
|
|
81
|
+
- 用户环境:`device`、`session`
|
|
82
|
+
- 用户交互 instrumentation:`userInteraction`
|
|
64
83
|
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
84
|
+
默认关闭的能力需要手动打开:
|
|
85
|
+
|
|
86
|
+
- `action`: 用户点击、输入、提交等操作事件
|
|
87
|
+
- `httpBody`: 请求/响应 body 采集
|
|
88
|
+
- `longTask`: 长任务
|
|
89
|
+
- `routeTiming`: 路由耗时
|
|
90
|
+
- `cspViolation`: CSP 违规
|
|
91
|
+
- `websocket`: WebSocket 事件
|
|
68
92
|
|
|
69
|
-
##
|
|
93
|
+
## 3. 给数据加上用户和路由
|
|
70
94
|
|
|
71
95
|
```javascript
|
|
72
|
-
new BkOpenTelemetry({
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
'
|
|
96
|
+
const bkOT = new BkOpenTelemetry({
|
|
97
|
+
app: {
|
|
98
|
+
name: 'demo-app',
|
|
99
|
+
environment: 'production',
|
|
100
|
+
version: '1.0.0',
|
|
101
|
+
},
|
|
102
|
+
transport: {
|
|
103
|
+
endpoint: 'https://your-otlp-collector',
|
|
104
|
+
token: 'your-report-token',
|
|
81
105
|
},
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
106
|
+
user: {
|
|
107
|
+
id: window.USER_ID,
|
|
108
|
+
},
|
|
109
|
+
route: {
|
|
110
|
+
getPathGroup: url => new URL(url).pathname,
|
|
86
111
|
},
|
|
87
112
|
});
|
|
88
113
|
```
|
|
89
114
|
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
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`。
|
|
115
|
+
`route.getPathGroup` 用来把高基数 URL 归类成低基数路径。例如 `/user/123` 可以归类成 `/user/:id`,方便聚合分析。
|
|
116
|
+
|
|
117
|
+
运行过程中也可以更新上下文:
|
|
100
118
|
|
|
101
|
-
|
|
119
|
+
```javascript
|
|
120
|
+
bkOT.setUser({ id: 'user-001' });
|
|
102
121
|
|
|
103
|
-
|
|
122
|
+
bkOT.setView({
|
|
123
|
+
id: 'view-001',
|
|
124
|
+
url: location.href,
|
|
125
|
+
urlPathGroup: '/user/:id',
|
|
126
|
+
loadingType: 'route_change',
|
|
127
|
+
});
|
|
128
|
+
```
|
|
104
129
|
|
|
105
|
-
|
|
130
|
+
## 4. 按需打开更多能力
|
|
106
131
|
|
|
107
132
|
```javascript
|
|
108
133
|
new BkOpenTelemetry({
|
|
109
|
-
|
|
110
|
-
|
|
134
|
+
transport: {
|
|
135
|
+
endpoint: 'https://your-otlp-collector',
|
|
136
|
+
token: 'your-report-token',
|
|
137
|
+
},
|
|
111
138
|
rum: {
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
redact: ({ body, type }) => (type === 'request' ? body.replace(/password/g, '***') : body),
|
|
139
|
+
plugins: {
|
|
140
|
+
action: true,
|
|
141
|
+
longTask: { threshold: 50 },
|
|
142
|
+
routeTiming: true,
|
|
143
|
+
websocket: true,
|
|
144
|
+
cspViolation: true,
|
|
145
|
+
httpBody: {
|
|
146
|
+
maxBodySize: 4 * 1024,
|
|
147
|
+
redact: ({ body }) => body.replace(/password=[^&]+/g, 'password=***'),
|
|
148
|
+
},
|
|
149
|
+
blankScreen: {
|
|
150
|
+
rootSelector: '#app',
|
|
151
|
+
checkDelay: 3000,
|
|
152
|
+
threshold: 0.8,
|
|
153
|
+
ignoreSelectors: ['.app-loading', '.page-skeleton'],
|
|
154
|
+
},
|
|
129
155
|
},
|
|
130
156
|
},
|
|
131
157
|
});
|
|
132
158
|
```
|
|
133
159
|
|
|
134
|
-
|
|
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
|
-
完整配置示例:
|
|
160
|
+
如果要关闭默认能力,显式写 `false`:
|
|
163
161
|
|
|
164
162
|
```javascript
|
|
165
163
|
new BkOpenTelemetry({
|
|
166
|
-
|
|
167
|
-
|
|
164
|
+
transport: {
|
|
165
|
+
endpoint: 'https://your-otlp-collector',
|
|
166
|
+
token: 'your-report-token',
|
|
167
|
+
},
|
|
168
168
|
rum: {
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
// 自定义"加载中"占位选择器,命中样本会从分母里剔除,避免骨架屏被误判为白屏
|
|
177
|
-
ignoreSelectors: ['.app-loading', '.page-skeleton'],
|
|
169
|
+
plugins: {
|
|
170
|
+
webVitals: false,
|
|
171
|
+
blankScreen: false,
|
|
172
|
+
},
|
|
173
|
+
instrumentations: {
|
|
174
|
+
fetch: false,
|
|
175
|
+
xhr: false,
|
|
178
176
|
},
|
|
179
177
|
},
|
|
180
178
|
});
|
|
181
179
|
```
|
|
182
180
|
|
|
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
|
-
## 公共属性
|
|
181
|
+
## 5. 加公共属性和脱敏
|
|
200
182
|
|
|
201
|
-
|
|
183
|
+
公共属性适合放业务线、页面、环境等每条数据都想带上的信息。
|
|
202
184
|
|
|
203
185
|
```javascript
|
|
204
186
|
new BkOpenTelemetry({
|
|
205
|
-
|
|
206
|
-
|
|
187
|
+
transport: {
|
|
188
|
+
endpoint: 'https://your-otlp-collector',
|
|
189
|
+
token: 'your-report-token',
|
|
190
|
+
},
|
|
207
191
|
attributes: {
|
|
208
192
|
page: () => ({
|
|
209
|
-
'rum.page.host':
|
|
210
|
-
'rum.page.path':
|
|
193
|
+
'rum.page.host': location.host,
|
|
194
|
+
'rum.page.path': location.pathname,
|
|
211
195
|
}),
|
|
212
196
|
custom: () => ({
|
|
213
197
|
'biz.team': 'demo',
|
|
@@ -219,53 +203,32 @@ new BkOpenTelemetry({
|
|
|
219
203
|
'biz.metric_source': 'rum',
|
|
220
204
|
}),
|
|
221
205
|
},
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
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, '*');
|
|
206
|
+
privacy: {
|
|
207
|
+
redactUrl: url => url.replace(/token=[^&]+/g, 'token=***'),
|
|
208
|
+
redactAttributes: attributes => {
|
|
209
|
+
const next = { ...attributes };
|
|
210
|
+
for (const key of Object.keys(next)) {
|
|
211
|
+
if (typeof next[key] === 'string') {
|
|
212
|
+
next[key] = next[key].replace(/\b\d{11}\b/g, '***');
|
|
245
213
|
}
|
|
246
214
|
}
|
|
247
|
-
return
|
|
215
|
+
return next;
|
|
248
216
|
},
|
|
249
|
-
url: url => url.replace(/token=[^&]+/g, 'token=***'),
|
|
250
217
|
},
|
|
251
218
|
});
|
|
252
219
|
```
|
|
253
220
|
|
|
254
|
-
- `
|
|
255
|
-
- `
|
|
256
|
-
|
|
257
|
-
`
|
|
258
|
-
|
|
259
|
-
|
|
221
|
+
- `attributes.page`: 页面、接口、资源等 RUM 数据都会带上。
|
|
222
|
+
- `attributes.custom`: `reportCustomEvent` 上报的自定义事件会带上。
|
|
223
|
+
- `attributes.error`: 错误数据会带上。
|
|
224
|
+
- `attributes.metric`: 指标数据会带上。
|
|
225
|
+
- `privacy.redactUrl`: 处理 URL,例如 `url.full`、`document.referrer`。
|
|
226
|
+
- `privacy.redactAttributes`: 处理 span/log 属性。
|
|
227
|
+
- `rum.plugins.httpBody.redact`: 只处理请求/响应 body。
|
|
260
228
|
|
|
261
|
-
|
|
229
|
+
## 6. 手动上报业务事件
|
|
262
230
|
|
|
263
231
|
```javascript
|
|
264
|
-
const bkOT = new BkOpenTelemetry({
|
|
265
|
-
endpoint: 'https://your-otlp-collector',
|
|
266
|
-
token: 'your-report-token',
|
|
267
|
-
});
|
|
268
|
-
|
|
269
232
|
bkOT.reportCustomEvent({
|
|
270
233
|
name: 'order.checkout',
|
|
271
234
|
attributes: {
|
|
@@ -275,7 +238,7 @@ bkOT.reportCustomEvent({
|
|
|
275
238
|
});
|
|
276
239
|
```
|
|
277
240
|
|
|
278
|
-
|
|
241
|
+
失败事件可以带 `error`:
|
|
279
242
|
|
|
280
243
|
```javascript
|
|
281
244
|
bkOT.reportCustomEvent({
|
|
@@ -287,9 +250,93 @@ bkOT.reportCustomEvent({
|
|
|
287
250
|
});
|
|
288
251
|
```
|
|
289
252
|
|
|
290
|
-
|
|
253
|
+
也可以手动上报用户操作:
|
|
254
|
+
|
|
255
|
+
```javascript
|
|
256
|
+
bkOT.reportAction({
|
|
257
|
+
type: 'click',
|
|
258
|
+
targetText: '立即购买',
|
|
259
|
+
targetTag: 'button',
|
|
260
|
+
});
|
|
261
|
+
```
|
|
291
262
|
|
|
292
|
-
|
|
263
|
+
## 7. 控制采样、调试和上报
|
|
264
|
+
|
|
265
|
+
```javascript
|
|
266
|
+
new BkOpenTelemetry({
|
|
267
|
+
debug: true,
|
|
268
|
+
sampling: {
|
|
269
|
+
rate: 0.2,
|
|
270
|
+
},
|
|
271
|
+
spanProcessor: 'batch',
|
|
272
|
+
transport: {
|
|
273
|
+
endpoint: 'https://your-otlp-collector',
|
|
274
|
+
token: 'your-report-token',
|
|
275
|
+
headers: {
|
|
276
|
+
'X-Custom-Header': 'value',
|
|
277
|
+
},
|
|
278
|
+
signals: {
|
|
279
|
+
traces: {
|
|
280
|
+
timeoutMillis: 10000,
|
|
281
|
+
},
|
|
282
|
+
},
|
|
283
|
+
},
|
|
284
|
+
});
|
|
285
|
+
```
|
|
286
|
+
|
|
287
|
+
- `debug`: 打开后会在控制台输出调试信息。
|
|
288
|
+
- `sampling.rate`: 采样率,范围 `0 - 1`,默认 `1`。
|
|
289
|
+
- `spanProcessor`: 默认 `batch`;本地调试可用 `simple`。
|
|
290
|
+
- `transport.headers`: 给所有信号加请求头。
|
|
291
|
+
- `transport.signals.traces/metrics/logs`: 分别配置不同信号。
|
|
292
|
+
|
|
293
|
+
如果三类信号要发到不同地址:
|
|
294
|
+
|
|
295
|
+
```javascript
|
|
296
|
+
new BkOpenTelemetry({
|
|
297
|
+
transport: {
|
|
298
|
+
endpoint: 'https://your-otlp-collector',
|
|
299
|
+
token: 'your-report-token',
|
|
300
|
+
signals: {
|
|
301
|
+
endpoints: {
|
|
302
|
+
traces: 'https://your-trace-collector/v1/traces',
|
|
303
|
+
metrics: 'https://your-metric-collector/v1/metrics',
|
|
304
|
+
logs: 'https://your-log-collector/v1/logs',
|
|
305
|
+
},
|
|
306
|
+
},
|
|
307
|
+
},
|
|
308
|
+
});
|
|
309
|
+
```
|
|
310
|
+
|
|
311
|
+
请求头优先级为:`token` 默认头 < `transport.headers` < 单信号 `headers`。
|
|
312
|
+
|
|
313
|
+
## 8. 生命周期
|
|
314
|
+
|
|
315
|
+
```javascript
|
|
316
|
+
await bkOT.flush();
|
|
317
|
+
await bkOT.shutdown();
|
|
318
|
+
```
|
|
319
|
+
|
|
320
|
+
- `flush`: 立即刷新插件、trace、metric、log。
|
|
321
|
+
- `shutdown`: 刷新后关闭 SDK,并卸载事件监听。
|
|
322
|
+
|
|
323
|
+
如果你要先注册插件,再手动启动:
|
|
324
|
+
|
|
325
|
+
```javascript
|
|
326
|
+
const bkOT = new BkOpenTelemetry({
|
|
327
|
+
autoStart: false,
|
|
328
|
+
transport: {
|
|
329
|
+
endpoint: 'https://your-otlp-collector',
|
|
330
|
+
token: 'your-report-token',
|
|
331
|
+
},
|
|
332
|
+
});
|
|
333
|
+
|
|
334
|
+
await bkOT.start();
|
|
335
|
+
```
|
|
336
|
+
|
|
337
|
+
## 9. 扩展插件
|
|
338
|
+
|
|
339
|
+
普通用户通常不需要看这一节。只有当默认采集不够用时,再使用插件扩展。
|
|
293
340
|
|
|
294
341
|
```javascript
|
|
295
342
|
import { BkOpenTelemetry, createPlugin } from '@blueking/open-telemetry';
|
|
@@ -308,19 +355,23 @@ const plugin = createPlugin({
|
|
|
308
355
|
|
|
309
356
|
const bkOT = new BkOpenTelemetry({
|
|
310
357
|
autoStart: false,
|
|
311
|
-
endpoint: 'https://your-otlp-collector',
|
|
312
|
-
token: 'your-report-token',
|
|
313
358
|
plugins: [plugin],
|
|
359
|
+
transport: {
|
|
360
|
+
endpoint: 'https://your-otlp-collector',
|
|
361
|
+
token: 'your-report-token',
|
|
362
|
+
},
|
|
314
363
|
});
|
|
315
364
|
|
|
316
365
|
await bkOT.start();
|
|
317
366
|
```
|
|
318
367
|
|
|
319
|
-
|
|
368
|
+
也可以在 `start()` 前链式注册:
|
|
320
369
|
|
|
321
|
-
|
|
370
|
+
```javascript
|
|
371
|
+
bkOT.use(plugin);
|
|
372
|
+
```
|
|
322
373
|
|
|
323
|
-
|
|
374
|
+
## 10. 接入原生 OpenTelemetry Instrumentation
|
|
324
375
|
|
|
325
376
|
```javascript
|
|
326
377
|
import { BkOpenTelemetry } from '@blueking/open-telemetry';
|
|
@@ -328,61 +379,18 @@ import { MyCustomInstrumentation } from './my-custom-instrumentation';
|
|
|
328
379
|
|
|
329
380
|
const bkOT = new BkOpenTelemetry({
|
|
330
381
|
autoStart: false,
|
|
331
|
-
endpoint: 'https://your-otlp-collector',
|
|
332
|
-
token: 'your-report-token',
|
|
333
382
|
instrumentations: [new MyCustomInstrumentation()],
|
|
383
|
+
transport: {
|
|
384
|
+
endpoint: 'https://your-otlp-collector',
|
|
385
|
+
token: 'your-report-token',
|
|
386
|
+
},
|
|
334
387
|
});
|
|
335
388
|
|
|
336
389
|
await bkOT.start();
|
|
337
390
|
```
|
|
338
391
|
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
## 生命周期
|
|
392
|
+
也可以在 `start()` 前链式注册:
|
|
342
393
|
|
|
343
394
|
```javascript
|
|
344
|
-
|
|
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
|
-
});
|
|
395
|
+
bkOT.useInstrumentation(new MyCustomInstrumentation());
|
|
388
396
|
```
|