jason-trace-log 1.0.5 → 1.0.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.
@@ -276,117 +276,6 @@
276
276
  // onFCP(saveMetric)
277
277
  };
278
278
 
279
- /**
280
- * base64.ts
281
- *
282
- * Licensed under the BSD 3-Clause License.
283
- * http://opensource.org/licenses/BSD-3-Clause
284
- *
285
- * References:
286
- * http://en.wikipedia.org/wiki/Base64
287
- *
288
- * @author Dan Kogai (https://github.com/dankogai)
289
- */
290
- const _hasBuffer = typeof Buffer === 'function';
291
- const _TD = typeof TextDecoder === 'function' ? new TextDecoder() : undefined;
292
- const _TE = typeof TextEncoder === 'function' ? new TextEncoder() : undefined;
293
- const b64ch = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=';
294
- const b64chs = Array.prototype.slice.call(b64ch);
295
- const b64tab = ((a) => {
296
- let tab = {};
297
- a.forEach((c, i) => tab[c] = i);
298
- return tab;
299
- })(b64chs);
300
- const _fromCC = String.fromCharCode.bind(String);
301
- const _U8Afrom = typeof Uint8Array.from === 'function'
302
- ? Uint8Array.from.bind(Uint8Array)
303
- : (it) => new Uint8Array(Array.prototype.slice.call(it, 0));
304
- const _mkUriSafe = (src) => src
305
- .replace(/=/g, '').replace(/[+\/]/g, (m0) => m0 == '+' ? '-' : '_');
306
- /**
307
- * polyfill version of `btoa`
308
- */
309
- const btoaPolyfill = (bin) => {
310
- // console.log('polyfilled');
311
- let u32, c0, c1, c2, asc = '';
312
- const pad = bin.length % 3;
313
- for (let i = 0; i < bin.length;) {
314
- if ((c0 = bin.charCodeAt(i++)) > 255 ||
315
- (c1 = bin.charCodeAt(i++)) > 255 ||
316
- (c2 = bin.charCodeAt(i++)) > 255)
317
- throw new TypeError('invalid character found');
318
- u32 = (c0 << 16) | (c1 << 8) | c2;
319
- asc += b64chs[u32 >> 18 & 63]
320
- + b64chs[u32 >> 12 & 63]
321
- + b64chs[u32 >> 6 & 63]
322
- + b64chs[u32 & 63];
323
- }
324
- return pad ? asc.slice(0, pad - 3) + "===".substring(pad) : asc;
325
- };
326
- /**
327
- * does what `window.btoa` of web browsers do.
328
- * @param {String} bin binary string
329
- * @returns {string} Base64-encoded string
330
- */
331
- const _btoa = typeof btoa === 'function' ? (bin) => btoa(bin)
332
- : _hasBuffer ? (bin) => Buffer.from(bin, 'binary').toString('base64')
333
- : btoaPolyfill;
334
- const _fromUint8Array = _hasBuffer
335
- ? (u8a) => Buffer.from(u8a).toString('base64')
336
- : (u8a) => {
337
- // cf. https://stackoverflow.com/questions/12710001/how-to-convert-uint8-array-to-base64-encoded-string/12713326#12713326
338
- const maxargs = 0x1000;
339
- let strs = [];
340
- for (let i = 0, l = u8a.length; i < l; i += maxargs) {
341
- strs.push(_fromCC.apply(null, u8a.subarray(i, i + maxargs)));
342
- }
343
- return _btoa(strs.join(''));
344
- };
345
- // This trick is found broken https://github.com/dankogai/js-base64/issues/130
346
- // const utob = (src: string) => unescape(encodeURIComponent(src));
347
- // reverting good old fationed regexp
348
- const cb_utob = (c) => {
349
- if (c.length < 2) {
350
- var cc = c.charCodeAt(0);
351
- return cc < 0x80 ? c
352
- : cc < 0x800 ? (_fromCC(0xc0 | (cc >>> 6))
353
- + _fromCC(0x80 | (cc & 0x3f)))
354
- : (_fromCC(0xe0 | ((cc >>> 12) & 0x0f))
355
- + _fromCC(0x80 | ((cc >>> 6) & 0x3f))
356
- + _fromCC(0x80 | (cc & 0x3f)));
357
- }
358
- else {
359
- var cc = 0x10000
360
- + (c.charCodeAt(0) - 0xD800) * 0x400
361
- + (c.charCodeAt(1) - 0xDC00);
362
- return (_fromCC(0xf0 | ((cc >>> 18) & 0x07))
363
- + _fromCC(0x80 | ((cc >>> 12) & 0x3f))
364
- + _fromCC(0x80 | ((cc >>> 6) & 0x3f))
365
- + _fromCC(0x80 | (cc & 0x3f)));
366
- }
367
- };
368
- const re_utob = /[\uD800-\uDBFF][\uDC00-\uDFFFF]|[^\x00-\x7F]/g;
369
- /**
370
- * @deprecated should have been internal use only.
371
- * @param {string} src UTF-8 string
372
- * @returns {string} UTF-16 string
373
- */
374
- const utob = (u) => u.replace(re_utob, cb_utob);
375
- //
376
- const _encode = _hasBuffer
377
- ? (s) => Buffer.from(s, 'utf8').toString('base64')
378
- : _TE
379
- ? (s) => _fromUint8Array(_TE.encode(s))
380
- : (s) => _btoa(utob(s));
381
- /**
382
- * converts a UTF-8-encoded string to a Base64 string.
383
- * @param {boolean} [urlsafe] if `true` make the result URL-safe
384
- * @returns {string} Base64 string
385
- */
386
- const encode = (src, urlsafe = false) => urlsafe
387
- ? _mkUriSafe(_encode(src))
388
- : _encode(src);
389
-
390
279
  var TraceDataSeverity;
