From 7f5e1a141fdbd370a61a55273a165a5255d2b295 Mon Sep 17 00:00:00 2001 From: Abhishek Kondur Date: Thu, 8 Dec 2022 14:20:50 +0530 Subject: [PATCH] store proxy metrics in the server --- logic/metrics/metrics.go | 17 ++++++----------- models/metrics.go | 19 ++++++++++++------- mq/handlers.go | 7 +++++++ 3 files changed, 25 insertions(+), 18 deletions(-) diff --git a/logic/metrics/metrics.go b/logic/metrics/metrics.go index 6a3372eb..a76573c9 100644 --- a/logic/metrics/metrics.go +++ b/logic/metrics/metrics.go @@ -1,19 +1,18 @@ package metrics import ( - "runtime" "time" "github.com/go-ping/ping" + proxy_metrics "github.com/gravitl/netclient/nm-proxy/metrics" "github.com/gravitl/netmaker/logger" "github.com/gravitl/netmaker/logic" "github.com/gravitl/netmaker/models" - "github.com/gravitl/netmaker/netclient/wireguard" "golang.zx2c4.com/wireguard/wgctrl" ) // Collect - collects metrics -func Collect(iface string, peerMap models.PeerMap) (*models.Metrics, error) { +func Collect(iface, network string, proxy bool, peerMap models.PeerMap) (*models.Metrics, error) { var metrics models.Metrics metrics.Connectivity = make(map[string]models.Metric) var wgclient, err = wgctrl.New() @@ -22,19 +21,13 @@ func Collect(iface string, peerMap models.PeerMap) (*models.Metrics, error) { return &metrics, err } defer wgclient.Close() - - if runtime.GOOS == "darwin" { - iface, err = wireguard.GetRealIface(iface) - if err != nil { - fillUnconnectedData(&metrics, peerMap) - return &metrics, err - } - } device, err := wgclient.Device(iface) if err != nil { fillUnconnectedData(&metrics, peerMap) return &metrics, err } + metrics.ProxyMetrics = make(map[string]proxy_metrics.Metric) + // TODO handle freebsd?? for i := range device.Peers { currPeer := device.Peers[i] @@ -88,6 +81,8 @@ func Collect(iface string, peerMap models.PeerMap) (*models.Metrics, error) { newMetric.TotalTime = 1 metrics.Connectivity[id] = newMetric + metrics.ProxyMetrics[id] = proxy_metrics.GetMetric(network, currPeer.PublicKey.String()) + proxy_metrics.ResetMetricsForPeer(network, currPeer.PublicKey.String()) } fillUnconnectedData(&metrics, peerMap) diff --git a/models/metrics.go b/models/metrics.go index ab641377..7432fdb0 100644 --- a/models/metrics.go +++ b/models/metrics.go @@ -1,15 +1,20 @@ package models -import "time" +import ( + "time" + + proxy "github.com/gravitl/netclient/nm-proxy/metrics" +) // Metrics - metrics struct type Metrics struct { - Network string `json:"network" bson:"network" yaml:"network"` - NodeID string `json:"node_id" bson:"node_id" yaml:"node_id"` - NodeName string `json:"node_name" bson:"node_name" yaml:"node_name"` - IsServer string `json:"isserver" bson:"isserver" yaml:"isserver" validate:"checkyesorno"` - Connectivity map[string]Metric `json:"connectivity" bson:"connectivity" yaml:"connectivity"` - FailoverPeers map[string]string `json:"needsfailover" bson:"needsfailover" yaml:"needsfailover"` + Network string `json:"network" bson:"network" yaml:"network"` + NodeID string `json:"node_id" bson:"node_id" yaml:"node_id"` + NodeName string `json:"node_name" bson:"node_name" yaml:"node_name"` + IsServer string `json:"isserver" bson:"isserver" yaml:"isserver" validate:"checkyesorno"` + Connectivity map[string]Metric `json:"connectivity" bson:"connectivity" yaml:"connectivity"` + FailoverPeers map[string]string `json:"needsfailover" bson:"needsfailover" yaml:"needsfailover"` + ProxyMetrics map[string]proxy.Metric `json:"proxy_metrics" bson:"proxy_metrics" yaml:"proxy_metrics"` } // Metric - holds a metric for data between nodes diff --git a/mq/handlers.go b/mq/handlers.go index a27f1806..7218276b 100644 --- a/mq/handlers.go +++ b/mq/handlers.go @@ -273,6 +273,13 @@ func updateNodeMetrics(currentNode *models.Node, newMetrics *models.Metrics) boo currMetric.ActualUptime = time.Duration(totalUpMinutes) * time.Minute delete(oldMetrics.Connectivity, k) // remove from old data newMetrics.Connectivity[k] = currMetric + if oldProxyMetric, ok := oldMetrics.ProxyMetrics[k]; ok { + newProxyMetric := newMetrics.ProxyMetrics[k] + newProxyMetric.TrafficSent += oldProxyMetric.TrafficSent + newProxyMetric.TrafficRecieved += oldProxyMetric.TrafficRecieved + newMetrics.ProxyMetrics[k] = newProxyMetric + } + } // add nodes that need failover