Skip to content

Commit 111fa80

Browse files
committed
eth: add metrics to eth handshake
1 parent 006d53c commit 111fa80

File tree

4 files changed

+144
-0
lines changed

4 files changed

+144
-0
lines changed

eth/handler.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ import (
3838
"github.com/ethereum/go-ethereum/ethdb"
3939
"github.com/ethereum/go-ethereum/event"
4040
"github.com/ethereum/go-ethereum/log"
41+
"github.com/ethereum/go-ethereum/metrics"
4142
"github.com/ethereum/go-ethereum/p2p"
4243
)
4344

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

426427
if err := h.peers.registerSnapExtension(peer); err != nil {
428+
if metrics.Enabled {
429+
if peer.Inbound() {
430+
metrics.GetOrRegisterMeter(snap.IngressRegistrationError, nil).Mark(1)
431+
} else {
432+
metrics.GetOrRegisterMeter(snap.EgressRegistrationError, nil).Mark(1)
433+
}
434+
}
427435
peer.Log().Warn("Snapshot extension registration failed", "err", err)
428436
return err
429437
}

eth/protocols/eth/handshake.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,14 @@
1717
package eth
1818

1919
import (
20+
"errors"
2021
"fmt"
2122
"math/big"
2223
"time"
2324

2425
"github.com/ethereum/go-ethereum/common"
2526
"github.com/ethereum/go-ethereum/core/forkid"
27+
"github.com/ethereum/go-ethereum/metrics"
2628
"github.com/ethereum/go-ethereum/p2p"
2729
)
2830

@@ -59,9 +61,11 @@ func (p *Peer) Handshake(network uint64, td *big.Int, head common.Hash, genesis
5961
select {
6062
case err := <-errc:
6163
if err != nil {
64+
markError(p, err)
6265
return err
6366
}
6467
case <-timeout.C:
68+
markError(p, p2p.DiscReadTimeout)
6569
return p2p.DiscReadTimeout
6670
}
6771
}
@@ -105,3 +109,25 @@ func (p *Peer) readStatus(network uint64, status *StatusPacket, genesis common.H
105109
}
106110
return nil
107111
}
112+
113+
// markError registers the error with the corresponding metric.
114+
func markError(p *Peer, err error) {
115+
if !metrics.Enabled {
116+
return
117+
}
118+
m := meters.Get(p.Inbound())
119+
switch errors.Unwrap(err) {
120+
case errNetworkIDMismatch:
121+
m.networkIDMismatch.Mark(1)
122+
case errProtocolVersionMismatch:
123+
m.protocolVersionMismatch.Mark(1)
124+
case errGenesisMismatch:
125+
m.genesisMismatch.Mark(1)
126+
case errForkIDRejected:
127+
m.forkidRejected.Mark(1)
128+
case p2p.DiscReadTimeout:
129+
m.timeoutError.Mark(1)
130+
default:
131+
m.peerError.Mark(1)
132+
}
133+
}

eth/protocols/eth/metrics.go

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
// Copyright 2023 The go-ethereum Authors
2+
// This file is part of the go-ethereum library.
3+
//
4+
// The go-ethereum library is free software: you can redistribute it and/or modify
5+
// it under the terms of the GNU Lesser General Public License as published by
6+
// the Free Software Foundation, either version 3 of the License, or
7+
// (at your option) any later version.
8+
//
9+
// The go-ethereum library is distributed in the hope that it will be useful,
10+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
// GNU Lesser General Public License for more details.
13+
//
14+
// You should have received a copy of the GNU Lesser General Public License
15+
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
16+
17+
package eth
18+
19+
import "github.com/ethereum/go-ethereum/metrics"
20+
21+
// meters stores ingress and egress handshake meters.
22+
var meters bidirectionalMeters
23+
24+
// bidirectionalMeters stores ingress and egress handshake meters.
25+
type bidirectionalMeters struct {
26+
ingress *hsMeters
27+
egress *hsMeters
28+
}
29+
30+
// Get returns the corresponding meter depending if ingress or egress is
31+
// desired.
32+
func (h *bidirectionalMeters) Get(ingress bool) *hsMeters {
33+
if ingress {
34+
return h.ingress
35+
}
36+
return h.egress
37+
}
38+
39+
// hsMeters is a collection of meters which track metrics related to the
40+
// eth subprotocol handshake.
41+
type hsMeters struct {
42+
// peerError measures the number of errors related to incorrect peer
43+
// behaviour, such as invalid message code, size, encoding, etc.
44+
peerError metrics.Meter
45+
46+
// timeoutError measures the number of timeouts.
47+
timeoutError metrics.Meter
48+
49+
// networkIDMismatch measures the number of network id mismatch errors.
50+
networkIDMismatch metrics.Meter
51+
52+
// protocolVersionMismatch measures the number of differing protocol
53+
// versions.
54+
protocolVersionMismatch metrics.Meter
55+
56+
// genesisMismatch measures the number of differing genesises.
57+
genesisMismatch metrics.Meter
58+
59+
// forkidRejected measures the number of differing forkids.
60+
forkidRejected metrics.Meter
61+
}
62+
63+
// newHandshakeMeters registers and returns handshake meters for the given
64+
// base.
65+
func newHandshakeMeters(base string) *hsMeters {
66+
return &hsMeters{
67+
peerError: metrics.NewRegisteredMeter(base+"error/peer", nil),
68+
timeoutError: metrics.NewRegisteredMeter(base+"error/timeout", nil),
69+
networkIDMismatch: metrics.NewRegisteredMeter(base+"error/network", nil),
70+
protocolVersionMismatch: metrics.NewRegisteredMeter(base+"error/version", nil),
71+
genesisMismatch: metrics.NewRegisteredMeter(base+"error/genesis", nil),
72+
forkidRejected: metrics.NewRegisteredMeter(base+"error/forkid", nil),
73+
}
74+
}
75+
76+
func init() {
77+
meters = bidirectionalMeters{
78+
ingress: newHandshakeMeters("eth/protocols/eth/ingress/handshake/"),
79+
egress: newHandshakeMeters("eth/protocols/eth/egress/handshake/"),
80+
}
81+
}

eth/protocols/snap/metrics.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// Copyright 2023 The go-ethereum Authors
2+
// This file is part of the go-ethereum library.
3+
//
4+
// The go-ethereum library is free software: you can redistribute it and/or modify
5+
// it under the terms of the GNU Lesser General Public License as published by
6+
// the Free Software Foundation, either version 3 of the License, or
7+
// (at your option) any later version.
8+
//
9+
// The go-ethereum library is distributed in the hope that it will be useful,
10+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
// GNU Lesser General Public License for more details.
13+
//
14+
// You should have received a copy of the GNU Lesser General Public License
15+
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
16+
17+
package snap
18+
19+
import (
20+
metrics "github.com/ethereum/go-ethereum/metrics"
21+
)
22+
23+
var (
24+
IngressRegistrationError = "eth/protocols/snap/ingress/registration/error"
25+
EgressRegistrationError = "eth/protocols/snap/egress/registration/error"
26+
27+
_ = metrics.NewRegisteredMeter(IngressRegistrationError, nil)
28+
_ = metrics.NewRegisteredMeter(EgressRegistrationError, nil)
29+
)

0 commit comments

Comments
 (0)