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.

222 lines
7.7 KiB

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