cwj_monitoring 0.0.26 → 0.0.27

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/README.md CHANGED
@@ -96,7 +96,7 @@ interface TransportConfig {
96
96
 
97
97
  **配置项:**
98
98
 
99
- - `loafThreshold`: LoAF 阈值 (ms),默认 `50`
99
+ - `loafThreshold`: LoAF 阈值 (ms),默认 `100`
100
100
  - `resourceThreshold`: 资源加载阈值 (ms),默认 `1000`
101
101
  - `filter`: 过滤函数,支持按类型过滤指标
102
102
 
package/dist/index.cjs CHANGED
@@ -117,7 +117,8 @@ const MAX_CACHE_LEN = 5;
117
117
  const MAX_WAITING_TIME = 3e4;
118
118
  const UUID = "track_uuid";
119
119
  const DEFAULT_RESOURCE_THRESHOLD = 1e3;
120
- const DEFAULT_LOAF_THRESHOLD = 50;
120
+ const DEFAULT_LOAF_THRESHOLD = 100;
121
+ const DEFAULT_LOAF_SINGLETIME = 50;
121
122
 
122
123
  var __defProp$3 = Object.defineProperty;
123
124
  var __defNormalProp$3 = (obj, key, value) => key in obj ? __defProp$3(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
@@ -669,41 +670,74 @@ const PerformancePlugin = (options = {}) => {
669
670
  continue;
670
671
  }
671
672
  const threshold = options.loafThreshold ?? DEFAULT_LOAF_THRESHOLD;
672
- if (entry.duration > threshold) {
673
- const scripts = Array.isArray(entry.scripts) ? entry.scripts : [];
674
- let isUserTriggered = false;
675
- let allIgnored = true;
676
- for (let i = 0; i < scripts.length; i++) {
677
- const s = scripts[i];
678
- if (!s)
679
- continue;
680
- if (!isUserTriggered && s.invokerType === "user-callback")
681
- isUserTriggered = true;
682
- if (allIgnored && !isIgnoredScriptSource(s.sourceURL)) {
683
- allIgnored = false;
684
- }
685
- if (isUserTriggered && !allIgnored)
686
- break;
687
- }
688
- if (isUserTriggered && !allIgnored) {
689
- context?.emit(EMIT_TYPE.PERFORMANCE_LOAF, {
690
- duration: entry.duration,
691
- startTime: entry.startTime,
692
- renderStart: entry.renderStart,
693
- styleAndLayoutStart: entry.styleAndLayoutStart,
694
- hadRecentInput: entry.hadRecentInput,
695
- scripts: entry.scripts.map((s) => ({
696
- duration: s.duration,
697
- invoker: s.invoker,
698
- invokerType: s.invokerType,
699
- sourceURL: s.sourceURL,
700
- functionName: s.functionName,
701
- sourceFunctionName: s.sourceFunctionName,
702
- sourceCharPosition: s.sourceCharPosition,
703
- startTime: s.startTime
704
- }))
705
- });
673
+ if (typeof entry.duration !== "number" || entry.duration <= threshold)
674
+ continue;
675
+ const scripts = Array.isArray(entry.scripts) ? entry.scripts : [];
676
+ let isUserTriggered = false;
677
+ let allIgnored = true;
678
+ for (let i = 0; i < scripts.length; i++) {
679
+ const s = scripts[i];
680
+ if (!s)
681
+ continue;
682
+ if (!isUserTriggered && s.invokerType === "user-callback")
683
+ isUserTriggered = true;
684
+ if (allIgnored && !isIgnoredScriptSource(s.sourceURL)) {
685
+ allIgnored = false;
706
686
  }
687
+ if (isUserTriggered && !allIgnored)
688
+ break;
689
+ }
690
+ if (isUserTriggered && !allIgnored) {
691
+ const baseInfo = {
692
+ duration: entry.duration,
693
+ startTime: entry.startTime,
694
+ renderStart: entry.renderStart,
695
+ styleAndLayoutStart: entry.styleAndLayoutStart,
696
+ hadRecentInput: entry.hadRecentInput,
697
+ scripts: entry.scripts.map((s) => ({
698
+ duration: s.duration,
699
+ invoker: s.invoker,
700
+ invokerType: s.invokerType,
701
+ sourceURL: s.sourceURL,
702
+ functionName: s.functionName,
703
+ sourceFunctionName: s.sourceFunctionName,
704
+ sourceCharPosition: s.sourceCharPosition,
705
+ startTime: s.startTime
706
+ }))
707
+ };
708
+ (async () => {
709
+ const longScripts = scripts.filter(
710
+ (s) => s && typeof s.duration === "number" && s.duration > DEFAULT_LOAF_SINGLETIME && s.sourceURL
711
+ );
712
+ const contexts = [];
713
+ for (const s of longScripts) {
714
+ try {
715
+ const url = String(s.sourceURL);
716
+ const resp = await fetch(url, { cache: "no-store" });
717
+ if (!resp.ok) {
718
+ contexts.push({ sourceURL: url, error: `http ${resp.status}` });
719
+ continue;
720
+ }
721
+ const text = await resp.text();
722
+ const pos = typeof s.sourceCharPosition === "number" ? s.sourceCharPosition : void 0;
723
+ if (pos !== void 0 && Number.isFinite(pos)) {
724
+ const start = Math.max(0, pos - 50);
725
+ const end = Math.min(text.length, pos + 50);
726
+ const snippet = text.substring(start, end);
727
+ contexts.push({ sourceURL: url, position: pos, snippet });
728
+ } else {
729
+ contexts.push({ sourceURL: url, error: "no-position" });
730
+ }
731
+ } catch (e) {
732
+ contexts.push({
733
+ sourceURL: s && s.sourceURL ? String(s.sourceURL) : "",
734
+ error: e?.message || String(e)
735
+ });
736
+ }
737
+ }
738
+ const info = { ...baseInfo, contexts };
739
+ context?.emit(EMIT_TYPE.PERFORMANCE_LOAF, info);
740
+ })();
707
741
  }
708
742
  }
709
743
  };