From 2cd6c6ef277802629ebe76621373c3e04fa3dd15 Mon Sep 17 00:00:00 2001 From: CN-JS-HuiBai Date: Fri, 10 Apr 2026 22:25:04 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E8=AF=AD=E6=B3=95=E9=94=99?= =?UTF-8?q?=E8=AF=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- server/prometheus-service.js | 46 +++++++++++++++++++----------------- 1 file changed, 24 insertions(+), 22 deletions(-) diff --git a/server/prometheus-service.js b/server/prometheus-service.js index d0dca3c..9f9d68f 100644 --- a/server/prometheus-service.js +++ b/server/prometheus-service.js @@ -611,32 +611,34 @@ async function getServerDetails(baseUrl, instance, job, settings = {}) { // 2. Fallback to Prometheus Targets API if (!foundIp) { - const targets = await getTargets(baseUrl); - const matchedTarget = targets.find(t => t.labels && t.labels.instance === node && t.labels.job === job); - if (matchedTarget) { + try { + const targets = await getTargets(baseUrl); + const matchedTarget = targets.find(t => t.labels && t.labels.instance === node && t.labels.job === job); + if (matchedTarget) { const scrapeUrl = matchedTarget.scrapeUrl || ''; try { - const urlObj = new URL(scrapeUrl); - const host = urlObj.hostname; - if (host.includes(':')) { - results.ipv6 = [host]; - results.ipv4 = []; - } else { - results.ipv4 = [host]; - results.ipv6 = []; - } + const urlObj = new URL(scrapeUrl); + const host = urlObj.hostname; + if (host.includes(':')) { + results.ipv6 = [host]; + results.ipv4 = []; + } else { + results.ipv4 = [host]; + results.ipv6 = []; + } foundIp = true; } catch (e) { - results.ipv4 = []; - results.ipv6 = []; + // Simple fallback if URL parsing fails + const host = scrapeUrl.split('//').pop().split('/')[0].split(':')[0]; + if (host) { + results.ipv4 = [host]; + results.ipv6 = []; + foundIp = true; + } } - } else { - results.ipv4 = []; - results.ipv6 = []; } - } else { - results.ipv4 = []; - results.ipv6 = []; + } catch (e) { + console.error(`[Prometheus] Error fetching target info for ${node}:`, e.message); } } @@ -646,8 +648,8 @@ async function getServerDetails(baseUrl, instance, job, settings = {}) { } } catch (err) { console.error(`[Prometheus] Critical error resolving IPs for ${node}:`, err.message); - results.ipv4 = []; - results.ipv6 = []; + results.ipv4 = results.ipv4 || []; + results.ipv6 = results.ipv6 || []; } // Group partitions