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