@arms/rum-core 0.0.25-beta.13 → 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
  }
@@ -22,11 +22,9 @@ export declare function generateSpanId(len?: number, radix?: number): string;
22
22
  /**
23
23
  * @description: 随机生成 eventId
24
24
  * @param sessionId {string} sessionId
25
- * @param sampled {boolean} 是否命中采样
26
- * @param samplingType {string} 采样类型
27
25
  * @return {String}
28
26
  */
29
- export declare function generateEventId(sessionId: string, sampled?: boolean, samplingType?: string): string;
27
+ export declare function generateEventId(sessionId: string): string;
30
28
  /**
31
29
  * @desc 函数防抖
32
30
  * @param func 回调函数
package/lib/utils/base.js CHANGED
@@ -95,19 +95,11 @@ function generateSpanId(len, radix) {
95
95
  /**
96
96
  * @description: 随机生成 eventId
97
97
  * @param sessionId {string} sessionId
98
- * @param sampled {boolean} 是否命中采样
99
- * @param samplingType {string} 采样类型
100
98
  * @return {String}
101
99
  */
102
- function generateEventId(sessionId, sampled, samplingType) {
103
- if (sampled === void 0) {
104
- sampled = true;
105
- }
106
- if (samplingType === void 0) {
107
- samplingType = '0';
108
- }
100
+ function generateEventId(sessionId) {
109
101
  var spanId = generateSpanId();
110
- return "00-" + sessionId + "-" + spanId + "-" + samplingType + (sampled ? '1' : '0');
102
+ return "00-" + sessionId + "-" + spanId;
111
103
  }
112
104
 
113
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.13",
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",