metrics.go 3.1 KB

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