@arms/rum-core 0.1.5 → 0.1.7

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.
Files changed (61) hide show
  1. package/es/index.d.ts +3 -0
  2. package/es/index.js +3 -0
  3. package/es/model/client.d.ts +5 -4
  4. package/es/model/client.js +32 -6
  5. package/es/model/reporter.d.ts +30 -7
  6. package/es/model/reporter.js +83 -25
  7. package/es/model/shell.d.ts +4 -4
  8. package/es/model/shell.js +13 -10
  9. package/es/monitor/index.d.ts +6 -0
  10. package/es/monitor/index.js +3 -0
  11. package/es/monitor/logger.d.ts +34 -0
  12. package/es/monitor/logger.js +112 -0
  13. package/es/monitor/telemetry.d.ts +42 -0
  14. package/es/monitor/telemetry.js +210 -0
  15. package/es/monitor/types.d.ts +46 -0
  16. package/es/monitor/types.js +1 -0
  17. package/es/types/client.d.ts +16 -3
  18. package/es/types/collector.d.ts +2 -2
  19. package/es/types/reporter.d.ts +2 -2
  20. package/es/types/rum-event.d.ts +9 -0
  21. package/es/types/rum-event.js +4 -0
  22. package/es/types/shell.d.ts +3 -3
  23. package/es/utils/base.js +2 -2
  24. package/es/utils/disposable.d.ts +12 -0
  25. package/es/utils/disposable.js +20 -0
  26. package/es/utils/events.js +2 -1
  27. package/es/utils/protobuf.d.ts +24 -0
  28. package/es/utils/protobuf.js +123 -0
  29. package/es/utils/throttle.d.ts +14 -0
  30. package/es/utils/throttle.js +40 -0
  31. package/lib/index.d.ts +3 -0
  32. package/lib/index.js +21 -0
  33. package/lib/model/client.d.ts +5 -4
  34. package/lib/model/client.js +31 -5
  35. package/lib/model/reporter.d.ts +30 -7
  36. package/lib/model/reporter.js +80 -22
  37. package/lib/model/shell.d.ts +4 -4
  38. package/lib/model/shell.js +11 -8
  39. package/lib/monitor/index.d.ts +6 -0
  40. package/lib/monitor/index.js +12 -0
  41. package/lib/monitor/logger.d.ts +34 -0
  42. package/lib/monitor/logger.js +117 -0
  43. package/lib/monitor/telemetry.d.ts +42 -0
  44. package/lib/monitor/telemetry.js +214 -0
  45. package/lib/monitor/types.d.ts +46 -0
  46. package/lib/monitor/types.js +3 -0
  47. package/lib/types/client.d.ts +16 -3
  48. package/lib/types/collector.d.ts +2 -2
  49. package/lib/types/reporter.d.ts +2 -2
  50. package/lib/types/rum-event.d.ts +9 -0
  51. package/lib/types/rum-event.js +2 -0
  52. package/lib/types/shell.d.ts +3 -3
  53. package/lib/utils/base.js +2 -2
  54. package/lib/utils/disposable.d.ts +12 -0
  55. package/lib/utils/disposable.js +24 -0
  56. package/lib/utils/events.js +2 -1
  57. package/lib/utils/protobuf.d.ts +24 -0
  58. package/lib/utils/protobuf.js +127 -0
  59. package/lib/utils/throttle.d.ts +14 -0
  60. package/lib/utils/throttle.js +44 -0
  61. package/package.json +10 -1
package/es/index.d.ts CHANGED
@@ -19,4 +19,7 @@ export * from './utils/trace';
19
19
  export * from './utils/verify';
20
20
  export * from './utils/url';
21
21
  export * from './utils/events';
22
+ export * from './utils/throttle';
23
+ export * from './utils/disposable';
24
+ export * from './monitor';
22
25
  export { Client, Context, Reporter, Shell };
package/es/index.js CHANGED
@@ -19,4 +19,7 @@ export * from './utils/trace';
19
19
  export * from './utils/verify';
20
20
  export * from './utils/url';
21
21
  export * from './utils/events';