391
280
  (function (TraceDataSeverity) {
392
281
  // 其他
@@ -524,12 +413,12 @@
524
413
  target instanceof HTMLAudioElement;
525
414
  };
526
415
  /**
527
- * 根据字符串生成hashcode
528
- *
529
- * @export
530
- * @param {string} str
531
- * @return {*} {number} 可为正数和负数
532
- */
416
+ * 根据字符串生成hashcode
417
+ *
418
+ * @export
419
+ * @param {string} str
420
+ * @return {*} {number} 可为正数和负数
421
+ */
533
422
  function hashCode(str) {
534
423
  var hash = 0;
535
424
  if (str.length == 0)
@@ -624,22 +513,22 @@
624
513
  }
625
514
  function getPerfLevel(data) {
626
515
  var level = TraceLevelType.Info;
627
- if (data.LCPRating === 'poor'
628
- || data.FIDRating === 'poor'
629
- || data.FCPRating === 'poor'
630
- || data.TTFBRating === 'poor'
631
- || data.CLSRating === 'poor'
632
- || data.INPRating === 'poor') {
516
+ if (data.LCPRating === 'poor' ||
517
+ data.FIDRating === 'poor' ||
518
+ data.FCPRating === 'poor' ||
519
+ data.TTFBRating === 'poor' ||
520
+ data.CLSRating === 'poor' ||
521
+ data.INPRating === 'poor') {
633
522
  // console.log('[getPerfLevel] error')
634
523
  level = TraceLevelType.Error;
635
524
  return;
636
525
  }
637
- if (data.LCPRating === 'needs improvement'
638
- || data.CLSRating === 'needs improvement'
639
- || data.FCPRating === 'needs improvement'
640
- || data.FIDRating === 'needs improvement'
641
- || data.INPRating === 'needs improvement'
642
- || data.TTFBRating === 'needs improvement') {
526
+ if (data.LCPRating === 'needs improvement' ||
527
+ data.CLSRating === 'needs improvement' ||
528
+ data.FCPRating === 'needs improvement' ||
529
+ data.FIDRating === 'needs improvement' ||
530
+ data.INPRating === 'needs improvement' ||
531
+ data.TTFBRating === 'needs improvement') {
643
532
  // console.log('[getPerfLevel] warn')
644
533
  level = TraceLevelType.Warn;
645
534
  return;
@@ -671,23 +560,19 @@
671
560
  var originFetch = window.fetch;
672
561
  // 拦截fetch
673
562
  var interceptFetch = function (_a) {
674
- var pagePath = _a.pagePath, onError = _a.onError, onBefore = _a.onBefore, onAfter = _a.onAfter;
563
+ var onError = _a.onError, onBefore = _a.onBefore, onAfter = _a.onAfter;
675
564
  return function () {
676
565
  var args = [];
677
566
  for (var _i = 0; _i < arguments.length; _i++) {
678
567
  args[_i] = arguments[_i];
679
568
  }
680
569
  return __awaiter(void 0, void 0, void 0, function () {
681
- var url, options, startTime, traceId, traceSegmentId, appId, appVersion, traceIdStr, segmentId, service, instance, endpoint, peer, res, err_1;
570
+ var url, options, startTime, res, err_1;
682
571
  return __generator(this, function (_a) {
683
572
  switch (_a.label) {
684
573
  case 0:
685
574
  url = args[0], options = args[1];
686
575
  startTime = getTimestamp();
687
- traceId = uuid();
688
- traceSegmentId = uuid();
689
- appId = uuid();
690
- appVersion = 'v1.0.0';
691
576
  if (Object.prototype.toString.call(args[0]) === '[object Request]') {
692
577
  url = new URL(url.url);
693
578
  }
@@ -703,12 +588,6 @@
703
588
  url.pathname = args[0];
704
589
  }
705
590
  }
706
- traceIdStr = String(encode(traceId));
707
- segmentId = String(encode(traceSegmentId));
708
- service = String(encode(appId));
709
- instance = String(encode(appVersion));
710
- endpoint = String(encode(pagePath));
711
- peer = String(encode(url.host));
712
591
  if (!options) {
713
592
  options = {};
714
593
  }
@@ -850,7 +729,9 @@
850
729
  });
851
730
  }
852
731
  var ltsData = convertTraceDataToLTS(data);
853
- ltsInstance.reportImmediately(ltsData);
732
+ // 使用 reportImmediately 的第二个参数传递标签
733
+ // 标签格式: { key: 'value' },最多支持 50 个标签
734
+ ltsInstance.report(ltsData);
854
735
  }
855
736
  catch (error) {
856
737
  console.error('Failed to send data to LTS:', error);
@@ -910,7 +791,6 @@
910
791
 
911
792
  var BaseTrace = /** @class */ (function () {
912
793
  function BaseTrace(options) {
913
- var _this = this;
914
794
  // 日志上报后端API
915
795
  this.dsn = '';
916
796
  // 页面ID
@@ -942,12 +822,13 @@
942
822
  this.maxBreadcrumb = 10;
943
823
  // 是否开启用户行为
944
824
  this.breadcrumbEnabled = true;
945
- this.observer = null;
946
825
  // 存储链路日志数据
947
826
  this.queue = [];
948
827
  // 发送请求时间间隔
949
828
  this.sendTimer = 1000;
950
- console.log('BaseTrace constructor.');
829
+ // 自定义全局字段
830
+ this.customGlobalFields = {};
831
+ this.debug && console.log('BaseTrace constructor.');
951
832
  this.pageId = uuid();
952
833
  this.dsn = options.dsn || '';
953
834
  this.appId = options.appId;
@@ -958,30 +839,26 @@
958
839
  id: generateUniqueId$1()
959
840
  };
960
841
  this.fpId = getFingerprintId('TraceCourse');
961
- this.observer = new PerformanceObserver(function (list, observer) {
962
- list.getEntries().forEach(function (entry) {
963
- _this.debug && console.debug("name : ".concat(entry.name));
964
- _this.debug && console.debug("type : ".concat(entry.entryType));
965
- _this.debug && console.debug("duration: ".concat(entry.duration));
966
- if (entry.entryType === 'resource') {
967
- _this.handleObserverResource(entry);
968
- }
969
- });
970
- });
842
+ this.customGlobalFields = options.customGlobalFields || {};
971
843
  }
972
844
  BaseTrace.prototype.log = function (log) {
973
- this.saveBreadcrumb({
974
- name: 'customer-log',
975
- level: log.level,
976
- type: dataTypes2BreadcrumbsType(log.type),
977
- category: dataCategory2BreadcrumbsCategory(log.type),
978
- message: log.message,
979
- time: getTimestamp()
980
- });
981
- this.debug && console.debug("log: ".concat(JSON.stringify(log)));
982
- this.send(log);
845
+ try {
846
+ this.saveBreadcrumb({
847
+ name: 'customer-log',
848
+ level: log.level,
849
+ type: dataTypes2BreadcrumbsType(log.type),
850
+ category: dataCategory2BreadcrumbsCategory(log.type),
851
+ message: log.message,
852
+ time: getTimestamp()
853
+ });
854
+ this.debug && console.debug("log: ".concat(JSON.stringify(log)));
855
+ this.send(log);
856
+ }
857
+ catch (error) {
858
+ console.error('Failed to log trace data:', error);
859
+ }
983
860
  };
984
- BaseTrace.prototype.info = function (message, tag) {
861
+ BaseTrace.prototype.info = function (message, tag, extra) {
985
862
  this.log({
986
863
  name: 'customer-info',
987
864
  type: TraceDataTypes.LOG,
@@ -989,10 +866,11 @@
989
866
  message: message,
990
867
  time: getTimestamp(),
991
868
  dataId: hashCode("".concat(message, "|").concat(tag || '')),
992
- tag: tag
869
+ tag: tag,
870
+ extra: extra
993
871
  });
994
872
  };
995
- BaseTrace.prototype.warn = function (message, tag) {
873
+ BaseTrace.prototype.warn = function (message, tag, extra) {
996
874
  this.log({
997
875
  name: 'customer-warning',
998
876
  type: TraceDataTypes.LOG,
@@ -1000,10 +878,11 @@
1000
878
  message: message,
1001
879
  time: getTimestamp(),
1002
880
  dataId: hashCode("".concat(message, "|").concat(tag || '')),
1003
- tag: tag
881
+ tag: tag,
882
+ extra: extra
1004
883
  });
1005
884
  };
1006
- BaseTrace.prototype.error = function (message, tag) {
885
+ BaseTrace.prototype.error = function (message, tag, extra) {
1007
886
  this.log({
1008
887
  name: 'customer-error',
1009
888
  type: TraceDataTypes.LOG,
@@ -1011,7 +890,8 @@
1011
890
  message: message,
1012
891
  time: getTimestamp(),
1013
892
  dataId: hashCode("".concat(message, "|").concat(tag || '')),
1014
- tag: tag
893
+ tag: tag,
894
+ extra: extra
1015
895
  });
1016
896
  };
1017
897
  BaseTrace.prototype.setTraceData = function (data) {
@@ -1029,30 +909,26 @@
1029
909
  level = getPerfLevel(data);
1030
910
  perf = data;
1031
911
  }
1032
- var traceData = {
1033
- type: type,
1034
- level: level,
1035
- createdAt: getTimestamp(),
1036
- updatedAt: getTimestamp(),
1037
- data: _data,
1038
- perf: perf,
1039
- breadcrumbs: this.breadcrumb,
1040
- traceId: uuid(),
1041
- ua: this.userAgent,
1042
- bt: this.browserType,
1043
- fpId: this.fpId,
1044
- appId: this.appId,
1045
- clientType: TraceClientTypes.BROWSER_H5,
1046
- url: document.URL,
1047
- pid: this.pageId,
1048
- uid: this.uid
1049
- };
912
+ var traceData = __assign({ type: type, level: level, createdAt: getTimestamp(), updatedAt: getTimestamp(), data: _data, perf: perf, breadcrumbs: this.breadcrumb, traceId: uuid(), ua: this.userAgent, bt: this.browserType, fpId: this.fpId, appId: this.appId, clientType: TraceClientTypes.BROWSER_H5, url: typeof document !== 'undefined' ? document.URL : '', pid: this.pageId, uid: this.uid }, this.customGlobalFields);
1050
913
  this.debug && console.log('[setTraceData]traceData: ', traceData);
1051
914
  return traceData;
1052
915
  };
1053
916
  BaseTrace.prototype.send = function (data) {
1054
- var traceData = this.setTraceData(data);
1055
- send(this.sendMethod, this.dsn, this.ltsConfig, traceData);
917
+ try {
918
+ var traceData = this.setTraceData(data);
919
+ send(this.sendMethod, this.dsn, this.ltsConfig, traceData);
920
+ }
921
+ catch (error) {
922
+ console.error('Failed to send trace data:', error);
923
+ }
924
+ };
925
+ BaseTrace.prototype.enqueueOrSend = function (data) {
926
+ if (this.sendMethod === exports.SendMethod.LTS) {
927
+ send(this.sendMethod, this.dsn, this.ltsConfig, data);
928
+ }
929
+ else {
930
+ this.queue.push(data);
931
+ }
1056
932
  };
1057
933
  BaseTrace.prototype.createPerfReport = function () {
1058
934
  var _this = this;
@@ -1065,7 +941,7 @@
1065
941
  if (isLatestVisibilityChangeSupported) {
1066
942
  var onVisibilityChange = function () {
1067
943
  if (document.visibilityState === 'hidden') {
1068
- console.log('this.send', _this.perfData);
944
+ _this.debug && console.log('this.send', _this.perfData);
1069
945
  _this.send(_this.perfData);
1070
946
  // removeEventListener('visibilitychange', onVisibilityChange, true)
1071
947
  }
@@ -1074,18 +950,63 @@
1074
950
  }
1075
951
  else {
1076
952
  addEventListener('pagehide', function () {
1077
- console.log('pagehide', _this.perfData);
953
+ _this.debug && console.log('pagehide', _this.perfData);
1078
954
  _this.send(_this.perfData);
1079
955
  }, { capture: true, once: true });
1080
956
  }
1081
957
  });
1082
958
  return report;
1083
959
  };
960
+ /**
961
+ * 保存 Promise 异常
962
+ */
963
+ BaseTrace.prototype.savePromiseError = function (reason) {
964
+ this.debug && console.log('[onPromiseRejection] reason: ', reason);
965
+ // 提取错误信息
966
+ var errorMessage = 'Unhandled Promise Rejection';
967
+ var errorStack = '';
968
+ var errorName = 'PromiseRejection';
969
+ if (reason instanceof Error) {
970
+ errorMessage = reason.message;
971
+ errorStack = reason.stack || '';
972
+ errorName = reason.name || 'Error';
973
+ }
974
+ else if (typeof reason === 'string') {
975
+ errorMessage = reason;
976
+ }
977
+ else {
978
+ errorMessage = String(reason);
979
+ }
980
+ var traceData = {
981
+ dataId: hashCode("promise-rejection-".concat(errorMessage, "-").concat(errorStack)),
982
+ name: 'promise-rejection',
983
+ level: TraceDataSeverity.Error,
984
+ message: errorMessage,
985
+ time: getTimestamp(),
986
+ type: TraceDataTypes.PROMISE,
987
+ stack: errorStack
988
+ };
989
+ this.resources.push(traceData);
990
+ this.breadcrumb.push({
991
+ name: errorName,
992
+ type: BreadcrumbTypes.UNHANDLEDREJECTION,
993
+ category: BreadcrumbsCategorys.Exception,
994
+ level: TraceDataSeverity.Error,
995
+ message: errorMessage,
996
+ stack: errorStack,
997
+ time: getTimestamp()
998
+ });
999
+ this.enqueueOrSend(this.setTraceData(traceData));
1000
+ };
1001
+ /**
1002
+ * 保存 JavaScript 运行时错误和资源加载错误
1003
+ */
1084
1004
  BaseTrace.prototype.saveError = function (event) {
1085
- console.log('[onResourceError] event: ', event);
1005
+ this.debug && console.log('[onResourceError] event: ', event);
1086
1006
  var target = event.target || event.srcElement;
1087
1007
  var isResTarget = isResourceTarget(target);
1088
1008
  if (!isResTarget) {
1009
+ // JavaScript 运行时错误
1089
1010
  var traceData = {
1090
1011
  dataId: hashCode("".concat(event.type, "-").concat(event.error.stack)),
1091
1012
  name: 'script-error',
@@ -1105,7 +1026,7 @@
1105
1026
  stack: event.error.stack,
1106
1027
  time: getTimestamp()
1107
1028
  });
1108
- this.queue.push(this.setTraceData(traceData));
1029
+ this.enqueueOrSend(this.setTraceData(traceData));
1109
1030
  }
1110
1031
  else {
1111
1032
  var url = target.getAttribute('src') || target.getAttribute('href');
@@ -1127,7 +1048,7 @@
1127
1048
  message: event.message,
1128
1049
  time: getTimestamp()
1129
1050
  });
1130
- this.queue.push(this.setTraceData(traceData));
1051
+ this.enqueueOrSend(this.setTraceData(traceData));
1131
1052
  }
1132
1053
  };
1133
1054
  BaseTrace.prototype.handleObserverResource = function (entry) {
@@ -1153,7 +1074,7 @@
1153
1074
  };
1154
1075
  // 这里的构造数据有问题,后续需要更新
1155
1076
  BaseTrace.prototype.onFetchError = function (message) {
1156
- console.log('[onFetchError] message: ', message);
1077
+ this.debug && console.log('[onFetchError] message: ', message);
1157
1078
  var traceBaseData = {
1158
1079
  dataId: hashCode("".concat(message.url, "-").concat(message.method, "-").concat(message.status, "-").concat(message.statusText)),
1159
1080
  name: 'fetch-error',
@@ -1163,31 +1084,20 @@
1163
1084
  type: TraceDataTypes.HTTP
1164
1085
  };
1165
1086
  var errorData = __assign(__assign({}, traceBaseData), { url: message.url, status: message.status, message: message.statusText, method: message.method, body: message.body, elapsedTime: message.elapsedTime, httpType: 'fetch' });
1166
- console.log('error data: ', errorData);
1167
- this.queue.push(this.setTraceData(errorData));
1087
+ this.debug && console.log('error data: ', errorData);
1088
+ this.enqueueOrSend(this.setTraceData(errorData));
1168
1089
  };
1169
1090
  BaseTrace.prototype.onGlobalError = function () {
1091
+ var _this = this;
1170
1092
  var _t = this;
1171
- console.log('onGlobalError');
1093
+ this.debug && console.log('onGlobalError');
1172
1094
  window.addEventListener('error', function (event) {
1173
1095
  _t.saveError(event);
1174
- });
1096
+ }, true);
1097
+ // 单独处理 Promise 异常
1175
1098
  window.addEventListener('unhandledrejection', function (event) {
1176
- // _t.saveError(event)
1177
- console.log(event);
1178
- if (event instanceof PromiseRejectionEvent) {
1179
- var errorEvent = new ErrorEvent('promiseRejection', {
1180
- message: event.reason.toString(),
1181
- // filename: event.filename,
1182
- // lineno: event.lineno,
1183
- // colno: event.colno,
1184
- error: event.reason
1185
- });
1186
- _t.saveError(errorEvent);
1187
- }
1188
- else if (event instanceof ErrorEvent) {
1189
- _t.saveError(event);
1190
- }
1099
+ _this.debug && console.log('[unhandledrejection] event: ', event);
1100
+ _t.savePromiseError(event.reason);
1191
1101
  });
1192
1102
  };
1193
1103
  BaseTrace.prototype.onGlobalClick = function () {
@@ -1207,17 +1117,21 @@
1207
1117
  });
1208
1118
  };
1209
1119
  BaseTrace.prototype.onObserverResource = function () {
1210
- // const observer = new PerformanceObserver((list, observer) => {
1211
- // list.getEntries().forEach((entry) => {
1212
- // console.log(`name : ${entry.name}`);
1213
- // console.log(`type : ${entry.entryType}`);
1214
- // console.log(`duration: ${entry.duration}`);
1215
- // _t.handleObserverResource(entry)
1216
- // });
1217
- // });
1218
- // observer.observe({
1219
- // entryTypes: ["resource"],
1220
- // });
1120
+ var _this = this;
1121
+ var _t = this;
1122
+ var observer = new PerformanceObserver(function (list, observer) {
1123
+ list.getEntries().forEach(function (entry) {
1124
+ _this.debug && console.log("name : ".concat(entry.name));
1125
+ _this.debug && console.log("type : ".concat(entry.entryType));
1126
+ _this.debug && console.log("duration: ".concat(entry.duration));
1127
+ if (entry.entryType === 'resource') {
1128
+ _t.handleObserverResource(entry);
1129
+ }
1130
+ });
1131
+ });
1132
+ observer.observe({
1133
+ entryTypes: ['resource']
1134
+ });
1221
1135
  };
1222
1136
  BaseTrace.prototype.saveBreadcrumb = function (data) {
1223
1137
  if (this.breadcrumbEnabled) {
@@ -1230,14 +1144,19 @@
1230
1144
  BaseTrace.prototype.setUserId = function (userId) {
1231
1145
  this.uid = userId;
1232
1146
  };
1147
+ /**
1148
+ * 设置自定义全局字段
1149
+ * @param fields 自定义全局字段
1150
+ */
1151
+ BaseTrace.prototype.setCustomGlobalFields = function (fields) {
1152
+ this.customGlobalFields = __assign(__assign({}, this.customGlobalFields), fields);
1153
+ };
1233
1154
  // 初始化实例
1234
1155
  BaseTrace.init = function (options) {
1235
1156
  var traceSdk = new BaseTrace(options);
1236
1157
  traceSdk.onGlobalError();
1237
- // traceSdk.onObserverResource()
1238
- traceSdk.observer.observe({
1239
- entryTypes: ['resource']
1240
- });
1158
+ // traceSdk.onGlobalClick()
1159
+ traceSdk.onObserverResource();
1241
1160
  window.fetch = interceptFetch({
1242
1161
  pagePath: '',
1243
1162
  onError: function (error) {
@@ -1275,14 +1194,15 @@
1275
1194
  });
1276
1195
  // 监听页面性能
1277
1196
  onVitals(traceSdk.createPerfReport());
1278
- setInterval(function () {
1279
- console.log('[queue] traceSdk.queue: ', traceSdk.queue);
1280
- var data = traceSdk.queue.shift();
1281
- console.log('[queue] data: ', data);
1282
- if (data)
1283
- send(traceSdk.sendMethod, traceSdk.dsn, traceSdk.ltsConfig, data);
1284
- }, traceSdk.sendTimer);
1285
- // @ts-ignore
1197
+ if (traceSdk.sendMethod !== exports.SendMethod.LTS) {
1198
+ setInterval(function () {
1199
+ traceSdk.debug && console.log('[queue] traceSdk.queue: ', traceSdk.queue);
1200
+ var data = traceSdk.queue.shift();
1201
+ traceSdk.debug && console.log('[queue] data: ', data);
1202
+ if (data)
1203
+ send(traceSdk.sendMethod, traceSdk.dsn, traceSdk.ltsConfig, data);
1204
+ }, traceSdk.sendTimer);
1205
+ }
1286
1206
  window.traceSdk = traceSdk;
1287
1207
  return traceSdk;
1288
1208
  };
@@ -1303,13 +1223,12 @@
1303
1223
  return instance;
1304
1224
  }
1305
1225
  instance = TraceSdk.init(options);
1306
- console.log('instance: ', instance);
1226
+ console.log('instances: ', instance);
1307
1227
  return instance;
1308
1228
  };
1309
- // @ts-ignore
1310
- window.traceSdkInit = init;
1311
1229
 
1312
1230
  exports.init = init;
1231
+ exports.BaseTrace = BaseTrace;
1313
1232
 
1314
1233
  Object.defineProperty(exports, '__esModule', { value: true });
1315
1234