diff --git a/server/latency-service.js b/server/latency-service.js index f918363..9683c63 100644 --- a/server/latency-service.js +++ b/server/latency-service.js @@ -34,33 +34,46 @@ async function pollLatency() { // 1. Parse prometheus text format for success and specific metrics let latency = null; - let success = false; - const lines = response.data.split('\n'); + const lines = response.data.split('\n').map(l => l.trim()).filter(l => l && !l.startsWith('#')); + // Track success but also try to parse latency anyway as a backup + let isProbeSuccess = false; for (const line of lines) { - // Match probe_success with optional labels: probe_success{...} 1 - if (line.match(/^probe_success({.*})?\s+1/)) { - success = true; + if (line.match(/^probe_success(\{.*\})?\s+1/)) { + isProbeSuccess = true; break; } } - if (success) { - // 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) { - // More robust regex: handle optional labels and scientific notation - // Supports: metric_name{...} 1.23e-4 or metric_name 0.05 - const regex = new RegExp(`^${metricName}\\s*(?:\\{[^}]*\\})?\\s+([\\d.eE+-]+)`); - for (const line of lines) { - const match = line.match(regex); - if (match) { - latency = parseFloat(match[1]) * 1000; // to ms + // Try a wider set of potential latency metrics + const targetMetrics = [ + 'probe_icmp_duration_seconds', + 'probe_http_duration_seconds', + 'probe_dns_lookup_time_seconds', + 'probe_duration_seconds' + ]; + + for (const metricName of targetMetrics) { + // Match: metric_name{labels} value + // Regex handles optional labels and scientific notation + const regex = new RegExp(`^${metricName}(?:\\{[^}]*\\})?\\s+([\\d.eE+-]+)`); + for (const line of lines) { + const match = line.match(regex); + if (match) { + const val = parseFloat(match[1]); + if (!isNaN(val)) { + latency = val * 1000; // to ms break; } } - if (latency !== null) break; } + if (latency !== null) break; + } + + // If probe reported failure but we found a duration, we might still want to show null/error + if (!isProbeSuccess && latency !== null) { + // If probe failed, force null to indicate error/offline on UI + latency = null; } // Save to Valkey