github.com/klaytn/klaytn@v1.12.1/node/cn/metrics.go (about) 1 // Modifications Copyright 2018 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 cn 22 23 import ( 24 "github.com/klaytn/klaytn/consensus/istanbul/backend" 25 metricutils "github.com/klaytn/klaytn/metrics/utils" 26 "github.com/klaytn/klaytn/networks/p2p" 27 "github.com/rcrowley/go-metrics" 28 ) 29 30 var ( 31 propTxnInPacketsMeter = metrics.NewRegisteredMeter("klay/prop/txns/in/packets", nil) 32 propTxnInTrafficMeter = metrics.NewRegisteredMeter("klay/prop/txns/in/traffic", nil) 33 propTxnOutPacketsMeter = metrics.NewRegisteredMeter("klay/prop/txns/out/packets", nil) 34 propTxnOutTrafficMeter = metrics.NewRegisteredMeter("klay/prop/txns/out/traffic", nil) 35 propTxPeersGauge = metrics.NewRegisteredGauge("klay/prop/tx/peers/gauge", nil) 36 propHashInPacketsMeter = metrics.NewRegisteredMeter("klay/prop/hashes/in/packets", nil) 37 propHashInTrafficMeter = metrics.NewRegisteredMeter("klay/prop/hashes/in/traffic", nil) 38 propHashOutPacketsMeter = metrics.NewRegisteredMeter("klay/prop/hashes/out/packets", nil) 39 propHashOutTrafficMeter = metrics.NewRegisteredMeter("klay/prop/hashes/out/traffic", nil) 40 propBlockInPacketsMeter = metrics.NewRegisteredMeter("klay/prop/blocks/in/packets", nil) 41 propBlockInTrafficMeter = metrics.NewRegisteredMeter("klay/prop/blocks/in/traffic", nil) 42 propBlockOutPacketsMeter = metrics.NewRegisteredMeter("klay/prop/blocks/out/packets", nil) 43 propBlockOutTrafficMeter = metrics.NewRegisteredMeter("klay/prop/blocks/out/traffic", nil) 44 reqHeaderInPacketsMeter = metrics.NewRegisteredMeter("klay/req/headers/in/packets", nil) 45 reqHeaderInTrafficMeter = metrics.NewRegisteredMeter("klay/req/headers/in/traffic", nil) 46 reqHeaderOutPacketsMeter = metrics.NewRegisteredMeter("klay/req/headers/out/packets", nil) 47 reqHeaderOutTrafficMeter = metrics.NewRegisteredMeter("klay/req/headers/out/traffic", nil) 48 reqBodyInPacketsMeter = metrics.NewRegisteredMeter("klay/req/bodies/in/packets", nil) 49 reqBodyInTrafficMeter = metrics.NewRegisteredMeter("klay/req/bodies/in/traffic", nil) 50 reqBodyOutPacketsMeter = metrics.NewRegisteredMeter("klay/req/bodies/out/packets", nil) 51 reqBodyOutTrafficMeter = metrics.NewRegisteredMeter("klay/req/bodies/out/traffic", nil) 52 reqStateInPacketsMeter = metrics.NewRegisteredMeter("klay/req/states/in/packets", nil) 53 reqStateInTrafficMeter = metrics.NewRegisteredMeter("klay/req/states/in/traffic", nil) 54 reqStateOutPacketsMeter = metrics.NewRegisteredMeter("klay/req/states/out/packets", nil) 55 reqStateOutTrafficMeter = metrics.NewRegisteredMeter("klay/req/states/out/traffic", nil) 56 reqReceiptInPacketsMeter = metrics.NewRegisteredMeter("klay/req/receipts/in/packets", nil) 57 reqReceiptInTrafficMeter = metrics.NewRegisteredMeter("klay/req/receipts/in/traffic", nil) 58 reqReceiptOutPacketsMeter = metrics.NewRegisteredMeter("klay/req/receipts/out/packets", nil) 59 reqReceiptOutTrafficMeter = metrics.NewRegisteredMeter("klay/req/receipts/out/traffic", nil) 60 miscInPacketsMeter = metrics.NewRegisteredMeter("klay/misc/in/packets", nil) 61 miscInTrafficMeter = metrics.NewRegisteredMeter("klay/misc/in/traffic", nil) 62 miscOutPacketsMeter = metrics.NewRegisteredMeter("klay/misc/out/packets", nil) 63 miscOutTrafficMeter = metrics.NewRegisteredMeter("klay/misc/out/traffic", nil) 64 txReceiveCounter = metrics.NewRegisteredCounter("klay/tx/recv/counter", nil) 65 txResendCounter = metrics.NewRegisteredCounter("klay/tx/resend/counter", nil) 66 txSendCounter = metrics.NewRegisteredCounter("klay/tx/send/counter", nil) 67 txResendRoutineGauge = metrics.NewRegisteredGauge("klay/tx/resend/routine/gauge", nil) 68 cnPeerCountGauge = metrics.NewRegisteredGauge("p2p/CNPeerCountGauge", nil) 69 pnPeerCountGauge = metrics.NewRegisteredGauge("p2p/PNPeerCountGauge", nil) 70 enPeerCountGauge = metrics.NewRegisteredGauge("p2p/ENPeerCountGauge", nil) 71 propConsensusIstanbulInPacketsMeter = metrics.NewRegisteredMeter("klay/prop/consensus/istanbul/in/packets", nil) 72 propConsensusIstanbulInTrafficMeter = metrics.NewRegisteredMeter("klay/prop/consensus/istanbul/in/traffic", nil) 73 propConsensusIstanbulOutPacketsMeter = metrics.NewRegisteredMeter("klay/prop/consensus/istanbul/out/packets", nil) 74 propConsensusIstanbulOutTrafficMeter = metrics.NewRegisteredMeter("klay/prop/consensus/istanbul/out/traffic", nil) 75 ) 76 77 // meteredMsgReadWriter is a wrapper around a p2p.MsgReadWriter, capable of 78 // accumulating the above defined metrics based on the data stream contents. 79 type meteredMsgReadWriter struct { 80 p2p.MsgReadWriter // Wrapped message stream to meter 81 version int // Protocol version to select correct meters 82 } 83 84 // newMeteredMsgWriter wraps a p2p MsgReadWriter with metering support. If the 85 // metrics system is disabled, this function returns the original object. 86 func newMeteredMsgWriter(rw p2p.MsgReadWriter) p2p.MsgReadWriter { 87 if !metricutils.Enabled { 88 return rw 89 } 90 return &meteredMsgReadWriter{MsgReadWriter: rw} 91 } 92 93 // Init sets the protocol version used by the stream to know which meters to 94 // increment in case of overlapping message ids between protocol versions. 95 func (rw *meteredMsgReadWriter) Init(version int) { 96 rw.version = version 97 } 98 99 func (rw *meteredMsgReadWriter) ReadMsg() (p2p.Msg, error) { 100 // Read the message and short circuit in case of an error 101 msg, err := rw.MsgReadWriter.ReadMsg() 102 if err != nil { 103 return msg, err 104 } 105 // Account for the data traffic 106 packets, traffic := miscInPacketsMeter, miscInTrafficMeter 107 switch { 108 case msg.Code == BlockHeadersMsg: 109 packets, traffic = reqHeaderInPacketsMeter, reqHeaderInTrafficMeter 110 case msg.Code == BlockBodiesMsg: 111 packets, traffic = reqBodyInPacketsMeter, reqBodyInTrafficMeter 112 113 case rw.version >= klay63 && msg.Code == NodeDataMsg: 114 packets, traffic = reqStateInPacketsMeter, reqStateInTrafficMeter 115 case rw.version >= klay63 && msg.Code == ReceiptsMsg: 116 packets, traffic = reqReceiptInPacketsMeter, reqReceiptInTrafficMeter 117 118 case msg.Code == NewBlockHashesMsg: 119 packets, traffic = propHashInPacketsMeter, propHashInTrafficMeter 120 case msg.Code == NewBlockMsg: 121 packets, traffic = propBlockInPacketsMeter, propBlockInTrafficMeter 122 case msg.Code == TxMsg: 123 packets, traffic = propTxnInPacketsMeter, propTxnInTrafficMeter 124 case msg.Code == backend.IstanbulMsg: 125 packets, traffic = propConsensusIstanbulInPacketsMeter, propConsensusIstanbulInTrafficMeter 126 } 127 packets.Mark(1) 128 traffic.Mark(int64(msg.Size)) 129 130 return msg, err 131 } 132 133 func (rw *meteredMsgReadWriter) WriteMsg(msg p2p.Msg) error { 134 // Account for the data traffic 135 packets, traffic := miscOutPacketsMeter, miscOutTrafficMeter 136 switch { 137 case msg.Code == BlockHeadersMsg: 138 packets, traffic = reqHeaderOutPacketsMeter, reqHeaderOutTrafficMeter 139 case msg.Code == BlockBodiesMsg: 140 packets, traffic = reqBodyOutPacketsMeter, reqBodyOutTrafficMeter 141 142 case rw.version >= klay63 && msg.Code == NodeDataMsg: 143 packets, traffic = reqStateOutPacketsMeter, reqStateOutTrafficMeter 144 case rw.version >= klay63 && msg.Code == ReceiptsMsg: 145 packets, traffic = reqReceiptOutPacketsMeter, reqReceiptOutTrafficMeter 146 147 case msg.Code == NewBlockHashesMsg: 148 packets, traffic = propHashOutPacketsMeter, propHashOutTrafficMeter 149 case msg.Code == NewBlockMsg: 150 packets, traffic = propBlockOutPacketsMeter, propBlockOutTrafficMeter 151 case msg.Code == TxMsg: 152 packets, traffic = propTxnOutPacketsMeter, propTxnOutTrafficMeter 153 case msg.Code == backend.IstanbulMsg: 154 packets, traffic = propConsensusIstanbulOutPacketsMeter, propConsensusIstanbulOutTrafficMeter 155 } 156 packets.Mark(1) 157 traffic.Mark(int64(msg.Size)) 158 159 // Send the packet to the p2p layer 160 return rw.MsgReadWriter.WriteMsg(msg) 161 }