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