Skip to content

Commit

Permalink
test: bandwidth totals
Browse files Browse the repository at this point in the history
  • Loading branch information
richard-ramos committed Sep 23, 2024
1 parent 57d9c00 commit 6f6983e
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 6 deletions.
17 changes: 17 additions & 0 deletions waku/v2/node/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package node
import (
"fmt"

"github.com/libp2p/go-libp2p/core/metrics"
"github.com/libp2p/go-libp2p/p2p/metricshelper"
"github.com/prometheus/client_golang/prometheus"
)
Expand Down Expand Up @@ -33,11 +34,20 @@ var peerStoreSize = prometheus.NewGauge(
Help: "Size of Peer Store",
})

var bandwidthTotal = prometheus.NewCounterVec(
prometheus.CounterOpts{
Name: "libp2p_network_bytes_total",
Help: "Bandwidth usage total",
},
[]string{"direction"},
)

var collectors = []prometheus.Collector{
gitVersion,
peerDials,
connectedPeers,
peerStoreSize,
bandwidthTotal,
}

// Metrics exposes the functions required to update prometheus metrics for the waku node
Expand All @@ -47,6 +57,7 @@ type Metrics interface {
RecordPeerConnected()
RecordPeerDisconnected()
SetPeerStoreSize(int)
RecordBandwidth(metrics.Stats)
}

type metricsImpl struct {
Expand Down Expand Up @@ -84,3 +95,9 @@ func (m *metricsImpl) RecordPeerDisconnected() {
func (m *metricsImpl) SetPeerStoreSize(size int) {
peerStoreSize.Set(float64(size))
}

func (m *metricsImpl) RecordBandwidth(stats metrics.Stats) {
bandwidthTotal.WithLabelValues("in").Add(float64(stats.TotalIn))
bandwidthTotal.WithLabelValues("out").Add(float64(stats.TotalOut))

}
32 changes: 26 additions & 6 deletions waku/v2/node/wakunode2.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (

"github.com/libp2p/go-libp2p/core/event"
"github.com/libp2p/go-libp2p/core/host"
"github.com/libp2p/go-libp2p/core/metrics"
"github.com/libp2p/go-libp2p/core/network"
"github.com/libp2p/go-libp2p/core/peer"
"github.com/libp2p/go-libp2p/core/peerstore"
Expand Down Expand Up @@ -84,11 +85,12 @@ type RLNRelay interface {
}

type WakuNode struct {
host host.Host
opts *WakuNodeParameters
log *zap.Logger
timesource timesource.Timesource
metrics Metrics
host host.Host
opts *WakuNodeParameters
log *zap.Logger
timesource timesource.Timesource
metrics Metrics
bandwidthCounter *metrics.BandwidthCounter

peerstore peerstore.Peerstore
peerConnector *peermanager.PeerConnectionStrategy
Expand Down Expand Up @@ -193,9 +195,11 @@ func New(opts ...WakuNodeOption) (*WakuNode, error) {
w.wakuFlag = enr.NewWakuEnrBitfield(w.opts.enableLightPush, w.opts.enableFilterFullNode, w.opts.enableStore, w.opts.enableRelay)
w.circuitRelayNodes = make(chan peer.AddrInfo)
w.metrics = newMetrics(params.prometheusReg)

w.metrics.RecordVersion(Version, GitCommit)

w.bandwidthCounter = metrics.NewBandwidthCounter()
params.libP2POpts = append(params.libP2POpts, libp2p.BandwidthReporter(w.bandwidthCounter))

// Setup peerstore wrapper
if params.peerstore != nil {
w.peerstore = wps.NewWakuPeerstore(params.peerstore)
Expand Down Expand Up @@ -358,6 +362,22 @@ func (w *WakuNode) Start(ctx context.Context) error {

w.host = host

// Bandwidth reporter created for comparing IDONTWANT performance
go func() {
ticker := time.NewTicker(time.Second)
defer ticker.Stop()

for {
select {
case <-ctx.Done():
return
case <-ticker.C:
totals := w.bandwidthCounter.GetBandwidthTotals()
w.metrics.RecordBandwidth(totals)
}
}
}()

if w.addressChangesSub, err = host.EventBus().Subscribe(new(event.EvtLocalAddressesUpdated)); err != nil {
return err
}
Expand Down

0 comments on commit 6f6983e

Please sign in to comment.