@arms/rum-core 0.0.9 → 0.0.18

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.
@@ -9,8 +9,10 @@ export default abstract class Reporter {
9
9
  protected eventQueue: RumEvent[];
10
10
  protected ctx: IContext;
11
11
  private timer;
12
- private FLUSH_TIME;
13
- private MAX_EVENT_COUNT;
12
+ getReportCfg(): {
13
+ flushTime?: number;
14
+ maxEventCount?: number;
15
+ };
14
16
  report(ctx: IContext): void;
15
17
  private pushToQueue;
16
18
  /**
@@ -4,6 +4,9 @@ exports.__esModule = true;
4
4
  exports["default"] = void 0;
5
5
  var _rumEvent = require("../types/rum-event");
6
6
  var _exception = require("../utils/exception");
7
+ var _is = require("../utils/is");
8
+ var FLUSH_TIME = 3000;
9
+ var MAX_EVENT_COUNT = 50;
7
10
  var Reporter = exports["default"] = /*#__PURE__*/function () {
8
11
  function Reporter() {
9
12
  this.name = 'reporter';
@@ -14,24 +17,35 @@ var Reporter = exports["default"] = /*#__PURE__*/function () {
14
17
  this.eventQueue = [];
15
18
  this.ctx = void 0;
16
19
  this.timer = void 0;
17
- this.FLUSH_TIME = 3000;
18
- this.MAX_EVENT_COUNT = 50;
19
20
  }
20
21
  var _proto = Reporter.prototype;
22
+ _proto.getReportCfg = function getReportCfg() {
23
+ var cfg = this.ctx.getConfig().reportConfig;
24
+ if (!(0, _is.isObject)(cfg)) {
25
+ cfg = {};
26
+ }
27
+ if (!cfg.flushTime || cfg.flushTime < 0 || cfg.flushTime > 10000) {
28
+ cfg.flushTime = FLUSH_TIME;
29
+ }
30
+ if (!cfg.maxEventCount || cfg.maxEventCount < 0 || cfg.maxEventCount > 100) {
31
+ cfg.maxEventCount = MAX_EVENT_COUNT;
32
+ }
33
+ return cfg;
34
+ };
21
35
  _proto.report = function report(ctx) {
22
36
  var _this = this;
23
37
  this.ctx = ctx;
24
38
  this.init(ctx);
25
39
  clearTimeout(this.timer);
26
40
  this.pushToQueue();
27
-
41
+ var reportConfig = this.getReportCfg();
28
42
  // 超过 MaxCount 直接 flush,否则延迟发送
29
- if (this.eventQueue.length >= this.MAX_EVENT_COUNT) {
43
+ if (this.eventQueue.length >= reportConfig.maxEventCount) {
30
44
  this.flushEventQueue();
31
45
  } else {
32
46
  this.timer = setTimeout(function () {
33
47
  _this.flushEventQueue();
34
- }, this.FLUSH_TIME);
48
+ }, reportConfig.flushTime);
35
49
  }
36
50
  };
37
51
  _proto.pushToQueue = function pushToQueue() {
@@ -87,6 +87,13 @@ export interface IConfiguration {
87
87
  * reporter 发送事件之前
88
88
  */
89
89
  beforeReport?(bundle: RumEventBundle): any;
90
+ /**
91
+ * reporter 发送节奏配置
92
+ */
93
+ reportConfig?: {
94
+ flushTime?: number;
95
+ maxEventCount?: number;
96
+ };
90
97
  /**
91
98
  * 各个 collector config
92
99
  */
@@ -4,11 +4,9 @@
4
4
  export declare function interceptFunction(options: any, name: string, callback: Function): void;
5
5
  /**
6
6
  * @description: GUID v4
7
- * @from https://gist.github.com/jed/982883
8
- * @param placeholder {String}
9
7
  * @return {String}
10
8
  */
11
- export declare function generateGUID(placeholder?: string): string;
9
+ export declare function generateGUID(): string;
12
10
  /**
13
11
  * @description: 唯一的字符串 universe unique id
14
12
  * @param len {String}
package/lib/utils/base.js CHANGED
@@ -22,14 +22,20 @@ function interceptFunction(options, name, callback) {
22
22
 
23
23
  /**
24
24
  * @description: GUID v4
25
- * @from https://gist.github.com/jed/982883
26
- * @param placeholder {String}
27
25
  * @return {String}
28
26
  */
29
- function generateGUID(placeholder) {
30
- return placeholder ?
31
- // eslint-disable-next-line no-bitwise
32
- (parseInt(placeholder, 10) ^ Math.random() * 16 >> parseInt(placeholder, 10) / 4).toString(16) : (1e7 + "-" + 1e3 + "-" + 4e3 + "-" + 8e3 + "-" + 1e11).replace(/[018]/g, generateGUID);
27
+ function generateGUID() {
28
+ try {
29
+ if (crypto && crypto.randomUUID) {
30
+ return crypto.randomUUID();
31
+ }
32
+ } catch (e) {
33
+ return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) {
34
+ var r = Math.random() * 16 | 0,
35
+ v = c == 'x' ? r : r & 0x3 | 0x8;
36
+ return v.toString(16);
37
+ });
38
+ }
33
39
  }
34
40
 
35
41
  /**
package/lib/utils/is.d.ts CHANGED
@@ -8,3 +8,5 @@ export declare const isArray: IsFnHelper<unknown[]>;
8
8
  export declare const isRegExp: IsFnHelper<string>;
9
9
  export declare const isBoolean: IsFnHelper<boolean>;
10
10
  export declare const isNumber: IsFnHelper<number | bigint>;
11
+ export declare const isNull: IsFnHelper<null>;
12
+ export declare const isObject: IsFnHelper<object>;
package/lib/utils/is.js CHANGED
@@ -1,7 +1,7 @@
1
1
  "use strict";
2
2
 
3
3
  exports.__esModule = true;
4
- exports.isString = exports.isRegExp = exports.isNumber = exports.isFunction = exports.isBoolean = exports.isArray = void 0;
4
+ exports.isString = exports.isRegExp = exports.isObject = exports.isNumber = exports.isNull = exports.isFunction = exports.isBoolean = exports.isArray = void 0;
5
5
  exports.isToString = isToString;
6
6
  exports.isTypeof = isTypeof;
7
7
  exports.isUndefined = void 0;
@@ -31,4 +31,10 @@ var isBoolean = exports.isBoolean = function isBoolean(value) {
31
31
  };
32
32
  var isNumber = exports.isNumber = function isNumber(value) {
33
33
  return isTypeof(value, 'number') && !isNaN(value) || isTypeof(value, 'bigint');
34
+ };
35
+ var isNull = exports.isNull = function isNull(value) {
36
+ return isTypeof(value, 'null');
37
+ };
38
+ var isObject = exports.isObject = function isObject(value) {
39
+ return !isNull(value) && isTypeof(value, 'object');
34
40
  };
@@ -15,7 +15,7 @@ function isMatchOption(item) {
15
15
  */
16
16
  function matchList(list, value, useStartsWith) {
17
17
  if (useStartsWith === void 0) {
18
- useStartsWith = false;
18
+ useStartsWith = true;
19
19
  }
20
20
  return list.some(function (item) {
21
21
  try {
@@ -1,5 +1,5 @@
1
1
  import { MatchOption } from "./match";
2
- export type PropagatorType = 'tracecontext' | 'b3' | 'b3multi' | 'jaeger';
2
+ export type PropagatorType = 'tracecontext' | 'b3' | 'b3multi' | 'jaeger' | 'sw8';
3
3
  export type TraceOption = {
4
4
  match: MatchOption;
5
5
  propagatorTypes: PropagatorType[];
@@ -12,10 +12,11 @@ export interface ITracingOption {
12
12
  tracestate?: boolean;
13
13
  baggage?: boolean;
14
14
  }
15
- export declare function generateTraceId(): any;
16
- export declare function generateSpanId(): any;
15
+ export declare function generateTraceId(): string;
16
+ export declare function generateSpanId(): string;
17
17
  export interface ITracingHeaders {
18
18
  [key: string]: string;
19
19
  }
20
+ export declare function generateTraceIdForSW8(pid: string): void;
20
21
  export declare function makeTracingHeaders(traceId: string, spanId: string, sampled: boolean, propagatorTypes: PropagatorType[], tracestate?: string, baggage?: string): ITracingHeaders;
21
22
  export declare function parseTracingOptions(tracingOption: boolean | ITracingOption): ITracingOption;
@@ -3,29 +3,21 @@
3
3
  exports.__esModule = true;
4
4
  exports.generateSpanId = generateSpanId;
5
5
  exports.generateTraceId = generateTraceId;
6
+ exports.generateTraceIdForSW8 = generateTraceIdForSW8;
6
7
  exports.isTraceOption = isTraceOption;
7
8
  exports.makeTracingHeaders = makeTracingHeaders;
8
9
  exports.parseTracingOptions = parseTracingOptions;
9
10
  var _match = require("./match");
10
11
  var _is = require("./is");
11
- var SPAN_ID_BYTES = 8;
12
- var TRACE_ID_BYTES = 16;
13
- var SHARED_CHAR_CODES_ARRAY = Array(32);
12
+ var _base = require("./base");
14
13
  function isTraceOption(option) {
15
14
  return option && (0, _match.isMatchOption)(option.match) && (0, _is.isArray)(option.propagatorTypes);
16
15
  }
17
- function generateId(bytes) {
18
- for (var i = 0; i < bytes * 2; i++) {
19
- SHARED_CHAR_CODES_ARRAY[i] = Math.floor(Math.random() * 16) + 48;
20
- // valid hex characters in the range 48-57 and 97-102
21
- if (SHARED_CHAR_CODES_ARRAY[i] >= 58) {
22
- SHARED_CHAR_CODES_ARRAY[i] += 39;
23
- }
24
- }
25
- return String.fromCharCode.apply(null, SHARED_CHAR_CODES_ARRAY.slice(0, bytes * 2));
16
+ function generateId() {
17
+ return (0, _base.generateGUID)().replace(/-/g, '');
26
18
  }
27
19
  function generateTraceId() {
28
- var traceId = generateId(TRACE_ID_BYTES);
20
+ var traceId = generateId();
29
21
  // 适配OpenTracing 实现(long long转成16进制最高位不能为0)
30
22
  if (traceId[0] === '0') {
31
23
  traceId = '1' + traceId.substring(1);
@@ -36,8 +28,10 @@ function generateTraceId() {
36
28
  return traceId;
37
29
  }
38
30
  function generateSpanId() {
39
- return generateId(SPAN_ID_BYTES);
31
+ return (0, _base.generateUUID)(8);
40
32
  }
33
+ //skywalking TraceId https://github.com/apache/skywalking-client-js
34
+ function generateTraceIdForSW8(pid) {}
41
35
  function makeTracingHeaders(traceId, spanId, sampled, propagatorTypes, tracestate, baggage) {
42
36
  var tracingHeaders = {};
43
37
  var sample = sampled ? '1' : '0';
@@ -55,6 +49,9 @@ function makeTracingHeaders(traceId, spanId, sampled, propagatorTypes, tracestat
55
49
  tracingHeaders['X-B3-SpanId'] = spanId;
56
50
  tracingHeaders['X-B3-Sampled'] = sample;
57
51
  break;
52
+ case 'sw8':
53
+ // tracingHeaders['sw8'] = `${sample}-${traceIdStr}-${segmentId}-${0}-${pid}-${app.version}-${href}-${host}`;;
54
+ break;
58
55
  case 'tracecontext': // https://www.w3.org/TR/trace-context/
59
56
  default:
60
57
  tracingHeaders['traceparent'] = "00-" + traceId + "-" + spanId + "-0" + sample;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@arms/rum-core",
3
- "version": "0.0.9",
3
+ "version": "0.0.18",
4
4
  "description": "arms rum javascript sdk core",
5
5
  "author": "guangli.fj <guangli.fj@alibaba-inc.com>",
6
6
  "license": "ISC",
@@ -18,6 +18,7 @@
18
18
  "scripts": {
19
19
  "start": "build-scripts build --watch",
20
20
  "build": "build-scripts build --skip-demo",
21
+ "prepublishOnly": "npm run build",
21
22
  "test": "node ./__tests__/@arms/core.test.js"
22
23
  },
23
24
  "dependencies": {