@arms/rum-core 0.0.20 → 0.0.23
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 +3 -3
- package/lib/utils/trace.d.ts +10 -2
- package/lib/utils/trace.js +32 -17
- 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,9 +7,9 @@ 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
|
}
|
package/lib/utils/trace.d.ts
CHANGED
|
@@ -8,6 +8,7 @@ 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;
|
|
@@ -17,6 +18,13 @@ 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,7 +25,7 @@ function generateTraceId() {
|
|
|
28
25
|
return traceId;
|
|
29
26
|
}
|
|
30
27
|
function generateSpanId() {
|
|
31
|
-
var list =
|
|
28
|
+
var list = Array(16);
|
|
32
29
|
for (var i = 0; i < 16; i++) {
|
|
33
30
|
list[i] = Math.floor(Math.random() * 16) + 48;
|
|
34
31
|
// valid hex characters in the range 48-57 and 97-102
|
|
@@ -36,13 +33,18 @@ function generateSpanId() {
|
|
|
36
33
|
list[i] += 39;
|
|
37
34
|
}
|
|
38
35
|
}
|
|
39
|
-
return String.fromCharCode.apply(null, list
|
|
36
|
+
return String.fromCharCode.apply(null, list);
|
|
40
37
|
}
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
38
|
+
function makeTracingHeaders(traceId, spanId, sampled, propagatorTypes, subOption) {
|
|
39
|
+
if (subOption === void 0) {
|
|
40
|
+
subOption = {};
|
|
41
|
+
}
|
|
44
42
|
var tracingHeaders = {};
|
|
45
43
|
var sample = sampled ? '1' : '0';
|
|
44
|
+
// skywalking 标准具备排他性,如果包含sw8,则只使用sw8
|
|
45
|
+
if (propagatorTypes.includes('sw8')) {
|
|
46
|
+
propagatorTypes = ['sw8'];
|
|
47
|
+
}
|
|
46
48
|
propagatorTypes.forEach(function (propagatorType) {
|
|
47
49
|
switch (propagatorType) {
|
|
48
50
|
case 'jaeger':
|
|
@@ -58,27 +60,37 @@ function makeTracingHeaders(traceId, spanId, sampled, propagatorTypes, tracestat
|
|
|
58
60
|
tracingHeaders['X-B3-Sampled'] = sample;
|
|
59
61
|
break;
|
|
60
62
|
case 'sw8':
|
|
61
|
-
//
|
|
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;
|
|
62
71
|
break;
|
|
63
|
-
case 'tracecontext':
|
|
72
|
+
case 'tracecontext':
|
|
64
73
|
default:
|
|
74
|
+
// https://www.w3.org/TR/trace-context/
|
|
65
75
|
tracingHeaders['traceparent'] = "00-" + traceId + "-" + spanId + "-0" + sample;
|
|
66
|
-
if (tracestate) {
|
|
67
|
-
tracingHeaders['tracestate'] = tracestate;
|
|
76
|
+
if (subOption.tracestate) {
|
|
77
|
+
tracingHeaders['tracestate'] = subOption.tracestate;
|
|
68
78
|
}
|
|
69
79
|
break;
|
|
70
80
|
}
|
|
71
81
|
});
|
|
72
|
-
if (baggage) {
|
|
73
|
-
tracingHeaders['baggage'] = baggage;
|
|
82
|
+
if (subOption.baggage) {
|
|
83
|
+
tracingHeaders['baggage'] = subOption.baggage;
|
|
74
84
|
}
|
|
75
85
|
return tracingHeaders;
|
|
76
86
|
}
|
|
77
87
|
function parseTracingOptions(tracingOption) {
|
|
88
|
+
var defPropagatorTypes = ['tracecontext'];
|
|
78
89
|
if ((0, _is.isBoolean)(tracingOption) || !tracingOption) {
|
|
79
90
|
return {
|
|
80
91
|
enable: !!tracingOption,
|
|
81
92
|
sample: 100,
|
|
93
|
+
propagatorTypes: defPropagatorTypes,
|
|
82
94
|
allowedUrls: [],
|
|
83
95
|
tracestate: true,
|
|
84
96
|
baggage: false
|
|
@@ -88,6 +100,8 @@ function parseTracingOptions(tracingOption) {
|
|
|
88
100
|
enable = _tracingOption$enable === void 0 ? true : _tracingOption$enable,
|
|
89
101
|
_tracingOption$sample = tracingOption.sample,
|
|
90
102
|
sample = _tracingOption$sample === void 0 ? 100 : _tracingOption$sample,
|
|
103
|
+
_tracingOption$propag = tracingOption.propagatorTypes,
|
|
104
|
+
propagatorTypes = _tracingOption$propag === void 0 ? defPropagatorTypes : _tracingOption$propag,
|
|
91
105
|
_tracingOption$allowe = tracingOption.allowedUrls,
|
|
92
106
|
allowedUrls = _tracingOption$allowe === void 0 ? [] : _tracingOption$allowe,
|
|
93
107
|
_tracingOption$traces = tracingOption.tracestate,
|
|
@@ -100,7 +114,7 @@ function parseTracingOptions(tracingOption) {
|
|
|
100
114
|
if ((0, _match.isMatchOption)(item)) {
|
|
101
115
|
traceOptions.push({
|
|
102
116
|
match: item,
|
|
103
|
-
propagatorTypes:
|
|
117
|
+
propagatorTypes: propagatorTypes
|
|
104
118
|
});
|
|
105
119
|
} else if (isTraceOption(item)) {
|
|
106
120
|
traceOptions.push(item);
|
|
@@ -110,6 +124,7 @@ function parseTracingOptions(tracingOption) {
|
|
|
110
124
|
return {
|
|
111
125
|
enable: (0, _is.isBoolean)(enable) ? enable : true,
|
|
112
126
|
sample: (0, _is.isNumber)(sample) ? sample : 100,
|
|
127
|
+
propagatorTypes: propagatorTypes,
|
|
113
128
|
allowedUrls: traceOptions,
|
|
114
129
|
tracestate: tracestate,
|
|
115
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.23",
|
|
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"
|