Browse Source

feat: 调整异常资源采样

zhangyuhan 1 year ago
parent
commit
e5dc941697

File diff suppressed because it is too large
+ 0 - 0
src/web/staticres/public-pc/js/detection-min.js


+ 51 - 11
src/web/staticres/public-pc/js/detection.js

@@ -3,9 +3,9 @@
     try {
     try {
       Sentry.init({
       Sentry.init({
         dsn: "https://ea0521e5b3584ca1879b940c165916e4@jysentry.jydev.jianyu360.cn/3",
         dsn: "https://ea0521e5b3584ca1879b940c165916e4@jysentry.jydev.jianyu360.cn/3",
-        release: "v4.8.7910",
+        release: "v4.8.7930",
         environment: "produce",
         environment: "produce",
-        sampleRate: 0.1
+        sampleRate: 0.08
       });
       });
       Sentry.setTag("url", location.href);
       Sentry.setTag("url", location.href);
       var id = document.cookie.match(/(^|;)\s*ud_safe\s*=\s*([^;]+)/);
       var id = document.cookie.match(/(^|;)\s*ud_safe\s*=\s*([^;]+)/);
@@ -217,11 +217,11 @@
 
 
   // 异常资源数组
   // 异常资源数组
   const failedResources = []
   const failedResources = []
+  const failedResourcesMap = {}
   // 异常域名数组
   // 异常域名数组
   const domains = []
   const domains = []
   // 备用域名映射
   // 备用域名映射
   const BackCDNs = {
   const BackCDNs = {
-    'cdn-jybx-webtest.jydev.jianyu360.com': 'jybx-webtest.jydev.jianyu360.com',
     'cdn-ali.jianyu360.cn': 'cdn-ali.jianyu360.com',
     'cdn-ali.jianyu360.cn': 'cdn-ali.jianyu360.com',
     'cdn-ali2.jianyu360.cn': 'cdn-ali2.jianyu360.com',
     'cdn-ali2.jianyu360.cn': 'cdn-ali2.jianyu360.com',
     'cdn-ali3.jianyu360.cn': 'cdn-ali3.jianyu360.com',
     'cdn-ali3.jianyu360.cn': 'cdn-ali3.jianyu360.com',
@@ -232,8 +232,13 @@
   const BackDomains = Object.keys(BackCDNs)
   const BackDomains = Object.keys(BackCDNs)
 
 
   // 处理异常资源
   // 处理异常资源
-  const handleResourceError = (url) => {
+  const handleResourceError = (url, type) => {
     failedResources.push(url);
     failedResources.push(url);
+    if (!failedResourcesMap[type]) {
+      failedResourcesMap[type] = []
+    }
+    failedResourcesMap[type].push(url)
+
     // 提取域名
     // 提取域名
     const domain = new URL(url).hostname
     const domain = new URL(url).hostname
     if(!domains.includes(domain)) {
     if(!domains.includes(domain)) {
@@ -249,21 +254,42 @@
   window.addEventListener('error', (e) => {
   window.addEventListener('error', (e) => {
     // 脚本错误、图片错误
     // 脚本错误、图片错误
     if(e.target.src && (e.target.tagName === 'SCRIPT' || e.target.tagName === 'IMG')) {
     if(e.target.src && (e.target.tagName === 'SCRIPT' || e.target.tagName === 'IMG')) {
-      handleResourceError(e.target.src)
+      handleResourceError(e.target.src, e.target.tagName)
     }
     }
 
 
     // CSS错误
     // CSS错误
     if(e.target.href && e.target.tagName === 'LINK') {
     if(e.target.href && e.target.tagName === 'LINK') {
-      handleResourceError(e.target.href)
+      handleResourceError(e.target.href, e.target.tagName)
     }
     }
   }, true)
   }, true)
 
 
   const currentTime = Date.now()
   const currentTime = Date.now()
 
 
-  function sendError (content) {
+  function sendError (content, options) {
     if (window.Sentry && typeof window.Sentry.captureMessage === 'function') {
     if (window.Sentry && typeof window.Sentry.captureMessage === 'function') {
-      Sentry.captureMessage(content)
+      Sentry.captureMessage(content, options)
+    }
+  }
+
+  // 异常资源随机取样
+  function getRandomResourceByType () {
+    let randomData = {}
+
+    for (let type in failedResourcesMap) {
+      if (failedResourcesMap.hasOwnProperty(type)) {
+        const group = failedResourcesMap[type]
+
+        // 检查 group 是否为数组
+        if (Object.prototype.toString.call(group) === '[object Array]' && group.length > 0) {
+          const realGroup = filterErrorURL(group)
+          if (realGroup.length > 0) {
+            randomData[type] = realGroup[Math.floor(Math.random() * realGroup.length)]
+          }
+        }
+      }
     }
     }
+
+    return randomData
   }
   }
 
 
   function filterErrorURL (urls) {
   function filterErrorURL (urls) {
@@ -284,8 +310,14 @@
   window.addEventListener('load', () => {
   window.addEventListener('load', () => {
     // 页面完全加载完毕
     // 页面完全加载完毕
     // console.log('load Failed resources:', failedResources)
     // console.log('load Failed resources:', failedResources)
-    if (failedResources.length > 0) {
-      sendError("[CDN] Has failedResources: " + filterErrorURL(failedResources).join('、'))
+    if (filterErrorURL(failedResources).length > 0) {
+      sendError(new Error('Error Resource'), {
+        level: 'info',
+        tags: getRandomResourceByType(),
+        extra: {
+          failedResources: failedResources
+        }
+      })
     }
     }
     // console.log('load Extracted domains:', domains)
     // console.log('load Extracted domains:', domains)
 
 
@@ -304,7 +336,15 @@
         })))
         })))
       })
       })
 
 
-      sendError("[CDN] Has wrong cdn: " + domains.join('、'))
+      sendError(new Error('Error CDN'), {
+        level: 'info',
+        tags: filterErrorURL(domains).join('、'),
+        extra: {
+          domains: domains,
+          failedResources: failedResources
+        }
+      })
+
       addCDNDialogNode('加载中……')
       addCDNDialogNode('加载中……')
 
 
       function stopUnlimitedReload (callback) {
       function stopUnlimitedReload (callback) {

Some files were not shown because too many files changed in this diff