Skip to content

Commit

Permalink
Add metrics to Libp2p networking processes
Browse files Browse the repository at this point in the history
  • Loading branch information
Djadih committed Feb 29, 2024
1 parent 9c67090 commit db4457c
Show file tree
Hide file tree
Showing 10 changed files with 1,439 additions and 984 deletions.
24 changes: 24 additions & 0 deletions common/common_metrics.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package common

import (
"github.com/dominant-strategies/go-quai/metrics_config"
"github.com/prometheus/client_golang/prometheus"
)

var (
messageMetrics *prometheus.CounterVec
peerMetrics *prometheus.GaugeVec
)

func init() {
registerMetrics()
}

func registerMetrics() {
messageMetrics = metrics_config.NewCounterVec("MessageCounters", "Counters to track messages sent over the P2P layer")
messageMetrics.WithLabelValues("sent")
messageMetrics.WithLabelValues("received")

peerMetrics = metrics_config.NewGaugeVec("PeerGauges", "Track the number of peers connected to this node")
peerMetrics.WithLabelValues("NumPeers").Set(0)
}
6 changes: 6 additions & 0 deletions common/stream_services.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ func ReadMessageFromStream(stream network.Stream) ([]byte, error) {
if _, err := io.ReadFull(stream, lenBytes); err != nil {
return nil, errors.Wrap(err, "failed to read message length")
}
if messageMetrics != nil {
messageMetrics.WithLabelValues("received").Inc()
}
msgLen := binary.BigEndian.Uint32(lenBytes)

// Now read the message
Expand Down Expand Up @@ -49,6 +52,9 @@ func WriteMessageToStream(stream network.Stream, msg []byte) error {
if _, err := stream.Write(lenBytes); err != nil {
return errors.Wrap(err, "failed to write message length to stream")
}
if messageMetrics != nil {
messageMetrics.WithLabelValues("sent").Inc()
}

// Then write the message itself
_, err := stream.Write(msg)
Expand Down
Loading

0 comments on commit db4457c

Please sign in to comment.