Skip to content

Commit

Permalink
p2p, p2p/discover: add dial metrics (ethereum#27621)
Browse files Browse the repository at this point in the history
This PR adds metrics for p2p dialing, which gives us visibility into the quality of the dial 
candidates  returned by our discovery methods.
  • Loading branch information
lightclient authored and devopsbo3 committed Nov 10, 2023
1 parent 7818ec3 commit 463ad60
Show file tree
Hide file tree
Showing 13 changed files with 277 additions and 51 deletions.
8 changes: 8 additions & 0 deletions eth/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ import (
"github.com/ethereum/go-ethereum/ethdb"
"github.com/ethereum/go-ethereum/event"
"github.com/ethereum/go-ethereum/log"
"github.com/ethereum/go-ethereum/metrics"
"github.com/ethereum/go-ethereum/p2p"
)

Expand Down Expand Up @@ -424,6 +425,13 @@ func (h *handler) runSnapExtension(peer *snap.Peer, handler snap.Handler) error
defer h.peerWG.Done()

if err := h.peers.registerSnapExtension(peer); err != nil {
if metrics.Enabled {
if peer.Inbound() {
snap.IngressRegistrationErrorMeter.Mark(1)
} else {
snap.EgressRegistrationErrorMeter.Mark(1)
}
}
peer.Log().Warn("Snapshot extension registration failed", "err", err)
return err
}
Expand Down
26 changes: 26 additions & 0 deletions eth/protocols/eth/handshake.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,14 @@
package eth

import (
"errors"
"fmt"
"math/big"
"time"

"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/forkid"
"github.com/ethereum/go-ethereum/metrics"
"github.com/ethereum/go-ethereum/p2p"
)

Expand Down Expand Up @@ -59,9 +61,11 @@ func (p *Peer) Handshake(network uint64, td *big.Int, head common.Hash, genesis
select {
case err := <-errc:
if err != nil {
markError(p, err)
return err
}
case <-timeout.C:
markError(p, p2p.DiscReadTimeout)
return p2p.DiscReadTimeout
}
}
Expand Down Expand Up @@ -105,3 +109,25 @@ func (p *Peer) readStatus(network uint64, status *StatusPacket, genesis common.H
}
return nil
}

// markError registers the error with the corresponding metric.
func markError(p *Peer, err error) {
if !metrics.Enabled {
return
}
m := meters.get(p.Inbound())
switch errors.Unwrap(err) {
case errNetworkIDMismatch:
m.networkIDMismatch.Mark(1)
case errProtocolVersionMismatch:
m.protocolVersionMismatch.Mark(1)
case errGenesisMismatch:
m.genesisMismatch.Mark(1)
case errForkIDRejected:
m.forkidRejected.Mark(1)
case p2p.DiscReadTimeout:
m.timeoutError.Mark(1)
default:
m.peerError.Mark(1)
}
}
81 changes: 81 additions & 0 deletions eth/protocols/eth/metrics.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
// Copyright 2023 The go-ethereum Authors
// This file is part of the go-ethereum library.
//
// The go-ethereum library is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// The go-ethereum library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.

package eth

import "github.com/ethereum/go-ethereum/metrics"

// meters stores ingress and egress handshake meters.
var meters bidirectionalMeters

// bidirectionalMeters stores ingress and egress handshake meters.
type bidirectionalMeters struct {
ingress *hsMeters
egress *hsMeters
}

// get returns the corresponding meter depending if ingress or egress is
// desired.
func (h *bidirectionalMeters) get(ingress bool) *hsMeters {
if ingress {
return h.ingress
}
return h.egress
}

// hsMeters is a collection of meters which track metrics related to the
// eth subprotocol handshake.
type hsMeters struct {
// peerError measures the number of errors related to incorrect peer
// behaviour, such as invalid message code, size, encoding, etc.
peerError metrics.Meter

// timeoutError measures the number of timeouts.
timeoutError metrics.Meter

// networkIDMismatch measures the number of network id mismatch errors.
networkIDMismatch metrics.Meter

// protocolVersionMismatch measures the number of differing protocol
// versions.
protocolVersionMismatch metrics.Meter

// genesisMismatch measures the number of differing genesises.
genesisMismatch metrics.Meter

// forkidRejected measures the number of differing forkids.
forkidRejected metrics.Meter
}

