@arms/rum-core 0.0.22 → 0.0.25-beta.11

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.
@@ -6,7 +6,7 @@ var _rumEvent = require("../types/rum-event");
6
6
  var _exception = require("../utils/exception");
7
7
  var _is = require("../utils/is");
8
8
  var FLUSH_TIME = 3000;
9
- var MAX_EVENT_COUNT = 50;
9
+ var MAX_EVENT_COUNT = 20;
10
10
  var Reporter = exports["default"] = /*#__PURE__*/function () {
11
11
  function Reporter() {
12
12
  this.name = 'reporter';
@@ -1,5 +1,5 @@
1
1
  import { IClient, IConfiguration } from "../types/client";
2
- import { RumEvent } from "../types/rum-event";
2
+ import { RumCustomEvent, RumEvent, RumExceptionEvent, RumResourceEvent } from "../types/rum-event";
3
3
  import { IShell } from "../types/shell";
4
4
  export default abstract class Shell implements IShell {
5
5
  client: IClient;
@@ -19,7 +19,15 @@ export default abstract class Shell implements IShell {
19
19
  abstract setConfig<T extends keyof IConfiguration>(key: T, value: IConfiguration[T]): void;
20
20
  abstract setConfig(value: IConfiguration): void;
21
21
  /**
22
- * 自定义上传数据
22
+ * 自定义事件上报
23
23
  */
24
- sendCustom(payload: RumEvent): void;
24
+ sendCustom(payload: RumCustomEvent): void;
25
+ /**
26
+ * 自定义异常上报
27
+ */
28
+ sendException(payload: RumExceptionEvent): void;
29
+ /**
30
+ * 自定义资源上报
31
+ */
32
+ sendResource(payload: RumResourceEvent): void;
25
33
  }
@@ -5,11 +5,12 @@ exports.__esModule = true;
5
5
  exports["default"] = void 0;
6
6
  var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends"));
7
7
  var _rumEvent = require("../types/rum-event");
8
- var _rumCore = require("@arms/rum-core");
8
+ var _client = _interopRequireDefault(require("../model/client"));
9
+ var _is = require("../utils/is");
9
10
  var Shell = exports["default"] = /*#__PURE__*/function () {
10
11
  function Shell(config) {
11
12
  this.client = void 0;
12
- this.client = new _rumCore.Client();
13
+ this.client = new _client["default"]();
13
14
  if (config && this.init) {
14
15
  this.init(config);
15
16
  }
@@ -38,7 +39,7 @@ var Shell = exports["default"] = /*#__PURE__*/function () {
38
39
  * set config
39
40
  */;
40
41
  /**
41
- * 自定义上传数据
42
+ * 自定义事件上报
42
43
  */
43
44
  _proto.sendCustom = function sendCustom(payload) {
44
45
  if (!payload.name || !payload.type) {
@@ -49,5 +50,35 @@ var Shell = exports["default"] = /*#__PURE__*/function () {
49
50
  });
50
51
  this.sendEvent(data);
51
52
  };
53
+ /**
54
+ * 自定义异常上报
55
+ */
56
+ _proto.sendException = function sendException(payload) {
57
+ if (!payload.name || !payload.message) {
58
+ return;
59
+ }
60
+ var data = (0, _extends2["default"])({
61
+ times: 1
62
+ }, payload, {
63
+ event_type: _rumEvent.RumEventType.EXCEPTION,
64
+ type: 'custom',
65
+ source: 'custom'
66
+ });
67
+ this.sendEvent(data);
68
+ };
69
+ /**
70
+ * 自定义资源上报
71
+ */
72
+ _proto.sendResource = function sendResource(payload) {
73
+ if (!payload.name || !payload.type || !(0, _is.isNumber)(payload.duration)) {
74
+ return;
75
+ }
76
+ var data = (0, _extends2["default"])({
77
+ times: 1
78
+ }, payload, {
79
+ event_type: _rumEvent.RumEventType.RESOURCE
80
+ });
81
+ this.sendEvent(data);
82
+ };
52
83
  return Shell;
53
84
  }();
@@ -2,6 +2,7 @@ import { ICollector } from './collector';
2
2
  import { IProcessor } from './processor';
3
3
  import { IReporter } from './reporter';
4
4
  import { RumEvent, RumEventBundle, IViewData } from './rum-event';
5
+ import { MatchOption } from "../utils/match";
5
6
  export declare enum EventType {
6
7
  /**
7
8
  * 收集数据
@@ -97,6 +98,16 @@ export interface IConfiguration {
97
98
  /**
98
99
  * 各个 collector config
99
100
  */
100
- collectors?: any;
101
+ collectors?: {
102
+ [key: string]: unknown;
103
+ };
104
+ /**
105
+ * 事件过滤器配置
106
+ */
107
+ filters?: {
108
+ view?: MatchOption | MatchOption[];
109
+ resource?: MatchOption | MatchOption[];
110
+ exception?: MatchOption | MatchOption[];
111
+ };
101
112
  [key: string]: any;
102
113
  }
@@ -21,6 +21,7 @@ export interface RumBaseEvent {
21
21
  timestamp?: number;
22
22
  times?: number;
23
23
  view?: IViewData;
24
+ snapshots?: string;
24
25
  [key: string]: BaseObjectValue;
25
26
  }
26
27
  export interface RumViewEvent extends RumBaseEvent {
@@ -29,7 +30,9 @@ export interface RumViewEvent extends RumBaseEvent {
29
30
  largest_contentful_paint?: number;
30
31
  first_input_delay?: number;
31
32
  cumulative_layout_shift?: number;
33
+ first_input_time?: number;
32
34
  loading_time?: number;
35
+ first_paint?: number;
33
36
  first_contentful_paint?: number;
34
37
  dom_interactive?: number;
35
38
  dom_content_loaded?: number;
@@ -41,14 +44,13 @@ export interface RumViewEvent extends RumBaseEvent {
41
44
  }
42
45
  export interface RumResourceEvent extends RumBaseEvent {
43
46
  name?: string;
44
- type: string;
47
+ type?: string;
45
48
  method?: string;
46
- status_code?: number;
49
+ status_code?: number | string;
47
50
  message?: string;
48
- success: 0 | 1;
49
- provider_type?: string;
51
+ success?: -1 | 0 | 1;
50
52
  trace_id?: string;
51
- duration: number;
53
+ duration?: number;
52
54
  size?: number;
53
55
  connect_duration?: number;
54
56
  ssl_duration?: number;
@@ -56,12 +58,13 @@ export interface RumResourceEvent extends RumBaseEvent {
56
58
  redirect_duration?: number;
57
59
  first_byte_duration?: number;
58
60
  download_duration?: number;
59
- url: string;
61
+ url?: string;
60
62
  timing_data?: string;
63
+ tracing_data?: string;
61
64
  }
62
65
  export interface RumExceptionEvent extends RumBaseEvent {
63
66
  source?: string;
64
- type?: 'crash' | 'cutsom' | 'error';
67
+ type?: 'crash' | 'custom' | 'error' | 'blank';
65
68
  name?: string;
66
69
  message?: string;
67
70
  file?: string;
@@ -83,7 +86,6 @@ export interface RumActionEvent extends RumBaseEvent {
83
86
  name?: string;
84
87
  target_name?: string;
85
88
  duration?: number;
86
- snapshots?: string;
87
89
  }
88
90
  export interface RumCustomEvent extends RumBaseEvent {
89
91
  type: string;
@@ -99,9 +101,12 @@ export declare enum AppType {
99
101
  export interface RumEventBundle {
100
102
  app: {
101
103
  id: string;
102
- env?: string;
103
- version?: string;
104
104
  type: AppType;
105
+ name?: string;
106
+ version?: string;
107
+ channel?: string;
108
+ env?: string;
109
+ package?: string;
105
110
  };
106
111
  user: {
107
112
  id: string;
@@ -115,8 +120,35 @@ export interface RumEventBundle {
115
120
  id: string;
116
121
  name: string;
117
122
  };
123
+ device?: {
124
+ id?: string;
125
+ type?: string;
126
+ brand?: string;
127
+ model?: string;
128
+ name?: string;
129
+ };
130
+ os?: {
131
+ type?: string;
132
+ version?: string;
133
+ container?: string;
134
+ container_version?: string;
135
+ };
136
+ geo?: {
137
+ country?: string;
138
+ country_id?: string;
139
+ province?: string;
140
+ province_id?: string;
141
+ city?: string;
142
+ city_id?: string;
143
+ };
144
+ isp?: {
145
+ id?: string;
146
+ name?: string;
147
+ };
118
148
  net?: {
119
- model: string;
149
+ model?: string;
150
+ name?: string;
120
151
  };
121
152
  events: Array<RumEvent>;
153
+ _v: string;
122
154
  }
@@ -8,8 +8,33 @@ export declare function interceptFunction(target: any, name: string, callback: F
8
8
  */
9
9
  export declare function generateGUID(): string;
10
10
  /**
11
- * @description: 唯一的字符串 universe unique id
12
- * @param len {String}
11
+ * @description: 随机生成 traceId sessionId
13
12
  * @return {String}
14
13
  */
15
- export declare function generateUUID(len?: number): string;
14
+ export declare function generateTraceId(): string;
15
+ /**
16
+ * @description: 随机生成 spanId
17
+ * @param len {number}
18
+ * @return {String}
19
+ */
20
+ export declare function generateSpanId(len?: number): string;
21
+ /**
22
+ * @description: 随机生成 eventId
23
+ * @param sessionId {string} sessionId
24
+ * @param sampled {boolean} 是否命中采样
25
+ * @param samplingType {string} 采样类型
26
+ * @return {String}
27
+ */
28
+ export declare function generateEventId(sessionId: string, sampled?: boolean, samplingType?: string): string;
29
+ /**
30
+ * @desc 函数防抖
31
+ * @param func 回调函数
32
+ * @param wait 延迟执行毫秒数
33
+ */
34
+ export declare function debounce(func: Function, wait: number): () => void;
35
+ /**
36
+ * @desc 函数延迟执行
37
+ * @param func 回调函数
38
+ * @param wait 延迟执行毫秒数
39
+ */
40
+ export declare function delay(func: Function, wait: number, ...args: any[]): number;
package/lib/utils/base.js CHANGED
@@ -1,9 +1,14 @@
1
1
  "use strict";
2
2
 
3
3
  exports.__esModule = true;
4
+ exports.debounce = debounce;
5
+ exports.delay = delay;
6
+ exports.generateEventId = generateEventId;
4
7
  exports.generateGUID = generateGUID;
5
- exports.generateUUID = generateUUID;
8
+ exports.generateSpanId = generateSpanId;
9
+ exports.generateTraceId = generateTraceId;
6
10
  exports.interceptFunction = interceptFunction;
11
+ var _is = require("./is");
7
12
  /**
8
13
  * 劫持函数
9
14
  */
@@ -13,9 +18,9 @@ function interceptFunction(target, name, callback) {
13
18
  for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
14
19
  args[_key] = arguments[_key];
15
20
  }
16
- callback.apply(target, args);
21
+ callback.apply(this, args);
17
22
  if (registeredMethod) {
18
- return registeredMethod.apply(target, args);
23
+ return registeredMethod.apply(this, args);
19
24
  }
20
25
  };
21
26
  }
@@ -42,24 +47,92 @@ function generateGUID() {
42
47
  }
43
48
 
44
49
  /**
45
- * @description: 唯一的字符串 universe unique id
46
- * @param len {String}
50
+ * @description: 随机生成 traceId sessionId
47
51
  * @return {String}
48
52
  */
49
- function generateUUID(len) {
53
+ function generateTraceId() {
54
+ var traceId = generateGUID().replace(/-/g, '');
55
+ // 适配OpenTracing 实现(long long转成16进制最高位不能为0)
56
+ if (traceId[0] === '0') {
57
+ traceId = '1' + traceId.substring(1);
58
+ }
59
+ if (traceId[16] === '0') {
60
+ traceId = traceId.substring(0, 16) + '1' + traceId.substring(17);
61
+ }
62
+ return traceId;
63
+ }
64
+
65
+ /**
66
+ * @description: 随机生成 spanId
67
+ * @param len {number}
68
+ * @return {String}
69
+ */
70
+ function generateSpanId(len) {
50
71
  if (len === void 0) {
51
- len = 8;
72
+ len = 16;
52
73
  }
53
- var str = new Array(len);
54
- var d = Date.now().toString(36).split('');
55
- var r, c;
56
- while (len-- > 0) {
57
- r = Math.random() * 36 | 0;
58
- c = r.toString(36);
59
- str[len] = r % 3 ? c : c.toUpperCase();
74
+ var list = Array(len);
75
+ for (var i = 0; i < len; i++) {
76
+ list[i] = Math.floor(Math.random() * 16) + 48;
77
+ // valid hex characters in the range 48-57 and 97-102
78
+ if (list[i] >= 58) {
79
+ list[i] += 39;
80
+ }
81
+ }
82
+ return String.fromCharCode.apply(null, list);
83
+ }
84
+
85
+ /**
86
+ * @description: 随机生成 eventId
87
+ * @param sessionId {string} sessionId
88
+ * @param sampled {boolean} 是否命中采样
89
+ * @param samplingType {string} 采样类型
90
+ * @return {String}
91
+ */
92
+ function generateEventId(sessionId, sampled, samplingType) {
93
+ if (sampled === void 0) {
94
+ sampled = true;
95
+ }
96
+ if (samplingType === void 0) {
97
+ samplingType = '0';
98
+ }
99
+ var spanId = generateSpanId();
100
+ return "00-" + sessionId + "-" + spanId + "-" + samplingType + (sampled ? '1' : '0');
101
+ }
102
+
103
+ /**
104
+ * @desc 函数防抖
105
+ * @param func 回调函数
106
+ * @param wait 延迟执行毫秒数
107
+ */
108
+ function debounce(func, wait) {
109
+ var timer;
110
+ if (!(0, _is.isFunction)(func)) {
111
+ return;
112
+ }
113
+ return function () {
114
+ var context = this;
115
+ var args = arguments;
116
+ if (timer) {
117
+ clearTimeout(timer);
118
+ }
119
+ timer = setTimeout(function () {
120
+ func.apply(context, args);
121
+ }, wait);
122
+ };
123
+ }
124
+
125
+ /**
126
+ * @desc 函数延迟执行
127
+ * @param func 回调函数
128
+ * @param wait 延迟执行毫秒数
129
+ */
130
+ function delay(func, wait) {
131
+ if (!(0, _is.isFunction)(func)) {
132
+ return;
60
133
  }
61
- for (var i = 0; i < 8; i++) {
62
- str.splice(i * 3 + 2, 0, d[i]);
134
+ for (var _len2 = arguments.length, args = new Array(_len2 > 2 ? _len2 - 2 : 0), _key2 = 2; _key2 < _len2; _key2++) {
135
+ args[_key2 - 2] = arguments[_key2];
63
136
  }
64
- return str.join('');
137
+ return setTimeout.apply(void 0, [func, +wait || 0].concat(args));
65
138
  }
@@ -4,3 +4,11 @@
4
4
  */
5
5
  export declare function performDraw(threshold: number): boolean;
6
6
  export declare function round(num: number, decimals: 0 | 1 | 2 | 3 | 4): number;
7
+ /**
8
+ * 保留指定位数的小数
9
+ * @param num 原数据
10
+ * @param decimal 小数位数
11
+ * @param min 限制最小值,否则返回undefined
12
+ * @returns
13
+ */
14
+ export declare function formatNumber(num: number, decimal?: number, min?: number): number;
@@ -1,6 +1,7 @@
1
1
  "use strict";
2
2
 
3
3
  exports.__esModule = true;
4
+ exports.formatNumber = formatNumber;
4
5
  exports.performDraw = performDraw;
5
6
  exports.round = round;
6
7
  /**
@@ -12,4 +13,32 @@ function performDraw(threshold) {
12
13
  }
13
14
  function round(num, decimals) {
14
15
  return +num.toFixed(decimals);
16
+ }
17
+
18
+ /**
19
+ * 保留指定位数的小数
20
+ * @param num 原数据
21
+ * @param decimal 小数位数
22
+ * @param min 限制最小值,否则返回undefined
23
+ * @returns
24
+ */
25
+ function formatNumber(num, decimal, min) {
26
+ if (decimal === void 0) {
27
+ decimal = 3;
28
+ }
29
+ if (min === void 0) {
30
+ min = 0;
31
+ }
32
+ if (!num) {
33
+ return num;
34
+ }
35
+ var str = num.toString();
36
+ var index = str.indexOf('.');
37
+ if (index !== -1) {
38
+ str = str.substring(0, decimal + index + 1);
39
+ } else {
40
+ str = str.substring(0);
41
+ }
42
+ var result = parseFloat(str);
43
+ return result >= min ? result : undefined;
15
44
  }
@@ -7,9 +7,9 @@ exports.find = find;
7
7
  exports.startsWith = startsWith;
8
8
  function find(array, predicate) {
9
9
  for (var i = 0; i < array.length; i += 1) {
10
- var _item = array[i];
11
- if (predicate(_item, i)) {
12
- return _item;
10
+ var item = array[i];
11
+ if (predicate(item, i)) {
12
+ return item;
13
13
  }
14
14
  }
15
15
  return undefined;
@@ -13,8 +13,6 @@ export interface ITracingOption {
13
13
  tracestate?: boolean;
14
14
  baggage?: boolean;
15
15
  }
16
- export declare function generateTraceId(): string;
17
- export declare function generateSpanId(): any;
18
16
  export interface ITracingHeaders {
19
17
  [key: string]: string;
20
18
  }
@@ -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 = {};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@arms/rum-core",
3
- "version": "0.0.22",
3
+ "version": "0.0.25-beta.11",
4
4
  "description": "arms rum javascript sdk core",
5
5
  "author": "guangli.fj <guangli.fj@alibaba-inc.com>",
6
6
  "license": "ISC",