From 6b6110464199c20296afbf1db9eafa3a56541cd1 Mon Sep 17 00:00:00 2001 From: CN-JS-HuiBai Date: Mon, 6 Apr 2026 01:00:35 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E5=BB=B6=E8=BF=9F=E6=A3=80?= =?UTF-8?q?=E6=B5=8B=E9=94=99=E8=AF=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- server/latency-service.js | 3 ++- server/prometheus-service.js | 24 +++++++++++++++++++----- 2 files changed, 21 insertions(+), 6 deletions(-) diff --git a/server/latency-service.js b/server/latency-service.js index 7a94d11..af677d0 100644 --- a/server/latency-service.js +++ b/server/latency-service.js @@ -48,7 +48,8 @@ async function pollLatency() { // Try specialized metrics first for better accuracy const priorityMetrics = ['probe_icmp_duration_seconds', 'probe_http_duration_seconds', 'probe_duration_seconds']; for (const metricName of priorityMetrics) { - const regex = new RegExp(`^${metricName}(?:\\{.*\\})?\\s+([\\d.]+)`); + // Support scientific notation (e.g. 1.23e-04) + const regex = new RegExp(`^${metricName}(?:\\{.*\\})?\\s+([\\d.eE+-]+)`); for (const line of lines) { const match = line.match(regex); if (match) { diff --git a/server/prometheus-service.js b/server/prometheus-service.js index a4132a6..3fcb884 100644 --- a/server/prometheus-service.js +++ b/server/prometheus-service.js @@ -805,12 +805,26 @@ module.exports = { if (!blackboxUrl || !target) return null; try { const normalized = blackboxUrl.trim().replace(/\/+$/, ''); - const params = new URLSearchParams({ query: `probe_duration_seconds{instance_name="${target}"}` }); + + // Construct a single optimized query searching for priority metrics and common labels + // Prioritize probe_icmp_duration_seconds OVER probe_duration_seconds + const queryExpr = `( + probe_icmp_duration_seconds{instance="${target}"} or + probe_icmp_duration_seconds{target="${target}"} or + probe_icmp_duration_seconds{instance_name="${target}"} or + probe_duration_seconds{instance="${target}"} or + probe_duration_seconds{target="${target}"} or + probe_duration_seconds{instance_name="${target}"} + )`; + + const params = new URLSearchParams({ query: queryExpr }); const res = await fetch(`${normalized}/api/v1/query?${params.toString()}`); - if (!res.ok) return null; - const data = await res.json(); - if (data.status === 'success' && data.data.result.length > 0) { - return parseFloat(data.data.result[0].value[1]) * 1000; + + if (res.ok) { + const data = await res.json(); + if (data.status === 'success' && data.data.result.length > 0) { + return parseFloat(data.data.result[0].value[1]) * 1000; + } } return null; } catch (err) {