metrics.go 2.8 KB

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