@arms/rum-core 0.0.19 → 0.0.22
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/lib/utils/base.d.ts +1 -1
- package/lib/utils/base.js +5 -5
- package/lib/utils/trace.d.ts +11 -3
- package/lib/utils/trace.js +39 -16
- package/package.json +3 -2
package/lib/utils/base.d.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* 劫持函数
|
|
3
3
|
*/
|
|
4
|
-
export declare function interceptFunction(
|
|
4
|
+
export declare function interceptFunction(target: any, name: string, callback: Function): void;
|
|
5
5
|
/**
|
|
6
6
|
* @description: GUID v4
|
|
7
7
|
* @return {String}
|
package/lib/utils/base.js
CHANGED
|
@@ -7,15 +7,15 @@ exports.interceptFunction = interceptFunction;
|
|
|
7
7
|
/**
|
|
8
8
|
* 劫持函数
|
|
9
9
|
*/
|
|
10
|
-
function interceptFunction(
|
|
11
|
-
var registeredMethod =
|
|
12
|
-
|
|
10
|
+
function interceptFunction(target, name, callback) {
|
|
11
|
+
var registeredMethod = target[name];
|
|
12
|
+
target[name] = function () {
|
|
13
13
|
for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
|
|
14
14
|
args[_key] = arguments[_key];
|
|
15
15
|
}
|
|
16
|
-
callback.apply(
|
|
16
|
+
callback.apply(target, args);
|
|
17
17
|
if (registeredMethod) {
|
|
18
|
-
return registeredMethod.apply(
|
|
18
|
+
return registeredMethod.apply(target, args);
|
|
19
19
|
}
|
|
20
20
|
};
|
|
21
21
|
}
|
package/lib/utils/trace.d.ts
CHANGED
|
@@ -8,15 +8,23 @@ export declare function isTraceOption(option: any): option is TraceOption;
|
|
|
8
8
|
export interface ITracingOption {
|
|
9
9
|
enable?: boolean;
|
|
10
10
|
sample?: number | undefined;
|
|
11
|
+
propagatorTypes?: PropagatorType[];
|
|
11
12
|
allowedUrls?: Array<MatchOption | TraceOption> | undefined;
|
|
12
13
|
tracestate?: boolean;
|
|
13
14
|
baggage?: boolean;
|
|
14
15
|
}
|
|
15
16
|
export declare function generateTraceId(): string;
|
|
16
|
-
export declare function generateSpanId():
|
|
17
|
+
export declare function generateSpanId(): any;
|
|
17
18
|
export interface ITracingHeaders {
|
|
18
19
|
[key: string]: string;
|
|
19
20
|
}
|
|
20
|
-
export
|
|
21
|
-
|
|
21
|
+
export interface TraceSubOption {
|
|
22
|
+
tracestate?: string;
|
|
23
|
+
baggage?: string;
|
|
24
|
+
appId?: string;
|
|
25
|
+
appVersion?: string;
|
|
26
|
+
viewName?: string;
|
|
27
|
+
host?: string;
|
|
28
|
+
}
|
|
29
|
+
export declare function makeTracingHeaders(traceId: string, spanId: string, sampled: boolean, propagatorTypes: PropagatorType[], subOption?: TraceSubOption): ITracingHeaders;
|
|
22
30
|
export declare function parseTracingOptions(tracingOption: boolean | ITracingOption): ITracingOption;
|
package/lib/utils/trace.js
CHANGED
|
@@ -3,21 +3,18 @@
|
|
|
3
3
|
exports.__esModule = true;
|
|
4
4
|
exports.generateSpanId = generateSpanId;
|
|
5
5
|
exports.generateTraceId = generateTraceId;
|
|
6
|
-
exports.generateTraceIdForSW8 = generateTraceIdForSW8;
|
|
7
6
|
exports.isTraceOption = isTraceOption;
|
|
8
7
|
exports.makeTracingHeaders = makeTracingHeaders;
|
|
9
8
|
exports.parseTracingOptions = parseTracingOptions;
|
|
10
9
|
var _match = require("./match");
|
|
11
10
|
var _is = require("./is");
|
|
12
11
|
var _base = require("./base");
|
|
12
|
+
var _jsBase = require("js-base64");
|
|
13
13
|
function isTraceOption(option) {
|
|
14
14
|
return option && (0, _match.isMatchOption)(option.match) && (0, _is.isArray)(option.propagatorTypes);
|
|
15
15
|
}
|
|
16
|
-
function generateId() {
|
|
17
|
-
return (0, _base.generateGUID)().replace(/-/g, '');
|
|
18
|
-
}
|
|
19
16
|
function generateTraceId() {
|
|
20
|
-
var traceId =
|
|
17
|
+
var traceId = (0, _base.generateGUID)().replace(/-/g, '');
|
|
21
18
|
// 适配OpenTracing 实现(long long转成16进制最高位不能为0)
|
|
22
19
|
if (traceId[0] === '0') {
|
|
23
20
|
traceId = '1' + traceId.substring(1);
|
|
@@ -28,13 +25,26 @@ function generateTraceId() {
|
|
|
28
25
|
return traceId;
|
|
29
26
|
}
|
|
30
27
|
function generateSpanId() {
|
|
31
|
-
|
|
28
|
+
var list = Array(16);
|
|
29
|
+
for (var i = 0; i < 16; i++) {
|
|
30
|
+
list[i] = Math.floor(Math.random() * 16) + 48;
|
|
31
|
+
// valid hex characters in the range 48-57 and 97-102
|
|
32
|
+
if (list[i] >= 58) {
|
|
33
|
+
list[i] += 39;
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
return String.fromCharCode.apply(null, list);
|
|
32
37
|
}
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
38
|
+
function makeTracingHeaders(traceId, spanId, sampled, propagatorTypes, subOption) {
|
|
39
|
+
if (subOption === void 0) {
|
|
40
|
+
subOption = {};
|
|
41
|
+
}
|
|
36
42
|
var tracingHeaders = {};
|
|
37
43
|
var sample = sampled ? '1' : '0';
|
|
44
|
+
// skywalking 标准具备排他性,如果包含sw8,则只使用sw8
|
|
45
|
+
if (propagatorTypes.includes('sw8')) {
|
|
46
|
+
propagatorTypes = ['sw8'];
|
|
47
|
+
}
|
|
38
48
|
propagatorTypes.forEach(function (propagatorType) {
|
|
39
49
|
switch (propagatorType) {
|
|
40
50
|
case 'jaeger':
|
|
@@ -50,27 +60,37 @@ function makeTracingHeaders(traceId, spanId, sampled, propagatorTypes, tracestat
|
|
|
50
60
|
tracingHeaders['X-B3-Sampled'] = sample;
|
|
51
61
|
break;
|
|
52
62
|
case 'sw8':
|
|
53
|
-
//
|
|
63
|
+
// https://github.com/apache/skywalking-client-js
|
|
64
|
+
var traceIdStr = String((0, _jsBase.encode)(traceId));
|
|
65
|
+
var segmentId = String((0, _jsBase.encode)(spanId));
|
|
66
|
+
var service = String((0, _jsBase.encode)(subOption.appId));
|
|
67
|
+
var instance = String((0, _jsBase.encode)(subOption.appVersion));
|
|
68
|
+
var endpoint = String((0, _jsBase.encode)(subOption.viewName));
|
|
69
|
+
var peer = String((0, _jsBase.encode)(subOption.host));
|
|
70
|
+
tracingHeaders['sw8'] = sample + "-" + traceIdStr + "-" + segmentId + "-" + 0 + "-" + service + "-" + instance + "-" + endpoint + "-" + peer;
|
|
54
71
|
break;
|
|
55
|
-
case 'tracecontext':
|
|
72
|
+
case 'tracecontext':
|
|
56
73
|
default:
|
|
74
|
+
// https://www.w3.org/TR/trace-context/
|
|
57
75
|
tracingHeaders['traceparent'] = "00-" + traceId + "-" + spanId + "-0" + sample;
|
|
58
|
-
if (tracestate) {
|
|
59
|
-
tracingHeaders['tracestate'] = tracestate;
|
|
76
|
+
if (subOption.tracestate) {
|
|
77
|
+
tracingHeaders['tracestate'] = subOption.tracestate;
|
|
60
78
|
}
|
|
61
79
|
break;
|
|
62
80
|
}
|
|
63
81
|
});
|
|
64
|
-
if (baggage) {
|
|
65
|
-
tracingHeaders['baggage'] = baggage;
|
|
82
|
+
if (subOption.baggage) {
|
|
83
|
+
tracingHeaders['baggage'] = subOption.baggage;
|
|
66
84
|
}
|
|
67
85
|
return tracingHeaders;
|
|
68
86
|
}
|
|
69
87
|
function parseTracingOptions(tracingOption) {
|
|
88
|
+
var defPropagatorTypes = ['tracecontext'];
|
|
70
89
|
if ((0, _is.isBoolean)(tracingOption) || !tracingOption) {
|
|
71
90
|
return {
|
|
72
91
|
enable: !!tracingOption,
|
|
73
92
|
sample: 100,
|
|
93
|
+
propagatorTypes: defPropagatorTypes,
|
|
74
94
|
allowedUrls: [],
|
|
75
95
|
tracestate: true,
|
|
76
96
|
baggage: false
|
|
@@ -80,6 +100,8 @@ function parseTracingOptions(tracingOption) {
|
|
|
80
100
|
enable = _tracingOption$enable === void 0 ? true : _tracingOption$enable,
|
|
81
101
|
_tracingOption$sample = tracingOption.sample,
|
|
82
102
|
sample = _tracingOption$sample === void 0 ? 100 : _tracingOption$sample,
|
|
103
|
+
_tracingOption$propag = tracingOption.propagatorTypes,
|
|
104
|
+
propagatorTypes = _tracingOption$propag === void 0 ? defPropagatorTypes : _tracingOption$propag,
|
|
83
105
|
_tracingOption$allowe = tracingOption.allowedUrls,
|
|
84
106
|
allowedUrls = _tracingOption$allowe === void 0 ? [] : _tracingOption$allowe,
|
|
85
107
|
_tracingOption$traces = tracingOption.tracestate,
|
|
@@ -92,7 +114,7 @@ function parseTracingOptions(tracingOption) {
|
|
|
92
114
|
if ((0, _match.isMatchOption)(item)) {
|
|
93
115
|
traceOptions.push({
|
|
94
116
|
match: item,
|
|
95
|
-
propagatorTypes:
|
|
117
|
+
propagatorTypes: propagatorTypes
|
|
96
118
|
});
|
|
97
119
|
} else if (isTraceOption(item)) {
|
|
98
120
|
traceOptions.push(item);
|
|
@@ -102,6 +124,7 @@ function parseTracingOptions(tracingOption) {
|
|
|
102
124
|
return {
|
|
103
125
|
enable: (0, _is.isBoolean)(enable) ? enable : true,
|
|
104
126
|
sample: (0, _is.isNumber)(sample) ? sample : 100,
|
|
127
|
+
propagatorTypes: propagatorTypes,
|
|
105
128
|
allowedUrls: traceOptions,
|
|
106
129
|
tracestate: tracestate,
|
|
107
130
|
baggage: baggage
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@arms/rum-core",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.22",
|
|
4
4
|
"description": "arms rum javascript sdk core",
|
|
5
5
|
"author": "guangli.fj <guangli.fj@alibaba-inc.com>",
|
|
6
6
|
"license": "ISC",
|
|
@@ -22,7 +22,8 @@
|
|
|
22
22
|
"test": "node ./__tests__/@arms/core.test.js"
|
|
23
23
|
},
|
|
24
24
|
"dependencies": {
|
|
25
|
-
"events": "^3.3.0"
|
|
25
|
+
"events": "^3.3.0",
|
|
26
|
+
"js-base64": "^3.7.7"
|
|
26
27
|
},
|
|
27
28
|
"devDependencies": {
|
|
28
29
|
"typescript": "^4.9.4"
|