mirror of
https://github.com/netbirdio/netbird.git
synced 2026-05-03 15:46:38 +00:00
Implements a comprehensive client metrics system to track peer connection stages and performance. The system supports multiple backend implementations (OpenTelemetry, VictoriaMetrics, and no-op) and tracks detailed connection stage durations from creation through WireGuard handshake. Key changes: - Add metrics package with pluggable backend implementations - Implement OpenTelemetry metrics backend - Implement VictoriaMetrics metrics backend - Add no-op metrics implementation for disabled state - Track connection stages: creation, semaphore, signaling, connection ready, and WireGuard handshake - Move WireGuard watcher functionality to conn.go - Refactor engine to integrate metrics tracking - Add metrics export endpoint in debug server
85 lines
1.8 KiB
Go
85 lines
1.8 KiB
Go
package peer
|
|
|
|
import (
|
|
"context"
|
|
"testing"
|
|
"time"
|
|
|
|
log "github.com/sirupsen/logrus"
|
|
|
|
"github.com/netbirdio/netbird/client/iface/configurer"
|
|
)
|
|
|
|
type MocWgIface struct {
|
|
stop bool
|
|
}
|
|
|
|
func (m *MocWgIface) GetStats() (map[string]configurer.WGStats, error) {
|
|
return map[string]configurer.WGStats{}, nil
|
|
}
|
|
|
|
func (m *MocWgIface) disconnect() {
|
|
m.stop = true
|
|
}
|
|
|
|
func TestWGWatcher_EnableWgWatcher(t *testing.T) {
|
|
checkPeriod = 5 * time.Second
|
|
wgHandshakeOvertime = 1 * time.Second
|
|
|
|
mlog := log.WithField("peer", "tet")
|
|
mocWgIface := &MocWgIface{}
|
|
watcher := NewWGWatcher(mlog, mocWgIface, "", newStateDump("peer", mlog, &Status{}), nil)
|
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
defer cancel()
|
|
|
|
onDisconnected := make(chan struct{}, 1)
|
|
go watcher.EnableWgWatcher(ctx, func() {
|
|
mlog.Infof("onDisconnectedFn")
|
|
onDisconnected <- struct{}{}
|
|
})
|
|
|
|
// wait for initial reading
|
|
time.Sleep(2 * time.Second)
|
|
mocWgIface.disconnect()
|
|
|
|
select {
|
|
case <-onDisconnected:
|
|
case <-time.After(10 * time.Second):
|
|
t.Errorf("timeout")
|
|
}
|
|
watcher.DisableWgWatcher()
|
|
}
|
|
|
|
func TestWGWatcher_ReEnable(t *testing.T) {
|
|
checkPeriod = 5 * time.Second
|
|
wgHandshakeOvertime = 1 * time.Second
|
|
|
|
mlog := log.WithField("peer", "tet")
|
|
mocWgIface := &MocWgIface{}
|
|
watcher := NewWGWatcher(mlog, mocWgIface, "", newStateDump("peer", mlog, &Status{}), nil)
|
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
defer cancel()
|
|
|
|
onDisconnected := make(chan struct{}, 1)
|
|
|
|
go watcher.EnableWgWatcher(ctx, func() {})
|
|
time.Sleep(1 * time.Second)
|
|
watcher.DisableWgWatcher()
|
|
|
|
go watcher.EnableWgWatcher(ctx, func() {
|
|
onDisconnected <- struct{}{}
|
|
})
|
|
|
|
time.Sleep(2 * time.Second)
|
|
mocWgIface.disconnect()
|
|
|
|
select {
|
|
case <-onDisconnected:
|
|
case <-time.After(10 * time.Second):
|
|
t.Errorf("timeout")
|
|
}
|
|
watcher.DisableWgWatcher()
|
|
}
|