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.

137 lines
5.2 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. // RouterPeerQueueRecv defines the time taken to read off of a peer's queue
  24. // before sending on the connection.
  25. RouterPeerQueueRecv metrics.Histogram
  26. // RouterPeerQueueSend defines the time taken to send on a peer's queue which
  27. // will later be read and sent on the connection (see RouterPeerQueueRecv).
  28. RouterPeerQueueSend metrics.Histogram
  29. // RouterChannelQueueSend defines the time taken to send on a p2p channel's
  30. // queue which will later be consued by the corresponding reactor/service.
  31. RouterChannelQueueSend metrics.Histogram
  32. // PeerQueueDroppedMsgs defines the number of messages dropped from a peer's
  33. // queue for a specific flow (i.e. Channel).
  34. PeerQueueDroppedMsgs metrics.Counter
  35. // PeerQueueMsgSize defines the average size of messages sent over a peer's
  36. // queue for a specific flow (i.e. Channel).
  37. PeerQueueMsgSize metrics.Gauge
  38. }
  39. // PrometheusMetrics returns Metrics build using Prometheus client library.
  40. // Optionally, labels can be provided along with their values ("foo",
  41. // "fooValue").
  42. //
  43. // nolint: lll
  44. func PrometheusMetrics(namespace string, labelsAndValues ...string) *Metrics {
  45. labels := []string{}
  46. for i := 0; i < len(labelsAndValues); i += 2 {
  47. labels = append(labels, labelsAndValues[i])
  48. }
  49. return &Metrics{
  50. Peers: prometheus.NewGaugeFrom(stdprometheus.GaugeOpts{
  51. Namespace: namespace,
  52. Subsystem: MetricsSubsystem,
  53. Name: "peers",
  54. Help: "Number of peers.",
  55. }, labels).With(labelsAndValues...),
  56. PeerReceiveBytesTotal: prometheus.NewCounterFrom(stdprometheus.CounterOpts{
  57. Namespace: namespace,
  58. Subsystem: MetricsSubsystem,
  59. Name: "peer_receive_bytes_total",
  60. Help: "Number of bytes received from a given peer.",
  61. }, append(labels, "peer_id", "chID")).With(labelsAndValues...),
  62. PeerSendBytesTotal: prometheus.NewCounterFrom(stdprometheus.CounterOpts{
  63. Namespace: namespace,
  64. Subsystem: MetricsSubsystem,
  65. Name: "peer_send_bytes_total",
  66. Help: "Number of bytes sent to a given peer.",
  67. }, append(labels, "peer_id", "chID")).With(labelsAndValues...),
  68. PeerPendingSendBytes: prometheus.NewGaugeFrom(stdprometheus.GaugeOpts{
  69. Namespace: namespace,
  70. Subsystem: MetricsSubsystem,
  71. Name: "peer_pending_send_bytes",
  72. Help: "Number of pending bytes to be sent to a given peer.",
  73. }, append(labels, "peer_id")).With(labelsAndValues...),
  74. RouterPeerQueueRecv: prometheus.NewHistogramFrom(stdprometheus.HistogramOpts{
  75. Namespace: namespace,
  76. Subsystem: MetricsSubsystem,
  77. Name: "router_peer_queue_recv",
  78. Help: "The time taken to read off of a peer's queue before sending on the connection.",
  79. }, labels).With(labelsAndValues...),
  80. RouterPeerQueueSend: prometheus.NewHistogramFrom(stdprometheus.HistogramOpts{
  81. Namespace: namespace,
  82. Subsystem: MetricsSubsystem,
  83. Name: "router_peer_queue_send",
  84. Help: "The time taken to send on a peer's queue which will later be read and sent on the connection (see RouterPeerQueueRecv).",
  85. }, labels).With(labelsAndValues...),
  86. RouterChannelQueueSend: prometheus.NewHistogramFrom(stdprometheus.HistogramOpts{
  87. Namespace: namespace,
  88. Subsystem: MetricsSubsystem,
  89. Name: "router_channel_queue_send",
  90. Help: "The time taken to send on a p2p channel's queue which will later be consued by the corresponding reactor/service.",
  91. }, labels).With(labelsAndValues...),
  92. PeerQueueDroppedMsgs: prometheus.NewCounterFrom(stdprometheus.CounterOpts{
  93. Namespace: namespace,
  94. Subsystem: MetricsSubsystem,
  95. Name: "router_channel_queue_dropped_msgs",
  96. Help: "The number of messages dropped from a peer's queue for a specific p2p Channel.",
  97. }, append(labels, "ch_id")).With(labelsAndValues...),
  98. PeerQueueMsgSize: prometheus.NewGaugeFrom(stdprometheus.GaugeOpts{
  99. Namespace: namespace,
  100. Subsystem: MetricsSubsystem,
  101. Name: "router_channel_queue_msg_size",
  102. Help: "The size of messages sent over a peer's queue for a specific p2p Channel.",
  103. }, append(labels, "ch_id")).With(labelsAndValues...),
  104. }
  105. }
  106. // NopMetrics returns no-op Metrics.
  107. func NopMetrics() *Metrics {
  108. return &Metrics{
  109. Peers: discard.NewGauge(),
  110. PeerReceiveBytesTotal: discard.NewCounter(),
  111. PeerSendBytesTotal: discard.NewCounter(),
  112. PeerPendingSendBytes: discard.NewGauge(),
  113. RouterPeerQueueRecv: discard.NewHistogram(),
  114. RouterPeerQueueSend: discard.NewHistogram(),
  115. RouterChannelQueueSend: discard.NewHistogram(),
  116. PeerQueueDroppedMsgs: discard.NewCounter(),
  117. PeerQueueMsgSize: discard.NewGauge(),
  118. }
  119. }