Skip to content
Snippets Groups Projects
Unverified Commit 0c1e54aa authored by Angelo Capossele's avatar Angelo Capossele Committed by GitHub
Browse files

:bug: Fix neighbor count in prometheus (#546)

parent 865ffd74
No related branches found
No related tags found
No related merge requests found
...@@ -7,6 +7,7 @@ import ( ...@@ -7,6 +7,7 @@ import (
gossipPkg "github.com/iotaledger/goshimmer/packages/gossip" gossipPkg "github.com/iotaledger/goshimmer/packages/gossip"
"github.com/iotaledger/hive.go/autopeering/selection" "github.com/iotaledger/hive.go/autopeering/selection"
"github.com/iotaledger/hive.go/events" "github.com/iotaledger/hive.go/events"
"go.uber.org/atomic"
) )
var ( var (
...@@ -14,11 +15,12 @@ var ( ...@@ -14,11 +15,12 @@ var (
neighborConnectionsLifeTime time.Duration neighborConnectionsLifeTime time.Duration
neighborMutex sync.RWMutex neighborMutex sync.RWMutex
connectionsCount uint64 neighborConnectionsCount atomic.Uint64
sumDistance uint64 autopeeringConnectionsCount uint64
minDistance = uint64(^uint32(0)) sumDistance uint64
maxDistance uint64 minDistance = uint64(^uint32(0))
distanceMutex sync.RWMutex maxDistance uint64
distanceMutex sync.RWMutex
) )
var ( var (
...@@ -29,10 +31,14 @@ var ( ...@@ -29,10 +31,14 @@ var (
neighborConnectionsLifeTime += time.Since(n.ConnectionEstablished()) neighborConnectionsLifeTime += time.Since(n.ConnectionEstablished())
}) })
onNeighborAdded = events.NewClosure(func(_ *gossipPkg.Neighbor) {
neighborConnectionsCount.Inc()
})
onAutopeeringSelection = events.NewClosure(func(ev *selection.PeeringEvent) { onAutopeeringSelection = events.NewClosure(func(ev *selection.PeeringEvent) {
distanceMutex.Lock() distanceMutex.Lock()
defer distanceMutex.Unlock() defer distanceMutex.Unlock()
connectionsCount++ autopeeringConnectionsCount++
distance := uint64(ev.Distance) distance := uint64(ev.Distance)
if distance < minDistance { if distance < minDistance {
minDistance = distance minDistance = distance
...@@ -61,11 +67,9 @@ func AvgNeighborConnectionLifeTime() float64 { ...@@ -61,11 +67,9 @@ func AvgNeighborConnectionLifeTime() float64 {
return float64(neighborConnectionsLifeTime.Milliseconds()) / float64(neighborDropCount) return float64(neighborConnectionsLifeTime.Milliseconds()) / float64(neighborDropCount)
} }
// ConnectionsCount returns the neighbors connections count. // NeighborConnectionsCount returns the neighbors connections count.
func ConnectionsCount() uint64 { func NeighborConnectionsCount() uint64 {
distanceMutex.RLock() return neighborConnectionsCount.Load()
defer distanceMutex.RUnlock()
return connectionsCount
} }
// AutopeeringDistanceStats returns statistics of the autopeering distance function. // AutopeeringDistanceStats returns statistics of the autopeering distance function.
...@@ -73,10 +77,10 @@ func AutopeeringDistanceStats() (min, max uint64, avg float64) { ...@@ -73,10 +77,10 @@ func AutopeeringDistanceStats() (min, max uint64, avg float64) {
distanceMutex.RLock() distanceMutex.RLock()
defer distanceMutex.RUnlock() defer distanceMutex.RUnlock()
min, max = minDistance, maxDistance min, max = minDistance, maxDistance
if connectionsCount == 0 { if autopeeringConnectionsCount == 0 {
avg = 0 avg = 0
return return
} }
avg = float64(sumDistance) / float64(connectionsCount) avg = float64(sumDistance) / float64(autopeeringConnectionsCount)
return return
} }
...@@ -130,6 +130,7 @@ func registerLocalMetrics() { ...@@ -130,6 +130,7 @@ func registerLocalMetrics() {
})) }))
gossip.Manager().Events().NeighborRemoved.Attach(onNeighborRemoved) gossip.Manager().Events().NeighborRemoved.Attach(onNeighborRemoved)
gossip.Manager().Events().NeighborAdded.Attach(onNeighborAdded)
autopeering.Selection().Events().IncomingPeering.Attach(onAutopeeringSelection) autopeering.Selection().Events().IncomingPeering.Attach(onAutopeeringSelection)
autopeering.Selection().Events().OutgoingPeering.Attach(onAutopeeringSelection) autopeering.Selection().Events().OutgoingPeering.Attach(onAutopeeringSelection)
......
...@@ -58,7 +58,7 @@ func registerAutopeeringMetrics() { ...@@ -58,7 +58,7 @@ func registerAutopeeringMetrics() {
func collectAutopeeringMetrics() { func collectAutopeeringMetrics() {
neighborDropCount.Set(float64(metrics.NeighborDropCount())) neighborDropCount.Set(float64(metrics.NeighborDropCount()))
avgNeighborConnectionLifeTime.Set(metrics.AvgNeighborConnectionLifeTime()) avgNeighborConnectionLifeTime.Set(metrics.AvgNeighborConnectionLifeTime())
connectionsCount.Set(float64(metrics.ConnectionsCount())) connectionsCount.Set(float64(metrics.NeighborConnectionsCount()))
min, max, avg := metrics.AutopeeringDistanceStats() min, max, avg := metrics.AutopeeringDistanceStats()
minDistance.Set(float64(min)) minDistance.Set(float64(min))
maxDistance.Set(float64(max)) maxDistance.Set(float64(max))
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment