@blueking/open-telemetry 0.0.1 → 0.0.3
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/package.json +4 -1
- package/playground/App.vue +0 -148
- package/playground/bk-ot.ts +0 -110
- package/playground/components/DemoLogPanel.vue +0 -89
- package/playground/composables/use-demo-log.ts +0 -39
- package/playground/index.html +0 -13
- package/playground/main.ts +0 -36
- package/playground/mock/index.ts +0 -43
- package/playground/pages/BlankScreenDemo.vue +0 -97
- package/playground/pages/BusinessDemo.vue +0 -74
- package/playground/pages/CustomPluginDemo.vue +0 -104
- package/playground/pages/ErrorDemo.vue +0 -75
- package/playground/pages/Home.vue +0 -133
- package/playground/pages/HttpDemo.vue +0 -120
- package/playground/pages/LongTaskDemo.vue +0 -66
- package/playground/pages/WebSocketDemo.vue +0 -100
- package/playground/router/index.ts +0 -92
- package/playground/style.css +0 -180
- package/src/browser.ts +0 -44
- package/src/core/config.ts +0 -327
- package/src/core/plugin.ts +0 -97
- package/src/core/processor.ts +0 -74
- package/src/core/route-observer.ts +0 -128
- package/src/core/sampling.ts +0 -64
- package/src/core/sdk.ts +0 -327
- package/src/core/url.ts +0 -66
- package/src/plugins/blank-screen.ts +0 -170
- package/src/plugins/common.ts +0 -131
- package/src/plugins/csp-violation.ts +0 -74
- package/src/plugins/device.ts +0 -91
- package/src/plugins/error.ts +0 -211
- package/src/plugins/http-body.ts +0 -437
- package/src/plugins/long-task.ts +0 -99
- package/src/plugins/page-view.ts +0 -83
- package/src/plugins/route-timing.ts +0 -89
- package/src/plugins/session.ts +0 -127
- package/src/plugins/web-vitals.ts +0 -159
- package/src/plugins/websocket.ts +0 -179
- package/tsconfig.app.json +0 -27
- package/tsconfig.dts.json +0 -13
- package/tsconfig.json +0 -7
- package/tsconfig.node.json +0 -26
- package/vite.config.ts +0 -70
package/src/plugins/session.ts
DELETED
|
@@ -1,127 +0,0 @@
|
|
|
1
|
-
/*
|
|
2
|
-
* Tencent is pleased to support the open source community by making
|
|
3
|
-
* 蓝鲸智云PaaS平台 (BlueKing PaaS) available.
|
|
4
|
-
*
|
|
5
|
-
* Copyright (C) 2017-2025 Tencent. All rights reserved.
|
|
6
|
-
*
|
|
7
|
-
* 蓝鲸智云PaaS平台 (BlueKing PaaS) is licensed under the MIT License.
|
|
8
|
-
*
|
|
9
|
-
* License for 蓝鲸智云PaaS平台 (BlueKing PaaS):
|
|
10
|
-
*
|
|
11
|
-
* ---------------------------------------------------
|
|
12
|
-
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
|
|
13
|
-
* documentation files (the "Software"), to deal in the Software without restriction, including without limitation
|
|
14
|
-
* the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and
|
|
15
|
-
* to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
|
16
|
-
*
|
|
17
|
-
* The above copyright notice and this permission notice shall be included in all copies or substantial portions of
|
|
18
|
-
* the Software.
|
|
19
|
-
*
|
|
20
|
-
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO
|
|
21
|
-
* THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
22
|
-
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
|
|
23
|
-
* CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
|
|
24
|
-
* IN THE SOFTWARE.
|
|
25
|
-
*/
|
|
26
|
-
|
|
27
|
-
import type { BkOTRumConfig } from '../core/config';
|
|
28
|
-
import type { BkOTPlugin } from '../core/plugin';
|
|
29
|
-
|
|
30
|
-
const DEFAULT_SESSION_STORAGE_KEY = 'bk_ot_session';
|
|
31
|
-
const DEFAULT_INACTIVITY_MS = 30 * 60 * 1000;
|
|
32
|
-
|
|
33
|
-
interface SessionRecord {
|
|
34
|
-
id: string;
|
|
35
|
-
// 会话上次活跃时间戳
|
|
36
|
-
ts: number;
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
const createId = () => {
|
|
40
|
-
if (typeof crypto !== 'undefined' && typeof crypto.randomUUID === 'function') {
|
|
41
|
-
return crypto.randomUUID();
|
|
42
|
-
}
|
|
43
|
-
return `session-${Date.now()}-${Math.random().toString(16).slice(2)}`;
|
|
44
|
-
};
|
|
45
|
-
|
|
46
|
-
const readRecord = (storageKey: string): null | SessionRecord => {
|
|
47
|
-
try {
|
|
48
|
-
const raw = window.sessionStorage.getItem(storageKey) ?? window.localStorage.getItem(storageKey);
|
|
49
|
-
if (!raw) {
|
|
50
|
-
return null;
|
|
51
|
-
}
|
|
52
|
-
const parsed = JSON.parse(raw) as SessionRecord;
|
|
53
|
-
if (typeof parsed?.id === 'string' && typeof parsed?.ts === 'number') {
|
|
54
|
-
return parsed;
|
|
55
|
-
}
|
|
56
|
-
return null;
|
|
57
|
-
} catch {
|
|
58
|
-
return null;
|
|
59
|
-
}
|
|
60
|
-
};
|
|
61
|
-
|
|
62
|
-
const writeRecord = (storageKey: string, record: SessionRecord) => {
|
|
63
|
-
try {
|
|
64
|
-
const serialized = JSON.stringify(record);
|
|
65
|
-
window.sessionStorage.setItem(storageKey, serialized);
|
|
66
|
-
// 同时写到 localStorage,让多 tab / 关闭页后短期内复用同一个 session
|
|
67
|
-
window.localStorage.setItem(storageKey, serialized);
|
|
68
|
-
} catch {
|
|
69
|
-
/* storage 不可用时忽略,运行期仍能继续 */
|
|
70
|
-
}
|
|
71
|
-
};
|
|
72
|
-
|
|
73
|
-
const resolveSessionId = (storageKey: string, inactivityMs: number) => {
|
|
74
|
-
const now = Date.now();
|
|
75
|
-
const existed = readRecord(storageKey);
|
|
76
|
-
if (existed && now - existed.ts < inactivityMs) {
|
|
77
|
-
const record: SessionRecord = { id: existed.id, ts: now };
|
|
78
|
-
writeRecord(storageKey, record);
|
|
79
|
-
return record.id;
|
|
80
|
-
}
|
|
81
|
-
const record: SessionRecord = { id: createId(), ts: now };
|
|
82
|
-
writeRecord(storageKey, record);
|
|
83
|
-
return record.id;
|
|
84
|
-
};
|
|
85
|
-
|
|
86
|
-
/**
|
|
87
|
-
* 会话级标识:带不活跃过期时间,超过 inactivityMs 视为新会话。
|
|
88
|
-
* 与 device 插件互补:device 是设备终身标识,session 反映"用户连续访问"语义。
|
|
89
|
-
*/
|
|
90
|
-
export const createSessionPlugin = (option: BkOTRumConfig['session']): BkOTPlugin => {
|
|
91
|
-
let teardown: (() => void) | undefined;
|
|
92
|
-
|
|
93
|
-
return {
|
|
94
|
-
name: 'session',
|
|
95
|
-
enabled: Boolean(option),
|
|
96
|
-
init(context) {
|
|
97
|
-
const storageKey =
|
|
98
|
-
typeof option === 'object' ? (option.storageKey ?? DEFAULT_SESSION_STORAGE_KEY) : DEFAULT_SESSION_STORAGE_KEY;
|
|
99
|
-
const inactivityMs =
|
|
100
|
-
typeof option === 'object' ? (option.inactivityMs ?? DEFAULT_INACTIVITY_MS) : DEFAULT_INACTIVITY_MS;
|
|
101
|
-
|
|
102
|
-
const refresh = () => {
|
|
103
|
-
const sessionId = typeof window === 'undefined' ? createId() : resolveSessionId(storageKey, inactivityMs);
|
|
104
|
-
context.setRuntimeAttributes({ 'session.id': sessionId });
|
|
105
|
-
};
|
|
106
|
-
|
|
107
|
-
refresh();
|
|
108
|
-
|
|
109
|
-
if (typeof window === 'undefined' || typeof document === 'undefined') {
|
|
110
|
-
return;
|
|
111
|
-
}
|
|
112
|
-
|
|
113
|
-
// 用户回到页面时检查是否过期,过期则轮换 sessionId
|
|
114
|
-
const onVisibilityChange = () => {
|
|
115
|
-
if (document.visibilityState === 'visible') {
|
|
116
|
-
refresh();
|
|
117
|
-
}
|
|
118
|
-
};
|
|
119
|
-
document.addEventListener('visibilitychange', onVisibilityChange);
|
|
120
|
-
teardown = () => document.removeEventListener('visibilitychange', onVisibilityChange);
|
|
121
|
-
},
|
|
122
|
-
shutdown() {
|
|
123
|
-
teardown?.();
|
|
124
|
-
teardown = undefined;
|
|
125
|
-
},
|
|
126
|
-
};
|
|
127
|
-
};
|
|
@@ -1,159 +0,0 @@
|
|
|
1
|
-
/*
|
|
2
|
-
* Tencent is pleased to support the open source community by making
|
|
3
|
-
* 蓝鲸智云PaaS平台 (BlueKing PaaS) available.
|
|
4
|
-
*
|
|
5
|
-
* Copyright (C) 2017-2025 Tencent. All rights reserved.
|
|
6
|
-
*
|
|
7
|
-
* 蓝鲸智云PaaS平台 (BlueKing PaaS) is licensed under the MIT License.
|
|
8
|
-
*
|
|
9
|
-
* License for 蓝鲸智云PaaS平台 (BlueKing PaaS):
|
|
10
|
-
*
|
|
11
|
-
* ---------------------------------------------------
|
|
12
|
-
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
|
|
13
|
-
* documentation files (the "Software"), to deal in the Software without restriction, including without limitation
|
|
14
|
-
* the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and
|
|
15
|
-
* to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
|
16
|
-
*
|
|
17
|
-
* The above copyright notice and this permission notice shall be included in all copies or substantial portions of
|
|
18
|
-
* the Software.
|
|
19
|
-
*
|
|
20
|
-
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO
|
|
21
|
-
* THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
22
|
-
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
|
|
23
|
-
* CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
|
|
24
|
-
* IN THE SOFTWARE.
|
|
25
|
-
*/
|
|
26
|
-
|
|
27
|
-
import type { BkOTRumConfig } from '../core/config';
|
|
28
|
-
import type { BkOTPlugin } from '../core/plugin';
|
|
29
|
-
import type { Attributes } from '@opentelemetry/api';
|
|
30
|
-
|
|
31
|
-
interface VitalLike {
|
|
32
|
-
attribution?: Record<string, unknown>;
|
|
33
|
-
id?: string;
|
|
34
|
-
name: string;
|
|
35
|
-
rating?: string;
|
|
36
|
-
value: number;
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
const getNavigationType = () => {
|
|
40
|
-
const navigation = performance.getEntriesByType('navigation')[0] as PerformanceNavigationTiming | undefined;
|
|
41
|
-
|
|
42
|
-
return navigation?.type || 'unknown';
|
|
43
|
-
};
|
|
44
|
-
|
|
45
|
-
// 仅挑选低基数、可枚举的字段进 metric attributes,避免指标维度爆炸
|
|
46
|
-
const getMetricDimensions = (metric: VitalLike): Attributes => ({
|
|
47
|
-
'rum.navigation.type': getNavigationType(),
|
|
48
|
-
'web_vital.name': metric.name,
|
|
49
|
-
'web_vital.rating': metric.rating,
|
|
50
|
-
});
|
|
51
|
-
|
|
52
|
-
// 把 attribution 中价值最高的字段抽出来(仅放进 span/log,不放进 metric)
|
|
53
|
-
const pickAttribution = (metric: VitalLike, redactUrl: (url: string) => string): Attributes => {
|
|
54
|
-
const attr = metric.attribution ?? {};
|
|
55
|
-
const result: Attributes = {};
|
|
56
|
-
|
|
57
|
-
const setIfDefined = (key: string, value: unknown, transform?: (value: string) => string) => {
|
|
58
|
-
if (value === undefined || value === null) return;
|
|
59
|
-
if (typeof value === 'string' || typeof value === 'number' || typeof value === 'boolean') {
|
|
60
|
-
result[key] = typeof value === 'string' && transform ? transform(value) : value;
|
|
61
|
-
}
|
|
62
|
-
};
|
|
63
|
-
|
|
64
|
-
switch (metric.name) {
|
|
65
|
-
case 'LCP':
|
|
66
|
-
setIfDefined('web_vital.lcp.url', attr.url, redactUrl);
|
|
67
|
-
setIfDefined('web_vital.lcp.target', attr.target);
|
|
68
|
-
setIfDefined('web_vital.lcp.element_render_delay', attr.elementRenderDelay);
|
|
69
|
-
setIfDefined('web_vital.lcp.resource_load_duration', attr.resourceLoadDuration);
|
|
70
|
-
setIfDefined('web_vital.lcp.time_to_first_byte', attr.timeToFirstByte);
|
|
71
|
-
break;
|
|
72
|
-
case 'CLS':
|
|
73
|
-
setIfDefined('web_vital.cls.largest_shift_target', attr.largestShiftTarget);
|
|
74
|
-
setIfDefined('web_vital.cls.largest_shift_value', attr.largestShiftValue);
|
|
75
|
-
setIfDefined('web_vital.cls.load_state', attr.loadState);
|
|
76
|
-
break;
|
|
77
|
-
case 'INP':
|
|
78
|
-
setIfDefined('web_vital.inp.interaction_target', attr.interactionTarget);
|
|
79
|
-
setIfDefined('web_vital.inp.interaction_type', attr.interactionType);
|
|
80
|
-
setIfDefined('web_vital.inp.input_delay', attr.inputDelay);
|
|
81
|
-
setIfDefined('web_vital.inp.processing_duration', attr.processingDuration);
|
|
82
|
-
setIfDefined('web_vital.inp.presentation_delay', attr.presentationDelay);
|
|
83
|
-
break;
|
|
84
|
-
case 'FCP':
|
|
85
|
-
setIfDefined('web_vital.fcp.time_to_first_byte', attr.timeToFirstByte);
|
|
86
|
-
setIfDefined('web_vital.fcp.load_state', attr.loadState);
|
|
87
|
-
break;
|
|
88
|
-
case 'TTFB':
|
|
89
|
-
setIfDefined('web_vital.ttfb.waiting_duration', attr.waitingDuration);
|
|
90
|
-
setIfDefined('web_vital.ttfb.dns_duration', attr.dnsDuration);
|
|
91
|
-
setIfDefined('web_vital.ttfb.connection_duration', attr.connectionDuration);
|
|
92
|
-
setIfDefined('web_vital.ttfb.request_duration', attr.requestDuration);
|
|
93
|
-
break;
|
|
94
|
-
default:
|
|
95
|
-
break;
|
|
96
|
-
}
|
|
97
|
-
return result;
|
|
98
|
-
};
|
|
99
|
-
|
|
100
|
-
export const createWebVitalsPlugin = (enabled: BkOTRumConfig['webVitals']): BkOTPlugin => ({
|
|
101
|
-
name: 'web-vitals',
|
|
102
|
-
enabled: Boolean(enabled),
|
|
103
|
-
async init(context) {
|
|
104
|
-
if (typeof window === 'undefined') {
|
|
105
|
-
return;
|
|
106
|
-
}
|
|
107
|
-
|
|
108
|
-
const { onCLS, onFCP, onINP, onLCP, onTTFB } = await import('web-vitals/attribution');
|
|
109
|
-
const clsHistogram = context.meter.createHistogram('browser.web_vital.cls', {
|
|
110
|
-
unit: '1',
|
|
111
|
-
description: 'Cumulative Layout Shift',
|
|
112
|
-
});
|
|
113
|
-
const durationHistogram = context.meter.createHistogram('browser.web_vital.duration', {
|
|
114
|
-
unit: 'ms',
|
|
115
|
-
description: 'Web Vitals duration metrics, including FCP, INP, LCP and TTFB',
|
|
116
|
-
});
|
|
117
|
-
|
|
118
|
-
const recordSpan = (metric: VitalLike) => {
|
|
119
|
-
const span = context.startSpan('browser.web_vital', {
|
|
120
|
-
...context.config.getPageAttributes(),
|
|
121
|
-
...context.config.getMetricAttributes(),
|
|
122
|
-
...getMetricDimensions(metric),
|
|
123
|
-
// 单条度量唯一 ID 仅放进 span,避免污染 metric 维度
|
|
124
|
-
'web_vital.id': metric.id,
|
|
125
|
-
'web_vital.value': metric.value,
|
|
126
|
-
...pickAttribution(metric, context.config.redactUrl),
|
|
127
|
-
});
|
|
128
|
-
span.end();
|
|
129
|
-
};
|
|
130
|
-
|
|
131
|
-
const recordDurationMetric = (metric: VitalLike) => {
|
|
132
|
-
durationHistogram.record(
|
|
133
|
-
metric.value,
|
|
134
|
-
context.applyRedact({
|
|
135
|
-
...context.config.getPageAttributes(),
|
|
136
|
-
...context.config.getMetricAttributes(),
|
|
137
|
-
...getMetricDimensions(metric),
|
|
138
|
-
})
|
|
139
|
-
);
|
|
140
|
-
recordSpan(metric);
|
|
141
|
-
};
|
|
142
|
-
|
|
143
|
-
onCLS(metric => {
|
|
144
|
-
clsHistogram.record(
|
|
145
|
-
metric.value,
|
|
146
|
-
context.applyRedact({
|
|
147
|
-
...context.config.getPageAttributes(),
|
|
148
|
-
...context.config.getMetricAttributes(),
|
|
149
|
-
...getMetricDimensions(metric),
|
|
150
|
-
})
|
|
151
|
-
);
|
|
152
|
-
recordSpan(metric);
|
|
153
|
-
});
|
|
154
|
-
onFCP(recordDurationMetric);
|
|
155
|
-
onINP(recordDurationMetric);
|
|
156
|
-
onLCP(recordDurationMetric);
|
|
157
|
-
onTTFB(recordDurationMetric);
|
|
158
|
-
},
|
|
159
|
-
});
|
package/src/plugins/websocket.ts
DELETED
|
@@ -1,179 +0,0 @@
|
|
|
1
|
-
/*
|
|
2
|
-
* Tencent is pleased to support the open source community by making
|
|
3
|
-
* 蓝鲸智云PaaS平台 (BlueKing PaaS) available.
|
|
4
|
-
*
|
|
5
|
-
* Copyright (C) 2017-2025 Tencent. All rights reserved.
|
|
6
|
-
*
|
|
7
|
-
* 蓝鲸智云PaaS平台 (BlueKing PaaS) is licensed under the MIT License.
|
|
8
|
-
*
|
|
9
|
-
* License for 蓝鲸智云PaaS平台 (BlueKing PaaS):
|
|
10
|
-
*
|
|
11
|
-
* ---------------------------------------------------
|
|
12
|
-
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
|
|
13
|
-
* documentation files (the "Software"), to deal in the Software without restriction, including without limitation
|
|
14
|
-
* the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and
|
|
15
|
-
* to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
|
16
|
-
*
|
|
17
|
-
* The above copyright notice and this permission notice shall be included in all copies or substantial portions of
|
|
18
|
-
* the Software.
|
|
19
|
-
*
|
|
20
|
-
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO
|
|
21
|
-
* THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
22
|
-
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
|
|
23
|
-
* CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
|
|
24
|
-
* IN THE SOFTWARE.
|
|
25
|
-
*/
|
|
26
|
-
|
|
27
|
-
import { SpanStatusCode } from '@opentelemetry/api';
|
|
28
|
-
import { SeverityNumber } from '@opentelemetry/api-logs';
|
|
29
|
-
|
|
30
|
-
import { shouldIgnoreUrl } from '../core/url';
|
|
31
|
-
|
|
32
|
-
import type { BkOTRumConfig } from '../core/config';
|
|
33
|
-
import type { BkOTPlugin } from '../core/plugin';
|
|
34
|
-
|
|
35
|
-
const getMessageByteLength = (data: unknown): number => {
|
|
36
|
-
if (data == null) return 0;
|
|
37
|
-
if (typeof data === 'string') {
|
|
38
|
-
return typeof TextEncoder === 'undefined' ? data.length : new TextEncoder().encode(data).byteLength;
|
|
39
|
-
}
|
|
40
|
-
if (data instanceof ArrayBuffer) return data.byteLength;
|
|
41
|
-
if (ArrayBuffer.isView(data)) return data.byteLength;
|
|
42
|
-
if (typeof Blob !== 'undefined' && data instanceof Blob) return data.size;
|
|
43
|
-
return 0;
|
|
44
|
-
};
|
|
45
|
-
|
|
46
|
-
const getWebSocketAttributes = (url: string, redactUrl: (url: string) => string) => {
|
|
47
|
-
const spanAttributes: Record<string, string> = {
|
|
48
|
-
'url.full': redactUrl(url),
|
|
49
|
-
'network.protocol.name': 'websocket',
|
|
50
|
-
};
|
|
51
|
-
|
|
52
|
-
try {
|
|
53
|
-
const parsed = new URL(url, typeof location === 'undefined' ? 'http://localhost' : location.href);
|
|
54
|
-
spanAttributes['server.address'] = parsed.host;
|
|
55
|
-
} catch {
|
|
56
|
-
/* ignore malformed URL and keep the raw, redacted URL only */
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
return {
|
|
60
|
-
spanAttributes,
|
|
61
|
-
metricAttributes: {
|
|
62
|
-
'network.protocol.name': 'websocket',
|
|
63
|
-
},
|
|
64
|
-
};
|
|
65
|
-
};
|
|
66
|
-
|
|
67
|
-
export const createWebSocketPlugin = (enabled: BkOTRumConfig['websocket']): BkOTPlugin => {
|
|
68
|
-
let originalWebSocket: typeof WebSocket | undefined;
|
|
69
|
-
|
|
70
|
-
return {
|
|
71
|
-
name: 'websocket',
|
|
72
|
-
enabled: Boolean(enabled),
|
|
73
|
-
init(context) {
|
|
74
|
-
if (typeof window === 'undefined' || typeof window.WebSocket === 'undefined') {
|
|
75
|
-
return;
|
|
76
|
-
}
|
|
77
|
-
|
|
78
|
-
const NativeWebSocket = window.WebSocket;
|
|
79
|
-
originalWebSocket = NativeWebSocket;
|
|
80
|
-
|
|
81
|
-
const messageCounter = context.meter.createCounter('browser.websocket.message.count', {
|
|
82
|
-
description: 'Total number of WebSocket messages observed',
|
|
83
|
-
});
|
|
84
|
-
const bytesCounter = context.meter.createCounter('browser.websocket.message.bytes', {
|
|
85
|
-
unit: 'By',
|
|
86
|
-
description: 'Total bytes transferred over WebSocket (best-effort)',
|
|
87
|
-
});
|
|
88
|
-
const errorCounter = context.meter.createCounter('browser.websocket.error.count', {
|
|
89
|
-
description: 'Total number of WebSocket error events',
|
|
90
|
-
});
|
|
91
|
-
|
|
92
|
-
const PatchedWebSocket = function (this: WebSocket, url: string | URL, protocols?: string | string[]) {
|
|
93
|
-
const urlValue = url.toString();
|
|
94
|
-
|
|
95
|
-
// 用户主动 ignore 或上报 endpoint,避免回环监控
|
|
96
|
-
if (shouldIgnoreUrl(context.config, urlValue)) {
|
|
97
|
-
return protocols === undefined ? new NativeWebSocket(url) : new NativeWebSocket(url, protocols);
|
|
98
|
-
}
|
|
99
|
-
|
|
100
|
-
const { metricAttributes, spanAttributes } = getWebSocketAttributes(urlValue, context.config.redactUrl);
|
|
101
|
-
// connect span 只覆盖"建立连接"阶段,避免长连接导致 span 永远不结束
|
|
102
|
-
const connectSpan = context.startSpan('websocket.connect', spanAttributes);
|
|
103
|
-
const startTime = performance.now();
|
|
104
|
-
const socket = protocols === undefined ? new NativeWebSocket(url) : new NativeWebSocket(url, protocols);
|
|
105
|
-
let connectEnded = false;
|
|
106
|
-
|
|
107
|
-
const endConnectSpan = (status: 'error' | 'opened') => {
|
|
108
|
-
if (connectEnded) return;
|
|
109
|
-
connectEnded = true;
|
|
110
|
-
if (status === 'error') {
|
|
111
|
-
connectSpan.setStatus({ code: SpanStatusCode.ERROR, message: 'websocket connect failed' });
|
|
112
|
-
}
|
|
113
|
-
connectSpan.setAttribute('websocket.connect.duration_ms', performance.now() - startTime);
|
|
114
|
-
connectSpan.end();
|
|
115
|
-
};
|
|
116
|
-
|
|
117
|
-
socket.addEventListener('open', () => endConnectSpan('opened'));
|
|
118
|
-
|
|
119
|
-
socket.addEventListener('message', event => {
|
|
120
|
-
messageCounter.add(1, { ...metricAttributes, 'websocket.direction': 'in' });
|
|
121
|
-
bytesCounter.add(getMessageByteLength(event.data), {
|
|
122
|
-
...metricAttributes,
|
|
123
|
-
'websocket.direction': 'in',
|
|
124
|
-
});
|
|
125
|
-
});
|
|
126
|
-
|
|
127
|
-
socket.addEventListener('error', () => {
|
|
128
|
-
errorCounter.add(1, metricAttributes);
|
|
129
|
-
endConnectSpan('error');
|
|
130
|
-
context.emitLog({
|
|
131
|
-
severityNumber: SeverityNumber.ERROR,
|
|
132
|
-
severityText: 'ERROR',
|
|
133
|
-
body: 'websocket.error',
|
|
134
|
-
attributes: spanAttributes,
|
|
135
|
-
});
|
|
136
|
-
});
|
|
137
|
-
|
|
138
|
-
socket.addEventListener('close', event => {
|
|
139
|
-
endConnectSpan('opened');
|
|
140
|
-
context.emitLog({
|
|
141
|
-
severityNumber: SeverityNumber.INFO,
|
|
142
|
-
severityText: 'INFO',
|
|
143
|
-
body: 'websocket.close',
|
|
144
|
-
attributes: {
|
|
145
|
-
...spanAttributes,
|
|
146
|
-
'websocket.close.code': event.code,
|
|
147
|
-
'websocket.close.reason': event.reason,
|
|
148
|
-
'websocket.close.was_clean': event.wasClean,
|
|
149
|
-
},
|
|
150
|
-
});
|
|
151
|
-
});
|
|
152
|
-
|
|
153
|
-
// 计量发送方向(无法拦截 send 的回调,所以包一层 send 方法)
|
|
154
|
-
const originalSend = socket.send.bind(socket);
|
|
155
|
-
socket.send = (data: ArrayBufferLike | ArrayBufferView | Blob | string) => {
|
|
156
|
-
messageCounter.add(1, { ...metricAttributes, 'websocket.direction': 'out' });
|
|
157
|
-
bytesCounter.add(getMessageByteLength(data), {
|
|
158
|
-
...metricAttributes,
|
|
159
|
-
'websocket.direction': 'out',
|
|
160
|
-
});
|
|
161
|
-
return originalSend(data);
|
|
162
|
-
};
|
|
163
|
-
|
|
164
|
-
return socket;
|
|
165
|
-
} as unknown as typeof WebSocket;
|
|
166
|
-
|
|
167
|
-
// 复制原型与静态常量(CONNECTING / OPEN / CLOSING / CLOSED 等),避免业务侧 WebSocket.OPEN 失效
|
|
168
|
-
PatchedWebSocket.prototype = NativeWebSocket.prototype;
|
|
169
|
-
Object.assign(PatchedWebSocket, NativeWebSocket);
|
|
170
|
-
|
|
171
|
-
window.WebSocket = PatchedWebSocket;
|
|
172
|
-
},
|
|
173
|
-
shutdown() {
|
|
174
|
-
if (originalWebSocket && typeof window !== 'undefined') {
|
|
175
|
-
window.WebSocket = originalWebSocket;
|
|
176
|
-
}
|
|
177
|
-
},
|
|
178
|
-
};
|
|
179
|
-
};
|
package/tsconfig.app.json
DELETED
|
@@ -1,27 +0,0 @@
|
|
|
1
|
-
{
|
|
2
|
-
"extends": "@vue/tsconfig/tsconfig.dom.json",
|
|
3
|
-
"compilerOptions": {
|
|
4
|
-
"tsBuildInfoFile": "./node_modules/.tmp/tsconfig.app.tsbuildinfo",
|
|
5
|
-
"types": ["vite/client"],
|
|
6
|
-
|
|
7
|
-
/* Linting */
|
|
8
|
-
"strict": true,
|
|
9
|
-
"noUnusedLocals": true,
|
|
10
|
-
"noUnusedParameters": true,
|
|
11
|
-
"erasableSyntaxOnly": false,
|
|
12
|
-
"noFallthroughCasesInSwitch": true,
|
|
13
|
-
"noUncheckedSideEffectImports": true
|
|
14
|
-
},
|
|
15
|
-
"include": [
|
|
16
|
-
"src/**/*.ts",
|
|
17
|
-
"src/**/*.tsx",
|
|
18
|
-
"src/**/*.d.ts",
|
|
19
|
-
"src/**/*.vue",
|
|
20
|
-
"playground/**/*.ts",
|
|
21
|
-
"playground/**/*.vue",
|
|
22
|
-
"vite.config.ts",
|
|
23
|
-
"wikis/**/*.ts",
|
|
24
|
-
"wikis/.vitepress/**/*.ts",
|
|
25
|
-
"mcp/**/*.ts"
|
|
26
|
-
]
|
|
27
|
-
}
|
package/tsconfig.dts.json
DELETED
|
@@ -1,13 +0,0 @@
|
|
|
1
|
-
{
|
|
2
|
-
"include": ["src/**/*.ts", "src/**/*.d.ts", "src/**/*.vue"],
|
|
3
|
-
"exclude": ["node_modules", "src/**/*.spec.ts"],
|
|
4
|
-
"extends": "./tsconfig.app.json",
|
|
5
|
-
|
|
6
|
-
"compilerOptions": {
|
|
7
|
-
"declaration": true,
|
|
8
|
-
"declarationDir": "./dist",
|
|
9
|
-
"emitDeclarationOnly": true,
|
|
10
|
-
"noEmit": false,
|
|
11
|
-
"noImplicitAny": false
|
|
12
|
-
}
|
|
13
|
-
}
|
package/tsconfig.json
DELETED
package/tsconfig.node.json
DELETED
|
@@ -1,26 +0,0 @@
|
|
|
1
|
-
{
|
|
2
|
-
"compilerOptions": {
|
|
3
|
-
"tsBuildInfoFile": "./node_modules/.tmp/tsconfig.node.tsbuildinfo",
|
|
4
|
-
"target": "ES2023",
|
|
5
|
-
"lib": ["ES2023"],
|
|
6
|
-
"module": "ESNext",
|
|
7
|
-
"types": ["node"],
|
|
8
|
-
"skipLibCheck": true,
|
|
9
|
-
|
|
10
|
-
/* Bundler mode */
|
|
11
|
-
"moduleResolution": "bundler",
|
|
12
|
-
"allowImportingTsExtensions": true,
|
|
13
|
-
"verbatimModuleSyntax": true,
|
|
14
|
-
"moduleDetection": "force",
|
|
15
|
-
"noEmit": true,
|
|
16
|
-
|
|
17
|
-
/* Linting */
|
|
18
|
-
"strict": true,
|
|
19
|
-
"noUnusedLocals": true,
|
|
20
|
-
"noUnusedParameters": true,
|
|
21
|
-
"erasableSyntaxOnly": true,
|
|
22
|
-
"noFallthroughCasesInSwitch": true,
|
|
23
|
-
"noUncheckedSideEffectImports": true
|
|
24
|
-
},
|
|
25
|
-
"include": ["vite.config.ts", "vitest.config.ts", "src/**/*.mjs"]
|
|
26
|
-
}
|
package/vite.config.ts
DELETED
|
@@ -1,70 +0,0 @@
|
|
|
1
|
-
/*
|
|
2
|
-
* Tencent is pleased to support the open source community by making
|
|
3
|
-
* 蓝鲸智云PaaS平台 (BlueKing PaaS) available.
|
|
4
|
-
*
|
|
5
|
-
* Copyright (C) 2021 THL A29 Limited, a Tencent company. All rights reserved.
|
|
6
|
-
*
|
|
7
|
-
* 蓝鲸智云PaaS平台 (BlueKing PaaS) is licensed under the MIT License.
|
|
8
|
-
*
|
|
9
|
-
* License for 蓝鲸智云PaaS平台 (BlueKing PaaS):
|
|
10
|
-
*
|
|
11
|
-
* ---------------------------------------------------
|
|
12
|
-
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
|
|
13
|
-
* documentation files (the "Software"), to deal in the Software without restriction, including without limitation
|
|
14
|
-
* the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and
|
|
15
|
-
* to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
|
16
|
-
*
|
|
17
|
-
* The above copyright notice and this permission notice shall be included in all copies or substantial portions of
|
|
18
|
-
* the Software.
|
|
19
|
-
*
|
|
20
|
-
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO
|
|
21
|
-
* THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
22
|
-
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
|
|
23
|
-
* CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
|
|
24
|
-
* IN THE SOFTWARE.
|
|
25
|
-
*/
|
|
26
|
-
import vue from '@vitejs/plugin-vue';
|
|
27
|
-
import fs from 'fs';
|
|
28
|
-
import path from 'path';
|
|
29
|
-
import { defineConfig } from 'vite';
|
|
30
|
-
import { analyzer, unstableRolldownAdapter } from 'vite-bundle-analyzer';
|
|
31
|
-
|
|
32
|
-
const resolve = (dir: string) => path.resolve(__dirname, dir);
|
|
33
|
-
const packageJson = JSON.parse(fs.readFileSync(resolve('./package.json'), 'utf-8'));
|
|
34
|
-
const externals = Object.keys(packageJson.dependencies || {});
|
|
35
|
-
const isExternal = (id: string) => externals.some(dep => id === dep || id.startsWith(`${dep}/`));
|
|
36
|
-
// https://vite.dev/config/
|
|
37
|
-
export default defineConfig(({ mode }) => {
|
|
38
|
-
const isCdn = mode === 'cdn';
|
|
39
|
-
const isProductionLike = mode === 'production' || isCdn;
|
|
40
|
-
|
|
41
|
-
return {
|
|
42
|
-
plugins: [vue(), mode === 'preview' ? unstableRolldownAdapter(analyzer()) : undefined].filter(Boolean),
|
|
43
|
-
root: resolve('./playground'),
|
|
44
|
-
build: {
|
|
45
|
-
emptyOutDir: !isCdn,
|
|
46
|
-
target: 'es2015',
|
|
47
|
-
outDir: resolve('./dist'),
|
|
48
|
-
sourcemap: isProductionLike ? 'hidden' : 'inline',
|
|
49
|
-
lib: {
|
|
50
|
-
entry: resolve(isCdn ? './src/browser.ts' : './src/index.ts'),
|
|
51
|
-
name: 'BkOpenTelemetry',
|
|
52
|
-
fileName: () => (isCdn ? 'bk-rum.global.js' : 'index.js'),
|
|
53
|
-
formats: [isCdn ? 'iife' : 'es'],
|
|
54
|
-
cssFileName: 'index',
|
|
55
|
-
},
|
|
56
|
-
minify: isProductionLike,
|
|
57
|
-
rolldownOptions: {
|
|
58
|
-
external: isCdn ? undefined : isExternal,
|
|
59
|
-
output: isCdn
|
|
60
|
-
? {
|
|
61
|
-
inlineDynamicImports: true,
|
|
62
|
-
}
|
|
63
|
-
: undefined,
|
|
64
|
-
},
|
|
65
|
-
},
|
|
66
|
-
server: {
|
|
67
|
-
allowedHosts: ['localhost', '127.0.0.1', 'appdev.woa.com'],
|
|
68
|
-
},
|
|
69
|
-
};
|
|
70
|
-
});
|