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.

108 lines
3.5 KiB

7 years ago
7 years ago
  1. package mempool
  2. import (
  3. "github.com/go-kit/kit/metrics"
  4. "github.com/go-kit/kit/metrics/discard"
  5. "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 = "mempool"
  12. )
  13. // Metrics contains metrics exposed by this package.
  14. // see MetricsProvider for descriptions.
  15. type Metrics struct {
  16. // Size of the mempool.
  17. Size metrics.Gauge
  18. // Histogram of transaction sizes, in bytes.
  19. TxSizeBytes metrics.Histogram
  20. // Number of failed transactions.
  21. FailedTxs metrics.Counter
  22. // RejectedTxs defines the number of rejected transactions. These are
  23. // transactions that passed CheckTx but failed to make it into the mempool
  24. // due to resource limits, e.g. mempool is full and no lower priority
  25. // transactions exist in the mempool.
  26. RejectedTxs metrics.Counter
  27. // EvictedTxs defines the number of evicted transactions. These are valid
  28. // transactions that passed CheckTx and existed in the mempool but were later
  29. // evicted to make room for higher priority valid transactions that passed
  30. // CheckTx.
  31. EvictedTxs metrics.Counter
  32. // Number of times transactions are rechecked in the mempool.
  33. RecheckTimes metrics.Counter
  34. }
  35. // PrometheusMetrics returns Metrics build using Prometheus client library.
  36. // Optionally, labels can be provided along with their values ("foo",
  37. // "fooValue").
  38. func PrometheusMetrics(namespace string, labelsAndValues ...string) *Metrics {
  39. labels := []string{}
  40. for i := 0; i < len(labelsAndValues); i += 2 {
  41. labels = append(labels, labelsAndValues[i])
  42. }
  43. return &Metrics{
  44. Size: prometheus.NewGaugeFrom(stdprometheus.GaugeOpts{
  45. Namespace: namespace,
  46. Subsystem: MetricsSubsystem,
  47. Name: "size",
  48. Help: "Size of the mempool (number of uncommitted transactions).",
  49. }, labels).With(labelsAndValues...),
  50. TxSizeBytes: prometheus.NewHistogramFrom(stdprometheus.HistogramOpts{
  51. Namespace: namespace,
  52. Subsystem: MetricsSubsystem,
  53. Name: "tx_size_bytes",
  54. Help: "Transaction sizes in bytes.",
  55. Buckets: stdprometheus.ExponentialBuckets(1, 3, 17),
  56. }, labels).With(labelsAndValues...),
  57. FailedTxs: prometheus.NewCounterFrom(stdprometheus.CounterOpts{
  58. Namespace: namespace,
  59. Subsystem: MetricsSubsystem,
  60. Name: "failed_txs",
  61. Help: "Number of failed transactions.",
  62. }, labels).With(labelsAndValues...),
  63. RejectedTxs: prometheus.NewCounterFrom(stdprometheus.CounterOpts{
  64. Namespace: namespace,
  65. Subsystem: MetricsSubsystem,
  66. Name: "rejected_txs",
  67. Help: "Number of rejected transactions.",
  68. }, labels).With(labelsAndValues...),
  69. EvictedTxs: prometheus.NewCounterFrom(stdprometheus.CounterOpts{
  70. Namespace: namespace,
  71. Subsystem: MetricsSubsystem,
  72. Name: "evicted_txs",
  73. Help: "Number of evicted transactions.",
  74. }, labels).With(labelsAndValues...),
  75. RecheckTimes: prometheus.NewCounterFrom(stdprometheus.CounterOpts{
  76. Namespace: namespace,
  77. Subsystem: MetricsSubsystem,
  78. Name: "recheck_times",
  79. Help: "Number of times transactions are rechecked in the mempool.",
  80. }, labels).With(labelsAndValues...),
  81. }
  82. }
  83. // NopMetrics returns no-op Metrics.
  84. func NopMetrics() *Metrics {
  85. return &Metrics{
  86. Size: discard.NewGauge(),
  87. TxSizeBytes: discard.NewHistogram(),
  88. FailedTxs: discard.NewCounter(),
  89. RejectedTxs: discard.NewCounter(),
  90. EvictedTxs: discard.NewCounter(),
  91. RecheckTimes: discard.NewCounter(),
  92. }
  93. }