github.com/noirx94/tendermintmp@v0.0.1/mempool/metrics.go (about)

     1  package mempool
     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 = "mempool"
    14  )
    15  
    16  // Metrics contains metrics exposed by this package.
    17  // see MetricsProvider for descriptions.
    18  type Metrics struct {
    19  	// Size of the mempool.
    20  	Size metrics.Gauge
    21  	// Histogram of transaction sizes, in bytes.
    22  	TxSizeBytes metrics.Histogram
    23  	// Number of failed transactions.
    24  	FailedTxs metrics.Counter
    25  	// Number of times transactions are rechecked in the mempool.
    26  	RecheckTimes metrics.Counter
    27  }
    28  
    29  // PrometheusMetrics returns Metrics build using Prometheus client library.
    30  // Optionally, labels can be provided along with their values ("foo",
    31  // "fooValue").
    32  func PrometheusMetrics(namespace string, labelsAndValues ...string) *Metrics {
    33  	labels := []string{}
    34  	for i := 0; i < len(labelsAndValues); i += 2 {
    35  		labels = append(labels, labelsAndValues[i])
    36  	}
    37  	return &Metrics{
    38  		Size: prometheus.NewGaugeFrom(stdprometheus.GaugeOpts{
    39  			Namespace: namespace,
    40  			Subsystem: MetricsSubsystem,
    41  			Name:      "size",
    42  			Help:      "Size of the mempool (number of uncommitted transactions).",
    43  		}, labels).With(labelsAndValues...),
    44  		TxSizeBytes: prometheus.NewHistogramFrom(stdprometheus.HistogramOpts{
    45  			Namespace: namespace,
    46  			Subsystem: MetricsSubsystem,
    47  			Name:      "tx_size_bytes",
    48  			Help:      "Transaction sizes in bytes.",
    49  			Buckets:   stdprometheus.ExponentialBuckets(1, 3, 17),
    50  		}, labels).With(labelsAndValues...),
    51  		FailedTxs: prometheus.NewCounterFrom(stdprometheus.CounterOpts{
    52  			Namespace: namespace,
    53  			Subsystem: MetricsSubsystem,
    54  			Name:      "failed_txs",
    55  			Help:      "Number of failed transactions.",
    56  		}, labels).With(labelsAndValues...),
    57  		RecheckTimes: prometheus.NewCounterFrom(stdprometheus.CounterOpts{
    58  			Namespace: namespace,
    59  			Subsystem: MetricsSubsystem,
    60  			Name:      "recheck_times",
    61  			Help:      "Number of times transactions are rechecked in the mempool.",
    62  		}, labels).With(labelsAndValues...),
    63  	}
    64  }
    65  
    66  // NopMetrics returns no-op Metrics.
    67  func NopMetrics() *Metrics {
    68  	return &Metrics{
    69  		Size:         discard.NewGauge(),
    70  		TxSizeBytes:  discard.NewHistogram(),
    71  		FailedTxs:    discard.NewCounter(),
    72  		RecheckTimes: discard.NewCounter(),
    73  	}
    74  }