@arms/rum-core 0.0.34 → 0.0.36

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 (47) hide show
  1. package/es/index.d.ts +18 -0
  2. package/es/index.js +18 -0
  3. package/es/model/client.d.ts +21 -0
  4. package/es/model/client.js +76 -0
  5. package/es/model/context.d.ts +18 -0
  6. package/es/model/context.js +38 -0
  7. package/es/model/reporter.d.ts +31 -0
  8. package/es/model/reporter.js +196 -0
  9. package/es/model/shell.d.ts +51 -0
  10. package/es/model/shell.js +160 -0
  11. package/es/style.js +1 -0
  12. package/es/types/client.d.ts +162 -0
  13. package/es/types/client.js +7 -0
  14. package/es/types/collector.d.ts +7 -0
  15. package/es/types/collector.js +1 -0
  16. package/es/types/processor.d.ts +8 -0
  17. package/es/types/processor.js +1 -0
  18. package/es/types/reporter.d.ts +8 -0
  19. package/es/types/reporter.js +1 -0
  20. package/es/types/rum-event.d.ts +177 -0
  21. package/es/types/rum-event.js +26 -0
  22. package/es/types/shell.d.ts +28 -0
  23. package/es/types/shell.js +1 -0
  24. package/es/utils/base.d.ts +60 -0
  25. package/es/utils/base.js +216 -0
  26. package/es/utils/combineConfig.d.ts +15 -0
  27. package/es/utils/combineConfig.js +101 -0
  28. package/es/utils/exception.d.ts +7 -0
  29. package/es/utils/exception.js +10 -0
  30. package/es/utils/is.d.ts +12 -0
  31. package/es/utils/is.js +39 -0
  32. package/es/utils/match.d.ts +7 -0
  33. package/es/utils/match.js +40 -0
  34. package/es/utils/number.d.ts +17 -0
  35. package/es/utils/number.js +40 -0
  36. package/es/utils/polyfills.d.ts +7 -0
  37. package/es/utils/polyfills.js +28 -0
  38. package/es/utils/trace.d.ts +28 -0
  39. package/es/utils/trace.js +105 -0
  40. package/es/utils/verify.d.ts +2 -0
  41. package/es/utils/verify.js +35 -0
  42. package/lib/model/reporter.js +1 -4
  43. package/lib/model/shell.d.ts +2 -1
  44. package/lib/model/shell.js +26 -20
  45. package/lib/utils/base.d.ts +2 -2
  46. package/lib/utils/base.js +9 -5
  47. package/package.json +6 -2
