github.com/pure-x-eth/consensus_tm@v0.0.0-20230502163723-e3c2ff987250/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  	// QuroumPrevoteMessageDelay is the interval in seconds between the proposal
    65  	// timestamp and the timestamp of the earliest prevote that achieved a quorum
    66  	// during the prevote step.
    67  	//
    68  	// To compute it, sum the voting power over each prevote received, in increasing
    69  	// order of timestamp. The timestamp of the first prevote to increase the sum to
    70  	// be above 2/3 of the total voting power of the network defines the endpoint
    71  	// the endpoint of the interval. Subtract the proposal timestamp from this endpoint
    72  	// to obtain the quorum delay.
    73  	QuorumPrevoteMessageDelay metrics.Gauge
    74  
    75  	// FullPrevoteMessageDelay is the interval in seconds between the proposal
    76  	// timestamp and the timestamp of the latest prevote in a round where 100%
    77  	// of the voting power on the network issued prevotes.
    78  	FullPrevoteMessageDelay metrics.Gauge
    79  }
    80  
    81  // PrometheusMetrics returns Metrics build using Prometheus client library.
    82  // Optionally, labels can be provided along with their values ("foo",
    83  // "fooValue").
    84  func PrometheusMetrics(namespace string, labelsAndValues ...string) *Metrics {
    85  	labels := []string{}
    86  	for i := 0; i < len(labelsAndValues); i += 2 {
    87  		labels = append(labels, labelsAndValues[i])
    88  	}
    89  	return &Metrics{
    90  		Height: prometheus.NewGaugeFrom(stdprometheus.GaugeOpts{
    91  			Namespace: namespace,
    92  			Subsystem: MetricsSubsystem,
    93  			Name:      "height",
    94  			Help:      "Height of the chain.",
    95  		}, labels).With(labelsAndValues...),
    96  		Rounds: prometheus.NewGaugeFrom(stdprometheus.GaugeOpts{
    97  			Namespace: namespace,
    98  			Subsystem: MetricsSubsystem,
    99  			Name:      "rounds",
   100  			Help:      "Number of rounds.",
   101  		}, labels).With(labelsAndValues...),
   102  
   103  		Validators: prometheus.NewGaugeFrom(stdprometheus.GaugeOpts{
   104  			Namespace: namespace,
   105  			Subsystem: MetricsSubsystem,
   106  			Name:      "validators",
   107  			Help:      "Number of validators.",
   108  		}, labels).With(labelsAndValues...),
   109  		ValidatorLastSignedHeight: prometheus.NewGaugeFrom(stdprometheus.GaugeOpts{
   110  			Namespace: namespace,
   111  			Subsystem: MetricsSubsystem,
   112  			Name:      "validator_last_signed_height",
   113  			Help:      "Last signed height for a validator",
   114  		}, append(labels, "validator_address")).With(labelsAndValues...),
   115  		ValidatorMissedBlocks: prometheus.NewGaugeFrom(stdprometheus.GaugeOpts{
   116  			Namespace: namespace,
   117  			Subsystem: MetricsSubsystem,
   118  			Name:      "validator_missed_blocks",
   119  			Help:      "Total missed blocks for a validator",
   120  		}, append(labels, "validator_address")).With(labelsAndValues...),
   121  		ValidatorsPower: prometheus.NewGaugeFrom(stdprometheus.GaugeOpts{
   122  			Namespace: namespace,
   123  			Subsystem: MetricsSubsystem,
   124  			Name:      "validators_power",
   125  			Help:      "Total power of all validators.",
   126  		}, labels).With(labelsAndValues...),
   127  		ValidatorPower: prometheus.NewGaugeFrom(stdprometheus.GaugeOpts{
   128  			Namespace: namespace,
   129  			Subsystem: MetricsSubsystem,
   130  			Name:      "validator_power",
   131  			Help:      "Power of a validator",
   132  		}, append(labels, "validator_address")).With(labelsAndValues...),
   133  		MissingValidators: prometheus.NewGaugeFrom(stdprometheus.GaugeOpts{
   134  			Namespace: namespace,
   135  			Subsystem: MetricsSubsystem,
   136  			Name:      "missing_validators",
   137  			Help:      "Number of validators who did not sign.",
   138  		}, labels).With(labelsAndValues...),
   139  		MissingValidatorsPower: prometheus.NewGaugeFrom(stdprometheus.GaugeOpts{
   140  			Namespace: namespace,
   141  			Subsystem: MetricsSubsystem,
   142  			Name:      "missing_validators_power",
   143  			Help:      "Total power of the missing validators.",
   144  		}, labels).With(labelsAndValues...),
   145  		ByzantineValidators: prometheus.NewGaugeFrom(stdprometheus.GaugeOpts{
   146  			Namespace: namespace,
   147  			Subsystem: MetricsSubsystem,
   148  			Name:      "byzantine_validators",
   149  			Help:      "Number of validators who tried to double sign.",
   150  		}, labels).With(labelsAndValues...),
   151  		ByzantineValidatorsPower: prometheus.NewGaugeFrom(stdprometheus.GaugeOpts{
   152  			Namespace: namespace,
   153  			Subsystem: MetricsSubsystem,
   154  			Name:      "byzantine_validators_power",
   155  			Help:      "Total power of the byzantine validators.",
   156  		}, labels).With(labelsAndValues...),
   157  		BlockIntervalSeconds: prometheus.NewHistogramFrom(stdprometheus.HistogramOpts{
   158  			Namespace: namespace,
   159  			Subsystem: MetricsSubsystem,
   160  			Name:      "block_interval_seconds",
   161  			Help:      "Time between this and the last block.",
   162  		}, labels).With(labelsAndValues...),
   163  		NumTxs: prometheus.NewGaugeFrom(stdprometheus.GaugeOpts{
   164  			Namespace: namespace,
   165  			Subsystem: MetricsSubsystem,
   166  			Name:      "num_txs",
   167  			Help:      "Number of transactions.",
   168  		}, labels).With(labelsAndValues...),
   169  		BlockSizeBytes: prometheus.NewGaugeFrom(stdprometheus.GaugeOpts{
   170  			Namespace: namespace,
   171  			Subsystem: MetricsSubsystem,
   172  			Name:      "block_size_bytes",
   173  			Help:      "Size of the block.",
   174  		}, labels).With(labelsAndValues...),
   175  		TotalTxs: prometheus.NewGaugeFrom(stdprometheus.GaugeOpts{
   176  			Namespace: namespace,
   177  			Subsystem: MetricsSubsystem,
   178  			Name:      "total_txs",
   179  			Help:      "Total number of transactions.",
   180  		}, labels).With(labelsAndValues...),
   181  		CommittedHeight: prometheus.NewGaugeFrom(stdprometheus.GaugeOpts{
   182  			Namespace: namespace,
   183  			Subsystem: MetricsSubsystem,
   184  			Name:      "latest_block_height",
   185  			Help:      "The latest block height.",
   186  		}, labels).With(labelsAndValues...),
   187  		FastSyncing: prometheus.NewGaugeFrom(stdprometheus.GaugeOpts{
   188  			Namespace: namespace,
   189  			Subsystem: MetricsSubsystem,
   190  			Name:      "fast_syncing",
   191  			Help:      "Whether or not a node is fast syncing. 1 if yes, 0 if no.",
   192  		}, labels).With(labelsAndValues...),
   193  		StateSyncing: prometheus.NewGaugeFrom(stdprometheus.GaugeOpts{
   194  			Namespace: namespace,
   195  			Subsystem: MetricsSubsystem,
   196  			Name:      "state_syncing",
   197  			Help:      "Whether or not a node is state syncing. 1 if yes, 0 if no.",
   198  		}, labels).With(labelsAndValues...),
   199  		BlockParts: prometheus.NewCounterFrom(stdprometheus.CounterOpts{
   200  			Namespace: namespace,
   201  			Subsystem: MetricsSubsystem,
   202  			Name:      "block_parts",
   203  			Help:      "Number of blockparts transmitted by peer.",
   204  		}, append(labels, "peer_id")).With(labelsAndValues...),
   205  		QuorumPrevoteMessageDelay: prometheus.NewGaugeFrom(stdprometheus.GaugeOpts{
   206  			Namespace: namespace,
   207  			Subsystem: MetricsSubsystem,
   208  			Name:      "quorum_prevote_message_delay",
   209  			Help: "Difference in seconds between the proposal timestamp and the timestamp " +
   210  				"of the latest prevote that achieved a quorum in the prevote step.",
   211  		}, labels).With(labelsAndValues...),
   212  		FullPrevoteMessageDelay: prometheus.NewGaugeFrom(stdprometheus.GaugeOpts{
   213  			Namespace: namespace,
   214  			Subsystem: MetricsSubsystem,
   215  			Name:      "full_prevote_message_delay",
   216  			Help: "Difference in seconds between the proposal timestamp and the timestamp " +
   217  				"of the latest prevote that achieved 100% of the voting power in the prevote step.",
   218  		}, labels).With(labelsAndValues...),
   219  	}
   220  }
   221  
   222  // NopMetrics returns no-op Metrics.
   223  func NopMetrics() *Metrics {
   224  	return &Metrics{
   225  		Height: discard.NewGauge(),
   226  
   227  		ValidatorLastSignedHeight: discard.NewGauge(),
   228  
   229  		Rounds: discard.NewGauge(),
   230  
   231  		Validators:               discard.NewGauge(),
   232  		ValidatorsPower:          discard.NewGauge(),
   233  		ValidatorPower:           discard.NewGauge(),
   234  		ValidatorMissedBlocks:    discard.NewGauge(),
   235  		MissingValidators:        discard.NewGauge(),
   236  		MissingValidatorsPower:   discard.NewGauge(),
   237  		ByzantineValidators:      discard.NewGauge(),
   238  		ByzantineValidatorsPower: discard.NewGauge(),
   239  
   240  		BlockIntervalSeconds: discard.NewHistogram(),
   241  
   242  		NumTxs:                    discard.NewGauge(),
   243  		BlockSizeBytes:            discard.NewGauge(),
   244  		TotalTxs:                  discard.NewGauge(),
   245  		CommittedHeight:           discard.NewGauge(),
   246  		FastSyncing:               discard.NewGauge(),
   247  		StateSyncing:              discard.NewGauge(),
   248  		BlockParts:                discard.NewCounter(),
   249  		QuorumPrevoteMessageDelay: discard.NewGauge(),
   250  		FullPrevoteMessageDelay:   discard.NewGauge(),
   251  	}
   252  }