github.com/klaytn/klaytn@v1.12.1/networks/p2p/metrics.go (about) 1 // Modifications Copyright 2018 The klaytn Authors 2 // Copyright 2015 The go-ethereum Authors 3 // This file is part of the go-ethereum library. 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 p2p/metrics.go (2018/06/04). 19 // Modified and improved for the klaytn development. 20 21 // Contains the meters and timers used by the networking layer. 22 23 package p2p 24 25 import ( 26 "net" 27 28 metricutils "github.com/klaytn/klaytn/metrics/utils" 29 30 "github.com/rcrowley/go-metrics" 31 ) 32 33 var ( 34 ingressConnectMeter = metrics.NewRegisteredMeter("p2p/InboundConnects", nil) 35 ingressTrafficMeter = metrics.NewRegisteredMeter("p2p/InboundTraffic", nil) 36 egressConnectMeter = metrics.NewRegisteredMeter("p2p/OutboundConnects", nil) 37 egressTrafficMeter = metrics.NewRegisteredMeter("p2p/OutboundTraffic", nil) 38 39 // The peer can be connected to one or more network ports. 40 // Therefore, the connection state with the abstracted peer is measured by peerXXXCountGauge 41 // and the connection at the network port level is measured by connectionXXXCountGauge. 42 peerCountGauge = metrics.NewRegisteredGauge("p2p/PeerCountGauge", nil) 43 peerInCountGauge = metrics.NewRegisteredGauge("p2p/PeerInCountGauge", nil) 44 peerOutCountGauge = metrics.NewRegisteredGauge("p2p/PeerOutCountGauge", nil) 45 46 connectionCountGauge = metrics.NewRegisteredGauge("p2p/ConnectionCountGauge", nil) 47 connectionInCountGauge = metrics.NewRegisteredGauge("p2p/ConnectionInCountGauge", nil) 48 connectionOutCountGauge = metrics.NewRegisteredGauge("p2p/ConnectionOutCountGauge", nil) 49 50 dialTryCounter = metrics.NewRegisteredCounter("p2p/DialTryCounter", nil) 51 dialFailCounter = metrics.NewRegisteredCounter("p2p/DialFailCounter", nil) 52 53 writeMsgTimeOutCounter = metrics.NewRegisteredCounter("p2p/WriteMsgTimeOutCounter", nil) 54 ) 55 56 // meteredConn is a wrapper around a network TCP connection that meters both the 57 // inbound and outbound network traffic. 58 type meteredConn struct { 59 *net.TCPConn // Network connection to wrap with metering 60 } 61 62 // newMeteredConn creates a new metered connection, also bumping the ingress or 63 // egress connection meter. If the metrics system is disabled, this function 64 // returns the original object. 65 func newMeteredConn(conn net.Conn, ingress bool) net.Conn { 66 // Short circuit if metrics are disabled 67 if !metricutils.Enabled { 68 return conn 69 } 70 // Otherwise bump the connection counters and wrap the connection 71 if ingress { 72 ingressConnectMeter.Mark(1) 73 } else { 74 egressConnectMeter.Mark(1) 75 } 76 return &meteredConn{conn.(*net.TCPConn)} 77 } 78 79 // Read delegates a network read to the underlying connection, bumping the ingress 80 // traffic meter along the way. 81 func (c *meteredConn) Read(b []byte) (n int, err error) { 82 n, err = c.TCPConn.Read(b) 83 ingressTrafficMeter.Mark(int64(n)) 84 return 85 } 86 87 // Write delegates a network write to the underlying connection, bumping the 88 // egress traffic meter along the way. 89 func (c *meteredConn) Write(b []byte) (n int, err error) { 90 n, err = c.TCPConn.Write(b) 91 egressTrafficMeter.Mark(int64(n)) 92 return 93 }