Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add metrics for peermanager scores #158

Merged
merged 3 commits into from
Oct 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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