bitbucket.org/number571/tendermint@v0.8.14/internal/consensus/metrics.go (about)

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