github.com/okex/exchain@v1.8.0/libs/tendermint/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  	"github.com/go-kit/kit/metrics/prometheus"
     7  	"github.com/okex/exchain/libs/tendermint/libs/fastmetrics"
     8  
     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.Gauge
    48  
    49  	// Number of transactions.
    50  	NumTxs metrics.Gauge
    51  	// Size of the block.
    52  	BlockSizeBytes metrics.Gauge
    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  
    60  	// Number of blockparts transmitted by peer.
    61  	BlockParts metrics.Counter
    62  
    63  	NewRoundProcessingTime  metrics.Gauge
    64  	ProposeProcessingTime   metrics.Gauge
    65  	PrevoteProcessingTime   metrics.Gauge
    66  	PrecommitProcessingTime metrics.Gauge
    67  	CommitProcessingTime    metrics.Gauge
    68  }
    69  
    70  // PrometheusMetrics returns Metrics build using Prometheus client library.
    71  // Optionally, labels can be provided along with their values ("foo",
    72  // "fooValue").
    73  func PrometheusMetrics(namespace string, labelsAndValues ...string) *Metrics {
    74  	labels := []string{}
    75  	for i := 0; i < len(labelsAndValues); i += 2 {
    76  		labels = append(labels, labelsAndValues[i])
    77  	}
    78  	return &Metrics{
    79  		Height: fastmetrics.NewGaugeFrom(stdprometheus.GaugeOpts{
    80  			Namespace: namespace,
    81  			Subsystem: MetricsSubsystem,
    82  			Name:      "height",
    83  			Help:      "Height of the chain.",
    84  		}, labels).With(labelsAndValues...),
    85  		Rounds: fastmetrics.NewGaugeFrom(stdprometheus.GaugeOpts{
    86  			Namespace: namespace,
    87  			Subsystem: MetricsSubsystem,
    88  			Name:      "rounds",
    89  			Help:      "Number of rounds.",
    90  		}, labels).With(labelsAndValues...),
    91  
    92  		Validators: fastmetrics.NewGaugeFrom(stdprometheus.GaugeOpts{
    93  			Namespace: namespace,
    94  			Subsystem: MetricsSubsystem,
    95  			Name:      "validators",
    96  			Help:      "Number of validators.",
    97  		}, labels).With(labelsAndValues...),
    98  		ValidatorLastSignedHeight: prometheus.NewGaugeFrom(stdprometheus.GaugeOpts{
    99  			Namespace: namespace,
   100  			Subsystem: MetricsSubsystem,
   101  			Name:      "validator_last_signed_height",
   102  			Help:      "Last signed height for a validator",
   103  		}, append(labels, "validator_address")).With(labelsAndValues...),
   104  		ValidatorMissedBlocks: prometheus.NewGaugeFrom(stdprometheus.GaugeOpts{
   105  			Namespace: namespace,
   106  			Subsystem: MetricsSubsystem,
   107  			Name:      "validator_missed_blocks",
   108  			Help:      "Total missed blocks for a validator",
   109  		}, append(labels, "validator_address")).With(labelsAndValues...),
   110  		ValidatorsPower: fastmetrics.NewGaugeFrom(stdprometheus.GaugeOpts{
   111  			Namespace: namespace,
   112  			Subsystem: MetricsSubsystem,
   113  			Name:      "validators_power",
   114  			Help:      "Total power of all validators.",
   115  		}, labels).With(labelsAndValues...),
   116  		ValidatorPower: prometheus.NewGaugeFrom(stdprometheus.GaugeOpts{
   117  			Namespace: namespace,
   118  			Subsystem: MetricsSubsystem,
   119  			Name:      "validator_power",
   120  			Help:      "Power of a validator",
   121  		}, append(labels, "validator_address")).With(labelsAndValues...),
   122  		MissingValidators: fastmetrics.NewGaugeFrom(stdprometheus.GaugeOpts{
   123  			Namespace: namespace,
   124  			Subsystem: MetricsSubsystem,
   125  			Name:      "missing_validators",
   126  			Help:      "Number of validators who did not sign.",
   127  		}, labels).With(labelsAndValues...),
   128  		MissingValidatorsPower: fastmetrics.NewGaugeFrom(stdprometheus.GaugeOpts{
   129  			Namespace: namespace,
   130  			Subsystem: MetricsSubsystem,
   131  			Name:      "missing_validators_power",
   132  			Help:      "Total power of the missing validators.",
   133  		}, labels).With(labelsAndValues...),
   134  		ByzantineValidators: fastmetrics.NewGaugeFrom(stdprometheus.GaugeOpts{
   135  			Namespace: namespace,
   136  			Subsystem: MetricsSubsystem,
   137  			Name:      "byzantine_validators",
   138  			Help:      "Number of validators who tried to double sign.",
   139  		}, labels).With(labelsAndValues...),
   140  		ByzantineValidatorsPower: fastmetrics.NewGaugeFrom(stdprometheus.GaugeOpts{
   141  			Namespace: namespace,
   142  			Subsystem: MetricsSubsystem,
   143  			Name:      "byzantine_validators_power",
   144  			Help:      "Total power of the byzantine validators.",
   145  		}, labels).With(labelsAndValues...),
   146  
   147  		BlockIntervalSeconds: fastmetrics.NewGaugeFrom(stdprometheus.GaugeOpts{
   148  			Namespace: namespace,
   149  			Subsystem: MetricsSubsystem,
   150  			Name:      "block_interval_seconds",
   151  			Help:      "Time between this and the last block.",
   152  		}, labels).With(labelsAndValues...),
   153  
   154  		NumTxs: fastmetrics.NewGaugeFrom(stdprometheus.GaugeOpts{
   155  			Namespace: namespace,
   156  			Subsystem: MetricsSubsystem,
   157  			Name:      "num_txs",
   158  			Help:      "Number of transactions.",
   159  		}, labels).With(labelsAndValues...),
   160  		BlockSizeBytes: fastmetrics.NewGaugeFrom(stdprometheus.GaugeOpts{
   161  			Namespace: namespace,
   162  			Subsystem: MetricsSubsystem,
   163  			Name:      "block_size_bytes",
   164  			Help:      "Size of the block.",
   165  		}, labels).With(labelsAndValues...),
   166  		TotalTxs: fastmetrics.NewGaugeFrom(stdprometheus.GaugeOpts{
   167  			Namespace: namespace,
   168  			Subsystem: MetricsSubsystem,
   169  			Name:      "total_txs",
   170  			Help:      "Total number of transactions.",
   171  		}, labels).With(labelsAndValues...),
   172  		CommittedHeight: fastmetrics.NewGaugeFrom(stdprometheus.GaugeOpts{
   173  			Namespace: namespace,
   174  			Subsystem: MetricsSubsystem,
   175  			Name:      "latest_block_height",
   176  			Help:      "The latest block height.",
   177  		}, labels).With(labelsAndValues...),
   178  		FastSyncing: fastmetrics.NewGaugeFrom(stdprometheus.GaugeOpts{
   179  			Namespace: namespace,
   180  			Subsystem: MetricsSubsystem,
   181  			Name:      "fast_syncing",
   182  			Help:      "Whether or not a node is fast 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  		NewRoundProcessingTime: prometheus.NewGaugeFrom(stdprometheus.GaugeOpts{
   191  			Namespace: namespace,
   192  			Subsystem: MetricsSubsystem,
   193  			Name:      "new_round_processing_time",
   194  			Help:      "Time about new round",
   195  		}, labels).With(labelsAndValues...),
   196  		ProposeProcessingTime: prometheus.NewGaugeFrom(stdprometheus.GaugeOpts{
   197  			Namespace: namespace,
   198  			Subsystem: MetricsSubsystem,
   199  			Name:      "propose_processing_time",
   200  			Help:      "Time about propose",
   201  		}, labels).With(labelsAndValues...),
   202  		PrevoteProcessingTime: prometheus.NewGaugeFrom(stdprometheus.GaugeOpts{
   203  			Namespace: namespace,
   204  			Subsystem: MetricsSubsystem,
   205  			Name:      "prevote_processing_time",
   206  			Help:      "Time about prevote",
   207  		}, labels).With(labelsAndValues...),
   208  		PrecommitProcessingTime: prometheus.NewGaugeFrom(stdprometheus.GaugeOpts{
   209  			Namespace: namespace,
   210  			Subsystem: MetricsSubsystem,
   211  			Name:      "precommit_processing_time",
   212  			Help:      "Time about precommit",
   213  		}, labels).With(labelsAndValues...),
   214  		CommitProcessingTime: prometheus.NewGaugeFrom(stdprometheus.GaugeOpts{
   215  			Namespace: namespace,
   216  			Subsystem: MetricsSubsystem,
   217  			Name:      "commit_processing_time",
   218  			Help:      "Time about commit",
   219  		}, labels).With(labelsAndValues...),
   220  	}
   221  }
   222  
   223  // NopMetrics returns no-op Metrics.
   224  func NopMetrics() *Metrics {
   225  	return &Metrics{
   226  		Height: discard.NewGauge(),
   227  
   228  		ValidatorLastSignedHeight: discard.NewGauge(),
   229  
   230  		Rounds: discard.NewGauge(),
   231  
   232  		Validators:               discard.NewGauge(),
   233  		ValidatorsPower:          discard.NewGauge(),
   234  		ValidatorPower:           discard.NewGauge(),
   235  		ValidatorMissedBlocks:    discard.NewGauge(),
   236  		MissingValidators:        discard.NewGauge(),
   237  		MissingValidatorsPower:   discard.NewGauge(),
   238  		ByzantineValidators:      discard.NewGauge(),
   239  		ByzantineValidatorsPower: discard.NewGauge(),
   240  
   241  		BlockIntervalSeconds: discard.NewGauge(),
   242  
   243  		NumTxs:          discard.NewGauge(),
   244  		BlockSizeBytes:  discard.NewGauge(),
   245  		TotalTxs:        discard.NewGauge(),
   246  		CommittedHeight: discard.NewGauge(),
   247  		FastSyncing:     discard.NewGauge(),
   248  		BlockParts:      discard.NewCounter(),
   249  
   250  		NewRoundProcessingTime:  discard.NewGauge(),
   251  		ProposeProcessingTime:   discard.NewGauge(),
   252  		PrevoteProcessingTime:   discard.NewGauge(),
   253  		PrecommitProcessingTime: discard.NewGauge(),
   254  		CommitProcessingTime:    discard.NewGauge(),
   255  	}
   256  }