From 0c1e54aa1b42d1ffe67db928734d761d92c5d182 Mon Sep 17 00:00:00 2001 From: Angelo Capossele <angelocapossele@gmail.com> Date: Thu, 25 Jun 2020 09:42:40 +0100 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B=20Fix=20neighbor=20count=20in=20pr?= =?UTF-8?q?ometheus=20(#546)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- plugins/metrics/autopeering.go | 30 +++++++++++++++++------------- plugins/metrics/plugin.go | 1 + plugins/prometheus/autopeering.go | 2 +- 3 files changed, 19 insertions(+), 14 deletions(-) diff --git a/plugins/metrics/autopeering.go b/plugins/metrics/autopeering.go index de0fa3b2..7a20ac62 100644 --- a/plugins/metrics/autopeering.go +++ b/plugins/metrics/autopeering.go @@ -7,6 +7,7 @@ import ( gossipPkg "github.com/iotaledger/goshimmer/packages/gossip" "github.com/iotaledger/hive.go/autopeering/selection" "github.com/iotaledger/hive.go/events" + "go.uber.org/atomic" ) var ( @@ -14,11 +15,12 @@ var ( neighborConnectionsLifeTime time.Duration neighborMutex sync.RWMutex - connectionsCount uint64 - sumDistance uint64 - minDistance = uint64(^uint32(0)) - maxDistance uint64 - distanceMutex sync.RWMutex + neighborConnectionsCount atomic.Uint64 + autopeeringConnectionsCount uint64 + sumDistance uint64 + minDistance = uint64(^uint32(0)) + maxDistance uint64 + distanceMutex sync.RWMutex ) var ( @@ -29,10 +31,14 @@ var ( neighborConnectionsLifeTime += time.Since(n.ConnectionEstablished()) }) + onNeighborAdded = events.NewClosure(func(_ *gossipPkg.Neighbor) { + neighborConnectionsCount.Inc() + }) + onAutopeeringSelection = events.NewClosure(func(ev *selection.PeeringEvent) { distanceMutex.Lock() defer distanceMutex.Unlock() - connectionsCount++ + autopeeringConnectionsCount++ distance := uint64(ev.Distance) if distance < minDistance { minDistance = distance @@ -61,11 +67,9 @@ func AvgNeighborConnectionLifeTime() float64 { return float64(neighborConnectionsLifeTime.Milliseconds()) / float64(neighborDropCount) } -// ConnectionsCount returns the neighbors connections count. -func ConnectionsCount() uint64 { - distanceMutex.RLock() - defer distanceMutex.RUnlock() - return connectionsCount +// NeighborConnectionsCount returns the neighbors connections count. +func NeighborConnectionsCount() uint64 { + return neighborConnectionsCount.Load() } // AutopeeringDistanceStats returns statistics of the autopeering distance function. @@ -73,10 +77,10 @@ func AutopeeringDistanceStats() (min, max uint64, avg float64) { distanceMutex.RLock() defer distanceMutex.RUnlock() min, max = minDistance, maxDistance - if connectionsCount == 0 { + if autopeeringConnectionsCount == 0 { avg = 0 return } - avg = float64(sumDistance) / float64(connectionsCount) + avg = float64(sumDistance) / float64(autopeeringConnectionsCount) return } diff --git a/plugins/metrics/plugin.go b/plugins/metrics/plugin.go index 0f51f900..c7c295f1 100644 --- a/plugins/metrics/plugin.go +++ b/plugins/metrics/plugin.go @@ -130,6 +130,7 @@ func registerLocalMetrics() { })) gossip.Manager().Events().NeighborRemoved.Attach(onNeighborRemoved) + gossip.Manager().Events().NeighborAdded.Attach(onNeighborAdded) autopeering.Selection().Events().IncomingPeering.Attach(onAutopeeringSelection) autopeering.Selection().Events().OutgoingPeering.Attach(onAutopeeringSelection) diff --git a/plugins/prometheus/autopeering.go b/plugins/prometheus/autopeering.go index bb8c9971..bd4b7435 100644 --- a/plugins/prometheus/autopeering.go +++ b/plugins/prometheus/autopeering.go @@ -58,7 +58,7 @@ func registerAutopeeringMetrics() { func collectAutopeeringMetrics() { neighborDropCount.Set(float64(metrics.NeighborDropCount())) avgNeighborConnectionLifeTime.Set(metrics.AvgNeighborConnectionLifeTime()) - connectionsCount.Set(float64(metrics.ConnectionsCount())) + connectionsCount.Set(float64(metrics.NeighborConnectionsCount())) min, max, avg := metrics.AutopeeringDistanceStats() minDistance.Set(float64(min)) maxDistance.Set(float64(max)) -- GitLab