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) { async function testConnection(url) {
const normalized = normalizeUrl(url); const normalized = normalizeUrl(url);
try { try {
const res = await axios.get(`${normalized}/api/v1/status/buildinfo`, { // Using native fetch to avoid follow-redirects/axios "protocol mismatch" issues in some Node environments
timeout: QUERY_TIMEOUT, const controller = new AbortController();
httpAgent, const timer = setTimeout(() => controller.abort(), QUERY_TIMEOUT);
httpsAgent
// 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) { } catch (err) {
if (err.name === 'AbortError') {
throw new Error('Connection timed out');
}
console.error(`[Prometheus] Connection test failed for ${normalized}:`, err.message); console.error(`[Prometheus] Connection test failed for ${normalized}:`, err.message);
throw err; throw err;
} }