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.

184 lines
6.1 KiB

7 years ago
7 years ago
7 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. // Number of rounds.
  18. Rounds metrics.Gauge
  19. // Number of validators.
  20. Validators metrics.Gauge
  21. // Total power of all validators.
  22. ValidatorsPower metrics.Gauge
  23. // Number of validators who did not sign.
  24. MissingValidators metrics.Gauge
  25. // Total power of the missing validators.
  26. MissingValidatorsPower metrics.Gauge
  27. // Number of validators who tried to double sign.
  28. ByzantineValidators metrics.Gauge
  29. // Total power of the byzantine validators.
  30. ByzantineValidatorsPower metrics.Gauge
  31. // Time between this and the last block.
  32. BlockIntervalSeconds metrics.Gauge
  33. // Number of transactions.
  34. NumTxs metrics.Gauge
  35. // Size of the block.
  36. BlockSizeBytes metrics.Gauge
  37. // Total number of transactions.
  38. TotalTxs metrics.Gauge
  39. // The latest block height.
  40. CommittedHeight metrics.Gauge
  41. // Whether or not a node is fast syncing. 1 if yes, 0 if no.
  42. FastSyncing metrics.Gauge
  43. // Number of blockparts transmitted by peer.
  44. BlockParts metrics.Counter
  45. }
  46. // PrometheusMetrics returns Metrics build using Prometheus client library.
  47. // Optionally, labels can be provided along with their values ("foo",
  48. // "fooValue").
  49. func PrometheusMetrics(namespace string, labelsAndValues ...string) *Metrics {
  50. labels := []string{}
  51. for i := 0; i < len(labelsAndValues); i += 2 {
  52. labels = append(labels, labelsAndValues[i])
  53. }
  54. return &Metrics{
  55. Height: prometheus.NewGaugeFrom(stdprometheus.GaugeOpts{
  56. Namespace: namespace,
  57. Subsystem: MetricsSubsystem,
  58. Name: "height",
  59. Help: "Height of the chain.",
  60. }, labels).With(labelsAndValues...),
  61. Rounds: prometheus.NewGaugeFrom(stdprometheus.GaugeOpts{
  62. Namespace: namespace,
  63. Subsystem: MetricsSubsystem,
  64. Name: "rounds",
  65. Help: "Number of rounds.",
  66. }, labels).With(labelsAndValues...),
  67. Validators: prometheus.NewGaugeFrom(stdprometheus.GaugeOpts{
  68. Namespace: namespace,
  69. Subsystem: MetricsSubsystem,
  70. Name: "validators",
  71. Help: "Number of validators.",
  72. }, labels).With(labelsAndValues...),
  73. ValidatorsPower: prometheus.NewGaugeFrom(stdprometheus.GaugeOpts{
  74. Namespace: namespace,
  75. Subsystem: MetricsSubsystem,
  76. Name: "validators_power",
  77. Help: "Total power of all validators.",
  78. }, labels).With(labelsAndValues...),
  79. MissingValidators: prometheus.NewGaugeFrom(stdprometheus.GaugeOpts{
  80. Namespace: namespace,
  81. Subsystem: MetricsSubsystem,
  82. Name: "missing_validators",
  83. Help: "Number of validators who did not sign.",
  84. }, labels).With(labelsAndValues...),
  85. MissingValidatorsPower: prometheus.NewGaugeFrom(stdprometheus.GaugeOpts{
  86. Namespace: namespace,
  87. Subsystem: MetricsSubsystem,
  88. Name: "missing_validators_power",
  89. Help: "Total power of the missing validators.",
  90. }, labels).With(labelsAndValues...),
  91. ByzantineValidators: prometheus.NewGaugeFrom(stdprometheus.GaugeOpts{
  92. Namespace: namespace,
  93. Subsystem: MetricsSubsystem,
  94. Name: "byzantine_validators",
  95. Help: "Number of validators who tried to double sign.",
  96. }, labels).With(labelsAndValues...),
  97. ByzantineValidatorsPower: prometheus.NewGaugeFrom(stdprometheus.GaugeOpts{
  98. Namespace: namespace,
  99. Subsystem: MetricsSubsystem,
  100. Name: "byzantine_validators_power",
  101. Help: "Total power of the byzantine validators.",
  102. }, labels).With(labelsAndValues...),
  103. BlockIntervalSeconds: prometheus.NewGaugeFrom(stdprometheus.GaugeOpts{
  104. Namespace: namespace,
  105. Subsystem: MetricsSubsystem,
  106. Name: "block_interval_seconds",
  107. Help: "Time between this and the last block.",
  108. }, labels).With(labelsAndValues...),
  109. NumTxs: prometheus.NewGaugeFrom(stdprometheus.GaugeOpts{
  110. Namespace: namespace,
  111. Subsystem: MetricsSubsystem,
  112. Name: "num_txs",
  113. Help: "Number of transactions.",
  114. }, labels).With(labelsAndValues...),
  115. BlockSizeBytes: prometheus.NewGaugeFrom(stdprometheus.GaugeOpts{
  116. Namespace: namespace,
  117. Subsystem: MetricsSubsystem,
  118. Name: "block_size_bytes",
  119. Help: "Size of the block.",
  120. }, labels).With(labelsAndValues...),
  121. TotalTxs: prometheus.NewGaugeFrom(stdprometheus.GaugeOpts{
  122. Namespace: namespace,
  123. Subsystem: MetricsSubsystem,
  124. Name: "total_txs",
  125. Help: "Total number of transactions.",
  126. }, labels).With(labelsAndValues...),
  127. CommittedHeight: prometheus.NewGaugeFrom(stdprometheus.GaugeOpts{
  128. Namespace: namespace,
  129. Subsystem: MetricsSubsystem,
  130. Name: "latest_block_height",
  131. Help: "The latest block height.",
  132. }, labels).With(labelsAndValues...),
  133. FastSyncing: prometheus.NewGaugeFrom(stdprometheus.GaugeOpts{
  134. Namespace: namespace,
  135. Subsystem: MetricsSubsystem,
  136. Name: "fast_syncing",
  137. Help: "Whether or not a node is fast syncing. 1 if yes, 0 if no.",
  138. }, labels).With(labelsAndValues...),
  139. BlockParts: prometheus.NewCounterFrom(stdprometheus.CounterOpts{
  140. Namespace: namespace,
  141. Subsystem: MetricsSubsystem,
  142. Name: "block_parts",
  143. Help: "Number of blockparts transmitted by peer.",
  144. }, append(labels, "peer_id")).With(labelsAndValues...),
  145. }
  146. }
  147. // NopMetrics returns no-op Metrics.
  148. func NopMetrics() *Metrics {
  149. return &Metrics{
  150. Height: discard.NewGauge(),
  151. Rounds: discard.NewGauge(),
  152. Validators: discard.NewGauge(),
  153. ValidatorsPower: discard.NewGauge(),
  154. MissingValidators: discard.NewGauge(),
  155. MissingValidatorsPower: discard.NewGauge(),
  156. ByzantineValidators: discard.NewGauge(),
  157. ByzantineValidatorsPower: discard.NewGauge(),
  158. BlockIntervalSeconds: discard.NewGauge(),
  159. NumTxs: discard.NewGauge(),
  160. BlockSizeBytes: discard.NewGauge(),
  161. TotalTxs: discard.NewGauge(),
  162. CommittedHeight: discard.NewGauge(),
  163. FastSyncing: discard.NewGauge(),
  164. BlockParts: discard.NewCounter(),
  165. }
  166. }