// newHandshakeMeters registers and returns handshake meters for the given
// base.
func newHandshakeMeters(base string) *hsMeters {
return &hsMeters{
peerError: metrics.NewRegisteredMeter(base+"error/peer", nil),
timeoutError: metrics.NewRegisteredMeter(base+"error/timeout", nil),
networkIDMismatch: metrics.NewRegisteredMeter(base+"error/network", nil),
protocolVersionMismatch: metrics.NewRegisteredMeter(base+"error/version", nil),
genesisMismatch: metrics.NewRegisteredMeter(base+"error/genesis", nil),
forkidRejected: metrics.NewRegisteredMeter(base+"error/forkid", nil),
}
}

func init() {
meters = bidirectionalMeters{
ingress: newHandshakeMeters("eth/protocols/eth/ingress/handshake/"),
egress: newHandshakeMeters("eth/protocols/eth/egress/handshake/"),
}
}
29 changes: 29 additions & 0 deletions eth/protocols/snap/metrics.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// Copyright 2023 The go-ethereum Authors
// This file is part of the go-ethereum library.
//
// The go-ethereum library is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// The go-ethereum library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.

package snap

import (
metrics "github.com/ethereum/go-ethereum/metrics"
)

var (
ingressRegistrationErrorName = "eth/protocols/snap/ingress/registration/error"
egressRegistrationErrorName = "eth/protocols/snap/egress/registration/error"

IngressRegistrationErrorMeter = metrics.NewRegisteredMeter(ingressRegistrationErrorName, nil)
EgressRegistrationErrorMeter = metrics.NewRegisteredMeter(egressRegistrationErrorName, nil)
)
5 changes: 3 additions & 2 deletions p2p/dial.go
Original file line number Diff line number Diff line change
Expand Up @@ -521,13 +521,14 @@ func (t *dialTask) resolve(d *dialScheduler) bool {

// dial performs the actual connection attempt.
func (t *dialTask) dial(d *dialScheduler, dest *enode.Node) error {
dialMeter.Mark(1)
fd, err := d.dialer.Dial(d.ctx, t.dest)
if err != nil {
d.log.Trace("Dial error", "id", t.dest.ID(), "addr", nodeAddr(t.dest), "conn", t.flags, "err", cleanupDialErr(err))
dialConnectionError.Mark(1)
return &dialError{err}
}
mfd := newMeteredConn(fd, false, &net.TCPAddr{IP: dest.IP(), Port: dest.TCP()})
return d.setupFunc(mfd, t.flags, dest)
return d.setupFunc(newMeteredConn(fd), t.flags, dest)
}

func (t *dialTask) String() string {
Expand Down
8 changes: 8 additions & 0 deletions p2p/discover/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package discover

import (
"fmt"
"net"

"github.com/ethereum/go-ethereum/metrics"
Expand All @@ -32,10 +33,17 @@ const (
)

var (
bucketsCounter []metrics.Counter
ingressTrafficMeter = metrics.NewRegisteredMeter(ingressMeterName, nil)
egressTrafficMeter = metrics.NewRegisteredMeter(egressMeterName, nil)
)

func init() {
for i := 0; i < nBuckets; i++ {
bucketsCounter = append(bucketsCounter, metrics.NewRegisteredCounter(fmt.Sprintf("%s/bucket/%d/count", moduleName, i), nil))
}
}

// meteredConn is a wrapper around a net.UDPConn that meters both the
// inbound and outbound network traffic.
type meteredUdpConn struct {
Expand Down
36 changes: 32 additions & 4 deletions p2p/discover/table.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import (

"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/log"
"github.com/ethereum/go-ethereum/metrics"
"github.com/ethereum/go-ethereum/p2p/enode"
"github.com/ethereum/go-ethereum/p2p/netutil"
)
Expand Down Expand Up @@ -80,7 +81,8 @@ type Table struct {
closeReq chan struct{}
closed chan struct{}

nodeAddedHook func(*node) // for testing
nodeAddedHook func(*bucket, *node)
nodeRemovedHook func(*bucket, *node)
}

// transport is implemented by the UDP transports.
Expand All @@ -98,6 +100,7 @@ type bucket struct {
entries []*node // live entries, sorted by time of last contact
replacements []*node // recently seen nodes to be used if revalidation fails
ips netutil.DistinctNetSet
index int
}

func newTable(t transport, db *enode.DB, cfg Config) (*Table, error) {
Expand All @@ -119,7 +122,8 @@ func newTable(t transport, db *enode.DB, cfg Config) (*Table, error) {
}
for i := range tab.buckets {
tab.buckets[i] = &bucket{
ips: netutil.DistinctNetSet{Subnet: bucketSubnet, Limit: bucketIPLimit},
index: i,
ips: netutil.DistinctNetSet{Subnet: bucketSubnet, Limit: bucketIPLimit},
}
}
tab.seedRand()
Expand All @@ -128,6 +132,22 @@ func newTable(t transport, db *enode.DB, cfg Config) (*Table, error) {
return tab, nil
}

func newMeteredTable(t transport, db *enode.DB, cfg Config) (*Table, error) {
tab, err := newTable(t, db, cfg)
if err != nil {
return nil, err
}
if metrics.Enabled {
tab.nodeAddedHook = func(b *bucket, n *node) {
bucketsCounter[b.index].Inc(1)
}
tab.nodeRemovedHook = func(b *bucket, n *node) {
bucketsCounter[b.index].Dec(1)
}
}
return tab, nil
}

// Nodes returns all nodes contained in the table.
func (tab *Table) Nodes() []*enode.Node {
if !tab.isInitDone() {
Expand Down Expand Up @@ -495,7 +515,7 @@ func (tab *Table) addSeenNode(n *node) {
n.addedAt = time.Now()

if tab.nodeAddedHook != nil {
tab.nodeAddedHook(n)
tab.nodeAddedHook(b, n)
}
}

Expand Down Expand Up @@ -539,7 +559,7 @@ func (tab *Table) addVerifiedNode(n *node) {
n.addedAt = time.Now()

if tab.nodeAddedHook != nil {
tab.nodeAddedHook(n)
tab.nodeAddedHook(b, n)
}
}

Expand Down Expand Up @@ -638,8 +658,16 @@ func (tab *Table) bumpInBucket(b *bucket, n *node) bool {
}

func (tab *Table) deleteInBucket(b *bucket, n *node) {
// Check if the node is actually in the bucket so the removed hook
// isn't called multiple times for the same node.
if !contains(b.entries, n.ID()) {
return
}
b.entries = deleteNode(b.entries, n)
tab.removeIP(b, n.IP())
if tab.nodeRemovedHook != nil {
tab.nodeRemovedHook(b, n)
}
}

func contains(ns []*node, id enode.ID) bool {
Expand Down
2 changes: 1 addition & 1 deletion p2p/discover/v4_udp.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ func ListenV4(c UDPConn, ln *enode.LocalNode, cfg Config) (*UDPv4, error) {
log: cfg.Log,
}

tab, err := newTable(t, ln.Database(), cfg)
tab, err := newMeteredTable(t, ln.Database(), cfg)
if err != nil {
return nil, err
}
Expand Down
2 changes: 1 addition & 1 deletion p2p/discover/v4_udp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -394,7 +394,7 @@ func TestUDPv4_pingMatchIP(t *testing.T) {
func TestUDPv4_successfulPing(t *testing.T) {
test := newUDPTest(t)
added := make(chan *node, 1)
test.table.nodeAddedHook = func(n *node) { added <- n }
test.table.nodeAddedHook = func(b *bucket, n *node) { added <- n }
defer test.close()

// The remote side sends a ping packet to initiate the exchange.
Expand Down
2 changes: 1 addition & 1 deletion p2p/discover/v5_udp.go
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ func newUDPv5(conn UDPConn, ln *enode.LocalNode, cfg Config) (*UDPv5, error) {
cancelCloseCtx: cancelCloseCtx,
}
t.talk = newTalkSystem(t)
tab, err := newTable(t, t.db, cfg)
tab, err := newMeteredTable(t, t.db, cfg)
if err != nil {
return nil, err
}
Expand Down
Loading

0 comments on commit 463ad60

Please sign in to comment.