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.

192 lines
4.6 KiB

  1. package metric
  2. import (
  3. "time"
  4. "github.com/hermeznetwork/hermez-node/log"
  5. "github.com/prometheus/client_golang/prometheus"
  6. )
  7. type (
  8. // Metric represents the metric type
  9. Metric string
  10. )
  11. const (
  12. namespaceError = "error"
  13. namespaceSync = "synchronizer"
  14. namespaceTxSelector = "txselector"
  15. namespaceAPI = "api"
  16. )
  17. var (
  18. // Errors errors count metric.
  19. Errors = prometheus.NewCounterVec(
  20. prometheus.CounterOpts{
  21. Namespace: namespaceError,
  22. Name: "errors",
  23. Help: "",
  24. }, []string{"error"})
  25. // WaitServerProof duration time to get the calculated
  26. // proof from the server.
  27. WaitServerProof = prometheus.NewHistogramVec(
  28. prometheus.HistogramOpts{
  29. Namespace: namespaceSync,
  30. Name: "wait_server_proof",
  31. Help: "",
  32. }, []string{"batch_number", "pipeline_number"})
  33. // Reorgs block reorg count
  34. Reorgs = prometheus.NewCounter(
  35. prometheus.CounterOpts{
  36. Namespace: namespaceSync,
  37. Name: "reorgs",
  38. Help: "",
  39. })
  40. // LastBlockNum last block synced
  41. LastBlockNum = prometheus.NewGauge(
  42. prometheus.GaugeOpts{
  43. Namespace: namespaceSync,
  44. Name: "synced_last_block_num",
  45. Help: "",
  46. })
  47. // EthLastBlockNum last eth block synced
  48. EthLastBlockNum = prometheus.NewGauge(
  49. prometheus.GaugeOpts{
  50. Namespace: namespaceSync,
  51. Name: "eth_last_block_num",
  52. Help: "",
  53. })
  54. // LastBatchNum last batch synced
  55. LastBatchNum = prometheus.NewGauge(
  56. prometheus.GaugeOpts{
  57. Namespace: namespaceSync,
  58. Name: "synced_last_batch_num",
  59. Help: "",
  60. })
  61. // EthLastBatchNum last eth batch synced
  62. EthLastBatchNum = prometheus.NewGauge(
  63. prometheus.GaugeOpts{
  64. Namespace: namespaceSync,
  65. Name: "eth_last_batch_num",
  66. Help: "",
  67. })
  68. // GetL2TxSelection L2 tx selection count
  69. GetL2TxSelection = prometheus.NewCounter(
  70. prometheus.CounterOpts{
  71. Namespace: namespaceTxSelector,
  72. Name: "get_l2_txselection_total",
  73. Help: "",
  74. })
  75. // GetL1L2TxSelection L1L2 tx selection count
  76. GetL1L2TxSelection = prometheus.NewCounter(
  77. prometheus.CounterOpts{
  78. Namespace: namespaceTxSelector,
  79. Name: "get_l1_l2_txselection_total",
  80. Help: "",
  81. })
  82. // SelectedL1CoordinatorTxs selected L1 coordinator tx count
  83. SelectedL1CoordinatorTxs = prometheus.NewGauge(
  84. prometheus.GaugeOpts{
  85. Namespace: namespaceTxSelector,
  86. Name: "selected_l1_coordinator_txs",
  87. Help: "",
  88. })
  89. // SelectedL1UserTxs selected L1 user tx count
  90. SelectedL1UserTxs = prometheus.NewGauge(
  91. prometheus.GaugeOpts{
  92. Namespace: namespaceTxSelector,
  93. Name: "selected_l1_user_txs",
  94. Help: "",
  95. })
  96. // SelectedL2Txs selected L2 tx count
  97. SelectedL2Txs = prometheus.NewGauge(
  98. prometheus.GaugeOpts{
  99. Namespace: namespaceTxSelector,
  100. Name: "selected_l2_txs",
  101. Help: "",
  102. })
  103. // DiscardedL2Txs discarded L2 tx count
  104. DiscardedL2Txs = prometheus.NewGauge(
  105. prometheus.GaugeOpts{
  106. Namespace: namespaceTxSelector,
  107. Name: "discarded_l2_txs",
  108. Help: "",
  109. })
  110. )
  111. func init() {
  112. if err := registerCollectors(); err != nil {
  113. log.Error(err)
  114. }
  115. }
  116. func registerCollectors() error {
  117. if err := registerCollector(Errors); err != nil {
  118. return err
  119. }
  120. if err := registerCollector(WaitServerProof); err != nil {
  121. return err
  122. }
  123. if err := registerCollector(Reorgs); err != nil {
  124. return err
  125. }
  126. if err := registerCollector(LastBlockNum); err != nil {
  127. return err
  128. }
  129. if err := registerCollector(LastBatchNum); err != nil {
  130. return err
  131. }
  132. if err := registerCollector(EthLastBlockNum); err != nil {
  133. return err
  134. }
  135. if err := registerCollector(EthLastBatchNum); err != nil {
  136. return err
  137. }
  138. if err := registerCollector(GetL2TxSelection); err != nil {
  139. return err
  140. }
  141. if err := registerCollector(GetL1L2TxSelection); err != nil {
  142. return err
  143. }
  144. if err := registerCollector(SelectedL1CoordinatorTxs); err != nil {
  145. return err
  146. }
  147. if err := registerCollector(SelectedL1UserTxs); err != nil {
  148. return err
  149. }
  150. return registerCollector(DiscardedL2Txs)
  151. }
  152. func registerCollector(collector prometheus.Collector) error {
  153. err := prometheus.Register(collector)
  154. if err != nil {
  155. if _, ok := err.(prometheus.AlreadyRegisteredError); !ok {
  156. return err
  157. }
  158. }
  159. return nil
  160. }
  161. // MeasureDuration measure the method execution duration
  162. // and save it into a histogram metric
  163. func MeasureDuration(histogram *prometheus.HistogramVec, start time.Time, lvs ...string) {
  164. duration := time.Since(start)
  165. histogram.WithLabelValues(lvs...).Observe(float64(duration.Milliseconds()))
  166. }
  167. // CollectError collect the error message and increment
  168. // the error count
  169. func CollectError(err error) {
  170. Errors.With(map[string]string{"error": err.Error()}).Inc()
  171. }