github.com/hardtosaygoodbye/go-ethereum@v1.10.16-0.20220122011429-97003b9e6c15/les/metrics.go (about)

     1  // Copyright 2016 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 les
    18  
    19  import (
    20  	"github.com/hardtosaygoodbye/go-ethereum/metrics"
    21  	"github.com/hardtosaygoodbye/go-ethereum/p2p"
    22  )
    23  
    24  var (
    25  	miscInPacketsMeter           = metrics.NewRegisteredMeter("les/misc/in/packets/total", nil)
    26  	miscInTrafficMeter           = metrics.NewRegisteredMeter("les/misc/in/traffic/total", nil)
    27  	miscInHeaderPacketsMeter     = metrics.NewRegisteredMeter("les/misc/in/packets/header", nil)
    28  	miscInHeaderTrafficMeter     = metrics.NewRegisteredMeter("les/misc/in/traffic/header", nil)
    29  	miscInBodyPacketsMeter       = metrics.NewRegisteredMeter("les/misc/in/packets/body", nil)
    30  	miscInBodyTrafficMeter       = metrics.NewRegisteredMeter("les/misc/in/traffic/body", nil)
    31  	miscInCodePacketsMeter       = metrics.NewRegisteredMeter("les/misc/in/packets/code", nil)
    32  	miscInCodeTrafficMeter       = metrics.NewRegisteredMeter("les/misc/in/traffic/code", nil)
    33  	miscInReceiptPacketsMeter    = metrics.NewRegisteredMeter("les/misc/in/packets/receipt", nil)
    34  	miscInReceiptTrafficMeter    = metrics.NewRegisteredMeter("les/misc/in/traffic/receipt", nil)
    35  	miscInTrieProofPacketsMeter  = metrics.NewRegisteredMeter("les/misc/in/packets/proof", nil)
    36  	miscInTrieProofTrafficMeter  = metrics.NewRegisteredMeter("les/misc/in/traffic/proof", nil)
    37  	miscInHelperTriePacketsMeter = metrics.NewRegisteredMeter("les/misc/in/packets/helperTrie", nil)
    38  	miscInHelperTrieTrafficMeter = metrics.NewRegisteredMeter("les/misc/in/traffic/helperTrie", nil)
    39  	miscInTxsPacketsMeter        = metrics.NewRegisteredMeter("les/misc/in/packets/txs", nil)
    40  	miscInTxsTrafficMeter        = metrics.NewRegisteredMeter("les/misc/in/traffic/txs", nil)
    41  	miscInTxStatusPacketsMeter   = metrics.NewRegisteredMeter("les/misc/in/packets/txStatus", nil)
    42  	miscInTxStatusTrafficMeter   = metrics.NewRegisteredMeter("les/misc/in/traffic/txStatus", nil)
    43  
    44  	miscOutPacketsMeter           = metrics.NewRegisteredMeter("les/misc/out/packets/total", nil)
    45  	miscOutTrafficMeter           = metrics.NewRegisteredMeter("les/misc/out/traffic/total", nil)
    46  	miscOutHeaderPacketsMeter     = metrics.NewRegisteredMeter("les/misc/out/packets/header", nil)
    47  	miscOutHeaderTrafficMeter     = metrics.NewRegisteredMeter("les/misc/out/traffic/header", nil)
    48  	miscOutBodyPacketsMeter       = metrics.NewRegisteredMeter("les/misc/out/packets/body", nil)
    49  	miscOutBodyTrafficMeter       = metrics.NewRegisteredMeter("les/misc/out/traffic/body", nil)
    50  	miscOutCodePacketsMeter       = metrics.NewRegisteredMeter("les/misc/out/packets/code", nil)
    51  	miscOutCodeTrafficMeter       = metrics.NewRegisteredMeter("les/misc/out/traffic/code", nil)
    52  	miscOutReceiptPacketsMeter    = metrics.NewRegisteredMeter("les/misc/out/packets/receipt", nil)
    53  	miscOutReceiptTrafficMeter    = metrics.NewRegisteredMeter("les/misc/out/traffic/receipt", nil)
    54  	miscOutTrieProofPacketsMeter  = metrics.NewRegisteredMeter("les/misc/out/packets/proof", nil)
    55  	miscOutTrieProofTrafficMeter  = metrics.NewRegisteredMeter("les/misc/out/traffic/proof", nil)
    56  	miscOutHelperTriePacketsMeter = metrics.NewRegisteredMeter("les/misc/out/packets/helperTrie", nil)
    57  	miscOutHelperTrieTrafficMeter = metrics.NewRegisteredMeter("les/misc/out/traffic/helperTrie", nil)
    58  	miscOutTxsPacketsMeter        = metrics.NewRegisteredMeter("les/misc/out/packets/txs", nil)
    59  	miscOutTxsTrafficMeter        = metrics.NewRegisteredMeter("les/misc/out/traffic/txs", nil)
    60  	miscOutTxStatusPacketsMeter   = metrics.NewRegisteredMeter("les/misc/out/packets/txStatus", nil)
    61  	miscOutTxStatusTrafficMeter   = metrics.NewRegisteredMeter("les/misc/out/traffic/txStatus", nil)
    62  
    63  	miscServingTimeHeaderTimer     = metrics.NewRegisteredTimer("les/misc/serve/header", nil)
    64  	miscServingTimeBodyTimer       = metrics.NewRegisteredTimer("les/misc/serve/body", nil)
    65  	miscServingTimeCodeTimer       = metrics.NewRegisteredTimer("les/misc/serve/code", nil)
    66  	miscServingTimeReceiptTimer    = metrics.NewRegisteredTimer("les/misc/serve/receipt", nil)
    67  	miscServingTimeTrieProofTimer  = metrics.NewRegisteredTimer("les/misc/serve/proof", nil)
    68  	miscServingTimeHelperTrieTimer = metrics.NewRegisteredTimer("les/misc/serve/helperTrie", nil)
    69  	miscServingTimeTxTimer         = metrics.NewRegisteredTimer("les/misc/serve/txs", nil)
    70  	miscServingTimeTxStatusTimer   = metrics.NewRegisteredTimer("les/misc/serve/txStatus", nil)
    71  
    72  	connectionTimer       = metrics.NewRegisteredTimer("les/connection/duration", nil)
    73  	serverConnectionGauge = metrics.NewRegisteredGauge("les/connection/server", nil)
    74  
    75  	totalCapacityGauge   = metrics.NewRegisteredGauge("les/server/totalCapacity", nil)
    76  	totalRechargeGauge   = metrics.NewRegisteredGauge("les/server/totalRecharge", nil)
    77  	blockProcessingTimer = metrics.NewRegisteredTimer("les/server/blockProcessingTime", nil)
    78  
    79  	requestServedMeter               = metrics.NewRegisteredMeter("les/server/req/avgServedTime", nil)
    80  	requestServedTimer               = metrics.NewRegisteredTimer("les/server/req/servedTime", nil)
    81  	requestEstimatedMeter            = metrics.NewRegisteredMeter("les/server/req/avgEstimatedTime", nil)
    82  	requestEstimatedTimer            = metrics.NewRegisteredTimer("les/server/req/estimatedTime", nil)
    83  	relativeCostHistogram            = metrics.NewRegisteredHistogram("les/server/req/relative", nil, metrics.NewExpDecaySample(1028, 0.015))
    84  	relativeCostHeaderHistogram      = metrics.NewRegisteredHistogram("les/server/req/relative/header", nil, metrics.NewExpDecaySample(1028, 0.015))
    85  	relativeCostBodyHistogram        = metrics.NewRegisteredHistogram("les/server/req/relative/body", nil, metrics.NewExpDecaySample(1028, 0.015))
    86  	relativeCostReceiptHistogram     = metrics.NewRegisteredHistogram("les/server/req/relative/receipt", nil, metrics.NewExpDecaySample(1028, 0.015))
    87  	relativeCostCodeHistogram        = metrics.NewRegisteredHistogram("les/server/req/relative/code", nil, metrics.NewExpDecaySample(1028, 0.015))
    88  	relativeCostProofHistogram       = metrics.NewRegisteredHistogram("les/server/req/relative/proof", nil, metrics.NewExpDecaySample(1028, 0.015))
    89  	relativeCostHelperProofHistogram = metrics.NewRegisteredHistogram("les/server/req/relative/helperTrie", nil, metrics.NewExpDecaySample(1028, 0.015))
    90  	relativeCostSendTxHistogram      = metrics.NewRegisteredHistogram("les/server/req/relative/txs", nil, metrics.NewExpDecaySample(1028, 0.015))
    91  	relativeCostTxStatusHistogram    = metrics.NewRegisteredHistogram("les/server/req/relative/txStatus", nil, metrics.NewExpDecaySample(1028, 0.015))
    92  
    93  	globalFactorGauge    = metrics.NewRegisteredGauge("les/server/globalFactor", nil)
    94  	recentServedGauge    = metrics.NewRegisteredGauge("les/server/recentRequestServed", nil)
    95  	recentEstimatedGauge = metrics.NewRegisteredGauge("les/server/recentRequestEstimated", nil)
    96  	sqServedGauge        = metrics.NewRegisteredGauge("les/server/servingQueue/served", nil)
    97  	sqQueuedGauge        = metrics.NewRegisteredGauge("les/server/servingQueue/queued", nil)
    98  
    99  	clientFreezeMeter = metrics.NewRegisteredMeter("les/server/clientEvent/freeze", nil)
   100  	clientErrorMeter  = metrics.NewRegisteredMeter("les/server/clientEvent/error", nil)
   101  
   102  	requestRTT       = metrics.NewRegisteredTimer("les/client/req/rtt", nil)
   103  	requestSendDelay = metrics.NewRegisteredTimer("les/client/req/sendDelay", nil)
   104  
   105  	serverSelectableGauge = metrics.NewRegisteredGauge("les/client/serverPool/selectable", nil)
   106  	serverDialedMeter     = metrics.NewRegisteredMeter("les/client/serverPool/dialed", nil)
   107  	serverConnectedGauge  = metrics.NewRegisteredGauge("les/client/serverPool/connected", nil)
   108  	sessionValueMeter     = metrics.NewRegisteredMeter("les/client/serverPool/sessionValue", nil)
   109  	totalValueGauge       = metrics.NewRegisteredGauge("les/client/serverPool/totalValue", nil)
   110  	suggestedTimeoutGauge = metrics.NewRegisteredGauge("les/client/serverPool/timeout", nil)
   111  )
   112  
   113  // meteredMsgReadWriter is a wrapper around a p2p.MsgReadWriter, capable of
   114  // accumulating the above defined metrics based on the data stream contents.
   115  type meteredMsgReadWriter struct {
   116  	p2p.MsgReadWriter     // Wrapped message stream to meter
   117  	version           int // Protocol version to select correct meters
   118  }
   119  
   120  // newMeteredMsgWriter wraps a p2p MsgReadWriter with metering support. If the
   121  // metrics system is disabled, this function returns the original object.
   122  func newMeteredMsgWriter(rw p2p.MsgReadWriter, version int) p2p.MsgReadWriter {
   123  	if !metrics.Enabled {
   124  		return rw
   125  	}
   126  	return &meteredMsgReadWriter{MsgReadWriter: rw, version: version}
   127  }
   128  
   129  func (rw *meteredMsgReadWriter) ReadMsg() (p2p.Msg, error) {
   130  	// Read the message and short circuit in case of an error
   131  	msg, err := rw.MsgReadWriter.ReadMsg()
   132  	if err != nil {
   133  		return msg, err
   134  	}
   135  	// Account for the data traffic
   136  	packets, traffic := miscInPacketsMeter, miscInTrafficMeter
   137  	packets.Mark(1)
   138  	traffic.Mark(int64(msg.Size))
   139  
   140  	return msg, err
   141  }
   142  
   143  func (rw *meteredMsgReadWriter) WriteMsg(msg p2p.Msg) error {
   144  	// Account for the data traffic
   145  	packets, traffic := miscOutPacketsMeter, miscOutTrafficMeter
   146  	packets.Mark(1)
   147  	traffic.Mark(int64(msg.Size))
   148  
   149  	// Send the packet to the p2p layer
   150  	return rw.MsgReadWriter.WriteMsg(msg)
   151  }