github.com/cerc-io/go-ethereum@v1.9.7/les/metrics.go (about) 1 // Copyright 2016 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 les 18 19 import ( 20 "github.com/ethereum/go-ethereum/metrics" 21 "github.com/ethereum/go-ethereum/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 clientRejectedMeter = metrics.NewRegisteredMeter("les/server/clientEvent/rejected", nil) 103 clientKickedMeter = metrics.NewRegisteredMeter("les/server/clientEvent/kicked", 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 112 // meteredMsgReadWriter is a wrapper around a p2p.MsgReadWriter, capable of 113 // accumulating the above defined metrics based on the data stream contents. 114 type meteredMsgReadWriter struct { 115 p2p.MsgReadWriter // Wrapped message stream to meter 116 version int // Protocol version to select correct meters 117 } 118 119 // newMeteredMsgWriter wraps a p2p MsgReadWriter with metering support. If the 120 // metrics system is disabled, this function returns the original object. 121 func newMeteredMsgWriter(rw p2p.MsgReadWriter, version int) p2p.MsgReadWriter { 122 if !metrics.Enabled { 123 return rw 124 } 125 return &meteredMsgReadWriter{MsgReadWriter: rw, version: version} 126 } 127 128 func (rw *meteredMsgReadWriter) ReadMsg() (p2p.Msg, error) { 129 // Read the message and short circuit in case of an error 130 msg, err := rw.MsgReadWriter.ReadMsg() 131 if err != nil { 132 return msg, err 133 } 134 // Account for the data traffic 135 packets, traffic := miscInPacketsMeter, miscInTrafficMeter 136 packets.Mark(1) 137 traffic.Mark(int64(msg.Size)) 138 139 return msg, err 140 } 141 142 func (rw *meteredMsgReadWriter) WriteMsg(msg p2p.Msg) error { 143 // Account for the data traffic 144 packets, traffic := miscOutPacketsMeter, miscOutTrafficMeter 145 packets.Mark(1) 146 traffic.Mark(int64(msg.Size)) 147 148 // Send the packet to the p2p layer 149 return rw.MsgReadWriter.WriteMsg(msg) 150 }