github.com/ethersphere/bee/v2@v2.2.0/pkg/p2p/libp2p/metrics.go (about)

     1  // Copyright 2020 The Swarm Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package libp2p
     6  
     7  import (
     8  	m "github.com/ethersphere/bee/v2/pkg/metrics"
     9  	"github.com/prometheus/client_golang/prometheus"
    10  )
    11  
    12  type metrics struct {
    13  	// all metrics fields must be exported
    14  	// to be able to return them by Metrics()
    15  	// using reflection
    16  	CreatedConnectionCount     prometheus.Counter
    17  	HandledConnectionCount     prometheus.Counter
    18  	CreatedStreamCount         prometheus.Counter
    19  	ClosedStreamCount          prometheus.Counter
    20  	StreamResetCount           prometheus.Counter
    21  	HandledStreamCount         prometheus.Counter
    22  	BlocklistedPeerCount       prometheus.Counter
    23  	BlocklistedPeerErrCount    prometheus.Counter
    24  	DisconnectCount            prometheus.Counter
    25  	ConnectBreakerCount        prometheus.Counter
    26  	UnexpectedProtocolReqCount prometheus.Counter
    27  	KickedOutPeersCount        prometheus.Counter
    28  	StreamHandlerErrResetCount prometheus.Counter
    29  	HeadersExchangeDuration    prometheus.Histogram
    30  }
    31  
    32  func newMetrics() metrics {
    33  	subsystem := "libp2p"
    34  
    35  	return metrics{
    36  		CreatedConnectionCount: prometheus.NewCounter(prometheus.CounterOpts{
    37  			Namespace: m.Namespace,
    38  			Subsystem: subsystem,
    39  			Name:      "created_connection_count",
    40  			Help:      "Number of initiated outgoing libp2p connections.",
    41  		}),
    42  		HandledConnectionCount: prometheus.NewCounter(prometheus.CounterOpts{
    43  			Namespace: m.Namespace,
    44  			Subsystem: subsystem,
    45  			Name:      "handled_connection_count",
    46  			Help:      "Number of handled incoming libp2p connections.",
    47  		}),
    48  		CreatedStreamCount: prometheus.NewCounter(prometheus.CounterOpts{
    49  			Namespace: m.Namespace,
    50  			Subsystem: subsystem,
    51  			Name:      "created_stream_count",
    52  			Help:      "Number of initiated outgoing libp2p streams.",
    53  		}),
    54  		ClosedStreamCount: prometheus.NewCounter(prometheus.CounterOpts{
    55  			Namespace: m.Namespace,
    56  			Subsystem: subsystem,
    57  			Name:      "closed_stream_count",
    58  			Help:      "Number of closed outgoing libp2p streams.",
    59  		}),
    60  		StreamResetCount: prometheus.NewCounter(prometheus.CounterOpts{
    61  			Namespace: m.Namespace,
    62  			Subsystem: subsystem,
    63  			Name:      "stream_reset_count",
    64  			Help:      "Number of outgoing libp2p streams resets.",
    65  		}),
    66  		HandledStreamCount: prometheus.NewCounter(prometheus.CounterOpts{
    67  			Namespace: m.Namespace,
    68  			Subsystem: subsystem,
    69  			Name:      "handled_stream_count",
    70  			Help:      "Number of handled incoming libp2p streams.",
    71  		}),
    72  		BlocklistedPeerCount: prometheus.NewCounter(prometheus.CounterOpts{
    73  			Namespace: m.Namespace,
    74  			Subsystem: subsystem,
    75  			Name:      "blocklisted_peer_count",
    76  			Help:      "Number of peers we've blocklisted.",
    77  		}),
    78  		BlocklistedPeerErrCount: prometheus.NewCounter(prometheus.CounterOpts{
    79  			Namespace: m.Namespace,
    80  			Subsystem: subsystem,
    81  			Name:      "blocklisted_peer_err_count",
    82  			Help:      "Number of peers we've been unable to blocklist.",
    83  		}),
    84  		DisconnectCount: prometheus.NewCounter(prometheus.CounterOpts{
    85  			Namespace: m.Namespace,
    86  			Subsystem: subsystem,
    87  			Name:      "disconnect_count",
    88  			Help:      "Number of peers we've disconnected from (initiated locally).",
    89  		}),
    90  		ConnectBreakerCount: prometheus.NewCounter(prometheus.CounterOpts{
    91  			Namespace: m.Namespace,
    92  			Subsystem: subsystem,
    93  			Name:      "connect_breaker_count",
    94  			Help:      "Number of times we got a closed breaker while connecting to another peer.",
    95  		}),
    96  		UnexpectedProtocolReqCount: prometheus.NewCounter(prometheus.CounterOpts{
    97  			Namespace: m.Namespace,
    98  			Subsystem: subsystem,
    99  			Name:      "unexpected_protocol_request_count",
   100  			Help:      "Number of requests the peer is not expecting.",
   101  		}),
   102  		KickedOutPeersCount: prometheus.NewCounter(prometheus.CounterOpts{
   103  			Namespace: m.Namespace,
   104  			Subsystem: subsystem,
   105  			Name:      "kickedout_peers_count",
   106  			Help:      "Number of total kicked-out peers.",
   107  		}),
   108  		StreamHandlerErrResetCount: prometheus.NewCounter(prometheus.CounterOpts{
   109  			Namespace: m.Namespace,
   110  			Subsystem: subsystem,
   111  			Name:      "stream_handler_error_reset_count",
   112  			Help:      "Number of total stream handler error resets.",
   113  		}),
   114  		HeadersExchangeDuration: prometheus.NewHistogram(prometheus.HistogramOpts{
   115  			Namespace: m.Namespace,
   116  			Subsystem: subsystem,
   117  			Name:      "headers_exchange_duration",
   118  			Help:      "The duration spent exchanging the headers.",
   119  		}),
   120  	}
   121  }
   122  
   123  func (s *Service) Metrics() []prometheus.Collector {
   124  	return append(m.PrometheusCollectorsFromFields(s.metrics), s.handshakeService.Metrics()...)
   125  }