github.com/Unheilbar/quorum@v1.0.0/p2p/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 // Contains the meters and timers used by the networking layer. 18 19 package p2p 20 21 import ( 22 "net" 23 24 "github.com/ethereum/go-ethereum/metrics" 25 ) 26 27 const ( 28 // ingressMeterName is the prefix of the per-packet inbound metrics. 29 ingressMeterName = "p2p/ingress" 30 31 // egressMeterName is the prefix of the per-packet outbound metrics. 32 egressMeterName = "p2p/egress" 33 34 // HandleHistName is the prefix of the per-packet serving time histograms. 35 HandleHistName = "p2p/handle" 36 ) 37 38 var ( 39 ingressConnectMeter = metrics.NewRegisteredMeter("p2p/serves", nil) 40 ingressTrafficMeter = metrics.NewRegisteredMeter(ingressMeterName, nil) 41 egressConnectMeter = metrics.NewRegisteredMeter("p2p/dials", nil) 42 egressTrafficMeter = metrics.NewRegisteredMeter(egressMeterName, nil) 43 activePeerGauge = metrics.NewRegisteredGauge("p2p/peers", nil) 44 ) 45 46 // meteredConn is a wrapper around a net.Conn that meters both the 47 // inbound and outbound network traffic. 48 type meteredConn struct { 49 net.Conn 50 } 51 52 // newMeteredConn creates a new metered connection, bumps the ingress or egress 53 // connection meter and also increases the metered peer count. If the metrics 54 // system is disabled, function returns the original connection. 55 func newMeteredConn(conn net.Conn, ingress bool, addr *net.TCPAddr) net.Conn { 56 // Short circuit if metrics are disabled 57 if !metrics.Enabled { 58 return conn 59 } 60 // Bump the connection counters and wrap the connection 61 if ingress { 62 ingressConnectMeter.Mark(1) 63 } else { 64 egressConnectMeter.Mark(1) 65 } 66 activePeerGauge.Inc(1) 67 return &meteredConn{Conn: conn} 68 } 69 70 // Read delegates a network read to the underlying connection, bumping the common 71 // and the peer ingress traffic meters along the way. 72 func (c *meteredConn) Read(b []byte) (n int, err error) { 73 n, err = c.Conn.Read(b) 74 ingressTrafficMeter.Mark(int64(n)) 75 return n, err 76 } 77 78 // Write delegates a network write to the underlying connection, bumping the common 79 // and the peer egress traffic meters along the way. 80 func (c *meteredConn) Write(b []byte) (n int, err error) { 81 n, err = c.Conn.Write(b) 82 egressTrafficMeter.Mark(int64(n)) 83 return n, err 84 } 85 86 // Close delegates a close operation to the underlying connection, unregisters 87 // the peer from the traffic registries and emits close event. 88 func (c *meteredConn) Close() error { 89 err := c.Conn.Close() 90 if err == nil { 91 activePeerGauge.Dec(1) 92 } 93 return err 94 }