@arms/rum-core 0.0.25-beta.9 → 0.0.28
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/index.d.ts +1 -0
- package/lib/index.js +7 -0
- package/lib/model/client.d.ts +2 -2
- package/lib/model/client.js +6 -2
- package/lib/model/context.d.ts +3 -2
- package/lib/model/context.js +6 -1
- package/lib/model/reporter.d.ts +3 -2
- package/lib/model/reporter.js +63 -3
- package/lib/types/client.d.ts +23 -1
- package/lib/types/processor.d.ts +1 -0
- package/lib/types/reporter.d.ts +2 -2
- package/lib/types/rum-event.d.ts +25 -7
- package/lib/types/rum-event.js +12 -1
- package/lib/utils/base.d.ts +15 -3
- package/lib/utils/base.js +52 -17
- package/lib/utils/is.js +1 -1
- package/lib/utils/match.d.ts +1 -1
- package/lib/utils/match.js +4 -1
- package/lib/utils/number.d.ts +4 -1
- package/lib/utils/number.js +6 -4
- package/lib/utils/trace.d.ts +0 -2
- package/lib/utils/trace.js +0 -25
- package/lib/utils/verify.d.ts +2 -0
- package/lib/utils/verify.js +39 -0
- package/package.json +2 -1
package/lib/index.d.ts
CHANGED
package/lib/index.js
CHANGED
|
@@ -99,4 +99,11 @@ Object.keys(_trace).forEach(function (key) {
|
|
|
99
99
|
if (Object.prototype.hasOwnProperty.call(_exportNames, key)) return;
|
|
100
100
|
if (key in exports && exports[key] === _trace[key]) return;
|
|
101
101
|
exports[key] = _trace[key];
|
|
102
|
+
});
|
|
103
|
+
var _verify = require("./utils/verify");
|
|
104
|
+
Object.keys(_verify).forEach(function (key) {
|
|
105
|
+
if (key === "default" || key === "__esModule") return;
|
|
106
|
+
if (Object.prototype.hasOwnProperty.call(_exportNames, key)) return;
|
|
107
|
+
if (key in exports && exports[key] === _verify[key]) return;
|
|
108
|
+
exports[key] = _verify[key];
|
|
102
109
|
});
|
package/lib/model/client.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { IClient, IConfiguration, IContext } from '../types/client';
|
|
1
|
+
import { IClient, IConfiguration, IContext, IRumSession } from '../types/client';
|
|
2
2
|
import { RumEvent } from '../types/rum-event';
|
|
3
3
|
import { IProcessor } from '../types/processor';
|
|
4
4
|
import { ICollector } from '../types/collector';
|
|
@@ -9,7 +9,7 @@ declare class Client implements IClient {
|
|
|
9
9
|
private processors;
|
|
10
10
|
private reporter;
|
|
11
11
|
private ctx;
|
|
12
|
-
init(config: IConfiguration): void;
|
|
12
|
+
init(config: IConfiguration, rumSession?: IRumSession): void;
|
|
13
13
|
sendEvent: (payload: RumEvent) => void;
|
|
14
14
|
setContext(ctx: IContext): void;
|
|
15
15
|
getContext(): IContext;
|
package/lib/model/client.js
CHANGED
|
@@ -6,6 +6,7 @@ exports["default"] = void 0;
|
|
|
6
6
|
var _client = require("../types/client");
|
|
7
7
|
var _events = require("events");
|
|
8
8
|
var _context = _interopRequireDefault(require("./context"));
|
|
9
|
+
var _is = require("../utils/is");
|
|
9
10
|
function _createForOfIteratorHelperLoose(o, allowArrayLike) { var it = typeof Symbol !== "undefined" && o[Symbol.iterator] || o["@@iterator"]; if (it) return (it = it.call(o)).next.bind(it); if (Array.isArray(o) || (it = _unsupportedIterableToArray(o)) || allowArrayLike && o && typeof o.length === "number") { if (it) o = it; var i = 0; return function () { if (i >= o.length) return { done: true }; return { done: false, value: o[i++] }; }; } throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
|
|
10
11
|
function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
|
|
11
12
|
function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i]; return arr2; }
|
|
@@ -22,13 +23,16 @@ var Client = /*#__PURE__*/function () {
|
|
|
22
23
|
};
|
|
23
24
|
}
|
|
24
25
|
var _proto = Client.prototype;
|
|
25
|
-
_proto.init = function init(config) {
|
|
26
|
+
_proto.init = function init(config, rumSession) {
|
|
26
27
|
var _this2 = this;
|
|
27
|
-
this.ctx = new _context["default"](config);
|
|
28
|
+
this.ctx = new _context["default"](config, rumSession);
|
|
28
29
|
var ctx = this.ctx,
|
|
29
30
|
collectors = this.collectors,
|
|
30
31
|
processors = this.processors,
|
|
31
32
|
reporter = this.reporter;
|
|
33
|
+
processors.forEach(function (processor) {
|
|
34
|
+
(0, _is.isFunction)(processor.setup) && processor.setup(ctx);
|
|
35
|
+
});
|
|
32
36
|
|
|
33
37
|
// collector 收集数据统一 push 到各匹配 processor
|
|
34
38
|
this.emitter.on(_client.EventType.collect, function (payload) {
|
package/lib/model/context.d.ts
CHANGED
|
@@ -1,10 +1,11 @@
|
|
|
1
|
-
import { IConfiguration, IContext } from "../types/client";
|
|
1
|
+
import { IConfiguration, IContext, IRumSession } from "../types/client";
|
|
2
2
|
import { RumEvent, IViewData } from "../types/rum-event";
|
|
3
3
|
declare class Context implements IContext {
|
|
4
4
|
private config;
|
|
5
5
|
private rumEvent;
|
|
6
6
|
private views;
|
|
7
|
-
|
|
7
|
+
session: IRumSession;
|
|
8
|
+
constructor(config: IConfiguration, rumSession?: IRumSession);
|
|
8
9
|
getConfig(): IConfiguration;
|
|
9
10
|
setConfig(config: IConfiguration): void;
|
|
10
11
|
getViews(): IViewData[];
|
package/lib/model/context.js
CHANGED
|
@@ -3,10 +3,15 @@
|
|
|
3
3
|
exports.__esModule = true;
|
|
4
4
|
exports["default"] = void 0;
|
|
5
5
|
var Context = /*#__PURE__*/function () {
|
|
6
|
-
function Context(config) {
|
|
6
|
+
function Context(config, rumSession) {
|
|
7
7
|
this.config = config;
|
|
8
8
|
this.rumEvent = void 0;
|
|
9
9
|
this.views = [];
|
|
10
|
+
this.session = void 0;
|
|
11
|
+
if (rumSession) {
|
|
12
|
+
this.session = rumSession;
|
|
13
|
+
this.session.init(this);
|
|
14
|
+
}
|
|
10
15
|
}
|
|
11
16
|
var _proto = Context.prototype;
|
|
12
17
|
_proto.getConfig = function getConfig() {
|
package/lib/model/reporter.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { IContext } from '../types/client';
|
|
2
|
-
import { RumEvent, IViewData } from '../types/rum-event';
|
|
2
|
+
import { RumEvent, IViewData, RumEventBundle } from '../types/rum-event';
|
|
3
3
|
export default abstract class Reporter {
|
|
4
4
|
name: string;
|
|
5
5
|
/**
|
|
@@ -19,10 +19,11 @@ export default abstract class Reporter {
|
|
|
19
19
|
* eventQueue 提取最新 View 作为公共 View,event 中不同 View Event 保留 View id
|
|
20
20
|
*/
|
|
21
21
|
protected flushEventQueue(): void;
|
|
22
|
+
mergeEvent(ctx: IContext, events: RumEvent[], view: IViewData): void;
|
|
22
23
|
/**
|
|
23
24
|
* 接口请求由各平台 sdk reporter 实现
|
|
24
25
|
*/
|
|
25
|
-
abstract request(ctx: IContext,
|
|
26
|
+
abstract request(ctx: IContext, bundle: RumEventBundle): void;
|
|
26
27
|
/**
|
|
27
28
|
* 初始化时
|
|
28
29
|
*/
|
package/lib/model/reporter.js
CHANGED
|
@@ -5,8 +5,10 @@ exports["default"] = void 0;
|
|
|
5
5
|
var _rumEvent = require("../types/rum-event");
|
|
6
6
|
var _exception = require("../utils/exception");
|
|
7
7
|
var _is = require("../utils/is");
|
|
8
|
+
var _verify = require("../utils/verify");
|
|
8
9
|
var FLUSH_TIME = 3000;
|
|
9
10
|
var MAX_EVENT_COUNT = 20;
|
|
11
|
+
var attrs = ['app', 'user', 'device', 'os', 'geo', 'isp', 'net', 'properties'];
|
|
10
12
|
var Reporter = exports["default"] = /*#__PURE__*/function () {
|
|
11
13
|
function Reporter() {
|
|
12
14
|
this.name = 'reporter';
|
|
@@ -108,19 +110,77 @@ var Reporter = exports["default"] = /*#__PURE__*/function () {
|
|
|
108
110
|
var curView = views[views.length - 1];
|
|
109
111
|
views.forEach(function (view) {
|
|
110
112
|
if (view.id === curView.id) {
|
|
111
|
-
var
|
|
113
|
+
var events = eventQueue.filter(function (event) {
|
|
112
114
|
var _event$view;
|
|
113
115
|
return ((_event$view = event.view) === null || _event$view === void 0 ? void 0 : _event$view.id) === view.id;
|
|
114
116
|
});
|
|
115
|
-
|
|
117
|
+
events.forEach(function (event) {
|
|
116
118
|
delete event.view;
|
|
117
119
|
});
|
|
118
120
|
}
|
|
119
121
|
});
|
|
120
|
-
|
|
122
|
+
var session = ctx.session;
|
|
123
|
+
var sampled = session ? session.getSampled() : true;
|
|
124
|
+
sampled && this.mergeEvent(ctx, eventQueue, curView);
|
|
121
125
|
this.eventQueue = [];
|
|
126
|
+
};
|
|
127
|
+
_proto.mergeEvent = function mergeEvent(ctx, events, view) {
|
|
128
|
+
var _config$net;
|
|
129
|
+
var config = ctx.getConfig();
|
|
130
|
+
var session = ctx.session;
|
|
131
|
+
var sessionId = session.getSessionId();
|
|
132
|
+
events.forEach(function (event) {
|
|
133
|
+
if (event.session_id === sessionId) {
|
|
134
|
+
delete event.session_id;
|
|
135
|
+
}
|
|
136
|
+
});
|
|
137
|
+
var bundle = {
|
|
138
|
+
app: {
|
|
139
|
+
id: config.pid,
|
|
140
|
+
env: config.env || 'prod',
|
|
141
|
+
version: config.version,
|
|
142
|
+
type: ''
|
|
143
|
+
},
|
|
144
|
+
user: {
|
|
145
|
+
id: session.getUserId()
|
|
146
|
+
},
|
|
147
|
+
session: {
|
|
148
|
+
id: sessionId
|
|
149
|
+
},
|
|
150
|
+
net: {
|
|
151
|
+
model: (_config$net = config.net) === null || _config$net === void 0 ? void 0 : _config$net.model
|
|
152
|
+
},
|
|
153
|
+
view: view,
|
|
154
|
+
events: events
|
|
155
|
+
};
|
|
156
|
+
attrs.forEach(function (key) {
|
|
157
|
+
var obj = config[key];
|
|
158
|
+
(0, _is.isObject)(obj) && Object.keys(obj).forEach(function (k) {
|
|
159
|
+
var val = obj[k];
|
|
160
|
+
// user 的 id 不能被覆盖
|
|
161
|
+
if (key === 'user' && k === 'id') return;
|
|
162
|
+
if (key === 'properties') {
|
|
163
|
+
bundle[key] = (0, _verify.verifyProperties)(obj);
|
|
164
|
+
} else if ((0, _is.isString)(val) || (0, _is.isNumber)(val)) {
|
|
165
|
+
if (!(key in bundle)) {
|
|
166
|
+
bundle[key] = {};
|
|
167
|
+
}
|
|
168
|
+
bundle[key][k] = val;
|
|
169
|
+
}
|
|
170
|
+
});
|
|
171
|
+
});
|
|
172
|
+
if (typeof config.beforeReport === 'function') {
|
|
173
|
+
bundle = config.beforeReport(bundle);
|
|
174
|
+
if (!bundle) return;
|
|
175
|
+
}
|
|
176
|
+
this.request(ctx, bundle);
|
|
122
177
|
}
|
|
123
178
|
|
|
179
|
+
// /**
|
|
180
|
+
// * 接口请求由各平台 sdk reporter 实现
|
|
181
|
+
// */
|
|
182
|
+
// abstract request(ctx: IContext, events: RumEvent[], view: IViewData): void;
|
|
183
|
+
|
|
124
184
|
/**
|
|
125
185
|
* 接口请求由各平台 sdk reporter 实现
|
|
126
186
|
*/;
|
package/lib/types/client.d.ts
CHANGED
|
@@ -10,6 +10,7 @@ export declare enum EventType {
|
|
|
10
10
|
collect = "collect"
|
|
11
11
|
}
|
|
12
12
|
export interface IContext {
|
|
13
|
+
session: IRumSession;
|
|
13
14
|
/**
|
|
14
15
|
* user config
|
|
15
16
|
*/
|
|
@@ -28,11 +29,28 @@ export interface IContext {
|
|
|
28
29
|
setRumEvent(rumEvent: RumEvent): void;
|
|
29
30
|
[key: string]: any;
|
|
30
31
|
}
|
|
32
|
+
export interface SessionConfig {
|
|
33
|
+
sampleRate?: number;
|
|
34
|
+
maxDuration?: number;
|
|
35
|
+
overtime?: number;
|
|
36
|
+
storage?: string;
|
|
37
|
+
}
|
|
38
|
+
export interface IRumSession {
|
|
39
|
+
init(ctx: IContext): void;
|
|
40
|
+
sessionConfig: SessionConfig;
|
|
41
|
+
getSessionId: () => string;
|
|
42
|
+
getSampled: () => boolean;
|
|
43
|
+
updateSession: () => void;
|
|
44
|
+
getEventId: () => string;
|
|
45
|
+
getViewId: () => string;
|
|
46
|
+
getUserId: () => string;
|
|
47
|
+
getBaseEvent: () => any;
|
|
48
|
+
}
|
|
31
49
|
export interface IClient {
|
|
32
50
|
/**
|
|
33
51
|
* 初始化
|
|
34
52
|
*/
|
|
35
|
-
init: (configuration: IConfiguration) => void;
|
|
53
|
+
init: (configuration: IConfiguration, rumSession?: IRumSession) => void;
|
|
36
54
|
/**
|
|
37
55
|
* 业务自定义上传数据
|
|
38
56
|
*/
|
|
@@ -95,6 +113,10 @@ export interface IConfiguration {
|
|
|
95
113
|
flushTime?: number;
|
|
96
114
|
maxEventCount?: number;
|
|
97
115
|
};
|
|
116
|
+
/**
|
|
117
|
+
* session config
|
|
118
|
+
*/
|
|
119
|
+
sessionConfig?: SessionConfig;
|
|
98
120
|
/**
|
|
99
121
|
* 各个 collector config
|
|
100
122
|
*/
|
package/lib/types/processor.d.ts
CHANGED
package/lib/types/reporter.d.ts
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import { IContext } from './client';
|
|
2
|
-
import {
|
|
2
|
+
import { RumEventBundle } from './rum-event';
|
|
3
3
|
export interface IReporter {
|
|
4
4
|
name: string;
|
|
5
5
|
init(ctx: IContext): void;
|
|
6
|
-
request(ctx: IContext,
|
|
6
|
+
request(ctx: IContext, bundle: RumEventBundle): void;
|
|
7
7
|
report(ctx: IContext): void;
|
|
8
8
|
}
|
package/lib/types/rum-event.d.ts
CHANGED
|
@@ -4,7 +4,8 @@ export declare enum RumEventType {
|
|
|
4
4
|
EXCEPTION = "exception",
|
|
5
5
|
LONG_TASK = "longtask",
|
|
6
6
|
ACTION = "action",
|
|
7
|
-
CUSTOM = "custom"
|
|
7
|
+
CUSTOM = "custom",
|
|
8
|
+
APPLICATION = "application"
|
|
8
9
|
}
|
|
9
10
|
export interface BaseObject {
|
|
10
11
|
[key: string]: BaseObjectValue;
|
|
@@ -15,14 +16,15 @@ export interface IViewData {
|
|
|
15
16
|
}
|
|
16
17
|
export type BaseObjectPrimitiveValue = string | number | boolean | null | undefined;
|
|
17
18
|
export type BaseObjectValue = BaseObjectPrimitiveValue | BaseObject | BaseObjectPrimitiveValue[] | IViewData;
|
|
18
|
-
export interface RumBaseEvent {
|
|
19
|
+
export interface RumBaseEvent extends BaseObject {
|
|
19
20
|
event_type?: RumEventType;
|
|
20
21
|
event_id?: string;
|
|
22
|
+
session_id?: string;
|
|
21
23
|
timestamp?: number;
|
|
22
24
|
times?: number;
|
|
23
25
|
view?: IViewData;
|
|
24
26
|
snapshots?: string;
|
|
25
|
-
|
|
27
|
+
properties?: BaseObject;
|
|
26
28
|
}
|
|
27
29
|
export interface RumViewEvent extends RumBaseEvent {
|
|
28
30
|
loading_type?: 'initial_load' | 'route_change';
|
|
@@ -42,13 +44,21 @@ export interface RumViewEvent extends RumBaseEvent {
|
|
|
42
44
|
url?: string;
|
|
43
45
|
timing_data?: string;
|
|
44
46
|
}
|
|
47
|
+
/**
|
|
48
|
+
* 枚举值,表示资源是否加载成功
|
|
49
|
+
*/
|
|
50
|
+
export declare enum ResourceStatus {
|
|
51
|
+
Unknown = -1,
|
|
52
|
+
Failed = 0,
|
|
53
|
+
Success = 1
|
|
54
|
+
}
|
|
45
55
|
export interface RumResourceEvent extends RumBaseEvent {
|
|
46
56
|
name?: string;
|
|
47
57
|
type?: string;
|
|
48
58
|
method?: string;
|
|
49
59
|
status_code?: number | string;
|
|
50
60
|
message?: string;
|
|
51
|
-
success?: -1 | 0 | 1;
|
|
61
|
+
success?: -1 | 0 | 1 | ResourceStatus;
|
|
52
62
|
trace_id?: string;
|
|
53
63
|
duration?: number;
|
|
54
64
|
size?: number;
|
|
@@ -93,15 +103,21 @@ export interface RumCustomEvent extends RumBaseEvent {
|
|
|
93
103
|
group?: string;
|
|
94
104
|
value: number;
|
|
95
105
|
}
|
|
96
|
-
export
|
|
106
|
+
export interface RumApplicationEvent extends RumBaseEvent {
|
|
107
|
+
type: string;
|
|
108
|
+
name: string;
|
|
109
|
+
duration: number;
|
|
110
|
+
}
|
|
111
|
+
export type RumEvent = RumViewEvent | RumResourceEvent | RumExceptionEvent | RumActionEvent | RumCustomEvent | RumApplicationEvent;
|
|
97
112
|
export declare enum AppType {
|
|
98
113
|
browser = "browser",
|
|
99
|
-
miniapp = "miniapp"
|
|
114
|
+
miniapp = "miniapp",
|
|
115
|
+
uniapp = "uniapp"
|
|
100
116
|
}
|
|
101
117
|
export interface RumEventBundle {
|
|
102
118
|
app: {
|
|
103
119
|
id: string;
|
|
104
|
-
type: AppType;
|
|
120
|
+
type: AppType | string;
|
|
105
121
|
name?: string;
|
|
106
122
|
version?: string;
|
|
107
123
|
channel?: string;
|
|
@@ -150,4 +166,6 @@ export interface RumEventBundle {
|
|
|
150
166
|
name?: string;
|
|
151
167
|
};
|
|
152
168
|
events: Array<RumEvent>;
|
|
169
|
+
properties?: BaseObject;
|
|
170
|
+
_v?: string;
|
|
153
171
|
}
|
package/lib/types/rum-event.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
|
|
3
3
|
exports.__esModule = true;
|
|
4
|
-
exports.RumEventType = exports.AppType = void 0;
|
|
4
|
+
exports.RumEventType = exports.ResourceStatus = exports.AppType = void 0;
|
|
5
5
|
var RumEventType = exports.RumEventType = /*#__PURE__*/function (RumEventType) {
|
|
6
6
|
RumEventType["VIEW"] = "view";
|
|
7
7
|
RumEventType["RESOURCE"] = "resource";
|
|
@@ -9,10 +9,21 @@ var RumEventType = exports.RumEventType = /*#__PURE__*/function (RumEventType) {
|
|
|
9
9
|
RumEventType["LONG_TASK"] = "longtask";
|
|
10
10
|
RumEventType["ACTION"] = "action";
|
|
11
11
|
RumEventType["CUSTOM"] = "custom";
|
|
12
|
+
RumEventType["APPLICATION"] = "application";
|
|
12
13
|
return RumEventType;
|
|
13
14
|
}({});
|
|
15
|
+
/**
|
|
16
|
+
* 枚举值,表示资源是否加载成功
|
|
17
|
+
*/
|
|
18
|
+
var ResourceStatus = exports.ResourceStatus = /*#__PURE__*/function (ResourceStatus) {
|
|
19
|
+
ResourceStatus[ResourceStatus["Unknown"] = -1] = "Unknown";
|
|
20
|
+
ResourceStatus[ResourceStatus["Failed"] = 0] = "Failed";
|
|
21
|
+
ResourceStatus[ResourceStatus["Success"] = 1] = "Success";
|
|
22
|
+
return ResourceStatus;
|
|
23
|
+
}({});
|
|
14
24
|
var AppType = exports.AppType = /*#__PURE__*/function (AppType) {
|
|
15
25
|
AppType["browser"] = "browser";
|
|
16
26
|
AppType["miniapp"] = "miniapp";
|
|
27
|
+
AppType["uniapp"] = "uniapp";
|
|
17
28
|
return AppType;
|
|
18
29
|
}({});
|
package/lib/utils/base.d.ts
CHANGED
|
@@ -8,11 +8,23 @@ export declare function interceptFunction(target: any, name: string, callback: F
|
|
|
8
8
|
*/
|
|
9
9
|
export declare function generateGUID(): string;
|
|
10
10
|
/**
|
|
11
|
-
* @description:
|
|
12
|
-
* @param len {String}
|
|
11
|
+
* @description: 随机生成 traceId 或 sessionId
|
|
13
12
|
* @return {String}
|
|
14
13
|
*/
|
|
15
|
-
export declare function
|
|
14
|
+
export declare function generateTraceId(): string;
|
|
15
|
+
/**
|
|
16
|
+
* @description: 随机生成 spanId
|
|
17
|
+
* @param len {number}
|
|
18
|
+
* @param radix {number}
|
|
19
|
+
* @return {String}
|
|
20
|
+
*/
|
|
21
|
+
export declare function generateSpanId(len?: number, radix?: number): string;
|
|
22
|
+
/**
|
|
23
|
+
* @description: 随机生成 eventId
|
|
24
|
+
* @param sessionId {string} sessionId
|
|
25
|
+
* @return {String}
|
|
26
|
+
*/
|
|
27
|
+
export declare function generateEventId(sessionId: string): string;
|
|
16
28
|
/**
|
|
17
29
|
* @desc 函数防抖
|
|
18
30
|
* @param func 回调函数
|
package/lib/utils/base.js
CHANGED
|
@@ -3,8 +3,10 @@
|
|
|
3
3
|
exports.__esModule = true;
|
|
4
4
|
exports.debounce = debounce;
|
|
5
5
|
exports.delay = delay;
|
|
6
|
+
exports.generateEventId = generateEventId;
|
|
6
7
|
exports.generateGUID = generateGUID;
|
|
7
|
-
exports.
|
|
8
|
+
exports.generateSpanId = generateSpanId;
|
|
9
|
+
exports.generateTraceId = generateTraceId;
|
|
8
10
|
exports.interceptFunction = interceptFunction;
|
|
9
11
|
var _is = require("./is");
|
|
10
12
|
/**
|
|
@@ -17,7 +19,7 @@ function interceptFunction(target, name, callback) {
|
|
|
17
19
|
args[_key] = arguments[_key];
|
|
18
20
|
}
|
|
19
21
|
callback.apply(this, args);
|
|
20
|
-
if (registeredMethod) {
|
|
22
|
+
if ((0, _is.isFunction)(registeredMethod)) {
|
|
21
23
|
return registeredMethod.apply(this, args);
|
|
22
24
|
}
|
|
23
25
|
};
|
|
@@ -32,8 +34,18 @@ function generateGUID() {
|
|
|
32
34
|
try {
|
|
33
35
|
if (crypto && crypto.randomUUID) {
|
|
34
36
|
guid = crypto.randomUUID();
|
|
37
|
+
} else if (crypto && crypto.getRandomValues) {
|
|
38
|
+
var buf = new Uint8Array(16);
|
|
39
|
+
crypto.getRandomValues(buf);
|
|
40
|
+
buf[6] = buf[6] & 0x0f | 0x40; // version 4 UUID
|
|
41
|
+
buf[8] = buf[8] & 0x3f | 0x80; // variant 10xxxxxx
|
|
42
|
+
guid = buf.reduce(function (str, _byte) {
|
|
43
|
+
return str + _byte.toString(16).padStart(2, '0');
|
|
44
|
+
}, '').replace(/^(.{8})(.{4})(.{4})(.{4})(.{12})$/, '\$1-\$2-\$3-\$4-\$5');
|
|
35
45
|
}
|
|
36
|
-
} catch (e) {
|
|
46
|
+
} catch (e) {
|
|
47
|
+
//
|
|
48
|
+
}
|
|
37
49
|
if (!guid) {
|
|
38
50
|
guid = 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) {
|
|
39
51
|
var r = Math.random() * 16 | 0,
|
|
@@ -45,26 +57,49 @@ function generateGUID() {
|
|
|
45
57
|
}
|
|
46
58
|
|
|
47
59
|
/**
|
|
48
|
-
* @description:
|
|
49
|
-
* @param len {String}
|
|
60
|
+
* @description: 随机生成 traceId 或 sessionId
|
|
50
61
|
* @return {String}
|
|
51
62
|
*/
|
|
52
|
-
function
|
|
63
|
+
function generateTraceId() {
|
|
64
|
+
var traceId = generateGUID().replace(/-/g, '');
|
|
65
|
+
// 适配OpenTracing 实现(long long转成16进制最高位不能为0)
|
|
66
|
+
if (traceId[0] === '0') {
|
|
67
|
+
traceId = '1' + traceId.substring(1);
|
|
68
|
+
}
|
|
69
|
+
if (traceId[16] === '0') {
|
|
70
|
+
traceId = traceId.substring(0, 16) + '1' + traceId.substring(17);
|
|
71
|
+
}
|
|
72
|
+
return traceId;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
/**
|
|
76
|
+
* @description: 随机生成 spanId
|
|
77
|
+
* @param len {number}
|
|
78
|
+
* @param radix {number}
|
|
79
|
+
* @return {String}
|
|
80
|
+
*/
|
|
81
|
+
function generateSpanId(len, radix) {
|
|
53
82
|
if (len === void 0) {
|
|
54
|
-
len =
|
|
83
|
+
len = 16;
|
|
55
84
|
}
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
var r, c;
|
|
59
|
-
while (len-- > 0) {
|
|
60
|
-
r = Math.random() * 36 | 0;
|
|
61
|
-
c = r.toString(36);
|
|
62
|
-
str[len] = r % 3 ? c : c.toUpperCase();
|
|
85
|
+
if (radix === void 0) {
|
|
86
|
+
radix = 16;
|
|
63
87
|
}
|
|
64
|
-
|
|
65
|
-
|
|
88
|
+
var result = '';
|
|
89
|
+
for (var i = 0; i < len; i++) {
|
|
90
|
+
result += Math.floor(Math.random() * radix).toString(radix);
|
|
66
91
|
}
|
|
67
|
-
return
|
|
92
|
+
return result;
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
/**
|
|
96
|
+
* @description: 随机生成 eventId
|
|
97
|
+
* @param sessionId {string} sessionId
|
|
98
|
+
* @return {String}
|
|
99
|
+
*/
|
|
100
|
+
function generateEventId(sessionId) {
|
|
101
|
+
var spanId = generateSpanId();
|
|
102
|
+
return "00-" + sessionId + "-" + spanId;
|
|
68
103
|
}
|
|
69
104
|
|
|
70
105
|
/**
|
package/lib/utils/is.js
CHANGED
|
@@ -33,7 +33,7 @@ var isNumber = exports.isNumber = function isNumber(value) {
|
|
|
33
33
|
return isTypeof(value, 'number') && !isNaN(value) || isTypeof(value, 'bigint');
|
|
34
34
|
};
|
|
35
35
|
var isNull = exports.isNull = function isNull(value) {
|
|
36
|
-
return
|
|
36
|
+
return value === null;
|
|
37
37
|
};
|
|
38
38
|
var isObject = exports.isObject = function isObject(value) {
|
|
39
39
|
return !isNull(value) && isTypeof(value, 'object');
|
package/lib/utils/match.d.ts
CHANGED
|
@@ -4,4 +4,4 @@ export declare function isMatchOption(item: unknown): item is MatchOption;
|
|
|
4
4
|
* 如果值有一个选项匹配,则返回true。在比较字符串时,useStartsWith: true时将把值与选项的开始进行比较,而不是要求精确匹配。
|
|
5
5
|
*/
|
|
6
6
|
export declare function matchList(list: MatchOption[], value: string, useStartsWith?: boolean): boolean;
|
|
7
|
-
export declare function urlMatch(url: string, list?: MatchOption[]): boolean;
|
|
7
|
+
export declare function urlMatch(url: string, list?: MatchOption | MatchOption[]): boolean;
|
package/lib/utils/match.js
CHANGED
|
@@ -35,7 +35,10 @@ function urlMatch(url, list) {
|
|
|
35
35
|
list = [];
|
|
36
36
|
}
|
|
37
37
|
try {
|
|
38
|
-
|
|
38
|
+
if (!(0, _is.isArray)(list)) {
|
|
39
|
+
list = [list];
|
|
40
|
+
}
|
|
41
|
+
var rules = [/https?:\/\/.*rum|retcode\.aliyuncs\.com/, /https?:\/\/.*log-global\.aliyuncs\.com/, /https?:\/\/.*\.mmstat\.com/, /data:(.+?)(;base64)?,(.+)$/].concat(list);
|
|
39
42
|
return matchList(rules, url);
|
|
40
43
|
} catch (e) {
|
|
41
44
|
return false;
|
package/lib/utils/number.d.ts
CHANGED
|
@@ -1,9 +1,12 @@
|
|
|
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;
|
|
1
5
|
/**
|
|
2
6
|
* Return true if the draw is successful
|
|
3
7
|
* @param threshold between 0 and 100
|
|
4
8
|
*/
|
|
5
9
|
export declare function performDraw(threshold: number): boolean;
|
|
6
|
-
export declare function round(num: number, decimals: 0 | 1 | 2 | 3 | 4): number;
|
|
7
10
|
/**
|
|
8
11
|
* 保留指定位数的小数
|
|
9
12
|
* @param num 原数据
|
package/lib/utils/number.js
CHANGED
|
@@ -1,9 +1,14 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
|
|
3
3
|
exports.__esModule = true;
|
|
4
|
+
exports.ONE_SECOND = exports.ONE_MINUTE = exports.ONE_HOUR = exports.ONE_DAY = void 0;
|
|
4
5
|
exports.formatNumber = formatNumber;
|
|
5
6
|
exports.performDraw = performDraw;
|
|
6
|
-
exports.
|
|
7
|
+
var ONE_SECOND = exports.ONE_SECOND = 1e3;
|
|
8
|
+
var ONE_MINUTE = exports.ONE_MINUTE = 60 * ONE_SECOND;
|
|
9
|
+
var ONE_HOUR = exports.ONE_HOUR = 60 * ONE_MINUTE;
|
|
10
|
+
var ONE_DAY = exports.ONE_DAY = 24 * ONE_HOUR;
|
|
11
|
+
|
|
7
12
|
/**
|
|
8
13
|
* Return true if the draw is successful
|
|
9
14
|
* @param threshold between 0 and 100
|
|
@@ -11,9 +16,6 @@ exports.round = round;
|
|
|
11
16
|
function performDraw(threshold) {
|
|
12
17
|
return threshold !== 0 && Math.random() * 100 <= threshold;
|
|
13
18
|
}
|
|
14
|
-
function round(num, decimals) {
|
|
15
|
-
return +num.toFixed(decimals);
|
|
16
|
-
}
|
|
17
19
|
|
|
18
20
|
/**
|
|
19
21
|
* 保留指定位数的小数
|
package/lib/utils/trace.d.ts
CHANGED
package/lib/utils/trace.js
CHANGED
|
@@ -1,40 +1,15 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
|
|
3
3
|
exports.__esModule = true;
|
|
4
|
-
exports.generateSpanId = generateSpanId;
|
|
5
|
-
exports.generateTraceId = generateTraceId;
|
|
6
4
|
exports.isTraceOption = isTraceOption;
|
|
7
5
|
exports.makeTracingHeaders = makeTracingHeaders;
|
|
8
6
|
exports.parseTracingOptions = parseTracingOptions;
|
|
9
7
|
var _match = require("./match");
|
|
10
8
|
var _is = require("./is");
|
|
11
|
-
var _base = require("./base");
|
|
12
9
|
var _jsBase = require("js-base64");
|
|
13
10
|
function isTraceOption(option) {
|
|
14
11
|
return option && (0, _match.isMatchOption)(option.match) && (0, _is.isArray)(option.propagatorTypes);
|
|
15
12
|
}
|
|
16
|
-
function generateTraceId() {
|
|
17
|
-
var traceId = (0, _base.generateGUID)().replace(/-/g, '');
|
|
18
|
-
// 适配OpenTracing 实现(long long转成16进制最高位不能为0)
|
|
19
|
-
if (traceId[0] === '0') {
|
|
20
|
-
traceId = '1' + traceId.substring(1);
|
|
21
|
-
}
|
|
22
|
-
if (traceId[16] === '0') {
|
|
23
|
-
traceId = traceId.substring(0, 16) + '1' + traceId.substring(17);
|
|
24
|
-
}
|
|
25
|
-
return traceId;
|
|
26
|
-
}
|
|
27
|
-
function generateSpanId() {
|
|
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);
|
|
37
|
-
}
|
|
38
13
|
function makeTracingHeaders(traceId, spanId, sampled, propagatorTypes, subOption) {
|
|
39
14
|
if (subOption === void 0) {
|
|
40
15
|
subOption = {};
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
exports.__esModule = true;
|
|
4
|
+
exports.verifyProperties = verifyProperties;
|
|
5
|
+
var _is = require("./is");
|
|
6
|
+
function verifyProperties(attrs) {
|
|
7
|
+
if (!(0, _is.isObject)(attrs)) {
|
|
8
|
+
return;
|
|
9
|
+
}
|
|
10
|
+
var copyAttrs = Object.assign({}, attrs);
|
|
11
|
+
Object.keys(copyAttrs).forEach(function (key) {
|
|
12
|
+
var val = copyAttrs[key];
|
|
13
|
+
// 验证 key 长度
|
|
14
|
+
if (key.length > 50) {
|
|
15
|
+
var shortKey = key.substring(0, 50);
|
|
16
|
+
copyAttrs[shortKey] = val;
|
|
17
|
+
delete copyAttrs[key];
|
|
18
|
+
key = shortKey;
|
|
19
|
+
}
|
|
20
|
+
// 验证 value 类型和长度
|
|
21
|
+
if (!(0, _is.isString)(val) && !(0, _is.isNumber)(val)) {
|
|
22
|
+
delete copyAttrs[key];
|
|
23
|
+
} else if ((0, _is.isString)(val) && val.length > 2e3) {
|
|
24
|
+
copyAttrs[key] = val.substring(0, 2e3);
|
|
25
|
+
}
|
|
26
|
+
});
|
|
27
|
+
var keys = Object.keys(copyAttrs);
|
|
28
|
+
if (!keys.length) {
|
|
29
|
+
return;
|
|
30
|
+
}
|
|
31
|
+
if (keys.length > 20) {
|
|
32
|
+
keys.forEach(function (key, index) {
|
|
33
|
+
if (index > 19) {
|
|
34
|
+
delete copyAttrs[key];
|
|
35
|
+
}
|
|
36
|
+
});
|
|
37
|
+
}
|
|
38
|
+
return copyAttrs;
|
|
39
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@arms/rum-core",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.28",
|
|
4
4
|
"description": "arms rum javascript sdk core",
|
|
5
5
|
"author": "guangli.fj <guangli.fj@alibaba-inc.com>",
|
|
6
6
|
"license": "ISC",
|
|
@@ -26,6 +26,7 @@
|
|
|
26
26
|
"js-base64": "^3.7.7"
|
|
27
27
|
},
|
|
28
28
|
"devDependencies": {
|
|
29
|
+
"@babel/runtime": "^7.24.5",
|
|
29
30
|
"typescript": "^4.9.4"
|
|
30
31
|
}
|
|
31
32
|
}
|