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.

81 lines
2.7 KiB

7 years ago
7 years ago
  1. package p2p
  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 = "p2p"
  12. )
  13. // Metrics contains metrics exposed by this package.
  14. type Metrics struct {
  15. // Number of peers.
  16. Peers metrics.Gauge
  17. // Number of bytes received from a given peer.
  18. PeerReceiveBytesTotal metrics.Counter
  19. // Number of bytes sent to a given peer.
  20. PeerSendBytesTotal metrics.Counter
  21. // Pending bytes to be sent to a given peer.
  22. PeerPendingSendBytes metrics.Gauge
  23. // Number of transactions submitted by each peer.
  24. NumTxs metrics.Gauge
  25. }
  26. // PrometheusMetrics returns Metrics build using Prometheus client library.
  27. // Optionally, labels can be provided along with their values ("foo",
  28. // "fooValue").
  29. func PrometheusMetrics(namespace string, labelsAndValues ...string) *Metrics {
  30. labels := []string{}
  31. for i := 0; i < len(labelsAndValues); i += 2 {
  32. labels = append(labels, labelsAndValues[i])
  33. }
  34. return &Metrics{
  35. Peers: prometheus.NewGaugeFrom(stdprometheus.GaugeOpts{
  36. Namespace: namespace,
  37. Subsystem: MetricsSubsystem,
  38. Name: "peers",
  39. Help: "Number of peers.",
  40. }, labels).With(labelsAndValues...),
  41. PeerReceiveBytesTotal: prometheus.NewCounterFrom(stdprometheus.CounterOpts{
  42. Namespace: namespace,
  43. Subsystem: MetricsSubsystem,
  44. Name: "peer_receive_bytes_total",
  45. Help: "Number of bytes received from a given peer.",
  46. }, append(labels, "peer_id")).With(labelsAndValues...),
  47. PeerSendBytesTotal: prometheus.NewCounterFrom(stdprometheus.CounterOpts{
  48. Namespace: namespace,
  49. Subsystem: MetricsSubsystem,
  50. Name: "peer_send_bytes_total",
  51. Help: "Number of bytes sent to a given peer.",
  52. }, append(labels, "peer_id")).With(labelsAndValues...),
  53. PeerPendingSendBytes: prometheus.NewGaugeFrom(stdprometheus.GaugeOpts{
  54. Namespace: namespace,
  55. Subsystem: MetricsSubsystem,
  56. Name: "peer_pending_send_bytes",
  57. Help: "Number of pending bytes to be sent to a given peer.",
  58. }, append(labels, "peer_id")).With(labelsAndValues...),
  59. NumTxs: prometheus.NewGaugeFrom(stdprometheus.GaugeOpts{
  60. Namespace: namespace,
  61. Subsystem: MetricsSubsystem,
  62. Name: "num_txs",
  63. Help: "Number of transactions submitted by each peer.",
  64. }, append(labels, "peer_id")).With(labelsAndValues...),
  65. }
  66. }
  67. // NopMetrics returns no-op Metrics.
  68. func NopMetrics() *Metrics {
  69. return &Metrics{
  70. Peers: discard.NewGauge(),
  71. PeerReceiveBytesTotal: discard.NewCounter(),
  72. PeerSendBytesTotal: discard.NewCounter(),
  73. PeerPendingSendBytes: discard.NewGauge(),
  74. NumTxs: discard.NewGauge(),
  75. }
  76. }