This commit is contained in:
CN-JS-HuiBai
2026-04-04 17:35:12 +08:00
parent 2300f7a417
commit 6a5f86b717

View File

@@ -39,13 +39,27 @@ function createClient(baseUrl) {
async function testConnection(url) {
const normalized = normalizeUrl(url);
try {
const res = await axios.get(`${normalized}/api/v1/status/buildinfo`, {
timeout: QUERY_TIMEOUT,
httpAgent,
httpsAgent
// Using native fetch to avoid follow-redirects/axios "protocol mismatch" issues in some Node environments
const controller = new AbortController();
const timer = setTimeout(() => controller.abort(), QUERY_TIMEOUT);
// Node native fetch - handles http/https automatically
const res = await fetch(`${normalized}/api/v1/status/buildinfo`, {
signal: controller.signal
});
return res.data?.data?.version || 'unknown';
clearTimeout(timer);
if (!res.ok) {
throw new Error(`Prometheus returned HTTP ${res.status}`);
}
const data = await res.json();
return data?.data?.version || 'unknown';
} catch (err) {
if (err.name === 'AbortError') {
throw new Error('Connection timed out');
}
console.error(`[Prometheus] Connection test failed for ${normalized}:`, err.message);
throw err;
}