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

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.
@@ -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;
@@ -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) {
@@ -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
- constructor(config: IConfiguration);
7
+ session: IRumSession;
8
+ constructor(config: IConfiguration, rumSession?: IRumSession);
8
9
  getConfig(): IConfiguration;
9
10
  setConfig(config: IConfiguration): void;
10
11
  getViews(): IViewData[];
@@ -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(config);
14
+ }
10
15
  }
11
16
  var _proto = Context.prototype;
12
17
  _proto.getConfig = function getConfig() {
@@ -117,7 +117,9 @@ var Reporter = exports["default"] = /*#__PURE__*/function () {
117
117
  });
118
118
  }
119
119
  });
120
- this.request(ctx, eventQueue, curView);
120
+ var session = ctx.session;
121
+ var sampled = session ? session.getSampled() : true;
122
+ sampled && this.request(ctx, eventQueue, curView);
121
123
  this.eventQueue = [];
122
124
  }
123
125
 
@@ -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,26 @@ 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
+ }
37
+ export interface IRumSession {
38
+ init(config: IConfiguration): void;
39
+ sessionConfig: SessionConfig;
40
+ getSessionId: () => string;
41
+ getSampled: () => boolean;
42
+ updateSession: () => void;
43
+ getEventId: () => string;
44
+ getViewId: () => string;
45
+ getUserId: () => string;
46
+ }
31
47
  export interface IClient {
32
48
  /**
33
49
  * 初始化
34
50
  */
35
- init: (configuration: IConfiguration) => void;
51
+ init: (configuration: IConfiguration, rumSession?: IRumSession) => void;
36
52
  /**
37
53
  * 业务自定义上传数据
38
54
  */
@@ -95,6 +111,10 @@ export interface IConfiguration {
95
111
  flushTime?: number;
96
112
  maxEventCount?: number;
97
113
  };
114
+ /**
115
+ * session config
116
+ */
117
+ sessionConfig?: SessionConfig;
98
118
  /**
99
119
  * 各个 collector config
100
120
  */
@@ -2,6 +2,7 @@ import { IContext } from './client';
2
2
  import { RumEvent } from './rum-event';
3
3
  export interface IProcessor {
4
4
  name: string;
5
+ setup?(ctx: IContext): void;
5
6
  process(ctx: IContext): RumEvent;
6
7
  match?(ctx: IContext): boolean;
7
8
  }
@@ -15,17 +15,16 @@ export declare function generateTraceId(): string;
15
15
  /**
16
16
  * @description: 随机生成 spanId
17
17
  * @param len {number}
18
+ * @param radix {number}
18
19
  * @return {String}
19
20
  */
20
- export declare function generateSpanId(len?: number): string;
21
+ export declare function generateSpanId(len?: number, radix?: number): string;
21
22
  /**
22
23
  * @description: 随机生成 eventId
23
24
  * @param sessionId {string} sessionId
24
- * @param sampled {boolean} 是否命中采样
25
- * @param samplingType {string} 采样类型
26
25
  * @return {String}
27
26
  */
28
- export declare function generateEventId(sessionId: string, sampled?: boolean, samplingType?: string): string;
27
+ export declare function generateEventId(sessionId: string): string;
29
28
  /**
30
29
  * @desc 函数防抖
31
30
  * @param func 回调函数
package/lib/utils/base.js CHANGED
@@ -34,8 +34,18 @@ function generateGUID() {
34
34
  try {
35
35
  if (crypto && crypto.randomUUID) {
36
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');
37
45
  }
38
- } catch (e) {}
46
+ } catch (e) {
47
+ //
48
+ }
39
49
  if (!guid) {
40
50
  guid = 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) {
41
51
  var r = Math.random() * 16 | 0,
@@ -65,39 +75,31 @@ function generateTraceId() {
65
75
  /**
66
76
  * @description: 随机生成 spanId
67
77
  * @param len {number}
78
+ * @param radix {number}
68
79
  * @return {String}
69
80
  */
70
- function generateSpanId(len) {
81
+ function generateSpanId(len, radix) {
71
82
  if (len === void 0) {
72
83
  len = 16;
73
84
  }
74
- var list = Array(len);
85
+ if (radix === void 0) {
86
+ radix = 16;
87
+ }
88
+ var result = '';
75
89
  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
- }
90
+ result += Math.floor(Math.random() * radix).toString(radix);
81
91
  }
82
- return String.fromCharCode.apply(null, list);
92
+ return result;
83
93
  }
84
94
 
85
95
  /**
86
96
  * @description: 随机生成 eventId
87
97
  * @param sessionId {string} sessionId
88
- * @param sampled {boolean} 是否命中采样
89
- * @param samplingType {string} 采样类型
90
98
  * @return {String}
91
99
  */
92
- function generateEventId(sessionId, sampled, samplingType) {
93
- if (sampled === void 0) {
94
- sampled = true;
95
- }
96
- if (samplingType === void 0) {
97
- samplingType = '0';
98
- }
100
+ function generateEventId(sessionId) {
99
101
  var spanId = generateSpanId();
100
- return "00-" + sessionId + "-" + spanId + "-" + samplingType + (sampled ? '1' : '0');
102
+ return "00-" + sessionId + "-" + spanId;
101
103
  }
102
104
 
103
105
  /**
@@ -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 原数据
@@ -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.round = round;
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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@arms/rum-core",
3
- "version": "0.0.25-beta.11",
3
+ "version": "0.0.25-beta.14",
4
4
  "description": "arms rum javascript sdk core",
5
5
  "author": "guangli.fj <guangli.fj@alibaba-inc.com>",
6
6
  "license": "ISC",