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

Adds new metrics for discovery #1129

Merged
merged 6 commits into from
Aug 6, 2020
Merged
Show file tree
Hide file tree
Changes from 5 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
2 changes: 2 additions & 0 deletions les/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,8 @@ var (

requestRTT = metrics.NewRegisteredTimer("les/client/req/rtt", nil)
requestSendDelay = metrics.NewRegisteredTimer("les/client/req/sendDelay", nil)

clientDiscoveredNodesCounter = metrics.NewRegisteredCounter("les/client/discovered", nil) // Counter for discovered nodes
mcortesi marked this conversation as resolved.
Show resolved Hide resolved
)

// meteredMsgReadWriter is a wrapper around a p2p.MsgReadWriter, capable of
Expand Down
1 change: 1 addition & 0 deletions les/serverpool.go
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,7 @@ func (pool *serverPool) discoverNodes() {
if err != nil {
continue
}
clientDiscoveredNodesCounter.Inc(1)
mcortesi marked this conversation as resolved.
Show resolved Hide resolved
pool.discNodes <- enode.NewV4(pubkey, n.IP, int(n.TCP), int(n.UDP))
}
}
Expand Down
1 change: 1 addition & 0 deletions p2p/dial.go
Original file line number Diff line number Diff line change
Expand Up @@ -328,6 +328,7 @@ type discoverTask struct {

func (t *discoverTask) Do(srv *Server) {
t.results = enode.ReadNodes(srv.discmix, t.want)
discoveredPeersCounter.Inc(int64(len(t.results)))
}

func (t *discoverTask) String() string {
Expand Down
24 changes: 24 additions & 0 deletions p2p/discover/metrics.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// Copyright 2018 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 discover

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

var (
ingressTrafficMeter = metrics.NewRegisteredMeter("discover/ingress", nil)
egressTrafficMeter = metrics.NewRegisteredMeter("discover/egress", nil)
mcortesi marked this conversation as resolved.
Show resolved Hide resolved
)
6 changes: 5 additions & 1 deletion p2p/discover/v4_udp.go
Original file line number Diff line number Diff line change
Expand Up @@ -670,8 +670,11 @@ func (t *UDPv4) send(toaddr *net.UDPAddr, toid enode.ID, req packetV4) ([]byte,
}

func (t *UDPv4) write(toaddr *net.UDPAddr, toid enode.ID, what string, packet []byte) error {
_, err := t.conn.WriteToUDP(packet, toaddr)
nbytes, err := t.conn.WriteToUDP(packet, toaddr)
t.log.Trace(">> "+what, "id", toid, "addr", toaddr, "err", err)
if err == nil {
egressTrafficMeter.Mark(int64(nbytes))
}
return err
}

Expand Down Expand Up @@ -709,6 +712,7 @@ func (t *UDPv4) readLoop(unhandled chan<- ReadPacket) {
buf := make([]byte, maxPacketSize)
for {
nbytes, from, err := t.conn.ReadFromUDP(buf)
ingressTrafficMeter.Mark(int64(nbytes))
if netutil.IsTemporaryError(err) {
// Ignore temporary read errors.
t.log.Debug("Temporary UDP read error", "err", err)
Expand Down
32 changes: 21 additions & 11 deletions p2p/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,22 +30,23 @@ import (
)

const (
MetricsInboundTraffic = "p2p/ingress" // Name for the registered inbound traffic meter
MetricsOutboundTraffic = "p2p/egress" // Name for the registered outbound traffic meter
MetricsOutboundConnects = "p2p/dials" // Name for the registered outbound connects meter
MetricsInboundConnects = "p2p/serves" // Name for the registered inbound connects meter
MetricsInboundTraffic = "p2p/ingress" // Name for the registered inbound traffic meter
MetricsOutboundTraffic = "p2p/egress" // Name for the registered outbound traffic meter

MeteredPeerLimit = 1024 // This amount of peers are individually metered
)

var (
ingressConnectMeter = metrics.NewRegisteredMeter(MetricsInboundConnects, nil) // Meter counting the ingress connections
ingressTrafficMeter = metrics.NewRegisteredMeter(MetricsInboundTraffic, nil) // Meter metering the cumulative ingress traffic
egressConnectMeter = metrics.NewRegisteredMeter(MetricsOutboundConnects, nil) // Meter counting the egress connections
egressTrafficMeter = metrics.NewRegisteredMeter(MetricsOutboundTraffic, nil) // Meter metering the cumulative egress traffic
activePeerGauge = metrics.NewRegisteredGauge("p2p/peers", nil) // Gauge tracking the current peer count
activeValidatorsPeerGauge = metrics.NewRegisteredGauge("p2p/peers/validators", nil) // Gauge tracking the current validators peer count
activeProxiesPeerGauge = metrics.NewRegisteredGauge("p2p/peers/proxies", nil) // Gauge tracking the current proxies peer count
ingressConnectMeter = metrics.NewRegisteredMeter("p2p/serves", nil) // Meter counting the ingress connections
ingressConnectWithHandshakeMeter = metrics.NewRegisteredMeter("p2p/serves/handshakes", nil) // Meter counting the ingress with successful handshake connections
ingressTrafficMeter = metrics.NewRegisteredMeter(MetricsInboundTraffic, nil) // Meter metering the cumulative ingress traffic
egressConnectMeter = metrics.NewRegisteredMeter("p2p/dials", nil) // Meter counting the egress connections
egressConnectWithHandshakeMeter = metrics.NewRegisteredMeter("p2p/dials/handshakes", nil) // Meter counting the egress with successful handshake connections
egressTrafficMeter = metrics.NewRegisteredMeter(MetricsOutboundTraffic, nil) // Meter metering the cumulative egress traffic
activePeerGauge = metrics.NewRegisteredGauge("p2p/peers", nil) // Gauge tracking the current peer count
activeValidatorsPeerGauge = metrics.NewRegisteredGauge("p2p/peers/validators", nil) // Gauge tracking the current validators peer count
activeProxiesPeerGauge = metrics.NewRegisteredGauge("p2p/peers/proxies", nil) // Gauge tracking the current proxies peer count
discoveredPeersCounter = metrics.NewRegisteredCounter("p2p/peers/discovered", nil) // Counter of the total discovered peers

PeerIngressRegistry = metrics.NewPrefixedChildRegistry(metrics.EphemeralRegistry, MetricsInboundTraffic+"/") // Registry containing the peer ingress
PeerEgressRegistry = metrics.NewPrefixedChildRegistry(metrics.EphemeralRegistry, MetricsOutboundTraffic+"/") // Registry containing the peer egress
Expand Down Expand Up @@ -94,6 +95,7 @@ type meteredConn struct {
connected time.Time // Connection time of the peer
addr *net.TCPAddr // TCP address of the peer
peer *Peer // Peer instance
ingress bool // Indicates wether connection inbound or outbound
mcortesi marked this conversation as resolved.
Show resolved Hide resolved

// trafficMetered denotes if the peer is registered in the traffic registries.
// Its value is true if the metered peer count doesn't reach the limit in the
Expand All @@ -118,6 +120,7 @@ func newMeteredConn(conn net.Conn, ingress bool, addr *net.TCPAddr) net.Conn {
log.Warn("Peer address is unspecified")
return conn
}

// Bump the connection counters and wrap the connection
if ingress {
ingressConnectMeter.Mark(1)
Expand All @@ -129,6 +132,7 @@ func newMeteredConn(conn net.Conn, ingress bool, addr *net.TCPAddr) net.Conn {
return &meteredConn{
Conn: conn,
addr: addr,
ingress: ingress,
connected: time.Now(),
}
}
Expand Down Expand Up @@ -161,6 +165,12 @@ func (c *meteredConn) Write(b []byte) (n int, err error) {

// handshakeDone is called after the connection passes the handshake.
func (c *meteredConn) handshakeDone(peer *Peer) {
if c.ingress {
ingressConnectWithHandshakeMeter.Mark(1)
} else {
egressConnectWithHandshakeMeter.Mark(1)
}

if atomic.AddInt32(&meteredPeerCount, 1) >= MeteredPeerLimit {
// Don't register the peer in the traffic registries.
atomic.AddInt32(&meteredPeerCount, -1)
Expand Down