@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.
- package/es/index.d.ts +3 -0
- package/es/index.js +3 -0
- package/es/model/client.d.ts +5 -4
- package/es/model/client.js +32 -6
- package/es/model/reporter.d.ts +30 -7
- package/es/model/reporter.js +83 -25
- package/es/model/shell.d.ts +4 -4
- package/es/model/shell.js +13 -10
- package/es/monitor/index.d.ts +6 -0
- package/es/monitor/index.js +3 -0
- package/es/monitor/logger.d.ts +34 -0
- package/es/monitor/logger.js +112 -0
- package/es/monitor/telemetry.d.ts +42 -0
- package/es/monitor/telemetry.js +210 -0
- package/es/monitor/types.d.ts +46 -0
- package/es/monitor/types.js +1 -0
- package/es/types/client.d.ts +16 -3
- package/es/types/collector.d.ts +2 -2
- package/es/types/reporter.d.ts +2 -2
- package/es/types/rum-event.d.ts +9 -0
- package/es/types/rum-event.js +4 -0
- package/es/types/shell.d.ts +3 -3
- package/es/utils/base.js +2 -2
- package/es/utils/disposable.d.ts +12 -0
- package/es/utils/disposable.js +20 -0
- package/es/utils/events.js +2 -1
- package/es/utils/protobuf.d.ts +24 -0
- package/es/utils/protobuf.js +123 -0
- package/es/utils/throttle.d.ts +14 -0
- package/es/utils/throttle.js +40 -0
- package/lib/index.d.ts +3 -0
- package/lib/index.js +21 -0
- package/lib/model/client.d.ts +5 -4
- package/lib/model/client.js +31 -5
- package/lib/model/reporter.d.ts +30 -7
- package/lib/model/reporter.js +80 -22
- package/lib/model/shell.d.ts +4 -4
- package/lib/model/shell.js +11 -8
- package/lib/monitor/index.d.ts +6 -0
- package/lib/monitor/index.js +12 -0
- package/lib/monitor/logger.d.ts +34 -0
- package/lib/monitor/logger.js +117 -0
- package/lib/monitor/telemetry.d.ts +42 -0
- package/lib/monitor/telemetry.js +214 -0
- package/lib/monitor/types.d.ts +46 -0
- package/lib/monitor/types.js +3 -0
- package/lib/types/client.d.ts +16 -3
- package/lib/types/collector.d.ts +2 -2
- package/lib/types/reporter.d.ts +2 -2
- package/lib/types/rum-event.d.ts +9 -0
- package/lib/types/rum-event.js +2 -0
- package/lib/types/shell.d.ts +3 -3
- package/lib/utils/base.js +2 -2
- package/lib/utils/disposable.d.ts +12 -0
- package/lib/utils/disposable.js +24 -0
- package/lib/utils/events.js +2 -1
- package/lib/utils/protobuf.d.ts +24 -0
- package/lib/utils/protobuf.js +127 -0
- package/lib/utils/throttle.d.ts +14 -0
- package/lib/utils/throttle.js +44 -0
- package/package.json +10 -1
package/lib/model/client.d.ts
CHANGED
|
@@ -1,17 +1,18 @@
|
|
|
1
1
|
import { IClient, IConfiguration, IContext, IRumSession } from '../types/client';
|
|
2
|
-
import { IConfigManager } from
|
|
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;
|
package/lib/model/client.js
CHANGED
|
@@ -5,6 +5,7 @@ exports.__esModule = true;
|
|
|
5
5
|
exports["default"] = void 0;
|
|
6
6
|
var _client = require("../types/client");
|
|
7
7
|
var _events = require("../utils/events");
|
|
8
|
+
var _telemetry = require("../monitor/telemetry");
|
|
8
9
|
var _context = _interopRequireDefault(require("./context"));
|
|
9
10
|
var _is = require("../utils/is");
|
|
10
11
|
function _createForOfIteratorHelperLoose(r, e) { var t = "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"]; if (t) return (t = t.call(r)).next.bind(t); if (Array.isArray(r) || (t = _unsupportedIterableToArray(r)) || e && r && "number" == typeof r.length) { t && (r = t); var o = 0; return function () { return o >= r.length ? { done: !0 } : { done: !1, value: r[o++] }; }; } throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
|
|
@@ -18,14 +19,39 @@ var Client = /*#__PURE__*/function () {
|
|
|
18
19
|
this.processors = void 0;
|
|
19
20
|
this.reporter = void 0;
|
|
20
21
|
this.ctx = void 0;
|
|
21
|
-
this.sendEvent = function (payload) {
|
|
22
|
-
_this.emitter.emit(_client.EventType.collect, payload);
|
|
22
|
+
this.sendEvent = function (payload, options) {
|
|
23
|
+
_this.emitter.emit(_client.EventType.collect, payload, options);
|
|
23
24
|
};
|
|
24
25
|
}
|
|
25
26
|
var _proto = Client.prototype;
|
|
26
|
-
_proto.init = function init(config, rumSession, configManager) {
|
|
27
|
+
_proto.init = function init(config, rumSession, configManager, monitorReporter) {
|
|
27
28
|
var _this2 = this;
|
|
28
29
|
this.ctx = new _context["default"](config, rumSession, configManager);
|
|
30
|
+
|
|
31
|
+
// Telemetry 初始化 —— 统一在 Core 中完成,各平台仅需传入 reporter
|
|
32
|
+
if (monitorReporter) {
|
|
33
|
+
var telemetryConfig = {
|
|
34
|
+
appType: (rumSession === null || rumSession === void 0 ? void 0 : rumSession.appType) || '',
|
|
35
|
+
sdkVersion: (rumSession === null || rumSession === void 0 ? void 0 : rumSession.sdkVersion) || '',
|
|
36
|
+
endpoint: config.endpoint,
|
|
37
|
+
getSessionId: rumSession ? function () {
|
|
38
|
+
return rumSession.getSessionId();
|
|
39
|
+
} : undefined,
|
|
40
|
+
getSessionSampled: rumSession ? function () {
|
|
41
|
+
return rumSession.getSampled();
|
|
42
|
+
} : undefined,
|
|
43
|
+
getViewId: function getViewId() {
|
|
44
|
+
var _this2$ctx;
|
|
45
|
+
var views = (_this2$ctx = _this2.ctx) === null || _this2$ctx === void 0 ? void 0 : _this2$ctx.getViews();
|
|
46
|
+
return views && views.length ? views[views.length - 1].id : undefined;
|
|
47
|
+
},
|
|
48
|
+
selfMonitor: config.selfMonitor
|
|
49
|
+
};
|
|
50
|
+
if (config.pid) {
|
|
51
|
+
telemetryConfig['app.id'] = config.pid;
|
|
52
|
+
}
|
|
53
|
+
_telemetry.Telemetry.init(telemetryConfig, monitorReporter);
|
|
54
|
+
}
|
|
29
55
|
var ctx = this.ctx,
|
|
30
56
|
collectors = this.collectors,
|
|
31
57
|
processors = this.processors,
|
|
@@ -35,7 +61,7 @@ var Client = /*#__PURE__*/function () {
|
|
|
35
61
|
});
|
|
36
62
|
|
|
37
63
|
// collector 收集数据统一 push 到各匹配 processor
|
|
38
|
-
this.emitter.on(_client.EventType.collect, function (payload) {
|
|
64
|
+
this.emitter.on(_client.EventType.collect, function (payload, options) {
|
|
39
65
|
ctx.setRumEvent(payload);
|
|
40
66
|
for (var _iterator = _createForOfIteratorHelperLoose(processors), _step; !(_step = _iterator()).done;) {
|
|
41
67
|
var processor = _step.value;
|
|
@@ -52,7 +78,7 @@ var Client = /*#__PURE__*/function () {
|
|
|
52
78
|
}
|
|
53
79
|
}
|
|
54
80
|
}
|
|
55
|
-
reporter.report(ctx);
|
|
81
|
+
reporter.report(ctx, options);
|
|
56
82
|
});
|
|
57
83
|
collectors.forEach(function (collector) {
|
|
58
84
|
collector.setup(ctx, _this2.sendEvent);
|
package/lib/model/reporter.d.ts
CHANGED
|
@@ -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
|
-
|
|
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
|
-
|
|
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 实现
|
package/lib/model/reporter.js
CHANGED
|
@@ -6,6 +6,7 @@ exports["default"] = void 0;
|
|
|
6
6
|
var _regenerator = _interopRequireDefault(require("@babel/runtime/regenerator"));
|
|
7
7
|
var _asyncToGenerator2 = _interopRequireDefault(require("@babel/runtime/helpers/asyncToGenerator"));
|
|
8
8
|
var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends"));
|
|
9
|
+
var _logger = require("../monitor/logger");
|
|
9
10
|
var _rumEvent = require("../types/rum-event");
|
|
10
11
|
var _exception = require("../utils/exception");
|
|
11
12
|
var _is = require("../utils/is");
|
|
@@ -14,13 +15,18 @@ var _url = require("../utils/url");
|
|
|
14
15
|
var FLUSH_TIME = 3000;
|
|
15
16
|
var MAX_EVENT_COUNT = 20;
|
|
16
17
|
var attrs = ['app', 'user', 'device', 'os', 'geo', 'isp', 'net', 'properties'];
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* 抽象上报器,负责事件排队、批量打包与发送。
|
|
21
|
+
*
|
|
22
|
+
* 上报模式(通过 SendEventOptions 控制):
|
|
23
|
+
* - 默认:事件进入队列,等待定时器或队列满后批量 flush
|
|
24
|
+
* - immediate:事件进入队列后立即 flush 整个队列
|
|
25
|
+
* - single:事件不进队列,独立打包立即上报
|
|
26
|
+
*/
|
|
17
27
|
var Reporter = exports["default"] = /*#__PURE__*/function () {
|
|
18
28
|
function Reporter() {
|
|
19
29
|
this.name = 'reporter';
|
|
20
|
-
/**
|
|
21
|
-
* 新上报事件优先检测队列是否超过 50 条,如果超过立即 report。
|
|
22
|
-
* 如果没超过则检测 FlushTime 内有无新上报事件,如果有新事件,重置 timer,无则 report
|
|
23
|
-
*/
|
|
24
30
|
this.eventQueue = [];
|
|
25
31
|
this.ctx = void 0;
|
|
26
32
|
this.timer = void 0;
|
|
@@ -39,25 +45,54 @@ var Reporter = exports["default"] = /*#__PURE__*/function () {
|
|
|
39
45
|
cfg.maxEventCount = MAX_EVENT_COUNT;
|
|
40
46
|
}
|
|
41
47
|
return cfg;
|
|
42
|
-
}
|
|
43
|
-
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* 上报入口,根据 options 决定上报策略:
|
|
52
|
+
* - single: 当前事件独立打包立即发送,不影响队列
|
|
53
|
+
* - immediate: 当前事件入队后立即 flush 整个队列
|
|
54
|
+
* - 默认: 当前事件入队,等待定时器或队列满后批量 flush
|
|
55
|
+
*/;
|
|
56
|
+
_proto.report = function report(ctx, options) {
|
|
44
57
|
var _this = this;
|
|
45
58
|
this.ctx = ctx;
|
|
46
59
|
if (!this._init) {
|
|
47
60
|
this.init(ctx);
|
|
48
61
|
this._init = true;
|
|
49
62
|
}
|
|
63
|
+
|
|
64
|
+
// single 优先级最高:当前事件单独上报,不影响队列
|
|
65
|
+
if (options && options.single) {
|
|
66
|
+
this.flushSingleEvent(ctx);
|
|
67
|
+
return;
|
|
68
|
+
}
|
|
50
69
|
clearTimeout(this.timer);
|
|
51
70
|
this.pushToQueue();
|
|
52
71
|
var reportConfig = this.getReportCfg();
|
|
53
|
-
|
|
54
|
-
if (this.eventQueue.length >= reportConfig.maxEventCount) {
|
|
72
|
+
if (options && options.immediate || this.eventQueue.length >= reportConfig.maxEventCount) {
|
|
55
73
|
this.flushEventQueue();
|
|
56
74
|
} else {
|
|
57
75
|
this.timer = setTimeout(function () {
|
|
58
76
|
_this.flushEventQueue();
|
|
59
77
|
}, reportConfig.flushTime);
|
|
60
78
|
}
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
/**
|
|
82
|
+
* 当前事件独立打包上报,不进入批量队列,不重置队列定时器
|
|
83
|
+
*/;
|
|
84
|
+
_proto.flushSingleEvent = function flushSingleEvent(ctx) {
|
|
85
|
+
var _event$view;
|
|
86
|
+
var session = ctx.session;
|
|
87
|
+
var sampled = session ? session.getSampled() : true;
|
|
88
|
+
if (!sampled) return;
|
|
89
|
+
var event = ctx.getRumEvent();
|
|
90
|
+
var views = ctx.getViews();
|
|
91
|
+
var curView = views[views.length - 1];
|
|
92
|
+
if (((_event$view = event.view) === null || _event$view === void 0 ? void 0 : _event$view.id) === curView.id) {
|
|
93
|
+
delete event.view;
|
|
94
|
+
}
|
|
95
|
+
this.dispatchEvents(ctx, [event], curView);
|
|
61
96
|
};
|
|
62
97
|
_proto.pushToQueue = function pushToQueue() {
|
|
63
98
|
var ctx = this.ctx,
|
|
@@ -120,8 +155,8 @@ var Reporter = exports["default"] = /*#__PURE__*/function () {
|
|
|
120
155
|
views.forEach(function (view) {
|
|
121
156
|
if (view.id === curView.id) {
|
|
122
157
|
var events = eventQueue.filter(function (event) {
|
|
123
|
-
var _event$
|
|
124
|
-
return ((_event$
|
|
158
|
+
var _event$view2;
|
|
159
|
+
return ((_event$view2 = event.view) === null || _event$view2 === void 0 ? void 0 : _event$view2.id) === view.id;
|
|
125
160
|
});
|
|
126
161
|
events.forEach(function (event) {
|
|
127
162
|
delete event.view;
|
|
@@ -130,28 +165,49 @@ var Reporter = exports["default"] = /*#__PURE__*/function () {
|
|
|
130
165
|
});
|
|
131
166
|
var session = ctx.session;
|
|
132
167
|
var sampled = session ? session.getSampled() : true;
|
|
133
|
-
sampled && this.
|
|
168
|
+
sampled && this.dispatchEvents(ctx, eventQueue, curView);
|
|
134
169
|
this.eventQueue = [];
|
|
135
|
-
}
|
|
136
|
-
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
/**
|
|
173
|
+
* 按 session_id 分组事件并分别打包上报:
|
|
174
|
+
* - 当前 session 的事件统一打包
|
|
175
|
+
* - 非当前 session 的事件按各自 session_id 单独打包
|
|
176
|
+
*/;
|
|
177
|
+
_proto.dispatchEvents = function dispatchEvents(ctx, events, view) {
|
|
137
178
|
var config = ctx.getConfig();
|
|
138
179
|
var session = ctx.session;
|
|
139
180
|
var sessionId = session.getSessionId();
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
// delete event.session_id;
|
|
143
|
-
// }
|
|
144
|
-
// });
|
|
181
|
+
var currentSessionEvents = [];
|
|
182
|
+
var otherSessionEventsMap = {};
|
|
145
183
|
for (var i = 0; i < events.length; i++) {
|
|
146
184
|
var e = events[i];
|
|
147
|
-
if (e.session_id === sessionId) {
|
|
185
|
+
if (!e.session_id || e.session_id === sessionId) {
|
|
148
186
|
delete e.session_id;
|
|
187
|
+
currentSessionEvents.push(e);
|
|
149
188
|
} else {
|
|
150
|
-
|
|
151
|
-
|
|
189
|
+
var sid = e.session_id;
|
|
190
|
+
if (!otherSessionEventsMap[sid]) {
|
|
191
|
+
otherSessionEventsMap[sid] = [];
|
|
192
|
+
}
|
|
193
|
+
delete e.session_id;
|
|
194
|
+
otherSessionEventsMap[sid].push(e);
|
|
152
195
|
}
|
|
153
196
|
}
|
|
154
|
-
if (
|
|
197
|
+
if (currentSessionEvents.length > 0) {
|
|
198
|
+
this.buildAndSend(ctx, config, session, sessionId, currentSessionEvents, view);
|
|
199
|
+
}
|
|
200
|
+
var otherSessionIds = Object.keys(otherSessionEventsMap);
|
|
201
|
+
for (var _i = 0; _i < otherSessionIds.length; _i++) {
|
|
202
|
+
var _sid = otherSessionIds[_i];
|
|
203
|
+
this.buildAndSend(ctx, config, session, _sid, otherSessionEventsMap[_sid], view);
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
/**
|
|
208
|
+
* 构建 bundle 并发送请求
|
|
209
|
+
*/;
|
|
210
|
+
_proto.buildAndSend = function buildAndSend(ctx, config, session, sessionId, events, view) {
|
|
155
211
|
var extend = (0, _url.getUrlParams)(config.endpoint);
|
|
156
212
|
var bundle = (0, _extends2["default"])({
|
|
157
213
|
app: {
|
|
@@ -222,6 +278,8 @@ var Reporter = exports["default"] = /*#__PURE__*/function () {
|
|
|
222
278
|
setTimeout(function () {
|
|
223
279
|
return _this2.tryRequest(bundle, retry + 1);
|
|
224
280
|
}, retryDelay);
|
|
281
|
+
} else {
|
|
282
|
+
_logger.logger.error('reporter', "Request failed after " + (retry + 1) + " attempts", _t);
|
|
225
283
|
}
|
|
226
284
|
case 4:
|
|
227
285
|
case "end":
|
package/lib/model/shell.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import { IClient, IConfiguration } from
|
|
2
|
-
import { RumCustomEvent, RumEvent, RumExceptionEvent, RumResourceEvent, RumViewEvent } from
|
|
3
|
-
import { IShell } from
|
|
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/lib/model/shell.js
CHANGED
|
@@ -40,9 +40,9 @@ var Shell = exports["default"] = /*#__PURE__*/function () {
|
|
|
40
40
|
* 初始化
|
|
41
41
|
*/
|
|
42
42
|
var _proto = Shell.prototype;
|
|
43
|
-
_proto.sendEvent = function sendEvent(payload) {
|
|
43
|
+
_proto.sendEvent = function sendEvent(payload, options) {
|
|
44
44
|
if (this.client) {
|
|
45
|
-
this.client.sendEvent(payload);
|
|
45
|
+
this.client.sendEvent(payload, options);
|
|
46
46
|
}
|
|
47
47
|
}
|
|
48
48
|
|
|
@@ -69,10 +69,11 @@ var Shell = exports["default"] = /*#__PURE__*/function () {
|
|
|
69
69
|
event_type: _rumEvent.RumEventType.CUSTOM
|
|
70
70
|
});
|
|
71
71
|
this.sendEvent(data);
|
|
72
|
-
}
|
|
72
|
+
}
|
|
73
|
+
|
|
73
74
|
/**
|
|
74
75
|
* 自定义视图上报
|
|
75
|
-
|
|
76
|
+
*/;
|
|
76
77
|
_proto.sendView = function sendView(payload) {
|
|
77
78
|
if (!(0, _is.isObject)(payload)) {
|
|
78
79
|
return;
|
|
@@ -94,10 +95,11 @@ var Shell = exports["default"] = /*#__PURE__*/function () {
|
|
|
94
95
|
event_type: _rumEvent.RumEventType.VIEW
|
|
95
96
|
});
|
|
96
97
|
this.sendEvent(data);
|
|
97
|
-
}
|
|
98
|
+
}
|
|
99
|
+
|
|
98
100
|
/**
|
|
99
101
|
* 自定义异常上报
|
|
100
|
-
|
|
102
|
+
*/;
|
|
101
103
|
_proto.sendException = function sendException(payload) {
|
|
102
104
|
if (!payload.name || !payload.message) {
|
|
103
105
|
return;
|
|
@@ -116,10 +118,11 @@ var Shell = exports["default"] = /*#__PURE__*/function () {
|
|
|
116
118
|
source: 'custom'
|
|
117
119
|
});
|
|
118
120
|
this.sendEvent(data);
|
|
119
|
-
}
|
|
121
|
+
}
|
|
122
|
+
|
|
120
123
|
/**
|
|
121
124
|
* 自定义资源上报
|
|
122
|
-
|
|
125
|
+
*/;
|
|
123
126
|
_proto.sendResource = function sendResource(payload) {
|
|
124
127
|
if (!payload.name || !payload.type || !(0, _is.isNumber)(payload.duration)) {
|
|
125
128
|
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,12 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
exports.__esModule = true;
|
|
4
|
+
exports.logger = exports.encodeLogGroup = exports.convertBundlesToLogGroup = exports.callMonitored = exports.Telemetry = void 0;
|
|
5
|
+
var _logger = require("./logger");
|
|
6
|
+
exports.logger = _logger.logger;
|
|
7
|
+
exports.callMonitored = _logger.callMonitored;
|
|
8
|
+
var _telemetry = require("./telemetry");
|
|
9
|
+
exports.Telemetry = _telemetry.Telemetry;
|
|
10
|
+
exports.convertBundlesToLogGroup = _telemetry.convertBundlesToLogGroup;
|
|
11
|
+
var _protobuf = require("../utils/protobuf");
|
|
12
|
+
exports.encodeLogGroup = _protobuf.encodeLogGroup;
|
|
@@ -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,117 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
exports.__esModule = true;
|
|
4
|
+
exports.callMonitored = callMonitored;
|
|
5
|
+
exports.logger = void 0;
|
|
6
|
+
var DEFAULT_REPORT_LEVELS = ['debug', 'error', 'crash'];
|
|
7
|
+
var Logger = /*#__PURE__*/function () {
|
|
8
|
+
function Logger() {
|
|
9
|
+
this.console = false;
|
|
10
|
+
this.reportLevels = DEFAULT_REPORT_LEVELS;
|
|
11
|
+
this.onError = void 0;
|
|
12
|
+
}
|
|
13
|
+
var _proto = Logger.prototype;
|
|
14
|
+
_proto.setConsole = function setConsole(enabled) {
|
|
15
|
+
this.console = enabled;
|
|
16
|
+
};
|
|
17
|
+
_proto.setReportLevels = function setReportLevels(levels) {
|
|
18
|
+
this.reportLevels = levels;
|
|
19
|
+
};
|
|
20
|
+
_proto.setOnError = function setOnError(handler) {
|
|
21
|
+
this.onError = handler;
|
|
22
|
+
};
|
|
23
|
+
_proto.debug = function debug() {
|
|
24
|
+
for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
|
|
25
|
+
args[_key] = arguments[_key];
|
|
26
|
+
}
|
|
27
|
+
this.log('debug', args);
|
|
28
|
+
};
|
|
29
|
+
_proto.info = function info() {
|
|
30
|
+
for (var _len2 = arguments.length, args = new Array(_len2), _key2 = 0; _key2 < _len2; _key2++) {
|
|
31
|
+
args[_key2] = arguments[_key2];
|
|
32
|
+
}
|
|
33
|
+
this.log('info', args);
|
|
34
|
+
};
|
|
35
|
+
_proto.warn = function warn() {
|
|
36
|
+
for (var _len3 = arguments.length, args = new Array(_len3), _key3 = 0; _key3 < _len3; _key3++) {
|
|
37
|
+
args[_key3] = arguments[_key3];
|
|
38
|
+
}
|
|
39
|
+
this.log('warn', args);
|
|
40
|
+
};
|
|
41
|
+
_proto.error = function error() {
|
|
42
|
+
for (var _len4 = arguments.length, args = new Array(_len4), _key4 = 0; _key4 < _len4; _key4++) {
|
|
43
|
+
args[_key4] = arguments[_key4];
|
|
44
|
+
}
|
|
45
|
+
this.log('error', args);
|
|
46
|
+
};
|
|
47
|
+
_proto.crash = function crash() {
|
|
48
|
+
for (var _len5 = arguments.length, args = new Array(_len5), _key5 = 0; _key5 < _len5; _key5++) {
|
|
49
|
+
args[_key5] = arguments[_key5];
|
|
50
|
+
}
|
|
51
|
+
this.log('crash', args);
|
|
52
|
+
};
|
|
53
|
+
_proto.log = function log(level, _ref) {
|
|
54
|
+
var _this$onError;
|
|
55
|
+
var module = _ref[0],
|
|
56
|
+
message = _ref[1],
|
|
57
|
+
error = _ref[2],
|
|
58
|
+
context = _ref[3];
|
|
59
|
+
if (this.console) {
|
|
60
|
+
var consoleFn = level === 'crash' ? 'error' : level === 'debug' ? 'log' : level;
|
|
61
|
+
console[consoleFn]("[RUM-SDK][" + module + "]", message, error || '');
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
// 仅上报配置中指定的级别
|
|
65
|
+
if (!this.reportLevels.includes(level)) return;
|
|
66
|
+
var stack;
|
|
67
|
+
if (error instanceof Error) {
|
|
68
|
+
stack = error.stack;
|
|
69
|
+
} else if (error && typeof error === 'object' && 'stack' in error) {
|
|
70
|
+
stack = String(error.stack);
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
// 截断过长的 stack(与 content 同等 12KB 上限)
|
|
74
|
+
if (stack && stack.length > 12288) {
|
|
75
|
+
stack = stack.slice(0, 12288);
|
|
76
|
+
}
|
|
77
|
+
(_this$onError = this.onError) === null || _this$onError === void 0 ? void 0 : _this$onError.call(this, Object.assign({
|
|
78
|
+
level: level,
|
|
79
|
+
content: message,
|
|
80
|
+
type: module,
|
|
81
|
+
stack: stack
|
|
82
|
+
}, context));
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
/**
|
|
86
|
+
* 函数包装器 — 用于箭头函数属性
|
|
87
|
+
* 用法: private handler = logger.wrap((e) => {...}, 'module', 'message?')
|
|
88
|
+
*/;
|
|
89
|
+
_proto.wrap = function wrap(fn, module, message) {
|
|
90
|
+
return function () {
|
|
91
|
+
for (var _len6 = arguments.length, args = new Array(_len6), _key6 = 0; _key6 < _len6; _key6++) {
|
|
92
|
+
args[_key6] = arguments[_key6];
|
|
93
|
+
}
|
|
94
|
+
return callMonitored(fn, this, args, module, message);
|
|
95
|
+
};
|
|
96
|
+
};
|
|
97
|
+
return Logger;
|
|
98
|
+
}();
|
|
99
|
+
var logger = exports.logger = new Logger();
|
|
100
|
+
|
|
101
|
+
/**
|
|
102
|
+
* 执行被监控函数,异常时自动上报
|
|
103
|
+
*/
|
|
104
|
+
function callMonitored(fn, context, args, module, message) {
|
|
105
|
+
try {
|
|
106
|
+
var result = fn.apply(context, args);
|
|
107
|
+
// 检测 async 函数返回的 Promise,附加 .catch 处理
|
|
108
|
+
if (result && typeof result["catch"] === 'function') {
|
|
109
|
+
result["catch"](function (e) {
|
|
110
|
+
logger.error(module || 'unknown', message || (e instanceof Error ? e.message : String(e)), e);
|
|
111
|
+
});
|
|
112
|
+
}
|
|
113
|
+
return result;
|
|
114
|
+
} catch (e) {
|
|
115
|
+
logger.error(module || 'unknown', message || (e instanceof Error ? e.message : String(e)), e);
|
|
116
|
+
}
|
|
117
|
+
}
|
|
@@ -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;
|