Skip to content

Commit

Permalink
Merge pull request #111 from ethstorage/metrics-base
Browse files Browse the repository at this point in the history
add RecordBandwidth metrics
  • Loading branch information
ping-ke authored Jan 9, 2024
2 parents 26c220f + 13763e8 commit adc366a
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 3 deletions.
27 changes: 25 additions & 2 deletions ethstorage/metrics/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"github.com/ethereum-optimism/optimism/op-service/metrics"
"github.com/ethereum/go-ethereum/common"
pb "github.com/libp2p/go-libp2p-pubsub/pb"
libp2pmetrics "github.com/libp2p/go-libp2p/core/metrics"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/collectors"
"github.com/prometheus/client_golang/prometheus/promhttp"
Expand Down Expand Up @@ -47,6 +48,8 @@ type Metricer interface {
Document() []metrics.DocumentedMetric
RecordGossipEvent(evType int32)
SetPeerScores(map[string]float64)

RecordBandwidth(ctx context.Context, bwc *libp2pmetrics.BandwidthCounter)
RecordUp()
RecordInfo(version string)
Serve(ctx context.Context, hostname string, port int) error
Expand Down Expand Up @@ -82,8 +85,9 @@ type Metrics struct {
SyncClientPerfCallTotal *prometheus.CounterVec
SyncClientPerfCallDurationSeconds *prometheus.HistogramVec

PeerCount prometheus.Gauge
DropPeerCount prometheus.Counter
PeerCount prometheus.Gauge
DropPeerCount prometheus.Counter
BandwidthTotal *prometheus.GaugeVec

SyncServerHandleReqTotal *prometheus.CounterVec
SyncServerHandleReqDurationSeconds *prometheus.HistogramVec
Expand Down Expand Up @@ -583,6 +587,22 @@ func (m *Metrics) ServerRecordTimeUsed(method string) func() {
}
}

func (m *Metrics) RecordBandwidth(ctx context.Context, bwc *libp2pmetrics.BandwidthCounter) {
tick := time.NewTicker(10 * time.Second)
defer tick.Stop()

for {
select {
case <-tick.C:
bwTotals := bwc.GetBandwidthTotals()
m.BandwidthTotal.WithLabelValues("in").Set(float64(bwTotals.TotalIn))
m.BandwidthTotal.WithLabelValues("out").Set(float64(bwTotals.TotalOut))
case <-ctx.Done():
return
}
}
}

// RecordInfo sets a pseudo-metric that contains versioning and
// config info for the es node.
func (m *Metrics) RecordInfo(version string) {
Expand Down Expand Up @@ -661,6 +681,9 @@ func (m *noopMetricer) RecordGossipEvent(evType int32) {
func (m *noopMetricer) SetPeerScores(scores map[string]float64) {
}

func (n *noopMetricer) RecordBandwidth(ctx context.Context, bwc *libp2pmetrics.BandwidthCounter) {
}

func (n *noopMetricer) RecordInfo(version string) {
}

Expand Down
2 changes: 1 addition & 1 deletion ethstorage/p2p/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ func (n *NodeP2P) init(resourcesCtx context.Context, rollupCfg *rollup.EsConfig,
}

if m != nil {
// go m.RecordBandwidth(resourcesCtx, bwc)
go m.RecordBandwidth(resourcesCtx, bwc)
}
}
return nil
Expand Down

0 comments on commit adc366a

Please sign in to comment.