@@ -0,0 +1,216 @@
1
+ import { isFunction } from "./is";
2
+
3
+ // 插值前缀
4
+ var PRE_SUF_FIX = '__';
5
+ // 类型匹配正则
6
+ var TYPE_REG = /^\[object ([a-z]*)\]$/;
7
+
8
+ /**
9
+ * 劫持函数或原型
10
+ */
11
+ export function interceptFunction(target, name, callback, isPrototype) {
12
+ if (isPrototype === void 0) {
13
+ isPrototype = false;
14
+ }
15
+ var registeredMethod = target[name];
16
+ target["" + PRE_SUF_FIX + name + PRE_SUF_FIX] = registeredMethod;
17
+ target[name] = function () {
18
+ var ctx = isPrototype ? this : target;
19
+ for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
20
+ args[_key] = arguments[_key];
21
+ }
22
+ callback.apply(ctx, args);
23
+ if (isFunction(registeredMethod)) {
24
+ return registeredMethod.apply(ctx, args);
25
+ }
26
+ };
27
+ }
28
+
29
+ /**
30
+ * 还原函数
31
+ */
32
+ export function restoreFunction(target, name) {
33
+ var key = "" + PRE_SUF_FIX + name + PRE_SUF_FIX;
34
+ if (typeof target[key] == 'function') {
35
+ target[name] = target[key];
36
+ delete target[key];
37
+ }
38
+ }
39
+
40
+ /**
41
+ * @description: GUID v4
42
+ * @return {String}
43
+ */
44
+ export function generateGUID() {
45
+ var guid = '';
46
+ try {
47
+ if (crypto && crypto.randomUUID) {
48
+ guid = crypto.randomUUID();
49
+ } else if (crypto && crypto.getRandomValues) {
50
+ var buf = new Uint8Array(16);
51
+ crypto.getRandomValues(buf);
52
+ buf[6] = buf[6] & 0x0f | 0x40; // version 4 UUID
53
+ buf[8] = buf[8] & 0x3f | 0x80; // variant 10xxxxxx
54
+ guid = buf.reduce(function (str, _byte) {
55
+ return str + _byte.toString(16).padStart(2, '0');
56
+ }, '').replace(/^(.{8})(.{4})(.{4})(.{4})(.{12})$/, '\$1-\$2-\$3-\$4-\$5');
57
+ }
58
+ } catch (e) {
59
+ //
60
+ }
61
+ if (!guid) {
62
+ guid = 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) {
63
+ var r = Math.random() * 16 | 0,
64
+ v = c == 'x' ? r : r & 0x3 | 0x8;
65
+ return v.toString(16);
66
+ });
67
+ }
68
+ return guid;
69
+ }
70
+
71
+ /**
72
+ * @description: 随机生成 traceId 或 sessionId
73
+ * @return {String}
74
+ */
75
+ export function generateTraceId() {
76
+ var traceId = generateGUID().replace(/-/g, '');
77
+ // 适配OpenTracing 实现(long long转成16进制最高位不能为0)
78
+ if (traceId[0] === '0') {
79
+ traceId = '1' + traceId.substring(1);
80
+ }
81
+ if (traceId[16] === '0') {
82
+ traceId = traceId.substring(0, 16) + '1' + traceId.substring(17);
83
+ }
84
+ return traceId;
85
+ }
86
+
87
+ /**
88
+ * @description: 随机生成 spanId
89
+ * @param len {number}
90
+ * @param radix {number}
91
+ * @return {String}
92
+ */
93
+ export function generateSpanId(len, radix) {
94
+ if (len === void 0) {
95
+ len = 16;
96
+ }
97
+ if (radix === void 0) {
98
+ radix = 16;
99
+ }
100
+ var result = '';
101
+ for (var i = 0; i < len; i++) {
102
+ result += Math.floor(Math.random() * radix).toString(radix);
103
+ }
104
+ return result;
105
+ }
106
+
107
+ /**
108
+ * @description: 随机生成 eventId
109
+ * @param sessionId {string} sessionId
110
+ * @return {String}
111
+ */
112
+ export function generateEventId(sessionId) {
113
+ var spanId = generateSpanId();
114
+ return "00-" + sessionId + "-" + spanId;
115
+ }
116
+
117
+ /**
118
+ * @desc 函数防抖
119
+ * @param func 回调函数
120
+ * @param wait 延迟执行毫秒数
121
+ */
122
+ export function debounce(func, wait) {
123
+ var timer;
124
+ if (!isFunction(func)) {
125
+ return;
126
+ }
127
+ return function () {
128
+ var context = this;
129
+ var args = arguments;
130
+ if (timer) {
131
+ clearTimeout(timer);
132
+ }
133
+ timer = setTimeout(function () {
134
+ func.apply(context, args);
135
+ }, wait);
136
+ };
137
+ }
138
+
139
+ /**
140
+ * @desc 函数延迟执行
141
+ * @param func 回调函数
142
+ * @param wait 延迟执行毫秒数
143
+ */
144
+ export function delay(func, wait) {
145
+ if (!isFunction(func)) {
146
+ return;
147
+ }
148
+ for (var _len2 = arguments.length, args = new Array(_len2 > 2 ? _len2 - 2 : 0), _key2 = 2; _key2 < _len2; _key2++) {
149
+ args[_key2 - 2] = arguments[_key2];
150
+ }
151
+ return setTimeout.apply(void 0, [func, +wait || 0].concat(args));
152
+ }
153
+
154
+ /**
155
+ * @desc 需要严格区分object和array
156
+ * @param obj 任意对象
157
+ */
158
+ export function getType(obj) {
159
+ var type = Object.prototype.toString.call(obj);
160
+ type = type.toLowerCase() || '';
161
+ var arr = type.match(TYPE_REG);
162
+ return arr === null || arr === void 0 ? void 0 : arr[1];
163
+ }
164
+
165
+ /**
166
+ * @desc 将字符串转换成正则表达式
167
+ * @param str 任意字符串
168
+ * 注意,这里只支持 /xxx/ 这种格式,不支持修饰符,例如 /xxx/ig
169
+ */
170
+ export function transStrToReg(str) {
171
+ if (getType(str) !== 'string') return str;
172
+ if (str.length > 2 && str[0] === '/' && str[str.length - 1] === '/') {
173
+ return new RegExp(str.substr(1, str.length - 2));
174
+ }
175
+ return str;
176
+ }
177
+
178
+ /**
179
+ *
180
+ * @param config 配置
181
+ * @param keys 需要转换的字段
182
+ */
183
+ export function toRegFormat(config, keys) {
184
+ if (getType(keys) === 'string') {
185
+ keys = [keys];
186
+ }
187
+ for (var i = 0, len = keys.length; i < len; i++) {
188
+ var paths = keys[i].split('.');
189
+ var lastIndex = paths.length - 1;
190
+ if (!config) break;
191
+ var tmp = config;
192
+ for (var j = 0, jlen = paths.length; j < jlen; j++) {
193
+ if (paths[j] === '[]') {
194
+ if (getType(tmp) === 'array') {
195
+ var lastPath = paths.splice(j + 1);
196
+ lastPath = lastPath.join('.');
197
+ for (var x = 0, xlen = tmp.length; x < xlen; x++) {
198
+ toRegFormat(tmp[x], lastPath);
199
+ }
200
+ }
201
+ break;
202
+ }
203
+ // 最后一层了,需要往前一层才能修改对象
204
+ if (lastIndex === j && getType(tmp[paths[j]]) === 'string') {
205
+ tmp[paths[j]] = transStrToReg(tmp[paths[j]]);
206
+ break;
207
+ }
208
+ tmp = tmp[paths[j]];
209
+ if (!tmp) break;
210
+ }
211
+ if (!tmp || getType(tmp) !== 'array') continue;
212
+ for (var _j = 0, _jlen = tmp.length; _j < _jlen; _j++) {
213
+ tmp[_j] = transStrToReg(tmp[_j]);
214
+ }
215
+ }
216
+ }
@@ -0,0 +1,15 @@
1
+ export declare function isRemoteConfigEnable(config: any): boolean;
2
+ /**
3
+ *
4
+ * @param config
5
+ * @returns
6
+ */
7
+ export declare function getRemoteConfigUrl(config: any): any;
8
+ /**
9
+ *
10
+ * @param oldConfig
11
+ * @param remoteConfig
12
+ * @returns config
13
+ */
14
+ declare function combineConfig(oldConfig?: {}, remoteConfig?: any): {};
15
+ export default combineConfig;
@@ -0,0 +1,101 @@
1
+ import _extends from "@babel/runtime/helpers/extends";
2
+ import { getType, toRegFormat } from './base';
3
+
4
+ // 需要检查是否需要字符串格式化为正则的配置路径
5
+ var REG_FORMAT_PATHS = ['filters.exception', 'filters.resource', 'tracing.allowedUrls.[].match'];
6
+ export function isRemoteConfigEnable(config) {
7
+ return config.remoteConfig === true || config.remoteConfig && config.remoteConfig.enable !== false;
8
+ }
9
+
10
+ /**
11
+ *
12
+ * @param config
13
+ * @returns
14
+ */
15
+ export function getRemoteConfigUrl(config) {
16
+ if (config.remoteConfig && config.remoteConfig.url) {
17
+ return config.remoteConfig.url;
18
+ }
19
+ var sereis = config.pid.split('@');
20
+ if (!sereis || sereis.length < 2) return;
21
+ var timestamp = Date.now();
22
+ return "//" + sereis[0] + "-sdk.rum.aliyuncs.com/config/" + (config.remoteConfig.region || 'cn-hangzhou') + "/" + sereis[1] + "?t=" + timestamp;
23
+ }
24
+
25
+ /**
26
+ *
27
+ * @param oldConfig
28
+ * @param remoteConfig
29
+ */
30
+ function doCover(oldConfig, remoteConfig) {
31
+ if (oldConfig === void 0) {
32
+ oldConfig = {};
33
+ }
34
+ if (remoteConfig === void 0) {
35
+ remoteConfig = {};
36
+ }
37
+ for (var key in oldConfig) {
38
+ var _remoteConfig, _remoteConfig2;
39
+ if (key === 'pid' || key === 'endpoint') continue;
40
+ if (((_remoteConfig = remoteConfig) === null || _remoteConfig === void 0 ? void 0 : _remoteConfig[key]) === undefined) continue;
41
+ var oldV = oldConfig[key];
42
+ var newV = (_remoteConfig2 = remoteConfig) === null || _remoteConfig2 === void 0 ? void 0 : _remoteConfig2[key];
43
+ if (getType(oldV) === 'object' && getType(newV) === 'object') {
44
+ doCover(oldV, newV);
45
+ } else {
46
+ oldConfig[key] = newV;
47
+ }
48
+ }
49
+ }
50
+
51
+ /**
52
+ *
53
+ * @param oldConfig
54
+ * @param remoteConfig
55
+ */
56
+ function doSupplement(oldConfig, remoteConfig) {
57
+ if (oldConfig === void 0) {
58
+ oldConfig = {};
59
+ }
60
+ if (remoteConfig === void 0) {
61
+ remoteConfig = {};
62
+ }
63
+ for (var key in remoteConfig) {
64
+ var _oldConfig;
65
+ if (((_oldConfig = oldConfig) === null || _oldConfig === void 0 ? void 0 : _oldConfig[key]) === undefined) {
66
+ oldConfig[key] = remoteConfig[key];
67
+ } else {
68
+ var oldV = oldConfig[key];
69
+ var newV = remoteConfig[key];
70
+ if (getType(oldV) === 'object' && getType(newV) === 'object') {
71
+ doSupplement(oldV, newV);
72
+ }
73
+ }
74
+ }
75
+ }
76
+
77
+ /**
78
+ *
79
+ * @param oldConfig
80
+ * @param remoteConfig
81
+ * @returns config
82
+ */
83
+ function combineConfig(oldConfig, remoteConfig) {
84
+ if (oldConfig === void 0) {
85
+ oldConfig = {};
86
+ }
87
+ if (remoteConfig === void 0) {
88
+ remoteConfig = null;
89
+ }
90
+ if (!remoteConfig) return null;
91
+ if (getType(oldConfig) !== 'object' || getType(remoteConfig) !== 'object') return null;
92
+ var config = _extends({}, oldConfig);
93
+ // 先检查远程配置的,是否有需要将字符串格式化为正则的
94
+ toRegFormat(remoteConfig, REG_FORMAT_PATHS);
95
+ // 执行覆盖
96
+ doCover(config, remoteConfig);
97
+ // 执行补充
98
+ doSupplement(config, remoteConfig);
99
+ return config;
100
+ }
101
+ export default combineConfig;
@@ -0,0 +1,7 @@
1
+ /**
2
+ * 获取错误对象唯一标识
3
+ */
4
+ export declare const getErrorID: (error: {
5
+ message?: string;
6
+ stack?: string;
7
+ }) => string;
@@ -0,0 +1,10 @@
1
+ /**
2
+ * 获取错误对象唯一标识
3
+ */
4
+ export var getErrorID = function getErrorID(error) {
5
+ var _error$message = error.message,
6
+ message = _error$message === void 0 ? '' : _error$message,
7
+ _error$stack = error.stack,
8
+ stack = _error$stack === void 0 ? '' : _error$stack;
9
+ return message + stack;
10
+ };
@@ -0,0 +1,12 @@
1
+ export type IsFnHelper<T = unknown> = (value: unknown) => value is T;
2
+ export declare function isTypeof<T = unknown>(value: unknown, type: string): value is T;
3
+ export declare const isFunction: (func: any) => boolean;
4
+ export declare const isString: IsFnHelper<string>;
5
+ export declare function isToString<T = unknown>(value: unknown, type: string): value is T;
6
+ export declare const isArray: IsFnHelper<unknown[]>;
7
+ export declare const isRegExp: IsFnHelper<string>;
8
+ export declare const isBoolean: IsFnHelper<boolean>;
9
+ export declare const isNumber: IsFnHelper<number | bigint>;
10
+ export declare const isNull: IsFnHelper<null>;
11
+ export declare const isObject: IsFnHelper<object>;
12
+ export declare function isZero(...args: number[]): boolean;
package/es/utils/is.js ADDED
@@ -0,0 +1,39 @@
1
+ export function isTypeof(value, type) {
2
+ return typeof value === type;
3
+ }
4
+ export var isFunction = function isFunction(func) {
5
+ return typeof func === 'function';
6
+ };
7
+
8
+ // export const isUndefined = ((value) => isTypeof(value, 'undefined')) as IsFnHelper<undefined>;
9
+
10
+ export var isString = function isString(value) {
11
+ return isTypeof(value, 'string');
12
+ };
13
+ export function isToString(value, type) {
14
+ return Object.prototype.toString.call(value) === "[object " + type + "]";
15
+ }
16
+ export var isArray = function isArray(value) {
17
+ return isToString(value, 'Array');
18
+ };
19
+ export var isRegExp = function isRegExp(value) {
20
+ return isToString(value, 'RegExp');
21
+ };
22
+ export var isBoolean = function isBoolean(value) {
23
+ return isTypeof(value, 'boolean');
24
+ };
25
+ export var isNumber = function isNumber(value) {
26
+ return isTypeof(value, 'number') && !isNaN(value) || isTypeof(value, 'bigint');
27
+ };
28
+ export var isNull = function isNull(value) {
29
+ return value === null;
30
+ };
31
+ export var isObject = function isObject(value) {
32
+ return !isNull(value) && isTypeof(value, 'object');
33
+ };
34
+ export function isZero() {
35
+ for (var i = 0; i < arguments.length; i++) {
36
+ if ((i < 0 || arguments.length <= i ? undefined : arguments[i]) !== 0) return false;
37
+ }
38
+ return true;
39
+ }
@@ -0,0 +1,7 @@
1
+ export type MatchOption = string | RegExp | ((value: string, option?: unknown) => boolean);
2
+ export declare function isMatchOption(item: unknown): item is MatchOption;
3
+ /**
4
+ * 如果值有一个选项匹配,则返回true。在比较字符串时,useStartsWith: true时将把值与选项的开始进行比较,而不是要求精确匹配。
5
+ */
6
+ export declare function matchList(list: MatchOption[], value: string, useStartsWith?: boolean, options?: unknown): boolean;
7
+ export declare function urlMatch(url: string, list?: MatchOption | MatchOption[]): boolean;
@@ -0,0 +1,40 @@
1
+ import { isArray, isFunction, isRegExp, isString } from "./is";
2
+ import { startsWith } from "./polyfills";
3
+ export function isMatchOption(item) {
4
+ return isString(item) || isFunction(item) || isRegExp(item);
5
+ }
6
+
7
+ /**
8
+ * 如果值有一个选项匹配,则返回true。在比较字符串时,useStartsWith: true时将把值与选项的开始进行比较,而不是要求精确匹配。
9
+ */
10
+ export function matchList(list, value, useStartsWith, options) {
11
+ if (useStartsWith === void 0) {
12
+ useStartsWith = true;
13
+ }
14
+ return list.some(function (item) {
15
+ try {
16
+ if (typeof item === 'function') {
17
+ return item(value, options);
18
+ } else if (item instanceof RegExp) {
19
+ return item.test(value);
20
+ } else if (typeof item === 'string') {
21
+ return useStartsWith ? startsWith(value, item) : item === value;
22
+ }
23
+ } catch (e) {}
24
+ return false;
25
+ });
26
+ }
27
+ export function urlMatch(url, list) {
28
+ if (list === void 0) {
29
+ list = [];
30
+ }
31
+ try {
32
+ if (!isArray(list)) {
33
+ list = [list];
34
+ }
35
+ var rules = [/^(https?:)?\/\/.*rum|retcode|log-global|log\.aliyuncs\.com/, /^(https?:)?\/\/.*\.mmstat\.com/, /^(https?:)?\/\/.*hm\.baidu\.com/, /^(https?:)?\/\/.*google-analytics\.com/, /data:(.+?)(;base64)?,(.+)$/].concat(list);
36
+ return matchList(rules, url);
37
+ } catch (e) {
38
+ return false;
39
+ }
40
+ }
@@ -0,0 +1,17 @@
1
+ export declare const ONE_SECOND = 1000;
2
+ export declare const ONE_MINUTE: number;
3
+ export declare const ONE_HOUR: number;
4
+ export declare const ONE_DAY: number;
5
+ /**
6
+ * Return true if the draw is successful
7
+ * @param threshold between 0 and 100
8
+ */
9
+ export declare function performDraw(threshold: number): boolean;
10
+ /**
11
+ * 保留指定位数的小数
12
+ * @param num 原数据
13
+ * @param decimal 小数位数
14
+ * @param min 限制最小值,否则返回undefined
15
+ * @returns
16
+ */
17
+ export declare function formatNumber(num: number, decimal?: number, min?: number): number;
@@ -0,0 +1,40 @@
1
+ export var ONE_SECOND = 1e3;
2
+ export var ONE_MINUTE = 60 * ONE_SECOND;
3
+ export var ONE_HOUR = 60 * ONE_MINUTE;
4
+ export var ONE_DAY = 24 * ONE_HOUR;
5
+
6
+ /**
7
+ * Return true if the draw is successful
8
+ * @param threshold between 0 and 100
9
+ */
10
+ export function performDraw(threshold) {
11
+ return threshold !== 0 && Math.random() * 100 <= threshold;
12
+ }
13
+
14
+ /**
15
+ * 保留指定位数的小数
16
+ * @param num 原数据
17
+ * @param decimal 小数位数
18
+ * @param min 限制最小值,否则返回undefined
19
+ * @returns
20
+ */
21
+ export function formatNumber(num, decimal, min) {
22
+ if (decimal === void 0) {
23
+ decimal = 3;
24
+ }
25
+ if (min === void 0) {
26
+ min = 0;
27
+ }
28
+ if (!num) {
29
+ return num;
30
+ }
31
+ var str = num.toString();
32
+ var index = str.indexOf('.');
33
+ if (index !== -1) {
34
+ str = str.substring(0, decimal + index + 1);
35
+ } else {
36
+ str = str.substring(0);
37
+ }
38
+ var result = parseFloat(str);
39
+ return result >= min ? result : undefined;
40
+ }
@@ -0,0 +1,7 @@
1
+ export declare function find<T, S extends T>(array: ArrayLike<T>, predicate: (item: T, index: number) => item is S): S | undefined;
2
+ export declare function find<T>(array: ArrayLike<T>, predicate: (item: T, index: number) => boolean): T | undefined;
3
+ export declare function startsWith(candidate: string, search: string): boolean;
4
+ export declare function endsWith(candidate: string, search: string): boolean;
5
+ export declare function assign<T, U>(target: T, source: U): T & U;
6
+ export declare function assign<T, U, V>(target: T, source1: U, source2: V): T & U & V;
7
+ export declare function assign<T, U, V, W>(target: T, source1: U, source2: V, source3: W): T & U & V & W;
@@ -0,0 +1,28 @@
1
+ export function find(array, predicate) {
2
+ for (var i = 0; i < array.length; i += 1) {
3
+ var item = array[i];
4
+ if (predicate(item, i)) {
5
+ return item;
6
+ }
7
+ }
8
+ return undefined;
9
+ }
10
+ export function startsWith(candidate, search) {
11
+ return candidate.slice(0, search.length) === search;
12
+ }
13
+ export function endsWith(candidate, search) {
14
+ return candidate.slice(-search.length) === search;
15
+ }
16
+ export function assign(target) {
17
+ for (var _len = arguments.length, toAssign = new Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) {
18
+ toAssign[_key - 1] = arguments[_key];
19
+ }
20
+ toAssign.forEach(function (source) {
21
+ for (var _key2 in source) {
22
+ if (Object.prototype.hasOwnProperty.call(source, _key2)) {
23
+ target[_key2] = source[_key2];
24
+ }
25
+ }
26
+ });
27
+ return target;
28
+ }
@@ -0,0 +1,28 @@
1
+ import { MatchOption } from "./match";
2
+ export type PropagatorType = 'tracecontext' | 'b3' | 'b3multi' | 'jaeger' | 'sw8';
3
+ export type TraceOption = {
4
+ match: MatchOption;
5
+ propagatorTypes: PropagatorType[];
6
+ };
7
+ export declare function isTraceOption(option: any): option is TraceOption;
8
+ export interface ITracingOption {
9
+ enable?: boolean;
10
+ sample?: number | undefined;
11
+ propagatorTypes?: PropagatorType[];
12
+ allowedUrls?: Array<MatchOption | TraceOption> | undefined;
13
+ tracestate?: boolean;
14
+ baggage?: boolean;
15
+ }
16
+ export interface ITracingHeaders {
17
+ [key: string]: string;
18
+ }
19
+ export interface TraceSubOption {
20
+ tracestate?: string;
21
+ baggage?: string;
22
+ appId?: string;
23
+ appVersion?: string;
24
+ viewName?: string;
25
+ host?: string;
26
+ }
27
+ export declare function makeTracingHeaders(traceId: string, spanId: string, sampled: boolean, propagatorTypes: PropagatorType[], subOption?: TraceSubOption): ITracingHeaders;
28
+ export declare function parseTracingOptions(tracingOption: boolean | ITracingOption): ITracingOption;
@@ -0,0 +1,105 @@
1
+ import { isMatchOption } from "./match";
2
+ import { isArray, isBoolean, isFunction, isNumber } from "./is";
3
+ export function isTraceOption(option) {
4
+ return option && isMatchOption(option.match) && isArray(option.propagatorTypes);
5
+ }
6
+ export function makeTracingHeaders(traceId, spanId, sampled, propagatorTypes, subOption) {
7
+ if (subOption === void 0) {
8
+ subOption = {};
9
+ }
10
+ var tracingHeaders = {};
11
+ var sample = sampled ? '1' : '0';
12
+ // skywalking 标准具备排他性,如果包含sw8,则只使用sw8
13
+ if (propagatorTypes.includes('sw8')) {
14
+ propagatorTypes = ['sw8'];
15
+ }
16
+ if (!isArray(propagatorTypes)) {
17
+ propagatorTypes = [propagatorTypes];
18
+ }
19
+ propagatorTypes.forEach(function (propagatorType) {
20
+ switch (propagatorType) {
21
+ case 'jaeger':
22
+ tracingHeaders['uber-trace-id'] = traceId + ":" + spanId + ":0:" + sample;
23
+ break;
24
+ case 'b3':
25
+ // https://github.com/openzipkin/b3-propagation
26
+ tracingHeaders['b3'] = traceId + "-" + spanId + "-" + sample;
27
+ break;
28
+ case 'b3multi':
29
+ tracingHeaders['X-B3-TraceId'] = traceId;
30
+ tracingHeaders['X-B3-SpanId'] = spanId;
31
+ tracingHeaders['X-B3-Sampled'] = sample;
32
+ break;
33
+ case 'sw8':
34
+ if (isFunction(btoa)) {
35
+ // https://github.com/apache/skywalking-client-js
36
+ var traceIdStr = btoa(traceId);
37
+ var segmentId = btoa(spanId);
38
+ var service = btoa(subOption.appId);
39
+ var instance = btoa(subOption.appVersion);
40
+ var endpoint = btoa(subOption.viewName);
41
+ var peer = btoa(subOption.host);
42
+ tracingHeaders['sw8'] = sample + "-" + traceIdStr + "-" + segmentId + "-" + 0 + "-" + service + "-" + instance + "-" + endpoint + "-" + peer;
43
+ }
44
+ break;
45
+ case 'tracecontext':
46
+ default:
47
+ // https://www.w3.org/TR/trace-context/
48
+ tracingHeaders['traceparent'] = "00-" + traceId + "-" + spanId + "-0" + sample;
49
+ if (subOption.tracestate) {
50
+ tracingHeaders['tracestate'] = subOption.tracestate;
51
+ }
52
+ break;
53
+ }
54
+ });
55
+ if (subOption.baggage) {
56
+ tracingHeaders['baggage'] = subOption.baggage;
57
+ }
58
+ return tracingHeaders;
59
+ }
60
+ export function parseTracingOptions(tracingOption) {
61
+ var defPropagatorTypes = ['tracecontext'];
62
+ if (isBoolean(tracingOption) || !tracingOption) {
63
+ return {
64
+ enable: !!tracingOption,
65
+ sample: 100,
66
+ propagatorTypes: defPropagatorTypes,
67
+ allowedUrls: [],
68
+ tracestate: true,
69
+ baggage: false
70
+ };
71
+ }
72
+ var _tracingOption$enable = tracingOption.enable,
73
+ enable = _tracingOption$enable === void 0 ? true : _tracingOption$enable,
74
+ _tracingOption$sample = tracingOption.sample,
75
+ sample = _tracingOption$sample === void 0 ? 100 : _tracingOption$sample,
76
+ _tracingOption$propag = tracingOption.propagatorTypes,
77
+ propagatorTypes = _tracingOption$propag === void 0 ? defPropagatorTypes : _tracingOption$propag,
78
+ _tracingOption$allowe = tracingOption.allowedUrls,
79
+ allowedUrls = _tracingOption$allowe === void 0 ? [] : _tracingOption$allowe,
80
+ _tracingOption$traces = tracingOption.tracestate,
81
+ tracestate = _tracingOption$traces === void 0 ? true : _tracingOption$traces,
82
+ _tracingOption$baggag = tracingOption.baggage,
83
+ baggage = _tracingOption$baggag === void 0 ? false : _tracingOption$baggag;
84
+ var traceOptions = [];
85
+ if (isArray(allowedUrls) && allowedUrls.length) {
86
+ allowedUrls.forEach(function (item) {
87
+ if (isMatchOption(item)) {
88
+ traceOptions.push({
89
+ match: item,
90
+ propagatorTypes: propagatorTypes
91
+ });
92
+ } else if (isTraceOption(item)) {
93
+ traceOptions.push(item);
94
+ }
95
+ });
96
+ }
97
+ return {
98
+ enable: isBoolean(enable) ? enable : true,
99
+ sample: isNumber(sample) ? sample : 100,
100
+ propagatorTypes: propagatorTypes,
101
+ allowedUrls: traceOptions,
102
+ tracestate: tracestate,
103
+ baggage: baggage
104
+ };
105
+ }
@@ -0,0 +1,2 @@
1
+ import { BaseObject } from "../types/rum-event";
2
+ export declare function verifyProperties(attrs: BaseObject): BaseObject;