Skip to content

Commit

Permalink
Add average P2P connection duration metric (ava-labs#2380)
Browse files Browse the repository at this point in the history
  • Loading branch information
joshua-kim authored Jan 4, 2023
1 parent 6a86eca commit 9bcdfad
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 0 deletions.
46 changes: 46 additions & 0 deletions network/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
package network

import (
"sync"
"time"

"github.com/prometheus/client_golang/prometheus"

"github.com/ava-labs/avalanchego/ids"
Expand Down Expand Up @@ -31,6 +34,11 @@ type metrics struct {
nodeUptimeRewardingStake prometheus.Gauge
nodeSubnetUptimeWeightedAverage *prometheus.GaugeVec
nodeSubnetUptimeRewardingStake *prometheus.GaugeVec
peerConnectedLifetimeAverage prometheus.Gauge

lock sync.RWMutex
peerConnectedStartTimes map[ids.NodeID]float64
peerConnectedStartTimesSum float64
}

func newMetrics(namespace string, registerer prometheus.Registerer, initialSubnetIDs set.Set[ids.ID]) (*metrics, error) {
Expand Down Expand Up @@ -129,6 +137,14 @@ func newMetrics(namespace string, registerer prometheus.Registerer, initialSubne
},
[]string{"subnetID"},
),
peerConnectedLifetimeAverage: prometheus.NewGauge(
prometheus.GaugeOpts{
Namespace: namespace,
Name: "peer_connected_duration_average",
Help: "The average duration of all peer connections in nanoseconds",
},
),
peerConnectedStartTimes: make(map[ids.NodeID]float64),
}

errs := wrappers.Errs{}
Expand All @@ -150,6 +166,7 @@ func newMetrics(namespace string, registerer prometheus.Registerer, initialSubne
registerer.Register(m.nodeUptimeRewardingStake),
registerer.Register(m.nodeSubnetUptimeWeightedAverage),
registerer.Register(m.nodeSubnetUptimeRewardingStake),
registerer.Register(m.peerConnectedLifetimeAverage),
)

// init subnet tracker metrics with whitelisted subnets
Expand All @@ -176,6 +193,13 @@ func (m *metrics) markConnected(peer peer.Peer) {
for subnetID := range trackedSubnets {
m.numSubnetPeers.WithLabelValues(subnetID.String()).Inc()
}

m.lock.Lock()
defer m.lock.Unlock()

now := float64(time.Now().UnixNano())
m.peerConnectedStartTimes[peer.ID()] = now
m.peerConnectedStartTimesSum += now
}

func (m *metrics) markDisconnected(peer peer.Peer) {
Expand All @@ -186,4 +210,26 @@ func (m *metrics) markDisconnected(peer peer.Peer) {
for subnetID := range trackedSubnets {
m.numSubnetPeers.WithLabelValues(subnetID.String()).Dec()
}

m.lock.Lock()
defer m.lock.Unlock()

peerID := peer.ID()
start := m.peerConnectedStartTimes[peerID]
m.peerConnectedStartTimesSum -= start

delete(m.peerConnectedStartTimes, peerID)
}

func (m *metrics) updatePeerConnectionLifetimeMetrics() {
m.lock.RLock()
defer m.lock.RUnlock()

avg := float64(0)
if n := len(m.peerConnectedStartTimes); n > 0 {
avgStartTime := m.peerConnectedStartTimesSum / float64(n)
avg = float64(time.Now().UnixNano()) - avgStartTime
}

m.peerConnectedLifetimeAverage.Set(avg)
}
3 changes: 3 additions & 0 deletions network/network.go
Original file line number Diff line number Diff line change
Expand Up @@ -341,6 +341,9 @@ func (n *network) HealthCheck(context.Context) (interface{}, error) {
details[SendFailRateKey] = sendFailRate
n.metrics.sendFailRate.Set(sendFailRate)

// emit metrics about the lifetime of peer connections
n.metrics.updatePeerConnectionLifetimeMetrics()

// Network layer is healthy
if healthy || !n.config.HealthConfig.Enabled {
return details, nil
Expand Down

0 comments on commit 9bcdfad

Please sign in to comment.