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.

73 lines
2.4 KiB

  1. package indexer
  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. // MetricsSubsystem is a the subsystem label for the indexer package.
  9. const MetricsSubsystem = "indexer"
  10. // Metrics contains metrics exposed by this package.
  11. type Metrics struct {
  12. // Latency for indexing block events.
  13. BlockEventsSeconds metrics.Histogram
  14. // Latency for indexing transaction events.
  15. TxEventsSeconds metrics.Histogram
  16. // Number of complete blocks indexed.
  17. BlocksIndexed metrics.Counter
  18. // Number of transactions indexed.
  19. TransactionsIndexed metrics.Counter
  20. }
  21. // PrometheusMetrics returns Metrics build using Prometheus client library.
  22. // Optionally, labels can be provided along with their values ("foo",
  23. // "fooValue").
  24. func PrometheusMetrics(namespace string, labelsAndValues ...string) *Metrics {
  25. labels := []string{}
  26. for i := 0; i < len(labelsAndValues); i += 2 {
  27. labels = append(labels, labelsAndValues[i])
  28. }
  29. return &Metrics{
  30. BlockEventsSeconds: prometheus.NewHistogramFrom(stdprometheus.HistogramOpts{
  31. Namespace: namespace,
  32. Subsystem: MetricsSubsystem,
  33. Name: "block_events_seconds",
  34. Help: "Latency for indexing block events.",
  35. }, labels).With(labelsAndValues...),
  36. TxEventsSeconds: prometheus.NewHistogramFrom(stdprometheus.HistogramOpts{
  37. Namespace: namespace,
  38. Subsystem: MetricsSubsystem,
  39. Name: "tx_events_seconds",
  40. Help: "Latency for indexing transaction events.",
  41. }, labels).With(labelsAndValues...),
  42. BlocksIndexed: prometheus.NewCounterFrom(stdprometheus.CounterOpts{
  43. Namespace: namespace,
  44. Subsystem: MetricsSubsystem,
  45. Name: "blocks_indexed",
  46. Help: "Number of complete blocks indexed.",
  47. }, labels).With(labelsAndValues...),
  48. TransactionsIndexed: prometheus.NewCounterFrom(stdprometheus.CounterOpts{
  49. Namespace: namespace,
  50. Subsystem: MetricsSubsystem,
  51. Name: "transactions_indexed",
  52. Help: "Number of transactions indexed.",
  53. }, labels).With(labelsAndValues...),
  54. }
  55. }
  56. // NopMetrics returns an indexer metrics stub that discards all samples.
  57. func NopMetrics() *Metrics {
  58. return &Metrics{
  59. BlockEventsSeconds: discard.NewHistogram(),
  60. TxEventsSeconds: discard.NewHistogram(),
  61. BlocksIndexed: discard.NewCounter(),
  62. TransactionsIndexed: discard.NewCounter(),
  63. }
  64. }