Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simplify gossip metrics #2984

Merged
merged 1 commit into from
May 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
144 changes: 79 additions & 65 deletions network/p2p/gossip/gossip.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ import (
)

const (
ioLabel = "io"
sentIO = "sent"
receivedIO = "received"

typeLabel = "type"
pushType = "push"
pullType = "pull"
Expand All @@ -42,13 +46,24 @@ var (
_ Set[*testTx] = (*EmptySet[*testTx])(nil)
_ Set[*testTx] = (*FullSet[*testTx])(nil)

metricLabels = []string{typeLabel}
pushLabels = prometheus.Labels{
ioTypeLabels = []string{ioLabel, typeLabel}
sentPushLabels = prometheus.Labels{
ioLabel: sentIO,
typeLabel: pushType,
}
receivedPushLabels = prometheus.Labels{
ioLabel: receivedIO,
typeLabel: pushType,
}
pullLabels = prometheus.Labels{
sentPullLabels = prometheus.Labels{
ioLabel: sentIO,
typeLabel: pullType,
}
receivedPullLabels = prometheus.Labels{
ioLabel: receivedIO,
typeLabel: pullType,
}
typeLabels = []string{typeLabel}
unsentLabels = prometheus.Labels{
typeLabel: unsentType,
}
Expand Down Expand Up @@ -84,10 +99,8 @@ type ValidatorGossiper struct {
// Metrics that are tracked across a gossip protocol. A given protocol should
// only use a single instance of Metrics.
type Metrics struct {
sentCount *prometheus.CounterVec
sentBytes *prometheus.CounterVec
receivedCount *prometheus.CounterVec
receivedBytes *prometheus.CounterVec
count *prometheus.CounterVec
bytes *prometheus.CounterVec
tracking *prometheus.GaugeVec
trackingLifetimeAverage prometheus.Gauge
topValidators *prometheus.GaugeVec
Expand All @@ -99,54 +112,70 @@ func NewMetrics(
namespace string,
) (Metrics, error) {
m := Metrics{
sentCount: prometheus.NewCounterVec(prometheus.CounterOpts{
Namespace: namespace,
Name: "gossip_sent_count",
Help: "amount of gossip sent (n)",
}, metricLabels),
sentBytes: prometheus.NewCounterVec(prometheus.CounterOpts{
Namespace: namespace,
Name: "gossip_sent_bytes",
Help: "amount of gossip sent (bytes)",
}, metricLabels),
receivedCount: prometheus.NewCounterVec(prometheus.CounterOpts{
Namespace: namespace,
Name: "gossip_received_count",
Help: "amount of gossip received (n)",
}, metricLabels),
receivedBytes: prometheus.NewCounterVec(prometheus.CounterOpts{
Namespace: namespace,
Name: "gossip_received_bytes",
Help: "amount of gossip received (bytes)",
}, metricLabels),
tracking: prometheus.NewGaugeVec(prometheus.GaugeOpts{
Namespace: namespace,
Name: "gossip_tracking",
Help: "number of gossipables being tracked",
}, metricLabels),
count: prometheus.NewCounterVec(
prometheus.CounterOpts{
Namespace: namespace,
Name: "gossip_count",
Help: "amount of gossip (n)",
},
ioTypeLabels,
),
bytes: prometheus.NewCounterVec(
prometheus.CounterOpts{
Namespace: namespace,
Name: "gossip_bytes",
Help: "amount of gossip (bytes)",
},
ioTypeLabels,
),
tracking: prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Namespace: namespace,
Name: "gossip_tracking",
Help: "number of gossipables being tracked",
},
typeLabels,
),
trackingLifetimeAverage: prometheus.NewGauge(prometheus.GaugeOpts{
Namespace: namespace,
Name: "gossip_tracking_lifetime_average",
Help: "average duration a gossipable has been tracked (ns)",
}),
topValidators: prometheus.NewGaugeVec(prometheus.GaugeOpts{
Namespace: namespace,
Name: "top_validators",
Help: "number of validators gossipables are sent to due to stake",
}, metricLabels),
topValidators: prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Namespace: namespace,
Name: "top_validators",
Help: "number of validators gossipables are sent to due to stake",
},
typeLabels,
),
}
err := utils.Err(
metrics.Register(m.sentCount),
metrics.Register(m.sentBytes),
metrics.Register(m.receivedCount),
metrics.Register(m.receivedBytes),
metrics.Register(m.count),
metrics.Register(m.bytes),
metrics.Register(m.tracking),
metrics.Register(m.trackingLifetimeAverage),
metrics.Register(m.topValidators),
)
return m, err
}

func (m *Metrics) observeMessage(labels prometheus.Labels, count int, bytes int) error {
countMetric, err := m.count.GetMetricWith(labels)
if err != nil {
return fmt.Errorf("failed to get count metric: %w", err)
}

bytesMetric, err := m.bytes.GetMetricWith(labels)
if err != nil {
return fmt.Errorf("failed to get bytes metric: %w", err)
}

countMetric.Add(float64(count))
bytesMetric.Add(float64(bytes))
return nil
}

func (v ValidatorGossiper) Gossip(ctx context.Context) error {
if !v.Validators.Has(ctx, v.NodeID) {
return nil
Expand Down Expand Up @@ -250,20 +279,11 @@ func (p *PullGossiper[_]) handleResponse(
}
}

receivedCountMetric, err := p.metrics.receivedCount.GetMetricWith(pullLabels)
if err != nil {
p.log.Error("failed to get received count metric", zap.Error(err))
return
}

receivedBytesMetric, err := p.metrics.receivedBytes.GetMetricWith(pullLabels)
if err != nil {
p.log.Error("failed to get received bytes metric", zap.Error(err))
return
if err := p.metrics.observeMessage(receivedPullLabels, len(gossip), receivedBytes); err != nil {
p.log.Error("failed to update metrics",
zap.Error(err),
)
}

receivedCountMetric.Add(float64(len(gossip)))
receivedBytesMetric.Add(float64(receivedBytes))
}

// NewPushGossiper returns an instance of PushGossiper
Expand Down Expand Up @@ -476,22 +496,16 @@ func (p *PushGossiper[T]) gossip(
return err
}

validatorsByStake := p.validators.Top(ctx, gossipParams.StakePercentage)

sentCountMetric, err := p.metrics.sentCount.GetMetricWith(pushLabels)
if err != nil {
return fmt.Errorf("failed to get sent count metric: %w", err)
}
sentBytesMetric, err := p.metrics.sentBytes.GetMetricWith(pushLabels)
if err != nil {
return fmt.Errorf("failed to get sent bytes metric: %w", err)
if err := p.metrics.observeMessage(sentPushLabels, len(gossip), sentBytes); err != nil {
return err
}

topValidatorsMetric, err := p.metrics.topValidators.GetMetricWith(metricsLabels)
if err != nil {
return fmt.Errorf("failed to get top validators metric: %w", err)
}
sentCountMetric.Add(float64(len(gossip)))
sentBytesMetric.Add(float64(sentBytes))

validatorsByStake := p.validators.Top(ctx, gossipParams.StakePercentage)
topValidatorsMetric.Set(float64(len(validatorsByStake)))

return p.client.AppGossip(
Expand Down
32 changes: 6 additions & 26 deletions network/p2p/gossip/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ package gossip

import (
"context"
"fmt"
"time"

"go.uber.org/zap"
Expand Down Expand Up @@ -73,24 +72,14 @@ func (h Handler[T]) AppRequest(_ context.Context, _ ids.NodeID, _ time.Time, req

return responseSize <= h.targetResponseSize
})

if err != nil {
return nil, err
}

sentCountMetric, err := h.metrics.sentCount.GetMetricWith(pullLabels)
if err != nil {
return nil, fmt.Errorf("failed to get sent count metric: %w", err)
}

sentBytesMetric, err := h.metrics.sentBytes.GetMetricWith(pullLabels)
if err != nil {
return nil, fmt.Errorf("failed to get sent bytes metric: %w", err)
if err := h.metrics.observeMessage(sentPullLabels, len(gossipBytes), responseSize); err != nil {
return nil, err
}

sentCountMetric.Add(float64(len(gossipBytes)))
sentBytesMetric.Add(float64(responseSize))

return MarshalAppResponse(gossipBytes)
}

Expand Down Expand Up @@ -123,18 +112,9 @@ func (h Handler[_]) AppGossip(_ context.Context, nodeID ids.NodeID, gossipBytes
}
}

receivedCountMetric, err := h.metrics.receivedCount.GetMetricWith(pushLabels)
if err != nil {
h.log.Error("failed to get received count metric", zap.Error(err))
return
if err := h.metrics.observeMessage(receivedPushLabels, len(gossip), receivedBytes); err != nil {
h.log.Error("failed to update metrics",
zap.Error(err),
)
}

receivedBytesMetric, err := h.metrics.receivedBytes.GetMetricWith(pushLabels)
if err != nil {
h.log.Error("failed to get received bytes metric", zap.Error(err))
return
}

receivedCountMetric.Add(float64(len(gossip)))
receivedBytesMetric.Add(float64(receivedBytes))
}
Loading