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.

124 lines
3.8 KiB

  1. package v2
  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 = "blockchain"
  12. )
  13. // Metrics contains metrics exposed by this package.
  14. type Metrics struct {
  15. // events_in
  16. EventsIn metrics.Counter
  17. // events_in
  18. EventsHandled metrics.Counter
  19. // events_out
  20. EventsOut metrics.Counter
  21. // errors_in
  22. ErrorsIn metrics.Counter
  23. // errors_handled
  24. ErrorsHandled metrics.Counter
  25. // errors_out
  26. ErrorsOut metrics.Counter
  27. // events_shed
  28. EventsShed metrics.Counter
  29. // events_sent
  30. EventsSent metrics.Counter
  31. // errors_sent
  32. ErrorsSent metrics.Counter
  33. // errors_shed
  34. ErrorsShed metrics.Counter
  35. }
  36. // Can we burn in the routine name here?
  37. func PrometheusMetrics(namespace string, labelsAndValues ...string) *Metrics {
  38. labels := []string{}
  39. for i := 0; i < len(labelsAndValues); i += 2 {
  40. labels = append(labels, labelsAndValues[i])
  41. }
  42. return &Metrics{
  43. EventsIn: prometheus.NewCounterFrom(stdprometheus.CounterOpts{
  44. Namespace: namespace,
  45. Subsystem: MetricsSubsystem,
  46. Name: "events_in",
  47. Help: "Events read from the channel.",
  48. }, labels).With(labelsAndValues...),
  49. EventsHandled: prometheus.NewCounterFrom(stdprometheus.CounterOpts{
  50. Namespace: namespace,
  51. Subsystem: MetricsSubsystem,
  52. Name: "events_handled",
  53. Help: "Events handled",
  54. }, labels).With(labelsAndValues...),
  55. EventsOut: prometheus.NewCounterFrom(stdprometheus.CounterOpts{
  56. Namespace: namespace,
  57. Subsystem: MetricsSubsystem,
  58. Name: "events_out",
  59. Help: "Events output from routine.",
  60. }, labels).With(labelsAndValues...),
  61. ErrorsIn: prometheus.NewCounterFrom(stdprometheus.CounterOpts{
  62. Namespace: namespace,
  63. Subsystem: MetricsSubsystem,
  64. Name: "errors_in",
  65. Help: "Errors read from the channel.",
  66. }, labels).With(labelsAndValues...),
  67. ErrorsHandled: prometheus.NewCounterFrom(stdprometheus.CounterOpts{
  68. Namespace: namespace,
  69. Subsystem: MetricsSubsystem,
  70. Name: "errors_handled",
  71. Help: "Errors handled.",
  72. }, labels).With(labelsAndValues...),
  73. ErrorsOut: prometheus.NewCounterFrom(stdprometheus.CounterOpts{
  74. Namespace: namespace,
  75. Subsystem: MetricsSubsystem,
  76. Name: "errors_out",
  77. Help: "Errors output from routine.",
  78. }, labels).With(labelsAndValues...),
  79. ErrorsSent: prometheus.NewCounterFrom(stdprometheus.CounterOpts{
  80. Namespace: namespace,
  81. Subsystem: MetricsSubsystem,
  82. Name: "errors_sent",
  83. Help: "Errors sent to routine.",
  84. }, labels).With(labelsAndValues...),
  85. ErrorsShed: prometheus.NewCounterFrom(stdprometheus.CounterOpts{
  86. Namespace: namespace,
  87. Subsystem: MetricsSubsystem,
  88. Name: "errors_shed",
  89. Help: "Errors dropped from sending.",
  90. }, labels).With(labelsAndValues...),
  91. EventsSent: prometheus.NewCounterFrom(stdprometheus.CounterOpts{
  92. Namespace: namespace,
  93. Subsystem: MetricsSubsystem,
  94. Name: "events_sent",
  95. Help: "Events sent to routine.",
  96. }, labels).With(labelsAndValues...),
  97. EventsShed: prometheus.NewCounterFrom(stdprometheus.CounterOpts{
  98. Namespace: namespace,
  99. Subsystem: MetricsSubsystem,
  100. Name: "events_shed",
  101. Help: "Events dropped from sending.",
  102. }, labels).With(labelsAndValues...),
  103. }
  104. }
  105. // NopMetrics returns no-op Metrics.
  106. func NopMetrics() *Metrics {
  107. return &Metrics{
  108. EventsIn: discard.NewCounter(),
  109. EventsHandled: discard.NewCounter(),
  110. EventsOut: discard.NewCounter(),
  111. ErrorsIn: discard.NewCounter(),
  112. ErrorsHandled: discard.NewCounter(),
  113. ErrorsOut: discard.NewCounter(),
  114. EventsShed: discard.NewCounter(),
  115. EventsSent: discard.NewCounter(),
  116. ErrorsSent: discard.NewCounter(),
  117. ErrorsShed: discard.NewCounter(),
  118. }
  119. }