github.com/ethereum/go-ethereum@v1.16.1/p2p/discover/metrics.go (about)

     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 discover
    18  
    19  import (
    20  	"fmt"
    21  	"net"
    22  	"net/netip"
    23  
    24  	"github.com/ethereum/go-ethereum/metrics"
    25  )
    26  
    27  const (
    28  	moduleName = "discover"
    29  	// ingressMeterName is the prefix of the per-packet inbound metrics.
    30  	ingressMeterName = moduleName + "/ingress"
    31  
    32  	// egressMeterName is the prefix of the per-packet outbound metrics.
    33  	egressMeterName = moduleName + "/egress"
    34  )
    35  
    36  var (
    37  	bucketsCounter      []*metrics.Counter
    38  	ingressTrafficMeter = metrics.NewRegisteredMeter(ingressMeterName, nil)
    39  	egressTrafficMeter  = metrics.NewRegisteredMeter(egressMeterName, nil)
    40  )
    41  
    42  func init() {
    43  	for i := 0; i < nBuckets; i++ {
    44  		bucketsCounter = append(bucketsCounter, metrics.NewRegisteredCounter(fmt.Sprintf("%s/bucket/%d/count", moduleName, i), nil))
    45  	}
    46  }
    47  
    48  // meteredUdpConn is a wrapper around a net.UDPConn that meters both the
    49  // inbound and outbound network traffic.
    50  type meteredUdpConn struct {
    51  	udpConn UDPConn
    52  }
    53  
    54  func newMeteredConn(conn UDPConn) UDPConn {
    55  	// Short circuit if metrics are disabled
    56  	if !metrics.Enabled() {
    57  		return conn
    58  	}
    59  	return &meteredUdpConn{udpConn: conn}
    60  }
    61  
    62  func (c *meteredUdpConn) Close() error {
    63  	return c.udpConn.Close()
    64  }
    65  
    66  func (c *meteredUdpConn) LocalAddr() net.Addr {
    67  	return c.udpConn.LocalAddr()
    68  }
    69  
    70  // ReadFromUDPAddrPort delegates a network read to the underlying connection, bumping the udp ingress traffic meter along the way.
    71  func (c *meteredUdpConn) ReadFromUDPAddrPort(b []byte) (n int, addr netip.AddrPort, err error) {
    72  	n, addr, err = c.udpConn.ReadFromUDPAddrPort(b)
    73  	ingressTrafficMeter.Mark(int64(n))
    74  	return n, addr, err
    75  }
    76  
    77  // WriteToUDPAddrPort delegates a network write to the underlying connection, bumping the udp egress traffic meter along the way.
    78  func (c *meteredUdpConn) WriteToUDPAddrPort(b []byte, addr netip.AddrPort) (n int, err error) {
    79  	n, err = c.udpConn.WriteToUDPAddrPort(b, addr)
    80  	egressTrafficMeter.Mark(int64(n))
    81  	return n, err
    82  }