You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

229 lines
8.1 KiB

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