github.com/klaytn/klaytn@v1.12.1/node/sc/metrics.go (about)

     1  // Modifications Copyright 2019 The klaytn Authors
     2  // Copyright 2015 The go-ethereum Authors
     3  // This file is part of go-ethereum.
     4  //
     5  // The go-ethereum library is free software: you can redistribute it and/or modify
     6  // it under the terms of the GNU Lesser General Public License as published by
     7  // the Free Software Foundation, either version 3 of the License, or
     8  // (at your option) any later version.
     9  //
    10  // The go-ethereum library is distributed in the hope that it will be useful,
    11  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    12  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    13  // GNU Lesser General Public License for more details.
    14  //
    15  // You should have received a copy of the GNU Lesser General Public License
    16  // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
    17  //
    18  // This file is derived from eth/metrics.go (2018/06/04).
    19  // Modified and improved for the klaytn development.
    20  
    21  package sc
    22  
    23  import (
    24  	metricutils "github.com/klaytn/klaytn/metrics/utils"
    25  	"github.com/klaytn/klaytn/networks/p2p"
    26  	"github.com/rcrowley/go-metrics"
    27  )
    28  
    29  var (
    30  	propTxnInPacketsMeter     = metrics.NewRegisteredMeter("klay/bridge/prop/txns/in/packets", nil)
    31  	propTxnInTrafficMeter     = metrics.NewRegisteredMeter("klay/bridge/prop/txns/in/traffic", nil)
    32  	propTxnOutPacketsMeter    = metrics.NewRegisteredMeter("klay/bridge/prop/txns/out/packets", nil)
    33  	propTxnOutTrafficMeter    = metrics.NewRegisteredMeter("klay/bridge/prop/txns/out/traffic", nil)
    34  	reqReceiptInPacketsMeter  = metrics.NewRegisteredMeter("klay/bridge/req/receipts/in/packets", nil)
    35  	reqReceiptInTrafficMeter  = metrics.NewRegisteredMeter("klay/bridge/req/receipts/in/traffic", nil)
    36  	reqReceiptOutPacketsMeter = metrics.NewRegisteredMeter("klay/bridge/req/receipts/out/packets", nil)
    37  	reqReceiptOutTrafficMeter = metrics.NewRegisteredMeter("klay/bridge/req/receipts/out/traffic", nil)
    38  	miscInPacketsMeter        = metrics.NewRegisteredMeter("klay/bridge/misc/in/packets", nil)
    39  	miscInTrafficMeter        = metrics.NewRegisteredMeter("klay/bridge/misc/in/traffic", nil)
    40  	miscOutPacketsMeter       = metrics.NewRegisteredMeter("klay/bridge/misc/out/packets", nil)
    41  	miscOutTrafficMeter       = metrics.NewRegisteredMeter("klay/bridge/misc/out/traffic", nil)
    42  
    43  	vtRequestEventMeter = metrics.NewRegisteredMeter("klay/bridge/vt/event/request", nil)
    44  	vtHandleEventMeter  = metrics.NewRegisteredMeter("klay/bridge/vt/event/handle", nil)
    45  
    46  	vtRecoveredRequestEventMeter = metrics.NewRegisteredMeter("klay/bridge/vt/event/recovery/request", nil)
    47  	vtPendingRequestEventCounter = metrics.NewRegisteredCounter("klay/bridge/vt/event/pend/request", nil)
    48  
    49  	vtRequestNonceCount     = metrics.NewRegisteredCounter("klay/bridge/vt/nonce/request", nil)
    50  	vtHandleNonceCount      = metrics.NewRegisteredCounter("klay/bridge/vt/nonce/handle", nil)
    51  	vtLowerHandleNonceCount = metrics.NewRegisteredCounter("klay/bridge/vt/nonce/lowerhandle", nil)
    52  
    53  	lastAnchoredBlockNumGauge = metrics.NewRegisteredGauge("klay/bridge/anchroing/blocknumber", nil)
    54  
    55  	// TODO-Klaytn-Servicechain need to add below metrics
    56  	// txReceiveCounter     = metrics.NewRegisteredCounter("klay/bridge/tx/recv/counter", nil)
    57  	// txResendCounter      = metrics.NewRegisteredCounter("klay/bridge/tx/resend/counter", nil)
    58  	// txResendGauge        = metrics.NewRegisteredGauge("klay/bridge/tx/resend/gauge", nil)
    59  	// txSendCounter        = metrics.NewRegisteredCounter("klay/bridge/tx/send/counter", nil)
    60  	// txResendRoutineGauge = metrics.NewRegisteredGauge("klay/bridge/tx/resend/routine/gauge", nil)
    61  )
    62  
    63  // meteredMsgReadWriter is a wrapper around a p2p.MsgReadWriter, capable of
    64  // accumulating the above defined metrics based on the data stream contents.
    65  type meteredMsgReadWriter struct {
    66  	p2p.MsgReadWriter     // Wrapped message stream to meter
    67  	version           int // Protocol version to select correct meters
    68  }
    69  
    70  // newMeteredMsgWriter wraps a p2p MsgReadWriter with metering support. If the
    71  // metrics system is disabled, this function returns the original object.
    72  func newMeteredMsgWriter(rw p2p.MsgReadWriter) p2p.MsgReadWriter {
    73  	if !metricutils.Enabled {
    74  		return rw
    75  	}
    76  	return &meteredMsgReadWriter{MsgReadWriter: rw}
    77  }
    78  
    79  // Init sets the protocol version used by the stream to know which meters to
    80  // increment in case of overlapping message ids between protocol versions.
    81  func (rw *meteredMsgReadWriter) Init(version int) {
    82  	rw.version = version
    83  }
    84  
    85  func (rw *meteredMsgReadWriter) ReadMsg() (p2p.Msg, error) {
    86  	// Read the message and short circuit in case of an error
    87  	msg, err := rw.MsgReadWriter.ReadMsg()
    88  	if err != nil {
    89  		return msg, err
    90  	}
    91  	// Account for the data traffic
    92  	packets, traffic := miscInPacketsMeter, miscInTrafficMeter
    93  	switch {
    94  	case msg.Code == ServiceChainTxsMsg: // If version check is needed, add `rw.version >= klay63`.
    95  		packets, traffic = propTxnInPacketsMeter, propTxnInTrafficMeter
    96  	case msg.Code == ServiceChainReceiptResponseMsg:
    97  		packets, traffic = reqReceiptInPacketsMeter, reqReceiptInTrafficMeter
    98  	case msg.Code == ServiceChainReceiptRequestMsg:
    99  	}
   100  	packets.Mark(1)
   101  	traffic.Mark(int64(msg.Size))
   102  
   103  	return msg, err
   104  }
   105  
   106  func (rw *meteredMsgReadWriter) WriteMsg(msg p2p.Msg) error {
   107  	// Account for the data traffic
   108  	packets, traffic := miscOutPacketsMeter, miscOutTrafficMeter
   109  	switch {
   110  	case msg.Code == ServiceChainTxsMsg: // If version check is needed, add `rw.version >= klay63`.
   111  		packets, traffic = propTxnOutPacketsMeter, propTxnOutTrafficMeter
   112  	case msg.Code == ServiceChainReceiptResponseMsg:
   113  		packets, traffic = reqReceiptOutPacketsMeter, reqReceiptOutTrafficMeter
   114  	}
   115  	packets.Mark(1)
   116  	traffic.Mark(int64(msg.Size))
   117  
   118  	// Send the packet to the p2p layer
   119  	return rw.MsgReadWriter.WriteMsg(msg)
   120  }