github.com/DFWallet/tendermint-cosmos@v0.0.2/p2p/metrics.go (about)

     1  package p2p
     2  
     3  import (
     4  	"github.com/go-kit/kit/metrics"
     5  	"github.com/go-kit/kit/metrics/discard"
     6  	"github.com/go-kit/kit/metrics/prometheus"
     7  	stdprometheus "github.com/prometheus/client_golang/prometheus"
     8  )
     9  
    10  const (
    11  	// MetricsSubsystem is a subsystem shared by all metrics exposed by this
    12  	// package.
    13  	MetricsSubsystem = "p2p"
    14  )
    15  
    16  // Metrics contains metrics exposed by this package.
    17  type Metrics struct {
    18  	// Number of peers.
    19  	Peers metrics.Gauge
    20  	// Number of bytes received from a given peer.
    21  	PeerReceiveBytesTotal metrics.Counter
    22  	// Number of bytes sent to a given peer.
    23  	PeerSendBytesTotal metrics.Counter
    24  	// Pending bytes to be sent to a given peer.
    25  	PeerPendingSendBytes metrics.Gauge
    26  	// Number of transactions submitted by each peer.
    27  	NumTxs metrics.Gauge
    28  }
    29  
    30  // PrometheusMetrics returns Metrics build using Prometheus client library.
    31  // Optionally, labels can be provided along with their values ("foo",
    32  // "fooValue").
    33  func PrometheusMetrics(namespace string, labelsAndValues ...string) *Metrics {
    34  	labels := []string{}
    35  	for i := 0; i < len(labelsAndValues); i += 2 {
    36  		labels = append(labels, labelsAndValues[i])
    37  	}
    38  	return &Metrics{
    39  		Peers: prometheus.NewGaugeFrom(stdprometheus.GaugeOpts{
    40  			Namespace: namespace,
    41  			Subsystem: MetricsSubsystem,
    42  			Name:      "peers",
    43  			Help:      "Number of peers.",
    44  		}, labels).With(labelsAndValues...),
    45  		PeerReceiveBytesTotal: prometheus.NewCounterFrom(stdprometheus.CounterOpts{
    46  			Namespace: namespace,
    47  			Subsystem: MetricsSubsystem,
    48  			Name:      "peer_receive_bytes_total",
    49  			Help:      "Number of bytes received from a given peer.",
    50  		}, append(labels, "peer_id", "chID")).With(labelsAndValues...),
    51  		PeerSendBytesTotal: prometheus.NewCounterFrom(stdprometheus.CounterOpts{
    52  			Namespace: namespace,
    53  			Subsystem: MetricsSubsystem,
    54  			Name:      "peer_send_bytes_total",
    55  			Help:      "Number of bytes sent to a given peer.",
    56  		}, append(labels, "peer_id", "chID")).With(labelsAndValues...),
    57  		PeerPendingSendBytes: prometheus.NewGaugeFrom(stdprometheus.GaugeOpts{
    58  			Namespace: namespace,
    59  			Subsystem: MetricsSubsystem,
    60  			Name:      "peer_pending_send_bytes",
    61  			Help:      "Number of pending bytes to be sent to a given peer.",
    62  		}, append(labels, "peer_id")).With(labelsAndValues...),
    63  		NumTxs: prometheus.NewGaugeFrom(stdprometheus.GaugeOpts{
    64  			Namespace: namespace,
    65  			Subsystem: MetricsSubsystem,
    66  			Name:      "num_txs",
    67  			Help:      "Number of transactions submitted by each peer.",
    68  		}, append(labels, "peer_id")).With(labelsAndValues...),
    69  	}
    70  }
    71  
    72  // NopMetrics returns no-op Metrics.
    73  func NopMetrics() *Metrics {
    74  	return &Metrics{
    75  		Peers:                 discard.NewGauge(),
    76  		PeerReceiveBytesTotal: discard.NewCounter(),
    77  		PeerSendBytesTotal:    discard.NewCounter(),
    78  		PeerPendingSendBytes:  discard.NewGauge(),
    79  		NumTxs:                discard.NewGauge(),
    80  	}
    81  }