Skip to content

Commit

Permalink
Add metrics for peermanager scores (#158)
Browse files Browse the repository at this point in the history
* Add metrics for peermanager scores

* fix tests
  • Loading branch information
philipsu522 authored Oct 4, 2023
1 parent 51802ea commit bf1224d
Show file tree
Hide file tree
Showing 11 changed files with 83 additions and 69 deletions.
7 changes: 7 additions & 0 deletions internal/p2p/metrics.gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions internal/p2p/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ var (
type Metrics struct {
// Number of peers.
Peers metrics.Gauge
// Score for each peer
PeerScore metrics.Gauge `metrics_labels:"peer_id"`
// Number of bytes per channel received from a given peer.
PeerReceiveBytesTotal metrics.Counter `metrics_labels:"peer_id, chID, message_type"`
// Number of bytes per channel sent to a given peer.
Expand Down
2 changes: 1 addition & 1 deletion internal/p2p/p2ptest/network.go
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,7 @@ func (n *Network) MakeNode(ctx context.Context, t *testing.T, opts NodeOptions)
RetryTimeJitter: time.Millisecond,
MaxPeers: opts.MaxPeers,
MaxConnected: opts.MaxConnected,
})
}, p2p.NopMetrics())
require.NoError(t, err)

router, err := p2p.NewRouter(
Expand Down
4 changes: 4 additions & 0 deletions internal/p2p/peermanager.go
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,7 @@ type PeerManager struct {
ready map[types.NodeID]bool // ready peers (Ready → Disconnected)
evict map[types.NodeID]bool // peers scheduled for eviction (Connected → EvictNext)
evicting map[types.NodeID]bool // peers being evicted (EvictNext → Disconnected)
metrics *Metrics
}

// NewPeerManager creates a new peer manager.
Expand All @@ -308,6 +309,7 @@ func NewPeerManager(
selfID types.NodeID,
peerDB dbm.DB,
options PeerManagerOptions,
metrics *Metrics,
) (*PeerManager, error) {
if selfID == "" {
return nil, errors.New("self ID not given")
Expand Down Expand Up @@ -339,6 +341,7 @@ func NewPeerManager(
evict: map[types.NodeID]bool{},
evicting: map[types.NodeID]bool{},
subscriptions: map[*PeerUpdates]*PeerUpdates{},
metrics: metrics,
}
if err = peerManager.configurePeers(); err != nil {
return nil, err
Expand Down Expand Up @@ -873,6 +876,7 @@ func (m *PeerManager) Advertise(peerID types.NodeID, limit uint16) []NodeAddress
}

for _, peer := range m.store.Ranked() {
m.metrics.PeerScore.With("peer_id", string(peerID)).Set(float64(int(peer.Score())))
if peer.ID == peerID {
continue
}
Expand Down
2 changes: 1 addition & 1 deletion internal/p2p/peermanager_scoring_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ func TestPeerScoring(t *testing.T) {

// create a mock peer manager
db := dbm.NewMemDB()
peerManager, err := NewPeerManager(log.NewNopLogger(), selfID, db, PeerManagerOptions{})
peerManager, err := NewPeerManager(log.NewNopLogger(), selfID, db, PeerManagerOptions{}, NopMetrics())
require.NoError(t, err)

// create a fake node
Expand Down
Loading

0 comments on commit bf1224d

Please sign in to comment.