修复延迟问题

This commit is contained in:
CN-JS-HuiBai
2026-04-06 01:03:40 +08:00
parent 6b61104641
commit 7fdac71062
2 changed files with 8 additions and 7 deletions

View File

@@ -910,13 +910,12 @@ app.put('/api/latency-routes/:id', requireAuth, async (req, res) => {
app.get('/api/metrics/latency', async (req, res) => { app.get('/api/metrics/latency', async (req, res) => {
try { try {
const [routes] = await db.query(` const [routes] = await db.query(`
SELECT r.*, s.url SELECT r.*, s.url, s.type as source_type
FROM latency_routes r FROM latency_routes r
JOIN prometheus_sources s ON r.source_id = s.id JOIN prometheus_sources s ON r.source_id = s.id
`); `);
if (routes.length === 0) { if (routes.length === 0) {
// Return empty routes array instead of null for consistency
return res.json({ routes: [] }); return res.json({ routes: [] });
} }
@@ -924,8 +923,8 @@ app.get('/api/metrics/latency', async (req, res) => {
// Try to get from Valkey first (filled by background latencyService) // Try to get from Valkey first (filled by background latencyService)
let latency = await cache.get(`latency:route:${route.id}`); let latency = await cache.get(`latency:route:${route.id}`);
// Fallback if not in cache (maybe service just started or failed) // Fallback if not in cache (only for prometheus sources, blackbox sources rely on the background service)
if (latency === null) { if (latency === null && route.source_type === 'prometheus') {
latency = await prometheusService.getLatency(route.url, route.latency_target); latency = await prometheusService.getLatency(route.url, route.latency_target);
} }

View File

@@ -38,7 +38,8 @@ async function pollLatency() {
const lines = response.data.split('\n'); const lines = response.data.split('\n');
for (const line of lines) { for (const line of lines) {
if (line.match(/^probe_success\s+1/)) { // Match probe_success with optional labels: probe_success{...} 1
if (line.match(/^probe_success({.*})?\s+1/)) {
success = true; success = true;
break; break;
} }
@@ -48,8 +49,9 @@ async function pollLatency() {
// Try specialized metrics first for better accuracy // Try specialized metrics first for better accuracy
const priorityMetrics = ['probe_icmp_duration_seconds', 'probe_http_duration_seconds', 'probe_duration_seconds']; const priorityMetrics = ['probe_icmp_duration_seconds', 'probe_http_duration_seconds', 'probe_duration_seconds'];
for (const metricName of priorityMetrics) { for (const metricName of priorityMetrics) {
// Support scientific notation (e.g. 1.23e-04) // More robust regex: handle optional labels and scientific notation
const regex = new RegExp(`^${metricName}(?:\\{.*\\})?\\s+([\\d.eE+-]+)`); // 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) { for (const line of lines) {
const match = line.match(regex); const match = line.match(regex);
if (match) { if (match) {