github.com/516108736/tendermint@v0.36.0/consensus/metrics.go (about)

     1  package consensus
     2  
     3  import (
     4  	"github.com/go-kit/kit/metrics"
     5  	"github.com/go-kit/kit/metrics/discard"
     6  
     7  	prometheus "github.com/go-kit/kit/metrics/prometheus"
     8  	stdprometheus "github.com/prometheus/client_golang/prometheus"
     9  )
    10  
    11  const (
    12  	// MetricsSubsystem is a subsystem shared by all metrics exposed by this
    13  	// package.
    14  	MetricsSubsystem = "consensus"
    15  )
    16  
    17  // Metrics contains metrics exposed by this package.
    18  type Metrics struct {
    19  	// Height of the chain.
    20  	Height metrics.Gauge
    21  
    22  	// ValidatorLastSignedHeight of a validator.
    23  	ValidatorLastSignedHeight metrics.Gauge
    24  
    25  	// Number of rounds.
    26  	Rounds metrics.Gauge
    27  
    28  	// Number of validators.
    29  	Validators metrics.Gauge
    30  	// Total power of all validators.
    31  	ValidatorsPower metrics.Gauge
    32  	// Power of a validator.
    33  	ValidatorPower metrics.Gauge
    34  	// Amount of blocks missed by a validator.
    35  	ValidatorMissedBlocks metrics.Gauge
    36  	// Number of validators who did not sign.
    37  	MissingValidators metrics.Gauge
    38  	// Total power of the missing validators.
    39  	MissingValidatorsPower metrics.Gauge
    40  	// Number of validators who tried to double sign.
    41  	ByzantineValidators metrics.Gauge
    42  	// Total power of the byzantine validators.
    43  	ByzantineValidatorsPower metrics.Gauge
    44  
    45  	// Time between this and the last block.
    46  	BlockIntervalSeconds metrics.Histogram
    47  
    48  	// Number of transactions.
    49  	NumTxs metrics.Gauge
    50  	// Size of the block.
    51  	BlockSizeBytes metrics.Gauge
    52  	// Total number of transactions.
    53  	TotalTxs metrics.Gauge
    54  	// The latest block height.
    55  	CommittedHeight metrics.Gauge
    56  	// Whether or not a node is fast syncing. 1 if yes, 0 if no.
    57  	FastSyncing metrics.Gauge
    58  	// Whether or not a node is state syncing. 1 if yes, 0 if no.
    59  	StateSyncing metrics.Gauge
    60  
    61  	// Number of blockparts transmitted by peer.
    62  	BlockParts metrics.Counter
    63  }
    64  
    65  // PrometheusMetrics returns Metrics build using Prometheus client library.
    66  // Optionally, labels can be provided along with their values ("foo",
    67  // "fooValue").
    68  func PrometheusMetrics(namespace string, labelsAndValues ...string) *Metrics {
    69  	labels := []string{}
    70  	for i := 0; i < len(labelsAndValues); i += 2 {
    71  		labels = append(labels, labelsAndValues[i])
    72  	}
    73  	return &Metrics{
    74  		Height: prometheus.NewGaugeFrom(stdprometheus.GaugeOpts{
    75  			Namespace: namespace,
    76  			Subsystem: MetricsSubsystem,
    77  			Name:      "height",
    78  			Help:      "Height of the chain.",
    79  		}, labels).With(labelsAndValues...),
    80  		Rounds: prometheus.NewGaugeFrom(stdprometheus.GaugeOpts{
    81  			Namespace: namespace,
    82  			Subsystem: MetricsSubsystem,
    83  			Name:      "rounds",
    84  			Help:      "Number of rounds.",
    85  		}, labels).With(labelsAndValues...),
    86  
    87  		Validators: prometheus.NewGaugeFrom(stdprometheus.GaugeOpts{
    88  			Namespace: namespace,
    89  			Subsystem: MetricsSubsystem,
    90  			Name:      "validators",
    91  			Help:      "Number of validators.",
    92  		}, labels).With(labelsAndValues...),
    93  		ValidatorLastSignedHeight: prometheus.NewGaugeFrom(stdprometheus.GaugeOpts{
    94  			Namespace: namespace,
    95  			Subsystem: MetricsSubsystem,
    96  			Name:      "validator_last_signed_height",
    97  			Help:      "Last signed height for a validator",
    98  		}, append(labels, "validator_address")).With(labelsAndValues...),
    99  		ValidatorMissedBlocks: prometheus.NewGaugeFrom(stdprometheus.GaugeOpts{
   100  			Namespace: namespace,
   101  			Subsystem: MetricsSubsystem,
   102  			Name:      "validator_missed_blocks",
   103  			Help:      "Total missed blocks for a validator",
   104  		}, append(labels, "validator_address")).With(labelsAndValues...),
   105  		ValidatorsPower: prometheus.NewGaugeFrom(stdprometheus.GaugeOpts{
   106  			Namespace: namespace,
   107  			Subsystem: MetricsSubsystem,
   108  			Name:      "validators_power",
   109  			Help:      "Total power of all validators.",
   110  		}, labels).With(labelsAndValues...),
   111  		ValidatorPower: prometheus.NewGaugeFrom(stdprometheus.GaugeOpts{
   112  			Namespace: namespace,
   113  			Subsystem: MetricsSubsystem,
   114  			Name:      "validator_power",
   115  			Help:      "Power of a validator",
   116  		}, append(labels, "validator_address")).With(labelsAndValues...),
   117  		MissingValidators: prometheus.NewGaugeFrom(stdprometheus.GaugeOpts{
   118  			Namespace: namespace,
   119  			Subsystem: MetricsSubsystem,
   120  			Name:      "missing_validators",
   121  			Help:      "Number of validators who did not sign.",
   122  		}, labels).With(labelsAndValues...),
   123  		MissingValidatorsPower: prometheus.NewGaugeFrom(stdprometheus.GaugeOpts{
   124  			Namespace: namespace,
   125  			Subsystem: MetricsSubsystem,
   126  			Name:      "missing_validators_power",
   127  			Help:      "Total power of the missing validators.",
   128  		}, labels).With(labelsAndValues...),
   129  		ByzantineValidators: prometheus.NewGaugeFrom(stdprometheus.GaugeOpts{
   130  			Namespace: namespace,
   131  			Subsystem: MetricsSubsystem,
   132  			Name:      "byzantine_validators",
   133  			Help:      "Number of validators who tried to double sign.",
   134  		}, labels).With(labelsAndValues...),
   135  		ByzantineValidatorsPower: prometheus.NewGaugeFrom(stdprometheus.GaugeOpts{
   136  			Namespace: namespace,
   137  			Subsystem: MetricsSubsystem,
   138  			Name:      "byzantine_validators_power",
   139  			Help:      "Total power of the byzantine validators.",
   140  		}, labels).With(labelsAndValues...),
   141  		BlockIntervalSeconds: prometheus.NewHistogramFrom(stdprometheus.HistogramOpts{
   142  			Namespace: namespace,
   143  			Subsystem: MetricsSubsystem,
   144  			Name:      "block_interval_seconds",
   145  			Help:      "Time between this and the last block.",
   146  		}, labels).With(labelsAndValues...),
   147  		NumTxs: prometheus.NewGaugeFrom(stdprometheus.GaugeOpts{
   148  			Namespace: namespace,
   149  			Subsystem: MetricsSubsystem,
   150  			Name:      "num_txs",
   151  			Help:      "Number of transactions.",
   152  		}, labels).With(labelsAndValues...),
   153  		BlockSizeBytes: prometheus.NewGaugeFrom(stdprometheus.GaugeOpts{
   154  			Namespace: namespace,
   155  			Subsystem: MetricsSubsystem,
   156  			Name:      "block_size_bytes",
   157  			Help:      "Size of the block.",
   158  		}, labels).With(labelsAndValues...),
   159  		TotalTxs: prometheus.NewGaugeFrom(stdprometheus.GaugeOpts{
   160  			Namespace: namespace,
   161  			Subsystem: MetricsSubsystem,
   162  			Name:      "total_txs",
   163  			Help:      "Total number of transactions.",
   164  		}, labels).With(labelsAndValues...),
   165  		CommittedHeight: prometheus.NewGaugeFrom(stdprometheus.GaugeOpts{
   166  			Namespace: namespace,
   167  			Subsystem: MetricsSubsystem,
   168  			Name:      "latest_block_height",
   169  			Help:      "The latest block height.",
   170  		}, labels).With(labelsAndValues...),
   171  		FastSyncing: prometheus.NewGaugeFrom(stdprometheus.GaugeOpts{
   172  			Namespace: namespace,
   173  			Subsystem: MetricsSubsystem,
   174  			Name:      "fast_syncing",
   175  			Help:      "Whether or not a node is fast syncing. 1 if yes, 0 if no.",
   176  		}, labels).With(labelsAndValues...),
   177  		StateSyncing: prometheus.NewGaugeFrom(stdprometheus.GaugeOpts{
   178  			Namespace: namespace,
   179  			Subsystem: MetricsSubsystem,
   180  			Name:      "state_syncing",
   181  			Help:      "Whether or not a node is state syncing. 1 if yes, 0 if no.",
   182  		}, labels).With(labelsAndValues...),
   183  		BlockParts: prometheus.NewCounterFrom(stdprometheus.CounterOpts{
   184  			Namespace: namespace,
   185  			Subsystem: MetricsSubsystem,
   186  			Name:      "block_parts",
   187  			Help:      "Number of blockparts transmitted by peer.",
   188  		}, append(labels, "peer_id")).With(labelsAndValues...),
   189  	}
   190  }
   191  
   192  // NopMetrics returns no-op Metrics.
   193  func NopMetrics() *Metrics {
   194  	return &Metrics{
   195  		Height: discard.NewGauge(),
   196  
   197  		ValidatorLastSignedHeight: discard.NewGauge(),
   198  
   199  		Rounds: discard.NewGauge(),
   200  
   201  		Validators:               discard.NewGauge(),
   202  		ValidatorsPower:          discard.NewGauge(),
   203  		ValidatorPower:           discard.NewGauge(),
   204  		ValidatorMissedBlocks:    discard.NewGauge(),
   205  		MissingValidators:        discard.NewGauge(),
   206  		MissingValidatorsPower:   discard.NewGauge(),
   207  		ByzantineValidators:      discard.NewGauge(),
   208  		ByzantineValidatorsPower: discard.NewGauge(),
   209  
   210  		BlockIntervalSeconds: discard.NewHistogram(),
   211  
   212  		NumTxs:          discard.NewGauge(),
   213  		BlockSizeBytes:  discard.NewGauge(),
   214  		TotalTxs:        discard.NewGauge(),
   215  		CommittedHeight: discard.NewGauge(),
   216  		FastSyncing:     discard.NewGauge(),
   217  		StateSyncing:    discard.NewGauge(),
   218  		BlockParts:      discard.NewCounter(),
   219  	}
   220  }