github.com/theQRL/go-zond@v0.1.1/p2p/metrics.go (about)

     1  // Copyright 2015 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  // Contains the meters and timers used by the networking layer.
    18  
    19  package p2p
    20  
    21  import (
    22  	"errors"
    23  	"net"
    24  
    25  	"github.com/theQRL/go-zond/metrics"
    26  )
    27  
    28  const (
    29  	// HandleHistName is the prefix of the per-packet serving time histograms.
    30  	HandleHistName = "p2p/handle"
    31  
    32  	// ingressMeterName is the prefix of the per-packet inbound metrics.
    33  	ingressMeterName = "p2p/ingress"
    34  
    35  	// egressMeterName is the prefix of the per-packet outbound metrics.
    36  	egressMeterName = "p2p/egress"
    37  )
    38  
    39  var (
    40  	activePeerGauge metrics.Gauge = metrics.NilGauge{}
    41  
    42  	ingressTrafficMeter = metrics.NewRegisteredMeter("p2p/ingress", nil)
    43  	egressTrafficMeter  = metrics.NewRegisteredMeter("p2p/egress", nil)
    44  
    45  	// general ingress/egress connection meters
    46  	serveMeter          metrics.Meter = metrics.NilMeter{}
    47  	serveSuccessMeter   metrics.Meter = metrics.NilMeter{}
    48  	dialMeter           metrics.Meter = metrics.NilMeter{}
    49  	dialSuccessMeter    metrics.Meter = metrics.NilMeter{}
    50  	dialConnectionError metrics.Meter = metrics.NilMeter{}
    51  
    52  	// handshake error meters
    53  	dialTooManyPeers        = metrics.NewRegisteredMeter("p2p/dials/error/saturated", nil)
    54  	dialAlreadyConnected    = metrics.NewRegisteredMeter("p2p/dials/error/known", nil)
    55  	dialSelf                = metrics.NewRegisteredMeter("p2p/dials/error/self", nil)
    56  	dialUselessPeer         = metrics.NewRegisteredMeter("p2p/dials/error/useless", nil)
    57  	dialUnexpectedIdentity  = metrics.NewRegisteredMeter("p2p/dials/error/id/unexpected", nil)
    58  	dialEncHandshakeError   = metrics.NewRegisteredMeter("p2p/dials/error/rlpx/enc", nil)
    59  	dialProtoHandshakeError = metrics.NewRegisteredMeter("p2p/dials/error/rlpx/proto", nil)
    60  )
    61  
    62  func init() {
    63  	if !metrics.Enabled {
    64  		return
    65  	}
    66  
    67  	activePeerGauge = metrics.NewRegisteredGauge("p2p/peers", nil)
    68  	serveMeter = metrics.NewRegisteredMeter("p2p/serves", nil)
    69  	serveSuccessMeter = metrics.NewRegisteredMeter("p2p/serves/success", nil)
    70  	dialMeter = metrics.NewRegisteredMeter("p2p/dials", nil)
    71  	dialSuccessMeter = metrics.NewRegisteredMeter("p2p/dials/success", nil)
    72  	dialConnectionError = metrics.NewRegisteredMeter("p2p/dials/error/connection", nil)
    73  }
    74  
    75  // markDialError matches errors that occur while setting up a dial connection
    76  // to the corresponding meter.
    77  func markDialError(err error) {
    78  	if !metrics.Enabled {
    79  		return
    80  	}
    81  	if err2 := errors.Unwrap(err); err2 != nil {
    82  		err = err2
    83  	}
    84  	switch err {
    85  	case DiscTooManyPeers:
    86  		dialTooManyPeers.Mark(1)
    87  	case DiscAlreadyConnected:
    88  		dialAlreadyConnected.Mark(1)
    89  	case DiscSelf:
    90  		dialSelf.Mark(1)
    91  	case DiscUselessPeer:
    92  		dialUselessPeer.Mark(1)
    93  	case DiscUnexpectedIdentity:
    94  		dialUnexpectedIdentity.Mark(1)
    95  	case errEncHandshakeError:
    96  		dialEncHandshakeError.Mark(1)
    97  	case errProtoHandshakeError:
    98  		dialProtoHandshakeError.Mark(1)
    99  	}
   100  }
   101  
   102  // meteredConn is a wrapper around a net.Conn that meters both the
   103  // inbound and outbound network traffic.
   104  type meteredConn struct {
   105  	net.Conn
   106  }
   107  
   108  // newMeteredConn creates a new metered connection, bumps the ingress or egress
   109  // connection meter and also increases the metered peer count. If the metrics
   110  // system is disabled, function returns the original connection.
   111  func newMeteredConn(conn net.Conn) net.Conn {
   112  	if !metrics.Enabled {
   113  		return conn
   114  	}
   115  	return &meteredConn{Conn: conn}
   116  }
   117  
   118  // Read delegates a network read to the underlying connection, bumping the common
   119  // and the peer ingress traffic meters along the way.
   120  func (c *meteredConn) Read(b []byte) (n int, err error) {
   121  	n, err = c.Conn.Read(b)
   122  	ingressTrafficMeter.Mark(int64(n))
   123  	return n, err
   124  }
   125  
   126  // Write delegates a network write to the underlying connection, bumping the common
   127  // and the peer egress traffic meters along the way.
   128  func (c *meteredConn) Write(b []byte) (n int, err error) {
   129  	n, err = c.Conn.Write(b)
   130  	egressTrafficMeter.Mark(int64(n))
   131  	return n, err
   132  }