22
+ export * from './utils/throttle';
23
+ export * from './utils/disposable';
24
+ export * from './monitor';
22
25
  export { Client, Context, Reporter, Shell };
@@ -1,17 +1,18 @@
1
1
  import { IClient, IConfiguration, IContext, IRumSession } from '../types/client';
2
- import { IConfigManager } from "../types/config";
3
- import { RumEvent } from '../types/rum-event';
2
+ import { IConfigManager } from '../types/config';
3
+ import { RumEvent, SendEventOptions } from '../types/rum-event';
4
4
  import { IProcessor } from '../types/processor';
5
5
  import { ICollector } from '../types/collector';
6
6
  import { IReporter } from '../types/reporter';
7
+ import { IMonitorReporter } from '../monitor/types';
7
8
  declare class Client implements IClient {
8
9
  private emitter;
9
10
  private collectors;
10
11
  private processors;
11
12
  private reporter;
12
13
  private ctx;
13
- init(config: IConfiguration, rumSession?: IRumSession, configManager?: IConfigManager): void;
14
- sendEvent: (payload: RumEvent) => void;
14
+ init(config: IConfiguration, rumSession?: IRumSession, configManager?: IConfigManager, monitorReporter?: IMonitorReporter): void;
15
+ sendEvent: (payload: RumEvent, options?: SendEventOptions) => void;
15
16
  setContext(ctx: IContext): void;
16
17
  getContext(): IContext;
17
18
  useCollectors(collectors: ICollector[]): void;
@@ -3,8 +3,9 @@ function _unsupportedIterableToArray(r, a) { if (r) { if ("string" == typeof r)
3
3
  function _arrayLikeToArray(r, a) { (null == a || a > r.length) && (a = r.length); for (var e = 0, n = Array(a); e < a; e++) n[e] = r[e]; return n; }
4
4
  import { EventType } from '../types/client';
5
5
  import { EventEmitter } from '../utils/events';
6
+ import { Telemetry } from '../monitor/telemetry';
6
7
  import Context from './context';
7
- import { isFunction } from "../utils/is";
8
+ import { isFunction } from '../utils/is';
8
9
  var Client = /*#__PURE__*/function () {
9
10
  function Client() {
10
11
  var _this = this;
@@ -13,14 +14,39 @@ var Client = /*#__PURE__*/function () {
13
14
  this.processors = void 0;
14
15
  this.reporter = void 0;
15
16
  this.ctx = void 0;
16
- this.sendEvent = function (payload) {
17
- _this.emitter.emit(EventType.collect, payload);
17
+ this.sendEvent = function (payload, options) {
18
+ _this.emitter.emit(EventType.collect, payload, options);
18
19
  };
19
20
  }
20
21
  var _proto = Client.prototype;
21
- _proto.init = function init(config, rumSession, configManager) {
22
+ _proto.init = function init(config, rumSession, configManager, monitorReporter) {
22
23
  var _this2 = this;
23
24
  this.ctx = new Context(config, rumSession, configManager);
25
+
26
+ // Telemetry 初始化 —— 统一在 Core 中完成,各平台仅需传入 reporter
27
+ if (monitorReporter) {
28
+ var telemetryConfig = {
29
+ appType: (rumSession === null || rumSession === void 0 ? void 0 : rumSession.appType) || '',
30
+ sdkVersion: (rumSession === null || rumSession === void 0 ? void 0 : rumSession.sdkVersion) || '',
31
+ endpoint: config.endpoint,
32
+ getSessionId: rumSession ? function () {
33
+ return rumSession.getSessionId();
34
+ } : undefined,
35
+ getSessionSampled: rumSession ? function () {
36
+ return rumSession.getSampled();
37
+ } : undefined,
38
+ getViewId: function getViewId() {
39
+ var _this2$ctx;
40
+ var views = (_this2$ctx = _this2.ctx) === null || _this2$ctx === void 0 ? void 0 : _this2$ctx.getViews();
41
+ return views && views.length ? views[views.length - 1].id : undefined;
42
+ },
43
+ selfMonitor: config.selfMonitor
44
+ };
45
+ if (config.pid) {
46
+ telemetryConfig['app.id'] = config.pid;
47
+ }
48
+ Telemetry.init(telemetryConfig, monitorReporter);
49
+ }
24
50
  var ctx = this.ctx,
25
51
  collectors = this.collectors,
26
52
  processors = this.processors,
@@ -30,7 +56,7 @@ var Client = /*#__PURE__*/function () {
30
56
  });
31
57
 
32
58
  // collector 收集数据统一 push 到各匹配 processor
33
- this.emitter.on(EventType.collect, function (payload) {
59
+ this.emitter.on(EventType.collect, function (payload, options) {
34
60
  ctx.setRumEvent(payload);
35
61
  for (var _iterator = _createForOfIteratorHelperLoose(processors), _step; !(_step = _iterator()).done;) {
36
62
  var processor = _step.value;
@@ -47,7 +73,7 @@ var Client = /*#__PURE__*/function () {
47
73
  }
48
74
  }
49
75
  }
50
- reporter.report(ctx);
76
+ reporter.report(ctx, options);
51
77
  });
52
78
  collectors.forEach(function (collector) {
53
79
  collector.setup(ctx, _this2.sendEvent);
@@ -1,23 +1,46 @@
1
1
  import { IContext } from '../types/client';
2
- import { RumEvent, IViewData, RumEventBundle } from '../types/rum-event';
2
+ import { RumEvent, IViewData, RumEventBundle, SendEventOptions } from '../types/rum-event';
3
+ /**
4
+ * 抽象上报器,负责事件排队、批量打包与发送。
5
+ *
6
+ * 上报模式(通过 SendEventOptions 控制):
7
+ * - 默认:事件进入队列,等待定时器或队列满后批量 flush
8
+ * - immediate:事件进入队列后立即 flush 整个队列
9
+ * - single:事件不进队列,独立打包立即上报
10
+ */
3
11
  export default abstract class Reporter {
4
12
  name: string;
5
- /**
6
- * 新上报事件优先检测队列是否超过 50 条,如果超过立即 report。
7
- * 如果没超过则检测 FlushTime 内有无新上报事件,如果有新事件,重置 timer,无则 report
8
- */
9
13
  protected eventQueue: RumEvent[];
10
14
  protected ctx: IContext;
11
15
  private timer;
12
16
  private _init;
13
17
  getReportCfg(): import("../types/client").IReportConfig;
14
- report(ctx: IContext): void;
18
+ /**
19
+ * 上报入口,根据 options 决定上报策略:
20
+ * - single: 当前事件独立打包立即发送,不影响队列
21
+ * - immediate: 当前事件入队后立即 flush 整个队列
22
+ * - 默认: 当前事件入队,等待定时器或队列满后批量 flush
23
+ */
24
+ report(ctx: IContext, options?: SendEventOptions): void;
25
+ /**
26
+ * 当前事件独立打包上报,不进入批量队列,不重置队列定时器
27
+ */
28
+ private flushSingleEvent;
15
29
  private pushToQueue;
16
30
  /**
17
31
  * eventQueue 提取最新 View 作为公共 View,event 中不同 View Event 保留 View id
18
32
  */
19
33
  protected flushEventQueue(): void;
20
- mergeEvent(ctx: IContext, events: RumEvent[], view: IViewData): void;
34
+ /**
35
+ * 按 session_id 分组事件并分别打包上报:
36
+ * - 当前 session 的事件统一打包
37
+ * - 非当前 session 的事件按各自 session_id 单独打包
38
+ */
39
+ protected dispatchEvents(ctx: IContext, events: RumEvent[], view: IViewData): void;
40
+ /**
41
+ * 构建 bundle 并发送请求
42
+ */
43
+ private buildAndSend;
21
44
  private tryRequest;
22
45
  /**
23
46
  * 接口请求由各平台 sdk reporter 实现
@@ -1,21 +1,27 @@
1
1
  import _asyncToGenerator from "@babel/runtime/helpers/asyncToGenerator";
2
2
  import _extends from "@babel/runtime/helpers/extends";
3
3
  import _regeneratorRuntime from "@babel/runtime/regenerator";
4
+ import { logger } from '../monitor/logger';
4
5
  import { RumEventType } from '../types/rum-event';
5
6
  import { getErrorID } from '../utils/exception';
6
- import { isNumber, isObject, isString } from "../utils/is";
7
- import { verifyProperties } from "../utils/verify";
8
- import { getUrlParams } from "../utils/url";
7
+ import { isNumber, isObject, isString } from '../utils/is';
8
+ import { verifyProperties } from '../utils/verify';
9
+ import { getUrlParams } from '../utils/url';
9
10
  var FLUSH_TIME = 3000;
10
11
  var MAX_EVENT_COUNT = 20;
11
12
  var attrs = ['app', 'user', 'device', 'os', 'geo', 'isp', 'net', 'properties'];
13
+
14
+ /**
15
+ * 抽象上报器,负责事件排队、批量打包与发送。
16
+ *
17
+ * 上报模式(通过 SendEventOptions 控制):
18
+ * - 默认:事件进入队列,等待定时器或队列满后批量 flush
19
+ * - immediate:事件进入队列后立即 flush 整个队列
20
+ * - single:事件不进队列,独立打包立即上报
21
+ */
12
22
  var Reporter = /*#__PURE__*/function () {
13
23
  function Reporter() {
14
24
  this.name = 'reporter';
15
- /**
16
- * 新上报事件优先检测队列是否超过 50 条,如果超过立即 report。
17
- * 如果没超过则检测 FlushTime 内有无新上报事件,如果有新事件,重置 timer,无则 report
18
- */
19
25
  this.eventQueue = [];
20
26
  this.ctx = void 0;
21
27
  this.timer = void 0;
@@ -34,25 +40,54 @@ var Reporter = /*#__PURE__*/function () {
34
40
  cfg.maxEventCount = MAX_EVENT_COUNT;
35
41
  }
36
42
  return cfg;
37
- };
38
- _proto.report = function report(ctx) {
43
+ }
44
+
45
+ /**
46
+ * 上报入口,根据 options 决定上报策略:
47
+ * - single: 当前事件独立打包立即发送,不影响队列
48
+ * - immediate: 当前事件入队后立即 flush 整个队列
49
+ * - 默认: 当前事件入队,等待定时器或队列满后批量 flush
50
+ */;
51
+ _proto.report = function report(ctx, options) {
39
52
  var _this = this;
40
53
  this.ctx = ctx;
41
54
  if (!this._init) {
42
55
  this.init(ctx);
43
56
  this._init = true;
44
57
  }
58
+
59
+ // single 优先级最高:当前事件单独上报,不影响队列
60
+ if (options && options.single) {
61
+ this.flushSingleEvent(ctx);
62
+ return;
63
+ }
45
64
  clearTimeout(this.timer);
46
65
  this.pushToQueue();
47
66
  var reportConfig = this.getReportCfg();
48
- // 超过 MaxCount 直接 flush,否则延迟发送
49
- if (this.eventQueue.length >= reportConfig.maxEventCount) {
67
+ if (options && options.immediate || this.eventQueue.length >= reportConfig.maxEventCount) {
50
68
  this.flushEventQueue();
51
69
  } else {
52
70
  this.timer = setTimeout(function () {
53
71
  _this.flushEventQueue();
54
72
  }, reportConfig.flushTime);
55
73
  }
74
+ }
75
+
76
+ /**
77
+ * 当前事件独立打包上报,不进入批量队列,不重置队列定时器
78
+ */;
79
+ _proto.flushSingleEvent = function flushSingleEvent(ctx) {
80
+ var _event$view;
81
+ var session = ctx.session;
82
+ var sampled = session ? session.getSampled() : true;
83
+ if (!sampled) return;
84
+ var event = ctx.getRumEvent();
85
+ var views = ctx.getViews();
86
+ var curView = views[views.length - 1];
87
+ if (((_event$view = event.view) === null || _event$view === void 0 ? void 0 : _event$view.id) === curView.id) {
88
+ delete event.view;
89
+ }
90
+ this.dispatchEvents(ctx, [event], curView);
56
91
  };
57
92
  _proto.pushToQueue = function pushToQueue() {
58
93
  var ctx = this.ctx,
@@ -115,8 +150,8 @@ var Reporter = /*#__PURE__*/function () {
115
150
  views.forEach(function (view) {
116
151
  if (view.id === curView.id) {
117
152
  var events = eventQueue.filter(function (event) {
118
- var _event$view;
119
- return ((_event$view = event.view) === null || _event$view === void 0 ? void 0 : _event$view.id) === view.id;
153
+ var _event$view2;
154
+ return ((_event$view2 = event.view) === null || _event$view2 === void 0 ? void 0 : _event$view2.id) === view.id;
120
155
  });
121
156
  events.forEach(function (event) {
122
157
  delete event.view;
@@ -125,28 +160,49 @@ var Reporter = /*#__PURE__*/function () {
125
160
  });
126
161
  var session = ctx.session;
127
162
  var sampled = session ? session.getSampled() : true;
128
- sampled && this.mergeEvent(ctx, eventQueue, curView);
163
+ sampled && this.dispatchEvents(ctx, eventQueue, curView);
129
164
  this.eventQueue = [];
130
- };
131
- _proto.mergeEvent = function mergeEvent(ctx, events, view) {
165
+ }
166
+
167
+ /**
168
+ * 按 session_id 分组事件并分别打包上报:
169
+ * - 当前 session 的事件统一打包
170
+ * - 非当前 session 的事件按各自 session_id 单独打包
171
+ */;
172
+ _proto.dispatchEvents = function dispatchEvents(ctx, events, view) {
132
173
  var config = ctx.getConfig();
133
174
  var session = ctx.session;
134
175
  var sessionId = session.getSessionId();
135
- // events.forEach(event => {
136
- // if (event.session_id === sessionId) {
137
- // delete event.session_id;
138
- // }
139
- // });
176
+ var currentSessionEvents = [];
177
+ var otherSessionEventsMap = {};
140
178
  for (var i = 0; i < events.length; i++) {
141
179
  var e = events[i];
142
- if (e.session_id === sessionId) {
180
+ if (!e.session_id || e.session_id === sessionId) {
143
181
  delete e.session_id;
182
+ currentSessionEvents.push(e);
144
183
  } else {
145
- events.splice(i, 1);
146
- i--;
184
+ var sid = e.session_id;
185
+ if (!otherSessionEventsMap[sid]) {
186
+ otherSessionEventsMap[sid] = [];
187
+ }
188
+ delete e.session_id;
189
+ otherSessionEventsMap[sid].push(e);
147
190
  }
148
191
  }
149
- if (events.length === 0) return;
192
+ if (currentSessionEvents.length > 0) {
193
+ this.buildAndSend(ctx, config, session, sessionId, currentSessionEvents, view);
194
+ }
195
+ var otherSessionIds = Object.keys(otherSessionEventsMap);
196
+ for (var _i = 0; _i < otherSessionIds.length; _i++) {
197
+ var _sid = otherSessionIds[_i];
198
+ this.buildAndSend(ctx, config, session, _sid, otherSessionEventsMap[_sid], view);
199
+ }
200
+ }
201
+
202
+ /**
203
+ * 构建 bundle 并发送请求
204
+ */;
205
+ _proto.buildAndSend = function buildAndSend(ctx, config, session, sessionId, events, view) {
150
206
  var extend = getUrlParams(config.endpoint);
151
207
  var bundle = _extends({
152
208
  app: {
@@ -217,6 +273,8 @@ var Reporter = /*#__PURE__*/function () {
217
273
  setTimeout(function () {
218
274
  return _this2.tryRequest(bundle, retry + 1);
219
275
  }, retryDelay);
276
+ } else {
277
+ logger.error('reporter', "Request failed after " + (retry + 1) + " attempts", _t);
220
278
  }
221
279
  case 4:
222
280
  case "end":
@@ -1,6 +1,6 @@
1
- import { IClient, IConfiguration } from "../types/client";
2
- import { RumCustomEvent, RumEvent, RumExceptionEvent, RumResourceEvent, RumViewEvent } from "../types/rum-event";
3
- import { IShell } from "../types/shell";
1
+ import { IClient, IConfiguration } from '../types/client';
2
+ import { RumCustomEvent, RumEvent, RumExceptionEvent, RumResourceEvent, RumViewEvent, SendEventOptions } from '../types/rum-event';
3
+ import { IShell } from '../types/shell';
4
4
  export default abstract class Shell implements IShell {
5
5
  client: IClient;
6
6
  constructor(config?: IConfiguration);
@@ -8,7 +8,7 @@ export default abstract class Shell implements IShell {
8
8
  * 初始化
9
9
  */
10
10
  abstract init(configuration: IConfiguration): Promise<Shell> | Shell;
11
- sendEvent(payload: RumEvent): void;
11
+ sendEvent(payload: RumEvent, options?: SendEventOptions): void;
12
12
  /**
13
13
  * get config
14
14
  */
package/es/model/shell.js CHANGED
@@ -1,7 +1,7 @@
1
1
  import _extends from "@babel/runtime/helpers/extends";
2
- import { RumEventType } from "../types/rum-event";
2
+ import { RumEventType } from '../types/rum-event';
3
3
  import Client from '../model/client';
4
- import { isNumber, isObject } from "../utils/is";
4
+ import { isNumber, isObject } from '../utils/is';
5
5
  var Shell = /*#__PURE__*/function () {
6
6
  function Shell(config) {
7
7
  // private callbacks: Function[] = [];
@@ -35,9 +35,9 @@ var Shell = /*#__PURE__*/function () {
35
35
  * 初始化
36
36
  */
37
37
  var _proto = Shell.prototype;
38
- _proto.sendEvent = function sendEvent(payload) {
38
+ _proto.sendEvent = function sendEvent(payload, options) {
39
39
  if (this.client) {
40
- this.client.sendEvent(payload);
40
+ this.client.sendEvent(payload, options);
41
41
  }
42
42
  }
43
43
 
@@ -64,10 +64,11 @@ var Shell = /*#__PURE__*/function () {
64
64
  event_type: RumEventType.CUSTOM
65
65
  });
66
66
  this.sendEvent(data);
67
- };
67
+ }
68
+
68
69
  /**
69
70
  * 自定义视图上报
70
- */
71
+ */;
71
72
  _proto.sendView = function sendView(payload) {
72
73
  if (!isObject(payload)) {
73
74
  return;
@@ -89,10 +90,11 @@ var Shell = /*#__PURE__*/function () {
89
90
  event_type: RumEventType.VIEW
90
91
  });
91
92
  this.sendEvent(data);
92
- };
93
+ }
94
+
93
95
  /**
94
96
  * 自定义异常上报
95
- */
97
+ */;
96
98
  _proto.sendException = function sendException(payload) {
97
99
  if (!payload.name || !payload.message) {
98
100
  return;
@@ -111,10 +113,11 @@ var Shell = /*#__PURE__*/function () {
111
113
  source: 'custom'
112
114
  });
113
115
  this.sendEvent(data);
114
- };
116
+ }
117
+
115
118
  /**
116
119
  * 自定义资源上报
117
- */
120
+ */;
118
121
  _proto.sendResource = function sendResource(payload) {
119
122
  if (!payload.name || !payload.type || !isNumber(payload.duration)) {
120
123
  return;
@@ -0,0 +1,6 @@
1
+ export { logger, callMonitored } from './logger';
2
+ export type { ILogger } from './logger';
3
+ export { Telemetry, convertBundlesToLogGroup } from './telemetry';
4
+ export type { TelemetryContext } from './telemetry';
5
+ export type { MonitorLogEvent, MonitorLogLevel, MonitorEventBundle, IMonitorReporter, ISelfMonitorConfig } from './types';
6
+ export { encodeLogGroup } from '../utils/protobuf';
@@ -0,0 +1,3 @@
1
+ export { logger, callMonitored } from './logger';
2
+ export { Telemetry, convertBundlesToLogGroup } from './telemetry';
3
+ export { encodeLogGroup } from '../utils/protobuf';
@@ -0,0 +1,34 @@
1
+ import { MonitorLogEvent, MonitorLogLevel } from './types';
2
+ type LogArgs = [module: string, message: string, error?: unknown, context?: Record<string, unknown>];
3
+ export interface ILogger {
4
+ debug(...args: LogArgs): void;
5
+ info(...args: LogArgs): void;
6
+ warn(...args: LogArgs): void;
7
+ error(...args: LogArgs): void;
8
+ crash(...args: LogArgs): void;
9
+ }
10
+ declare class Logger implements ILogger {
11
+ private console;
12
+ private reportLevels;
13
+ private onError?;
14
+ setConsole(enabled: boolean): void;
15
+ setReportLevels(levels: MonitorLogLevel[]): void;
16
+ setOnError(handler?: (event: MonitorLogEvent) => void): void;
17
+ debug(...args: LogArgs): void;
18
+ info(...args: LogArgs): void;
19
+ warn(...args: LogArgs): void;
20
+ error(...args: LogArgs): void;
21
+ crash(...args: LogArgs): void;
22
+ private log;
23
+ /**
24
+ * 函数包装器 — 用于箭头函数属性
25
+ * 用法: private handler = logger.wrap((e) => {...}, 'module', 'message?')
26
+ */
27
+ wrap<T extends (...args: any[]) => unknown>(fn: T, module: string, message?: string): T;
28
+ }
29
+ export declare const logger: Logger;
30
+ /**
31
+ * 执行被监控函数,异常时自动上报
32
+ */
33
+ export declare function callMonitored<T extends (...args: any[]) => unknown>(fn: T, context?: any, args?: any, module?: string, message?: string): ReturnType<T> | undefined;
34
+ export {};
@@ -0,0 +1,112 @@
1
+ var DEFAULT_REPORT_LEVELS = ['debug', 'error', 'crash'];
2
+ var Logger = /*#__PURE__*/function () {
3
+ function Logger() {
4
+ this.console = false;
5
+ this.reportLevels = DEFAULT_REPORT_LEVELS;
6
+ this.onError = void 0;
7
+ }
8
+ var _proto = Logger.prototype;
9
+ _proto.setConsole = function setConsole(enabled) {
10
+ this.console = enabled;
11
+ };
12
+ _proto.setReportLevels = function setReportLevels(levels) {
13
+ this.reportLevels = levels;
14
+ };
15
+ _proto.setOnError = function setOnError(handler) {
16
+ this.onError = handler;
17
+ };
18
+ _proto.debug = function debug() {
19
+ for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
20
+ args[_key] = arguments[_key];
21
+ }
22
+ this.log('debug', args);
23
+ };
24
+ _proto.info = function info() {
25
+ for (var _len2 = arguments.length, args = new Array(_len2), _key2 = 0; _key2 < _len2; _key2++) {
26
+ args[_key2] = arguments[_key2];
27
+ }
28
+ this.log('info', args);
29
+ };
30
+ _proto.warn = function warn() {
31
+ for (var _len3 = arguments.length, args = new Array(_len3), _key3 = 0; _key3 < _len3; _key3++) {
32
+ args[_key3] = arguments[_key3];
33
+ }
34
+ this.log('warn', args);
35
+ };
36
+ _proto.error = function error() {
37
+ for (var _len4 = arguments.length, args = new Array(_len4), _key4 = 0; _key4 < _len4; _key4++) {
38
+ args[_key4] = arguments[_key4];
39
+ }
40
+ this.log('error', args);
41
+ };
42
+ _proto.crash = function crash() {
43
+ for (var _len5 = arguments.length, args = new Array(_len5), _key5 = 0; _key5 < _len5; _key5++) {
44
+ args[_key5] = arguments[_key5];
45
+ }
46
+ this.log('crash', args);
47
+ };
48
+ _proto.log = function log(level, _ref) {
49
+ var _this$onError;
50
+ var module = _ref[0],
51
+ message = _ref[1],
52
+ error = _ref[2],
53
+ context = _ref[3];
54
+ if (this.console) {
55
+ var consoleFn = level === 'crash' ? 'error' : level === 'debug' ? 'log' : level;
56
+ console[consoleFn]("[RUM-SDK][" + module + "]", message, error || '');
57
+ }
58
+
59
+ // 仅上报配置中指定的级别
60
+ if (!this.reportLevels.includes(level)) return;
61
+ var stack;
62
+ if (error instanceof Error) {
63
+ stack = error.stack;
64
+ } else if (error && typeof error === 'object' && 'stack' in error) {
65
+ stack = String(error.stack);
66
+ }
67
+
68
+ // 截断过长的 stack(与 content 同等 12KB 上限)
69
+ if (stack && stack.length > 12288) {
70
+ stack = stack.slice(0, 12288);
71
+ }
72
+ (_this$onError = this.onError) === null || _this$onError === void 0 ? void 0 : _this$onError.call(this, Object.assign({
73
+ level: level,
74
+ content: message,
75
+ type: module,
76
+ stack: stack
77
+ }, context));
78
+ }
79
+
80
+ /**
81
+ * 函数包装器 — 用于箭头函数属性
82
+ * 用法: private handler = logger.wrap((e) => {...}, 'module', 'message?')
83
+ */;
84
+ _proto.wrap = function wrap(fn, module, message) {
85
+ return function () {
86
+ for (var _len6 = arguments.length, args = new Array(_len6), _key6 = 0; _key6 < _len6; _key6++) {
87
+ args[_key6] = arguments[_key6];
88
+ }
89
+ return callMonitored(fn, this, args, module, message);
90
+ };
91
+ };
92
+ return Logger;
93
+ }();
94
+ export var logger = new Logger();
95
+
96
+ /**
97
+ * 执行被监控函数,异常时自动上报
98
+ */
99
+ export function callMonitored(fn, context, args, module, message) {
100
+ try {
101
+ var result = fn.apply(context, args);
102
+ // 检测 async 函数返回的 Promise,附加 .catch 处理
103
+ if (result && typeof result["catch"] === 'function') {
104
+ result["catch"](function (e) {
105
+ logger.error(module || 'unknown', message || (e instanceof Error ? e.message : String(e)), e);
106
+ });
107
+ }
108
+ return result;
109
+ } catch (e) {
110
+ logger.error(module || 'unknown', message || (e instanceof Error ? e.message : String(e)), e);
111
+ }
112
+ }
@@ -0,0 +1,42 @@
1
+ import { MonitorLogEvent, MonitorEventBundle, IMonitorReporter, ISelfMonitorConfig } from './types';
2
+ export interface TelemetryContext {
3
+ appType: string;
4
+ sdkVersion: string;
5
+ endpoint: string;
6
+ getSessionId?: () => string | undefined;
7
+ getViewId?: () => string | undefined;
8
+ getSessionSampled?: () => boolean;
9
+ /** 自监控配置 */
10
+ selfMonitor?: ISelfMonitorConfig | boolean;
11
+ [key: string]: unknown;
12
+ }
13
+ export declare class Telemetry {
14
+ private static instance;
15
+ static init(context: TelemetryContext, reporter?: IMonitorReporter): Telemetry;
16
+ static get(): Telemetry | undefined;
17
+ private buffer;
18
+ private queue;
19
+ private sentEvents;
20
+ private started;
21
+ private enabled;
22
+ private sampled;
23
+ private timer;
24
+ private reporter?;
25
+ private context;
26
+ private isReporting;
27
+ private endpointParams;
28
+ constructor(context: TelemetryContext, reporter?: IMonitorReporter);
29
+ private start;
30
+ addEvent(event: MonitorLogEvent): void;
31
+ flush(): void;
32
+ setReporter(reporter: IMonitorReporter): void;
33
+ private flushBuffer;
34
+ private processSnapshot;
35
+ private shouldSend;
36
+ private buildBundle;
37
+ private sendBatch;
38
+ }
39
+ /**
40
+ * 将 MonitorEventBundle 数组转换并编码为 SLS LogGroup Protobuf 格式
41
+ */
42
+ export declare function convertBundlesToLogGroup(data: MonitorEventBundle[]): Uint8Array;