github.com/neatio-net/neatio@v1.7.3-0.20231114194659-f4d7a2226baa/neatptc/metrics.go (about)

     1  package neatptc
     2  
     3  import (
     4  	"github.com/neatio-net/neatio/chain/consensus"
     5  	"github.com/neatio-net/neatio/network/p2p"
     6  	"github.com/neatio-net/neatio/utilities/metrics"
     7  )
     8  
     9  var (
    10  	propTxnInPacketsMeter     = metrics.NewRegisteredMeter("eth/prop/txns/in/packets", nil)
    11  	propTxnInTrafficMeter     = metrics.NewRegisteredMeter("eth/prop/txns/in/traffic", nil)
    12  	propTxnOutPacketsMeter    = metrics.NewRegisteredMeter("eth/prop/txns/out/packets", nil)
    13  	propTxnOutTrafficMeter    = metrics.NewRegisteredMeter("eth/prop/txns/out/traffic", nil)
    14  	propHashInPacketsMeter    = metrics.NewRegisteredMeter("eth/prop/hashes/in/packets", nil)
    15  	propHashInTrafficMeter    = metrics.NewRegisteredMeter("eth/prop/hashes/in/traffic", nil)
    16  	propHashOutPacketsMeter   = metrics.NewRegisteredMeter("eth/prop/hashes/out/packets", nil)
    17  	propHashOutTrafficMeter   = metrics.NewRegisteredMeter("eth/prop/hashes/out/traffic", nil)
    18  	propBlockInPacketsMeter   = metrics.NewRegisteredMeter("eth/prop/blocks/in/packets", nil)
    19  	propBlockInTrafficMeter   = metrics.NewRegisteredMeter("eth/prop/blocks/in/traffic", nil)
    20  	propBlockOutPacketsMeter  = metrics.NewRegisteredMeter("eth/prop/blocks/out/packets", nil)
    21  	propBlockOutTrafficMeter  = metrics.NewRegisteredMeter("eth/prop/blocks/out/traffic", nil)
    22  	reqHeaderInPacketsMeter   = metrics.NewRegisteredMeter("eth/req/headers/in/packets", nil)
    23  	reqHeaderInTrafficMeter   = metrics.NewRegisteredMeter("eth/req/headers/in/traffic", nil)
    24  	reqHeaderOutPacketsMeter  = metrics.NewRegisteredMeter("eth/req/headers/out/packets", nil)
    25  	reqHeaderOutTrafficMeter  = metrics.NewRegisteredMeter("eth/req/headers/out/traffic", nil)
    26  	reqBodyInPacketsMeter     = metrics.NewRegisteredMeter("eth/req/bodies/in/packets", nil)
    27  	reqBodyInTrafficMeter     = metrics.NewRegisteredMeter("eth/req/bodies/in/traffic", nil)
    28  	reqBodyOutPacketsMeter    = metrics.NewRegisteredMeter("eth/req/bodies/out/packets", nil)
    29  	reqBodyOutTrafficMeter    = metrics.NewRegisteredMeter("eth/req/bodies/out/traffic", nil)
    30  	reqStateInPacketsMeter    = metrics.NewRegisteredMeter("eth/req/states/in/packets", nil)
    31  	reqStateInTrafficMeter    = metrics.NewRegisteredMeter("eth/req/states/in/traffic", nil)
    32  	reqStateOutPacketsMeter   = metrics.NewRegisteredMeter("eth/req/states/out/packets", nil)
    33  	reqStateOutTrafficMeter   = metrics.NewRegisteredMeter("eth/req/states/out/traffic", nil)
    34  	reqReceiptInPacketsMeter  = metrics.NewRegisteredMeter("eth/req/receipts/in/packets", nil)
    35  	reqReceiptInTrafficMeter  = metrics.NewRegisteredMeter("eth/req/receipts/in/traffic", nil)
    36  	reqReceiptOutPacketsMeter = metrics.NewRegisteredMeter("eth/req/receipts/out/packets", nil)
    37  	reqReceiptOutTrafficMeter = metrics.NewRegisteredMeter("eth/req/receipts/out/traffic", nil)
    38  	miscInPacketsMeter        = metrics.NewRegisteredMeter("eth/misc/in/packets", nil)
    39  	miscInTrafficMeter        = metrics.NewRegisteredMeter("eth/misc/in/traffic", nil)
    40  	miscOutPacketsMeter       = metrics.NewRegisteredMeter("eth/misc/out/packets", nil)
    41  	miscOutTrafficMeter       = metrics.NewRegisteredMeter("eth/misc/out/traffic", nil)
    42  )
    43  
    44  type meteredMsgReadWriter struct {
    45  	p2p.MsgReadWriter
    46  	version int
    47  }
    48  
    49  func newMeteredMsgWriter(rw p2p.MsgReadWriter) p2p.MsgReadWriter {
    50  	if !metrics.Enabled {
    51  		return rw
    52  	}
    53  	return &meteredMsgReadWriter{MsgReadWriter: rw}
    54  }
    55  
    56  func (rw *meteredMsgReadWriter) Init(version int) {
    57  	rw.version = version
    58  }
    59  
    60  func (rw *meteredMsgReadWriter) ReadMsg() (p2p.Msg, error) {
    61  
    62  	msg, err := rw.MsgReadWriter.ReadMsg()
    63  	if err != nil {
    64  		return msg, err
    65  	}
    66  
    67  	packets, traffic := miscInPacketsMeter, miscInTrafficMeter
    68  	switch {
    69  	case msg.Code == BlockHeadersMsg:
    70  		packets, traffic = reqHeaderInPacketsMeter, reqHeaderInTrafficMeter
    71  	case msg.Code == BlockBodiesMsg:
    72  		packets, traffic = reqBodyInPacketsMeter, reqBodyInTrafficMeter
    73  
    74  	case rw.version >= consensus.Eth63 && msg.Code == NodeDataMsg:
    75  		packets, traffic = reqStateInPacketsMeter, reqStateInTrafficMeter
    76  	case rw.version >= consensus.Eth63 && msg.Code == ReceiptsMsg:
    77  		packets, traffic = reqReceiptInPacketsMeter, reqReceiptInTrafficMeter
    78  
    79  	case msg.Code == NewBlockHashesMsg:
    80  		packets, traffic = propHashInPacketsMeter, propHashInTrafficMeter
    81  	case msg.Code == NewBlockMsg:
    82  		packets, traffic = propBlockInPacketsMeter, propBlockInTrafficMeter
    83  	case msg.Code == TxMsg:
    84  		packets, traffic = propTxnInPacketsMeter, propTxnInTrafficMeter
    85  	}
    86  	packets.Mark(1)
    87  	traffic.Mark(int64(msg.Size))
    88  
    89  	return msg, err
    90  }
    91  
    92  func (rw *meteredMsgReadWriter) WriteMsg(msg p2p.Msg) error {
    93  
    94  	packets, traffic := miscOutPacketsMeter, miscOutTrafficMeter
    95  	switch {
    96  	case msg.Code == BlockHeadersMsg:
    97  		packets, traffic = reqHeaderOutPacketsMeter, reqHeaderOutTrafficMeter
    98  	case msg.Code == BlockBodiesMsg:
    99  		packets, traffic = reqBodyOutPacketsMeter, reqBodyOutTrafficMeter
   100  
   101  	case rw.version >= consensus.Eth63 && msg.Code == NodeDataMsg:
   102  		packets, traffic = reqStateOutPacketsMeter, reqStateOutTrafficMeter
   103  	case rw.version >= consensus.Eth63 && msg.Code == ReceiptsMsg:
   104  		packets, traffic = reqReceiptOutPacketsMeter, reqReceiptOutTrafficMeter
   105  
   106  	case msg.Code == NewBlockHashesMsg:
   107  		packets, traffic = propHashOutPacketsMeter, propHashOutTrafficMeter
   108  	case msg.Code == NewBlockMsg:
   109  		packets, traffic = propBlockOutPacketsMeter, propBlockOutTrafficMeter
   110  	case msg.Code == TxMsg:
   111  		packets, traffic = propTxnOutPacketsMeter, propTxnOutTrafficMeter
   112  	}
   113  	packets.Mark(1)
   114  	traffic.Mark(int64(msg.Size))
   115  
   116  	return rw.MsgReadWriter.WriteMsg(msg)
   117  }