@blueking/open-telemetry 0.0.5 → 0.0.7
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 +254 -198
- package/dist/bk-rum.global.js +9 -8
- package/dist/bk-rum.global.js.map +1 -1
- package/dist/core/config.d.ts +134 -36
- package/dist/core/id.d.ts +8 -0
- package/dist/core/plugin.d.ts +24 -6
- 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 +14 -9
- package/dist/core/throttle.d.ts +20 -0
- package/dist/core/url.d.ts +4 -4
- package/dist/index.d.ts +6 -1
- package/dist/index.js +1397 -650
- 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 +16 -3
- package/dist/plugins/device.d.ts +20 -4
- package/dist/plugins/error.d.ts +2 -2
- package/dist/plugins/http-body.d.ts +2 -19
- package/dist/plugins/long-task.d.ts +12 -4
- 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 -6
- package/dist/plugins/session.d.ts +2 -6
- 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 +7 -3
package/README.md
CHANGED
|
@@ -1,165 +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
|
-
<script src="https://unpkg.com/@blueking/open-telemetry
|
|
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>`。
|
|
70
|
+
|
|
71
|
+
## 2. 先理解默认采集
|
|
72
|
+
|
|
73
|
+
不写 `rum` 配置时,SDK 已经默认开启常用 RUM 能力:
|
|
74
|
+
|
|
75
|
+
- 页面加载:`documentLoad`
|
|
76
|
+
- 页面访问:`pageView`
|
|
77
|
+
- 接口请求:`fetch`、`xhr`
|
|
78
|
+
- 静态资源:`resource`
|
|
79
|
+
- JS 错误:`error`
|
|
80
|
+
- 页面体验:`webVitals`、`blankScreen`
|
|
81
|
+
- 用户环境:`device`、`session`
|
|
82
|
+
- 用户交互 instrumentation:`userInteraction`
|
|
83
|
+
|
|
84
|
+
默认关闭的能力需要手动打开:
|
|
85
|
+
|
|
86
|
+
- `action`: 用户点击、输入、提交等操作事件
|
|
87
|
+
- `httpBody`: 请求/响应 body 采集
|
|
88
|
+
- `longTask`: 长任务
|
|
89
|
+
- `routeTiming`: 路由耗时
|
|
90
|
+
- `cspViolation`: CSP 违规
|
|
91
|
+
- `websocket`: WebSocket 事件
|
|
92
|
+
|
|
93
|
+
## 3. 给数据加上用户和路由
|
|
48
94
|
|
|
49
95
|
```javascript
|
|
50
|
-
new BkOpenTelemetry({
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
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',
|
|
105
|
+
},
|
|
106
|
+
user: {
|
|
107
|
+
id: window.USER_ID,
|
|
108
|
+
},
|
|
109
|
+
route: {
|
|
110
|
+
getPathGroup: url => new URL(url).pathname,
|
|
57
111
|
},
|
|
58
112
|
});
|
|
59
113
|
```
|
|
60
114
|
|
|
61
|
-
`
|
|
115
|
+
`route.getPathGroup` 用来把高基数 URL 归类成低基数路径。例如 `/user/123` 可以归类成 `/user/:id`,方便聚合分析。
|
|
116
|
+
|
|
117
|
+
运行过程中也可以更新上下文:
|
|
62
118
|
|
|
63
|
-
|
|
119
|
+
```javascript
|
|
120
|
+
bkOT.setUser({ id: 'user-001' });
|
|
64
121
|
|
|
65
|
-
|
|
66
|
-
|
|
122
|
+
bkOT.setView({
|
|
123
|
+
id: 'view-001',
|
|
124
|
+
url: location.href,
|
|
125
|
+
urlPathGroup: '/user/:id',
|
|
126
|
+
loadingType: 'route_change',
|
|
127
|
+
});
|
|
67
128
|
```
|
|
68
129
|
|
|
69
|
-
##
|
|
130
|
+
## 4. 按需打开更多能力
|
|
70
131
|
|
|
71
132
|
```javascript
|
|
72
133
|
new BkOpenTelemetry({
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
debug: false,
|
|
77
|
-
sampleRate: 1,
|
|
78
|
-
headers: {
|
|
79
|
-
'X-Custom-Header': 'value',
|
|
134
|
+
transport: {
|
|
135
|
+
endpoint: 'https://your-otlp-collector',
|
|
136
|
+
token: 'your-report-token',
|
|
80
137
|
},
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
138
|
+
rum: {
|
|
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
|
+
},
|
|
155
|
+
},
|
|
85
156
|
},
|
|
86
157
|
});
|
|
87
158
|
```
|
|
88
159
|
|
|
89
|
-
|
|
90
|
-
- `token`: 上报 token,会写入 `Authorization: Bearer xxx`。
|
|
91
|
-
- `environment`: 环境名,默认 `production`。
|
|
92
|
-
- `enabled`: 是否启用 SDK,默认 `true`。
|
|
93
|
-
- `debug`: 是否在控制台打印上报数据,默认 `false`。
|
|
94
|
-
- `sampleRate`: 采样率,范围 `0 - 1`,默认 `1`。
|
|
95
|
-
- `headers`: 附加上报请求头,显式传入的 header 可覆盖 token 生成的同名 header。
|
|
96
|
-
- `signalEndpoints`: 分别配置 trace、metric、log 的完整上报地址,优先级高于统一 `endpoint`。
|
|
97
|
-
- `traces` / `metrics` / `logs`: 分别配置 trace、metric、log 的高级请求参数;其中 `endpoint` 优先级低于 `signalEndpoints`。
|
|
98
|
-
|
|
99
|
-
## RUM 采集配置
|
|
100
|
-
|
|
101
|
-
所有采集能力统一放在 `rum` 下配置:
|
|
160
|
+
如果要关闭默认能力,显式写 `false`:
|
|
102
161
|
|
|
103
162
|
```javascript
|
|
104
163
|
new BkOpenTelemetry({
|
|
105
|
-
|
|
106
|
-
|
|
164
|
+
transport: {
|
|
165
|
+
endpoint: 'https://your-otlp-collector',
|
|
166
|
+
token: 'your-report-token',
|
|
167
|
+
},
|
|
107
168
|
rum: {
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
blankScreen: true,
|
|
116
|
-
device: true,
|
|
117
|
-
session: true,
|
|
118
|
-
websocket: true,
|
|
119
|
-
longTask: { threshold: 50 },
|
|
120
|
-
routeTiming: true,
|
|
121
|
-
cspViolation: true,
|
|
122
|
-
httpBody: {
|
|
123
|
-
maxBodySize: 4 * 1024,
|
|
124
|
-
redact: ({ body, type }) => (type === 'request' ? body.replace(/password/g, '***') : body),
|
|
169
|
+
plugins: {
|
|
170
|
+
webVitals: false,
|
|
171
|
+
blankScreen: false,
|
|
172
|
+
},
|
|
173
|
+
instrumentations: {
|
|
174
|
+
fetch: false,
|
|
175
|
+
xhr: false,
|
|
125
176
|
},
|
|
126
177
|
},
|
|
127
178
|
});
|
|
128
179
|
```
|
|
129
180
|
|
|
130
|
-
|
|
181
|
+
## 5. 加公共属性和脱敏
|
|
131
182
|
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
常用说明:
|
|
135
|
-
|
|
136
|
-
- `documentLoad`: 采集页面加载性能。
|
|
137
|
-
- `fetch` / `xhr`: 采集 HTTP 请求,并给同源请求注入 `traceparent`。
|
|
138
|
-
- `userInteraction`: 采集用户交互。
|
|
139
|
-
- `pageView`: 采集页面访问和 SPA 路由变化。
|
|
140
|
-
- `error`: 采集 JS 错误、未处理 Promise rejection、资源加载失败。
|
|
141
|
-
- `webVitals`: 采集 LCP、CLS、INP、FCP、TTFB。
|
|
142
|
-
- `blankScreen`: 检测白屏。
|
|
143
|
-
- `device`: 采集浏览器、系统、网络等设备信息。
|
|
144
|
-
- `session`: 维护匿名会话。
|
|
145
|
-
- `websocket`: 采集 WebSocket 连接和消息事件。
|
|
146
|
-
- `longTask`: 采集长任务,`threshold` 表示最小时长阈值。
|
|
147
|
-
- `routeTiming`: 采集 SPA 路由切换耗时。
|
|
148
|
-
- `cspViolation`: 采集 CSP violation。
|
|
149
|
-
- `httpBody`: 采集请求/响应 body,建议配合 `redact` 做脱敏。
|
|
150
|
-
|
|
151
|
-
## 公共属性
|
|
152
|
-
|
|
153
|
-
通过 `attributes` 给不同类型数据补充公共属性:
|
|
183
|
+
公共属性适合放业务线、页面、环境等每条数据都想带上的信息。
|
|
154
184
|
|
|
155
185
|
```javascript
|
|
156
186
|
new BkOpenTelemetry({
|
|
157
|
-
|
|
158
|
-
|
|
187
|
+
transport: {
|
|
188
|
+
endpoint: 'https://your-otlp-collector',
|
|
189
|
+
token: 'your-report-token',
|
|
190
|
+
},
|
|
159
191
|
attributes: {
|
|
160
192
|
page: () => ({
|
|
161
|
-
'rum.page.host':
|
|
162
|
-
'rum.page.path':
|
|
193
|
+
'rum.page.host': location.host,
|
|
194
|
+
'rum.page.path': location.pathname,
|
|
163
195
|
}),
|
|
164
196
|
custom: () => ({
|
|
165
197
|
'biz.team': 'demo',
|
|
@@ -171,53 +203,32 @@ new BkOpenTelemetry({
|
|
|
171
203
|
'biz.metric_source': 'rum',
|
|
172
204
|
}),
|
|
173
205
|
},
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
## 脱敏配置
|
|
183
|
-
|
|
184
|
-
通过 `redact` 统一处理敏感数据:
|
|
185
|
-
|
|
186
|
-
```javascript
|
|
187
|
-
new BkOpenTelemetry({
|
|
188
|
-
endpoint: 'https://your-otlp-collector',
|
|
189
|
-
token: 'your-report-token',
|
|
190
|
-
redact: {
|
|
191
|
-
attributes: attributes => {
|
|
192
|
-
const result = { ...attributes };
|
|
193
|
-
for (const key of Object.keys(result)) {
|
|
194
|
-
const value = result[key];
|
|
195
|
-
if (typeof value === 'string' && /\b\d{15}|\d{18}\b/.test(value)) {
|
|
196
|
-
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, '***');
|
|
197
213
|
}
|
|
198
214
|
}
|
|
199
|
-
return
|
|
215
|
+
return next;
|
|
200
216
|
},
|
|
201
|
-
url: url => url.replace(/token=[^&]+/g, 'token=***'),
|
|
202
217
|
},
|
|
203
218
|
});
|
|
204
219
|
```
|
|
205
220
|
|
|
206
|
-
- `
|
|
207
|
-
- `
|
|
208
|
-
|
|
209
|
-
`
|
|
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。
|
|
210
228
|
|
|
211
|
-
##
|
|
212
|
-
|
|
213
|
-
`new BkOpenTelemetry()` 会返回 SDK 实例,可以通过 `reportCustomEvent` 上报业务自定义事件:
|
|
229
|
+
## 6. 手动上报业务事件
|
|
214
230
|
|
|
215
231
|
```javascript
|
|
216
|
-
const bkOT = new BkOpenTelemetry({
|
|
217
|
-
endpoint: 'https://your-otlp-collector',
|
|
218
|
-
token: 'your-report-token',
|
|
219
|
-
});
|
|
220
|
-
|
|
221
232
|
bkOT.reportCustomEvent({
|
|
222
233
|
name: 'order.checkout',
|
|
223
234
|
attributes: {
|
|
@@ -227,7 +238,7 @@ bkOT.reportCustomEvent({
|
|
|
227
238
|
});
|
|
228
239
|
```
|
|
229
240
|
|
|
230
|
-
|
|
241
|
+
失败事件可以带 `error`:
|
|
231
242
|
|
|
232
243
|
```javascript
|
|
233
244
|
bkOT.reportCustomEvent({
|
|
@@ -239,9 +250,93 @@ bkOT.reportCustomEvent({
|
|
|
239
250
|
});
|
|
240
251
|
```
|
|
241
252
|
|
|
242
|
-
|
|
253
|
+
也可以手动上报用户操作:
|
|
254
|
+
|
|
255
|
+
```javascript
|
|
256
|
+
bkOT.reportAction({
|
|
257
|
+
type: 'click',
|
|
258
|
+
targetText: '立即购买',
|
|
259
|
+
targetTag: 'button',
|
|
260
|
+
});
|
|
261
|
+
```
|
|
262
|
+
|
|
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`。
|
|
243
312
|
|
|
244
|
-
|
|
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
|
+
普通用户通常不需要看这一节。只有当默认采集不够用时,再使用插件扩展。
|
|
245
340
|
|
|
246
341
|
```javascript
|
|
247
342
|
import { BkOpenTelemetry, createPlugin } from '@blueking/open-telemetry';
|
|
@@ -260,19 +355,23 @@ const plugin = createPlugin({
|
|
|
260
355
|
|
|
261
356
|
const bkOT = new BkOpenTelemetry({
|
|
262
357
|
autoStart: false,
|
|
263
|
-
endpoint: 'https://your-otlp-collector',
|
|
264
|
-
token: 'your-report-token',
|
|
265
358
|
plugins: [plugin],
|
|
359
|
+
transport: {
|
|
360
|
+
endpoint: 'https://your-otlp-collector',
|
|
361
|
+
token: 'your-report-token',
|
|
362
|
+
},
|
|
266
363
|
});
|
|
267
364
|
|
|
268
365
|
await bkOT.start();
|
|
269
366
|
```
|
|
270
367
|
|
|
271
|
-
|
|
368
|
+
也可以在 `start()` 前链式注册:
|
|
272
369
|
|
|
273
|
-
|
|
370
|
+
```javascript
|
|
371
|
+
bkOT.use(plugin);
|
|
372
|
+
```
|
|
274
373
|
|
|
275
|
-
|
|
374
|
+
## 10. 接入原生 OpenTelemetry Instrumentation
|
|
276
375
|
|
|
277
376
|
```javascript
|
|
278
377
|
import { BkOpenTelemetry } from '@blueking/open-telemetry';
|
|
@@ -280,61 +379,18 @@ import { MyCustomInstrumentation } from './my-custom-instrumentation';
|
|
|
280
379
|
|
|
281
380
|
const bkOT = new BkOpenTelemetry({
|
|
282
381
|
autoStart: false,
|
|
283
|
-
endpoint: 'https://your-otlp-collector',
|
|
284
|
-
token: 'your-report-token',
|
|
285
382
|
instrumentations: [new MyCustomInstrumentation()],
|
|
383
|
+
transport: {
|
|
384
|
+
endpoint: 'https://your-otlp-collector',
|
|
385
|
+
token: 'your-report-token',
|
|
386
|
+
},
|
|
286
387
|
});
|
|
287
388
|
|
|
288
389
|
await bkOT.start();
|
|
289
390
|
```
|
|
290
391
|
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
## 生命周期
|
|
392
|
+
也可以在 `start()` 前链式注册:
|
|
294
393
|
|
|
295
394
|
```javascript
|
|
296
|
-
|
|
297
|
-
endpoint: 'https://your-otlp-collector',
|
|
298
|
-
token: 'your-report-token',
|
|
299
|
-
});
|
|
300
|
-
|
|
301
|
-
await bkOT.flush();
|
|
302
|
-
await bkOT.shutdown();
|
|
303
|
-
```
|
|
304
|
-
|
|
305
|
-
- `flush`: 立即刷新插件、trace、metric、log。
|
|
306
|
-
- `shutdown`: 刷新后关闭 SDK,并卸载事件监听。
|
|
307
|
-
|
|
308
|
-
## 完整示例
|
|
309
|
-
|
|
310
|
-
```javascript
|
|
311
|
-
import { BkOpenTelemetry } from '@blueking/open-telemetry';
|
|
312
|
-
|
|
313
|
-
export const bkOT = new BkOpenTelemetry({
|
|
314
|
-
endpoint: 'https://your-otlp-collector',
|
|
315
|
-
token: 'your-report-token',
|
|
316
|
-
environment: 'production',
|
|
317
|
-
debug: false,
|
|
318
|
-
sampleRate: 1,
|
|
319
|
-
rum: {
|
|
320
|
-
fetch: true,
|
|
321
|
-
xhr: true,
|
|
322
|
-
documentLoad: true,
|
|
323
|
-
userInteraction: true,
|
|
324
|
-
longTask: { threshold: 50 },
|
|
325
|
-
routeTiming: true,
|
|
326
|
-
httpBody: {
|
|
327
|
-
maxBodySize: 4 * 1024,
|
|
328
|
-
redact: ({ body }) => body.replace(/("password"\s*:\s*)"[^"]*"/g, '$1"***"'),
|
|
329
|
-
},
|
|
330
|
-
},
|
|
331
|
-
attributes: {
|
|
332
|
-
custom: () => ({
|
|
333
|
-
'biz.team': 'rum-demo',
|
|
334
|
-
}),
|
|
335
|
-
},
|
|
336
|
-
redact: {
|
|
337
|
-
url: url => url.replace(/token=[^&]+/g, 'token=***'),
|
|
338
|
-
},
|
|
339
|
-
});
|
|
395
|
+
bkOT.useInstrumentation(new MyCustomInstrumentation());
|
|
340
396
|
```
|