github.com/ethereumproject/go-ethereum@v5.5.2+incompatible/eth/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 eth 18 19 import ( 20 "github.com/ethereumproject/go-ethereum/metrics" 21 "github.com/ethereumproject/go-ethereum/p2p" 22 ) 23 24 // meteredMsgReadWriter is a wrapper around a p2p.MsgReadWriter, capable of 25 // accumulating the above defined metrics based on the data stream contents. 26 type meteredMsgReadWriter struct { 27 p2p.MsgReadWriter // Wrapped message stream to meter 28 version int // Protocol version to select correct meters 29 } 30 31 // newMeteredMsgWriter wraps a p2p MsgReadWriter with metering support. If the 32 // metrics system is disabled, this function returns the original object. 33 func newMeteredMsgWriter(rw p2p.MsgReadWriter) p2p.MsgReadWriter { 34 return &meteredMsgReadWriter{MsgReadWriter: rw} 35 } 36 37 // Init sets the protocol version used by the stream to know which meters to 38 // increment in case of overlapping message ids between protocol versions. 39 func (rw *meteredMsgReadWriter) Init(version int) { 40 rw.version = version 41 } 42 43 func (rw *meteredMsgReadWriter) ReadMsg() (p2p.Msg, error) { 44 msg, err := rw.MsgReadWriter.ReadMsg() 45 if err != nil { 46 return msg, err 47 } 48 49 messages, bytes := metrics.MsgMiscIn, metrics.MsgMiscInBytes 50 switch { 51 case msg.Code == BlockHeadersMsg: 52 messages, bytes = metrics.MsgHeaderIn, metrics.MsgHeaderInBytes 53 case msg.Code == BlockBodiesMsg: 54 messages, bytes = metrics.MsgBodyIn, metrics.MsgBodyInBytes 55 case rw.version >= eth63 && msg.Code == NodeDataMsg: 56 messages, bytes = metrics.MsgStateIn, metrics.MsgStateInBytes 57 case rw.version >= eth63 && msg.Code == ReceiptsMsg: 58 messages, bytes = metrics.MsgReceiptIn, metrics.MsgReceiptInBytes 59 case msg.Code == NewBlockHashesMsg: 60 messages, bytes = metrics.MsgHashIn, metrics.MsgHashInBytes 61 case msg.Code == NewBlockMsg: 62 messages, bytes = metrics.MsgBlockIn, metrics.MsgBlockInBytes 63 case msg.Code == TxMsg: 64 messages, bytes = metrics.MsgTXNIn, metrics.MsgTXNInBytes 65 } 66 messages.Mark(1) 67 bytes.Mark(int64(msg.Size)) 68 69 return msg, nil 70 } 71 72 func (rw *meteredMsgReadWriter) WriteMsg(msg p2p.Msg) error { 73 messages, bytes := metrics.MsgMiscOut, metrics.MsgMiscOutBytes 74 switch { 75 case msg.Code == BlockHeadersMsg: 76 messages, bytes = metrics.MsgHeaderOut, metrics.MsgHeaderOutBytes 77 case msg.Code == BlockBodiesMsg: 78 messages, bytes = metrics.MsgBodyOut, metrics.MsgBodyOutBytes 79 case rw.version >= eth63 && msg.Code == NodeDataMsg: 80 messages, bytes = metrics.MsgStateOut, metrics.MsgStateOutBytes 81 case rw.version >= eth63 && msg.Code == ReceiptsMsg: 82 messages, bytes = metrics.MsgReceiptOut, metrics.MsgReceiptOutBytes 83 case msg.Code == NewBlockHashesMsg: 84 messages, bytes = metrics.MsgHashOut, metrics.MsgHashOutBytes 85 case msg.Code == NewBlockMsg: 86 messages, bytes = metrics.MsgBlockOut, metrics.MsgBlockOutBytes 87 case msg.Code == TxMsg: 88 messages, bytes = metrics.MsgTXNOut, metrics.MsgTXNOutBytes 89 } 90 messages.Mark(1) 91 bytes.Mark(int64(msg.Size)) 92 93 return rw.MsgReadWriter.WriteMsg(msg) 94 }