Add tunnel latency and reconnect telemetry to ping logic

This commit is contained in:
Marc Schäfer
2025-10-07 09:13:04 +02:00
parent ceef228665
commit 0d55e35784

11
util.go
View File

@@ -2,6 +2,7 @@ package main
import ( import (
"bytes" "bytes"
"context"
"encoding/base64" "encoding/base64"
"encoding/hex" "encoding/hex"
"encoding/json" "encoding/json"
@@ -14,6 +15,7 @@ import (
"math/rand" "math/rand"
"github.com/fosrl/newt/internal/telemetry"
"github.com/fosrl/newt/logger" "github.com/fosrl/newt/logger"
"github.com/fosrl/newt/proxy" "github.com/fosrl/newt/proxy"
"github.com/fosrl/newt/websocket" "github.com/fosrl/newt/websocket"
@@ -229,7 +231,7 @@ func pingWithRetry(tnet *netstack.Net, dst string, timeout time.Duration) (stopC
return stopChan, fmt.Errorf("initial ping attempts failed, continuing in background") return stopChan, fmt.Errorf("initial ping attempts failed, continuing in background")
} }
func startPingCheck(tnet *netstack.Net, serverIP string, client *websocket.Client) chan struct{} { func startPingCheck(tnet *netstack.Net, serverIP string, client *websocket.Client, tunnelID string) chan struct{} {
maxInterval := 6 * time.Second maxInterval := 6 * time.Second
currentInterval := pingInterval currentInterval := pingInterval
consecutiveFailures := 0 consecutiveFailures := 0
@@ -292,6 +294,9 @@ func startPingCheck(tnet *netstack.Net, serverIP string, client *websocket.Clien
if !connectionLost { if !connectionLost {
connectionLost = true connectionLost = true
logger.Warn("Connection to server lost after %d failures. Continuous reconnection attempts will be made.", consecutiveFailures) logger.Warn("Connection to server lost after %d failures. Continuous reconnection attempts will be made.", consecutiveFailures)
if tunnelID != "" {
telemetry.IncReconnect(context.Background(), "", tunnelID, telemetry.ReasonTimeout)
}
stopFunc = client.SendMessageInterval("newt/ping/request", map[string]interface{}{}, 3*time.Second) stopFunc = client.SendMessageInterval("newt/ping/request", map[string]interface{}{}, 3*time.Second)
// Send registration message to the server for backward compatibility // Send registration message to the server for backward compatibility
err := client.SendMessage("newt/wg/register", map[string]interface{}{ err := client.SendMessage("newt/wg/register", map[string]interface{}{
@@ -318,6 +323,10 @@ func startPingCheck(tnet *netstack.Net, serverIP string, client *websocket.Clien
} else { } else {
// Track recent latencies // Track recent latencies
recentLatencies = append(recentLatencies, latency) recentLatencies = append(recentLatencies, latency)
// Record tunnel latency (limit sampling to this periodic check)
if tunnelID != "" {
telemetry.ObserveTunnelLatency(context.Background(), "", tunnelID, "wireguard", latency.Seconds())
}
if len(recentLatencies) > 10 { if len(recentLatencies) > 10 {
recentLatencies = recentLatencies[1:] recentLatencies = recentLatencies[1:]
} }