github.com/core-coin/go-core/v2@v2.1.9/les/metrics.go (about)

     1  // Copyright 2016 by the Authors
     2  // This file is part of the go-core library.
     3  //
     4  // The go-core 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-core 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-core library. If not, see <http://www.gnu.org/licenses/>.
    16  
    17  package les
    18  
    19  import (
    20  	"github.com/core-coin/go-core/v2/metrics"
    21  	"github.com/core-coin/go-core/v2/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  	clientConnectionGauge = metrics.NewRegisteredGauge("les/connection/client", nil)
    75  
    76  	totalCapacityGauge   = metrics.NewRegisteredGauge("les/server/totalCapacity", nil)
    77  	totalRechargeGauge   = metrics.NewRegisteredGauge("les/server/totalRecharge", nil)
    78  	totalConnectedGauge  = metrics.NewRegisteredGauge("les/server/totalConnected", nil)
    79  	blockProcessingTimer = metrics.NewRegisteredTimer("les/server/blockProcessingTime", nil)
    80  
    81  	requestServedMeter               = metrics.NewRegisteredMeter("les/server/req/avgServedTime", nil)
    82  	requestServedTimer               = metrics.NewRegisteredTimer("les/server/req/servedTime", nil)
    83  	requestEstimatedMeter            = metrics.NewRegisteredMeter("les/server/req/avgEstimatedTime", nil)
    84  	requestEstimatedTimer            = metrics.NewRegisteredTimer("les/server/req/estimatedTime", nil)
    85  	relativeCostHistogram            = metrics.NewRegisteredHistogram("les/server/req/relative", nil, metrics.NewExpDecaySample(1028, 0.015))
    86  	relativeCostHeaderHistogram      = metrics.NewRegisteredHistogram("les/server/req/relative/header", nil, metrics.NewExpDecaySample(1028, 0.015))
    87  	relativeCostBodyHistogram        = metrics.NewRegisteredHistogram("les/server/req/relative/body", nil, metrics.NewExpDecaySample(1028, 0.015))
    88  	relativeCostReceiptHistogram     = metrics.NewRegisteredHistogram("les/server/req/relative/receipt", nil, metrics.NewExpDecaySample(1028, 0.015))
    89  	relativeCostCodeHistogram        = metrics.NewRegisteredHistogram("les/server/req/relative/code", nil, metrics.NewExpDecaySample(1028, 0.015))
    90  	relativeCostProofHistogram       = metrics.NewRegisteredHistogram("les/server/req/relative/proof", nil, metrics.NewExpDecaySample(1028, 0.015))
    91  	relativeCostHelperProofHistogram = metrics.NewRegisteredHistogram("les/server/req/relative/helperTrie", nil, metrics.NewExpDecaySample(1028, 0.015))
    92  	relativeCostSendTxHistogram      = metrics.NewRegisteredHistogram("les/server/req/relative/txs", nil, metrics.NewExpDecaySample(1028, 0.015))
    93  	relativeCostTxStatusHistogram    = metrics.NewRegisteredHistogram("les/server/req/relative/txStatus", nil, metrics.NewExpDecaySample(1028, 0.015))
    94  
    95  	globalFactorGauge    = metrics.NewRegisteredGauge("les/server/globalFactor", nil)
    96  	recentServedGauge    = metrics.NewRegisteredGauge("les/server/recentRequestServed", nil)
    97  	recentEstimatedGauge = metrics.NewRegisteredGauge("les/server/recentRequestEstimated", nil)
    98  	sqServedGauge        = metrics.NewRegisteredGauge("les/server/servingQueue/served", nil)
    99  	sqQueuedGauge        = metrics.NewRegisteredGauge("les/server/servingQueue/queued", nil)
   100  
   101  	clientConnectedMeter    = metrics.NewRegisteredMeter("les/server/clientEvent/connected", nil)
   102  	clientActivatedMeter    = metrics.NewRegisteredMeter("les/server/clientEvent/activated", nil)
   103  	clientDeactivatedMeter  = metrics.NewRegisteredMeter("les/server/clientEvent/deactivated", nil)
   104  	clientDisconnectedMeter = metrics.NewRegisteredMeter("les/server/clientEvent/disconnected", nil)
   105  	clientFreezeMeter       = metrics.NewRegisteredMeter("les/server/clientEvent/freeze", nil)
   106  	clientErrorMeter        = metrics.NewRegisteredMeter("les/server/clientEvent/error", nil)
   107  
   108  	requestRTT       = metrics.NewRegisteredTimer("les/client/req/rtt", nil)
   109  	requestSendDelay = metrics.NewRegisteredTimer("les/client/req/sendDelay", nil)
   110  
   111  	serverSelectableGauge = metrics.NewRegisteredGauge("les/client/serverPool/selectable", nil)
   112  	serverDialedMeter     = metrics.NewRegisteredMeter("les/client/serverPool/dialed", nil)
   113  	serverConnectedGauge  = metrics.NewRegisteredGauge("les/client/serverPool/connected", nil)
   114  	sessionValueMeter     = metrics.NewRegisteredMeter("les/client/serverPool/sessionValue", nil)
   115  	totalValueGauge       = metrics.NewRegisteredGauge("les/client/serverPool/totalValue", nil)
   116  	suggestedTimeoutGauge = metrics.NewRegisteredGauge("les/client/serverPool/timeout", nil)
   117  )
   118  
   119  // meteredMsgReadWriter is a wrapper around a p2p.MsgReadWriter, capable of
   120  // accumulating the above defined metrics based on the data stream contents.
   121  type meteredMsgReadWriter struct {
   122  	p2p.MsgReadWriter     // Wrapped message stream to meter
   123  	version           int // Protocol version to select correct meters
   124  }
   125  
   126  // newMeteredMsgWriter wraps a p2p MsgReadWriter with metering support. If the
   127  // metrics system is disabled, this function returns the original object.
   128  func newMeteredMsgWriter(rw p2p.MsgReadWriter, version int) p2p.MsgReadWriter {
   129  	if !metrics.Enabled {
   130  		return rw
   131  	}
   132  	return &meteredMsgReadWriter{MsgReadWriter: rw, version: version}
   133  }
   134  
   135  func (rw *meteredMsgReadWriter) ReadMsg() (p2p.Msg, error) {
   136  	// Read the message and short circuit in case of an error
   137  	msg, err := rw.MsgReadWriter.ReadMsg()
   138  	if err != nil {
   139  		return msg, err
   140  	}
   141  	// Account for the data traffic
   142  	packets, traffic := miscInPacketsMeter, miscInTrafficMeter
   143  	packets.Mark(1)
   144  	traffic.Mark(int64(msg.Size))
   145  
   146  	return msg, err
   147  }
   148  
   149  func (rw *meteredMsgReadWriter) WriteMsg(msg p2p.Msg) error {
   150  	// Account for the data traffic
   151  	packets, traffic := miscOutPacketsMeter, miscOutTrafficMeter
   152  	packets.Mark(1)
   153  	traffic.Mark(int64(msg.Size))
   154  
   155  	// Send the packet to the p2p layer
   156  	return rw.MsgReadWriter.WriteMsg(msg)
   157  }