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.

74 lines
2.4 KiB

6 years ago
6 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. // Number of times transactions are rechecked in the mempool.
  23. RecheckTimes metrics.Counter
  24. }
  25. // PrometheusMetrics returns Metrics build using Prometheus client library.
  26. // Optionally, labels can be provided along with their values ("foo",
  27. // "fooValue").
  28. func PrometheusMetrics(namespace string, labelsAndValues ...string) *Metrics {
  29. labels := []string{}
  30. for i := 0; i < len(labelsAndValues); i += 2 {
  31. labels = append(labels, labelsAndValues[i])
  32. }
  33. return &Metrics{
  34. Size: prometheus.NewGaugeFrom(stdprometheus.GaugeOpts{
  35. Namespace: namespace,
  36. Subsystem: MetricsSubsystem,
  37. Name: "size",
  38. Help: "Size of the mempool (number of uncommitted transactions).",
  39. }, labels).With(labelsAndValues...),
  40. TxSizeBytes: prometheus.NewHistogramFrom(stdprometheus.HistogramOpts{
  41. Namespace: namespace,
  42. Subsystem: MetricsSubsystem,
  43. Name: "tx_size_bytes",
  44. Help: "Transaction sizes in bytes.",
  45. Buckets: stdprometheus.ExponentialBuckets(1, 3, 17),
  46. }, labels).With(labelsAndValues...),
  47. FailedTxs: prometheus.NewCounterFrom(stdprometheus.CounterOpts{
  48. Namespace: namespace,
  49. Subsystem: MetricsSubsystem,
  50. Name: "failed_txs",
  51. Help: "Number of failed transactions.",
  52. }, labels).With(labelsAndValues...),
  53. RecheckTimes: prometheus.NewCounterFrom(stdprometheus.CounterOpts{
  54. Namespace: namespace,
  55. Subsystem: MetricsSubsystem,
  56. Name: "recheck_times",
  57. Help: "Number of times transactions are rechecked in the mempool.",
  58. }, labels).With(labelsAndValues...),
  59. }
  60. }
  61. // NopMetrics returns no-op Metrics.
  62. func NopMetrics() *Metrics {
  63. return &Metrics{
  64. Size: discard.NewGauge(),
  65. TxSizeBytes: discard.NewHistogram(),
  66. FailedTxs: discard.NewCounter(),
  67. RecheckTimes: discard.NewCounter(),
  68. }
  69. }