github.com/Finschia/ostracon@v1.1.5/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  	"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.Gauge
    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  	// Metrics for measuring performance
    82  	// ////////////////////////////////////
    83  
    84  	// Number of blocks that are we couldn't receive
    85  	MissingProposal metrics.Gauge
    86  
    87  	// Number of rounds turned over.
    88  	RoundFailures metrics.Histogram
    89  
    90  	// Execution time profiling of each step
    91  	DurationProposal           metrics.Histogram
    92  	DurationPrevote            metrics.Histogram
    93  	DurationPrecommit          metrics.Histogram
    94  	DurationCommitExecuting    metrics.Histogram
    95  	DurationCommitCommitting   metrics.Histogram
    96  	DurationCommitRechecking   metrics.Histogram
    97  	DurationWaitingForNewRound metrics.Histogram
    98  
    99  	DurationGaugeProposal           metrics.Gauge
   100  	DurationGaugePrevote            metrics.Gauge
   101  	DurationGaugePrecommit          metrics.Gauge
   102  	DurationGaugeCommitExecuting    metrics.Gauge
   103  	DurationGaugeCommitCommitting   metrics.Gauge
   104  	DurationGaugeCommitRechecking   metrics.Gauge
   105  	DurationGaugeWaitingForNewRound metrics.Gauge
   106  }
   107  
   108  // PrometheusMetrics returns Metrics build using Prometheus client library.
   109  // Optionally, labels can be provided along with their values ("foo",
   110  // "fooValue").
   111  func PrometheusMetrics(namespace string, labelsAndValues ...string) *Metrics {
   112  	labels := []string{}
   113  	for i := 0; i < len(labelsAndValues); i += 2 {
   114  		labels = append(labels, labelsAndValues[i])
   115  	}
   116  	return &Metrics{
   117  		Height: prometheus.NewGaugeFrom(stdprometheus.GaugeOpts{
   118  			Namespace: namespace,
   119  			Subsystem: MetricsSubsystem,
   120  			Name:      "height",
   121  			Help:      "Height of the chain.",
   122  		}, labels).With(labelsAndValues...),
   123  		Rounds: prometheus.NewGaugeFrom(stdprometheus.GaugeOpts{
   124  			Namespace: namespace,
   125  			Subsystem: MetricsSubsystem,
   126  			Name:      "rounds",
   127  			Help:      "Number of rounds.",
   128  		}, labels).With(labelsAndValues...),
   129  
   130  		Validators: prometheus.NewGaugeFrom(stdprometheus.GaugeOpts{
   131  			Namespace: namespace,
   132  			Subsystem: MetricsSubsystem,
   133  			Name:      "validators",
   134  			Help:      "Number of validators.",
   135  		}, labels).With(labelsAndValues...),
   136  		ValidatorLastSignedHeight: prometheus.NewGaugeFrom(stdprometheus.GaugeOpts{
   137  			Namespace: namespace,
   138  			Subsystem: MetricsSubsystem,
   139  			Name:      "validator_last_signed_height",
   140  			Help:      "Last signed height for a validator",
   141  		}, append(labels, "validator_address")).With(labelsAndValues...),
   142  		ValidatorMissedBlocks: prometheus.NewGaugeFrom(stdprometheus.GaugeOpts{
   143  			Namespace: namespace,
   144  			Subsystem: MetricsSubsystem,
   145  			Name:      "validator_missed_blocks",
   146  			Help:      "Total missed blocks for a validator",
   147  		}, append(labels, "validator_address")).With(labelsAndValues...),
   148  		ValidatorsPower: prometheus.NewGaugeFrom(stdprometheus.GaugeOpts{
   149  			Namespace: namespace,
   150  			Subsystem: MetricsSubsystem,
   151  			Name:      "validators_power",
   152  			Help:      "Total power of all validators.",
   153  		}, labels).With(labelsAndValues...),
   154  		ValidatorPower: prometheus.NewGaugeFrom(stdprometheus.GaugeOpts{
   155  			Namespace: namespace,
   156  			Subsystem: MetricsSubsystem,
   157  			Name:      "validator_power",
   158  			Help:      "Power of a validator",
   159  		}, append(labels, "validator_address")).With(labelsAndValues...),
   160  		MissingValidators: prometheus.NewGaugeFrom(stdprometheus.GaugeOpts{
   161  			Namespace: namespace,
   162  			Subsystem: MetricsSubsystem,
   163  			Name:      "missing_validators",
   164  			Help:      "Number of validators who did not sign.",
   165  		}, labels).With(labelsAndValues...),
   166  		MissingValidatorsPower: prometheus.NewGaugeFrom(stdprometheus.GaugeOpts{
   167  			Namespace: namespace,
   168  			Subsystem: MetricsSubsystem,
   169  			Name:      "missing_validators_power",
   170  			Help:      "Total power of the missing validators.",
   171  		}, labels).With(labelsAndValues...),
   172  		ByzantineValidators: prometheus.NewGaugeFrom(stdprometheus.GaugeOpts{
   173  			Namespace: namespace,
   174  			Subsystem: MetricsSubsystem,
   175  			Name:      "byzantine_validators",
   176  			Help:      "Number of validators who tried to double sign.",
   177  		}, labels).With(labelsAndValues...),
   178  		ByzantineValidatorsPower: prometheus.NewGaugeFrom(stdprometheus.GaugeOpts{
   179  			Namespace: namespace,
   180  			Subsystem: MetricsSubsystem,
   181  			Name:      "byzantine_validators_power",
   182  			Help:      "Total power of the byzantine validators.",
   183  		}, labels).With(labelsAndValues...),
   184  		BlockIntervalSeconds: prometheus.NewGaugeFrom(stdprometheus.GaugeOpts{
   185  			Namespace: namespace,
   186  			Subsystem: MetricsSubsystem,
   187  			Name:      "block_interval_seconds",
   188  			Help:      "Time between this and the last block.",
   189  		}, labels).With(labelsAndValues...),
   190  		NumTxs: prometheus.NewGaugeFrom(stdprometheus.GaugeOpts{
   191  			Namespace: namespace,
   192  			Subsystem: MetricsSubsystem,
   193  			Name:      "num_txs",
   194  			Help:      "Number of transactions.",
   195  		}, labels).With(labelsAndValues...),
   196  		BlockSizeBytes: prometheus.NewGaugeFrom(stdprometheus.GaugeOpts{
   197  			Namespace: namespace,
   198  			Subsystem: MetricsSubsystem,
   199  			Name:      "block_size_bytes",
   200  			Help:      "Size of the block.",
   201  		}, labels).With(labelsAndValues...),
   202  		TotalTxs: prometheus.NewGaugeFrom(stdprometheus.GaugeOpts{
   203  			Namespace: namespace,
   204  			Subsystem: MetricsSubsystem,
   205  			Name:      "total_txs",
   206  			Help:      "Total number of transactions.",
   207  		}, labels).With(labelsAndValues...),
   208  		CommittedHeight: prometheus.NewGaugeFrom(stdprometheus.GaugeOpts{
   209  			Namespace: namespace,
   210  			Subsystem: MetricsSubsystem,
   211  			Name:      "latest_block_height",
   212  			Help:      "The latest block height.",
   213  		}, labels).With(labelsAndValues...),
   214  		FastSyncing: prometheus.NewGaugeFrom(stdprometheus.GaugeOpts{
   215  			Namespace: namespace,
   216  			Subsystem: MetricsSubsystem,
   217  			Name:      "fast_syncing",
   218  			Help:      "Whether or not a node is fast syncing. 1 if yes, 0 if no.",
   219  		}, labels).With(labelsAndValues...),
   220  		StateSyncing: prometheus.NewGaugeFrom(stdprometheus.GaugeOpts{
   221  			Namespace: namespace,
   222  			Subsystem: MetricsSubsystem,
   223  			Name:      "state_syncing",
   224  			Help:      "Whether or not a node is state syncing. 1 if yes, 0 if no.",
   225  		}, labels).With(labelsAndValues...),
   226  		BlockParts: prometheus.NewCounterFrom(stdprometheus.CounterOpts{
   227  			Namespace: namespace,
   228  			Subsystem: MetricsSubsystem,
   229  			Name:      "block_parts",
   230  			Help:      "Number of blockparts transmitted by peer.",
   231  		}, append(labels, "peer_id")).With(labelsAndValues...),
   232  		QuorumPrevoteMessageDelay: prometheus.NewGaugeFrom(stdprometheus.GaugeOpts{
   233  			Namespace: namespace,
   234  			Subsystem: MetricsSubsystem,
   235  			Name:      "quorum_prevote_message_delay",
   236  			Help: "Difference in seconds between the proposal timestamp and the timestamp " +
   237  				"of the latest prevote that achieved a quorum in the prevote step.",
   238  		}, labels).With(labelsAndValues...),
   239  		FullPrevoteMessageDelay: prometheus.NewGaugeFrom(stdprometheus.GaugeOpts{
   240  			Namespace: namespace,
   241  			Subsystem: MetricsSubsystem,
   242  			Name:      "full_prevote_message_delay",
   243  			Help: "Difference in seconds between the proposal timestamp and the timestamp " +
   244  				"of the latest prevote that achieved 100% of the voting power in the prevote step.",
   245  		}, labels).With(labelsAndValues...),
   246  		MissingProposal: prometheus.NewGaugeFrom(stdprometheus.GaugeOpts{
   247  			Namespace: namespace,
   248  			Subsystem: MetricsSubsystem,
   249  			Name:      "missing_proposal",
   250  			Help:      "Number of blocks we couldn't receive",
   251  		}, labels).With(labelsAndValues...),
   252  		RoundFailures: prometheus.NewHistogramFrom(stdprometheus.HistogramOpts{
   253  			Namespace: namespace,
   254  			Subsystem: MetricsSubsystem,
   255  			Name:      "round_failures",
   256  			Help:      "Number of rounds failed on consensus",
   257  			Buckets:   stdprometheus.LinearBuckets(0, 1, 5),
   258  		}, labels).With(labelsAndValues...),
   259  		DurationProposal: prometheus.NewHistogramFrom(stdprometheus.HistogramOpts{
   260  			Namespace: namespace,
   261  			Subsystem: MetricsSubsystem,
   262  			Name:      "duration_proposal",
   263  			Help:      "Duration of proposal step",
   264  			Buckets:   stdprometheus.LinearBuckets(100, 100, 10),
   265  		}, labels).With(labelsAndValues...),
   266  		DurationPrevote: prometheus.NewHistogramFrom(stdprometheus.HistogramOpts{
   267  			Namespace: namespace,
   268  			Subsystem: MetricsSubsystem,
   269  			Name:      "duration_prevote",
   270  			Help:      "Duration of prevote step",
   271  			Buckets:   stdprometheus.LinearBuckets(100, 100, 10),
   272  		}, labels).With(labelsAndValues...),
   273  		DurationPrecommit: prometheus.NewHistogramFrom(stdprometheus.HistogramOpts{
   274  			Namespace: namespace,
   275  			Subsystem: MetricsSubsystem,
   276  			Name:      "duration_precommit",
   277  			Help:      "Duration of precommit step",
   278  			Buckets:   stdprometheus.LinearBuckets(100, 100, 10),
   279  		}, labels).With(labelsAndValues...),
   280  		DurationCommitExecuting: prometheus.NewHistogramFrom(stdprometheus.HistogramOpts{
   281  			Namespace: namespace,
   282  			Subsystem: MetricsSubsystem,
   283  			Name:      "duration_commit_executing",
   284  			Help:      "Duration of executing block txs",
   285  			Buckets:   stdprometheus.LinearBuckets(100, 100, 10),
   286  		}, labels).With(labelsAndValues...),
   287  		DurationCommitCommitting: prometheus.NewHistogramFrom(stdprometheus.HistogramOpts{
   288  			Namespace: namespace,
   289  			Subsystem: MetricsSubsystem,
   290  			Name:      "duration_commit_committing",
   291  			Help:      "Duration of committing updated state",
   292  			Buckets:   stdprometheus.LinearBuckets(100, 100, 10),
   293  		}, labels).With(labelsAndValues...),
   294  		DurationCommitRechecking: prometheus.NewHistogramFrom(stdprometheus.HistogramOpts{
   295  			Namespace: namespace,
   296  			Subsystem: MetricsSubsystem,
   297  			Name:      "duration_commit_rechecking",
   298  			Help:      "Duration of rechecking mempool txs",
   299  			Buckets:   stdprometheus.LinearBuckets(100, 100, 10),
   300  		}, labels).With(labelsAndValues...),
   301  		DurationWaitingForNewRound: prometheus.NewHistogramFrom(stdprometheus.HistogramOpts{
   302  			Namespace: namespace,
   303  			Subsystem: MetricsSubsystem,
   304  			Name:      "duration_waiting_for_new_round",
   305  			Help:      "Duration of waiting for next new round",
   306  			Buckets:   stdprometheus.LinearBuckets(100, 100, 10),
   307  		}, labels).With(labelsAndValues...),
   308  		DurationGaugeProposal: prometheus.NewGaugeFrom(stdprometheus.GaugeOpts{
   309  			Namespace: namespace,
   310  			Subsystem: MetricsSubsystem,
   311  			Name:      "duration_gauge_proposal",
   312  			Help:      "Duration of proposal step",
   313  		}, labels).With(labelsAndValues...),
   314  		DurationGaugePrevote: prometheus.NewGaugeFrom(stdprometheus.GaugeOpts{
   315  			Namespace: namespace,
   316  			Subsystem: MetricsSubsystem,
   317  			Name:      "duration_gauge_prevote",
   318  			Help:      "Duration of prevote step",
   319  		}, labels).With(labelsAndValues...),
   320  		DurationGaugePrecommit: prometheus.NewGaugeFrom(stdprometheus.GaugeOpts{
   321  			Namespace: namespace,
   322  			Subsystem: MetricsSubsystem,
   323  			Name:      "duration_gauge_precommit",
   324  			Help:      "Duration of precommit step",
   325  		}, labels).With(labelsAndValues...),
   326  		DurationGaugeCommitExecuting: prometheus.NewGaugeFrom(stdprometheus.GaugeOpts{
   327  			Namespace: namespace,
   328  			Subsystem: MetricsSubsystem,
   329  			Name:      "duration_gauge_commit_executing",
   330  			Help:      "Duration of executing block txs",
   331  		}, labels).With(labelsAndValues...),
   332  		DurationGaugeCommitCommitting: prometheus.NewGaugeFrom(stdprometheus.GaugeOpts{
   333  			Namespace: namespace,
   334  			Subsystem: MetricsSubsystem,
   335  			Name:      "duration_gauge_commit_committing",
   336  			Help:      "Duration of committing updated state",
   337  		}, labels).With(labelsAndValues...),
   338  		DurationGaugeCommitRechecking: prometheus.NewGaugeFrom(stdprometheus.GaugeOpts{
   339  			Namespace: namespace,
   340  			Subsystem: MetricsSubsystem,
   341  			Name:      "duration_gauge_commit_rechecking",
   342  			Help:      "Duration of rechecking mempool txs",
   343  		}, labels).With(labelsAndValues...),
   344  		DurationGaugeWaitingForNewRound: prometheus.NewGaugeFrom(stdprometheus.GaugeOpts{
   345  			Namespace: namespace,
   346  			Subsystem: MetricsSubsystem,
   347  			Name:      "duration_gauge_waiting_for_new_round",
   348  			Help:      "Duration of waiting for next new round",
   349  		}, labels).With(labelsAndValues...),
   350  	}
   351  }
   352  
   353  // NopMetrics returns no-op Metrics.
   354  func NopMetrics() *Metrics {
   355  	return &Metrics{
   356  		Height: discard.NewGauge(),
   357  
   358  		ValidatorLastSignedHeight: discard.NewGauge(),
   359  
   360  		Rounds: discard.NewGauge(),
   361  
   362  		Validators:               discard.NewGauge(),
   363  		ValidatorsPower:          discard.NewGauge(),
   364  		ValidatorPower:           discard.NewGauge(),
   365  		ValidatorMissedBlocks:    discard.NewGauge(),
   366  		MissingValidators:        discard.NewGauge(),
   367  		MissingValidatorsPower:   discard.NewGauge(),
   368  		ByzantineValidators:      discard.NewGauge(),
   369  		ByzantineValidatorsPower: discard.NewGauge(),
   370  
   371  		BlockIntervalSeconds: discard.NewGauge(),
   372  
   373  		NumTxs:                    discard.NewGauge(),
   374  		BlockSizeBytes:            discard.NewGauge(),
   375  		TotalTxs:                  discard.NewGauge(),
   376  		CommittedHeight:           discard.NewGauge(),
   377  		FastSyncing:               discard.NewGauge(),
   378  		StateSyncing:              discard.NewGauge(),
   379  		BlockParts:                discard.NewCounter(),
   380  		QuorumPrevoteMessageDelay: discard.NewGauge(),
   381  		FullPrevoteMessageDelay:   discard.NewGauge(),
   382  
   383  		MissingProposal: discard.NewGauge(),
   384  		RoundFailures:   discard.NewHistogram(),
   385  
   386  		DurationProposal:           discard.NewHistogram(),
   387  		DurationPrevote:            discard.NewHistogram(),
   388  		DurationPrecommit:          discard.NewHistogram(),
   389  		DurationCommitExecuting:    discard.NewHistogram(),
   390  		DurationCommitCommitting:   discard.NewHistogram(),
   391  		DurationCommitRechecking:   discard.NewHistogram(),
   392  		DurationWaitingForNewRound: discard.NewHistogram(),
   393  
   394  		DurationGaugeProposal:           discard.NewGauge(),
   395  		DurationGaugePrevote:            discard.NewGauge(),
   396  		DurationGaugePrecommit:          discard.NewGauge(),
   397  		DurationGaugeCommitExecuting:    discard.NewGauge(),
   398  		DurationGaugeCommitCommitting:   discard.NewGauge(),
   399  		DurationGaugeCommitRechecking:   discard.NewGauge(),
   400  		DurationGaugeWaitingForNewRound: discard.NewGauge(),
   401  	}
   402  }