github.com/intfoundation/intchain@v0.0.0-20220727031208-4316ad31ca73/intprotocol/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 package intprotocol 18 19 import ( 20 "github.com/intfoundation/intchain/consensus" 21 "github.com/intfoundation/intchain/metrics" 22 "github.com/intfoundation/intchain/p2p" 23 ) 24 25 var ( 26 propTxnInPacketsMeter = metrics.NewRegisteredMeter("eth/prop/txns/in/packets", nil) 27 propTxnInTrafficMeter = metrics.NewRegisteredMeter("eth/prop/txns/in/traffic", nil) 28 propTxnOutPacketsMeter = metrics.NewRegisteredMeter("eth/prop/txns/out/packets", nil) 29 propTxnOutTrafficMeter = metrics.NewRegisteredMeter("eth/prop/txns/out/traffic", nil) 30 propHashInPacketsMeter = metrics.NewRegisteredMeter("eth/prop/hashes/in/packets", nil) 31 propHashInTrafficMeter = metrics.NewRegisteredMeter("eth/prop/hashes/in/traffic", nil) 32 propHashOutPacketsMeter = metrics.NewRegisteredMeter("eth/prop/hashes/out/packets", nil) 33 propHashOutTrafficMeter = metrics.NewRegisteredMeter("eth/prop/hashes/out/traffic", nil) 34 propBlockInPacketsMeter = metrics.NewRegisteredMeter("eth/prop/blocks/in/packets", nil) 35 propBlockInTrafficMeter = metrics.NewRegisteredMeter("eth/prop/blocks/in/traffic", nil) 36 propBlockOutPacketsMeter = metrics.NewRegisteredMeter("eth/prop/blocks/out/packets", nil) 37 propBlockOutTrafficMeter = metrics.NewRegisteredMeter("eth/prop/blocks/out/traffic", nil) 38 reqHeaderInPacketsMeter = metrics.NewRegisteredMeter("eth/req/headers/in/packets", nil) 39 reqHeaderInTrafficMeter = metrics.NewRegisteredMeter("eth/req/headers/in/traffic", nil) 40 reqHeaderOutPacketsMeter = metrics.NewRegisteredMeter("eth/req/headers/out/packets", nil) 41 reqHeaderOutTrafficMeter = metrics.NewRegisteredMeter("eth/req/headers/out/traffic", nil) 42 reqBodyInPacketsMeter = metrics.NewRegisteredMeter("eth/req/bodies/in/packets", nil) 43 reqBodyInTrafficMeter = metrics.NewRegisteredMeter("eth/req/bodies/in/traffic", nil) 44 reqBodyOutPacketsMeter = metrics.NewRegisteredMeter("eth/req/bodies/out/packets", nil) 45 reqBodyOutTrafficMeter = metrics.NewRegisteredMeter("eth/req/bodies/out/traffic", nil) 46 reqStateInPacketsMeter = metrics.NewRegisteredMeter("eth/req/states/in/packets", nil) 47 reqStateInTrafficMeter = metrics.NewRegisteredMeter("eth/req/states/in/traffic", nil) 48 reqStateOutPacketsMeter = metrics.NewRegisteredMeter("eth/req/states/out/packets", nil) 49 reqStateOutTrafficMeter = metrics.NewRegisteredMeter("eth/req/states/out/traffic", nil) 50 reqReceiptInPacketsMeter = metrics.NewRegisteredMeter("eth/req/receipts/in/packets", nil) 51 reqReceiptInTrafficMeter = metrics.NewRegisteredMeter("eth/req/receipts/in/traffic", nil) 52 reqReceiptOutPacketsMeter = metrics.NewRegisteredMeter("eth/req/receipts/out/packets", nil) 53 reqReceiptOutTrafficMeter = metrics.NewRegisteredMeter("eth/req/receipts/out/traffic", nil) 54 miscInPacketsMeter = metrics.NewRegisteredMeter("eth/misc/in/packets", nil) 55 miscInTrafficMeter = metrics.NewRegisteredMeter("eth/misc/in/traffic", nil) 56 miscOutPacketsMeter = metrics.NewRegisteredMeter("eth/misc/out/packets", nil) 57 miscOutTrafficMeter = metrics.NewRegisteredMeter("eth/misc/out/traffic", nil) 58 ) 59 60 // meteredMsgReadWriter is a wrapper around a p2p.MsgReadWriter, capable of 61 // accumulating the above defined metrics based on the data stream contents. 62 type meteredMsgReadWriter struct { 63 p2p.MsgReadWriter // Wrapped message stream to meter 64 version int // Protocol version to select correct meters 65 } 66 67 // newMeteredMsgWriter wraps a p2p MsgReadWriter with metering support. If the 68 // metrics system is disabled, this function returns the original object. 69 func newMeteredMsgWriter(rw p2p.MsgReadWriter) p2p.MsgReadWriter { 70 if !metrics.Enabled { 71 return rw 72 } 73 return &meteredMsgReadWriter{MsgReadWriter: rw} 74 } 75 76 // Init sets the protocol version used by the stream to know which meters to 77 // increment in case of overlapping message ids between protocol versions. 78 func (rw *meteredMsgReadWriter) Init(version int) { 79 rw.version = version 80 } 81 82 func (rw *meteredMsgReadWriter) ReadMsg() (p2p.Msg, error) { 83 // Read the message and short circuit in case of an error 84 msg, err := rw.MsgReadWriter.ReadMsg() 85 if err != nil { 86 return msg, err 87 } 88 // Account for the data traffic 89 packets, traffic := miscInPacketsMeter, miscInTrafficMeter 90 switch { 91 case msg.Code == BlockHeadersMsg: 92 packets, traffic = reqHeaderInPacketsMeter, reqHeaderInTrafficMeter 93 case msg.Code == BlockBodiesMsg: 94 packets, traffic = reqBodyInPacketsMeter, reqBodyInTrafficMeter 95 96 case rw.version >= consensus.Eth63 && msg.Code == NodeDataMsg: 97 packets, traffic = reqStateInPacketsMeter, reqStateInTrafficMeter 98 case rw.version >= consensus.Eth63 && msg.Code == ReceiptsMsg: 99 packets, traffic = reqReceiptInPacketsMeter, reqReceiptInTrafficMeter 100 101 case msg.Code == NewBlockHashesMsg: 102 packets, traffic = propHashInPacketsMeter, propHashInTrafficMeter 103 case msg.Code == NewBlockMsg: 104 packets, traffic = propBlockInPacketsMeter, propBlockInTrafficMeter 105 case msg.Code == TxMsg: 106 packets, traffic = propTxnInPacketsMeter, propTxnInTrafficMeter 107 } 108 packets.Mark(1) 109 traffic.Mark(int64(msg.Size)) 110 111 return msg, err 112 } 113 114 func (rw *meteredMsgReadWriter) WriteMsg(msg p2p.Msg) error { 115 // Account for the data traffic 116 packets, traffic := miscOutPacketsMeter, miscOutTrafficMeter 117 switch { 118 case msg.Code == BlockHeadersMsg: 119 packets, traffic = reqHeaderOutPacketsMeter, reqHeaderOutTrafficMeter 120 case msg.Code == BlockBodiesMsg: 121 packets, traffic = reqBodyOutPacketsMeter, reqBodyOutTrafficMeter 122 123 case rw.version >= consensus.Eth63 && msg.Code == NodeDataMsg: 124 packets, traffic = reqStateOutPacketsMeter, reqStateOutTrafficMeter 125 case rw.version >= consensus.Eth63 && msg.Code == ReceiptsMsg: 126 packets, traffic = reqReceiptOutPacketsMeter, reqReceiptOutTrafficMeter 127 128 case msg.Code == NewBlockHashesMsg: 129 packets, traffic = propHashOutPacketsMeter, propHashOutTrafficMeter 130 case msg.Code == NewBlockMsg: 131 packets, traffic = propBlockOutPacketsMeter, propBlockOutTrafficMeter 132 case msg.Code == TxMsg: 133 packets, traffic = propTxnOutPacketsMeter, propTxnOutTrafficMeter 134 } 135 packets.Mark(1) 136 traffic.Mark(int64(msg.Size)) 137 138 // Send the packet to the p2p layer 139 return rw.MsgReadWriter.WriteMsg(msg) 140 }