github.com/onflow/flow-go@v0.35.7-crescendo-preview.23-atree-inlining/module/metrics/gossipsub_score.go (about)

     1  package metrics
     2  
     3  import (
     4  	"time"
     5  
     6  	"github.com/prometheus/client_golang/prometheus"
     7  	"github.com/prometheus/client_golang/prometheus/promauto"
     8  
     9  	"github.com/onflow/flow-go/module"
    10  	"github.com/onflow/flow-go/network/channels"
    11  )
    12  
    13  type GossipSubScoreMetrics struct {
    14  	peerScore           prometheus.Histogram
    15  	appSpecificScore    prometheus.Histogram
    16  	behaviourPenalty    prometheus.Histogram
    17  	ipCollocationFactor prometheus.Histogram
    18  
    19  	timeInMesh             prometheus.HistogramVec
    20  	meshMessageDelivery    prometheus.HistogramVec
    21  	firstMessageDelivery   prometheus.HistogramVec
    22  	invalidMessageDelivery prometheus.HistogramVec
    23  
    24  	// warningStateGauge is a gauge that keeps track of the number of peers in the warning state.
    25  	warningStateGauge prometheus.Gauge
    26  }
    27  
    28  var _ module.GossipSubScoringMetrics = (*GossipSubScoreMetrics)(nil)
    29  
    30  func NewGossipSubScoreMetrics(prefix string) *GossipSubScoreMetrics {
    31  	gs := &GossipSubScoreMetrics{}
    32  
    33  	gs.peerScore = promauto.NewHistogram(
    34  		prometheus.HistogramOpts{
    35  			Namespace: namespaceNetwork,
    36  			Subsystem: subsystemGossip,
    37  			Name:      prefix + "gossipsub_overall_peer_score",
    38  			Help:      "overall peer score from gossipsub peer scoring",
    39  			Buckets:   []float64{-100, 0, 100},
    40  		},
    41  	)
    42  
    43  	gs.appSpecificScore = promauto.NewHistogram(
    44  		prometheus.HistogramOpts{
    45  			Namespace: namespaceNetwork,
    46  			Subsystem: subsystemGossip,
    47  			Name:      prefix + "gossipsub_app_specific_score",
    48  			Help:      "app specific score from gossipsub peer scoring",
    49  			Buckets:   []float64{-100, 0, 100},
    50  		},
    51  	)
    52  
    53  	gs.behaviourPenalty = promauto.NewHistogram(
    54  		prometheus.HistogramOpts{
    55  			Namespace: namespaceNetwork,
    56  			Subsystem: subsystemGossip,
    57  			Name:      prefix + "gossipsub_behaviour_penalty_score",
    58  			Help:      "behaviour penalty from gossipsub peer scoring",
    59  			Buckets:   []float64{10, 100, 1000},
    60  		},
    61  	)
    62  
    63  	gs.ipCollocationFactor = promauto.NewHistogram(
    64  		prometheus.HistogramOpts{
    65  			Namespace: namespaceNetwork,
    66  			Subsystem: subsystemGossip,
    67  			Name:      prefix + "gossipsub_ip_collocation_factor_score",
    68  			Help:      "ip collocation factor from gossipsub peer scoring",
    69  			Buckets:   []float64{10, 100, 1000},
    70  		},
    71  	)
    72  
    73  	gs.timeInMesh = *promauto.NewHistogramVec(
    74  		prometheus.HistogramOpts{
    75  			Namespace: namespaceNetwork,
    76  			Subsystem: subsystemGossip,
    77  			Name:      prefix + "gossipsub_time_in_mesh_quantum_count",
    78  			Help:      "time in mesh from gossipsub scoring; number of the time quantum a peer has been in the mesh",
    79  			Buckets:   []float64{1, 24, 168}, // 1h, 1d, 1w with quantum of 1h
    80  		},
    81  		[]string{LabelChannel},
    82  	)
    83  
    84  	gs.meshMessageDelivery = *promauto.NewHistogramVec(
    85  		prometheus.HistogramOpts{
    86  			Namespace: namespaceNetwork,
    87  			Subsystem: subsystemGossip,
    88  			Name:      prefix + "gossipsub_mesh_message_delivery",
    89  			Buckets:   []float64{100, 1000, 10_000},
    90  			Help:      "mesh message delivery from gossipsub peer scoring; number of messages delivered to the mesh of local peer; decayed over time; and capped at certain value",
    91  		},
    92  		[]string{LabelChannel},
    93  	)
    94  
    95  	gs.invalidMessageDelivery = *promauto.NewHistogramVec(
    96  		prometheus.HistogramOpts{
    97  			Namespace: namespaceNetwork,
    98  			Subsystem: subsystemGossip,
    99  			Buckets:   []float64{10, 100, 1000},
   100  			Name:      prefix + "gossipsub_invalid_message_delivery_count",
   101  			Help:      "invalid message delivery from gossipsub peer scoring; number of invalid messages delivered to the mesh of local peer; decayed over time; and capped at certain value",
   102  		},
   103  		[]string{LabelChannel},
   104  	)
   105  
   106  	gs.firstMessageDelivery = *promauto.NewHistogramVec(
   107  		prometheus.HistogramOpts{
   108  			Namespace: namespaceNetwork,
   109  			Subsystem: subsystemGossip,
   110  			Buckets:   []float64{100, 1000, 10_000},
   111  			Name:      prefix + "gossipsub_first_message_delivery_count",
   112  			Help:      "first message delivery from gossipsub peer scoring; number of fresh messages delivered to the mesh of local peer; decayed over time; and capped at certain value",
   113  		},
   114  		[]string{LabelChannel},
   115  	)
   116  
   117  	gs.warningStateGauge = promauto.NewGauge(
   118  		prometheus.GaugeOpts{
   119  			Namespace: namespaceNetwork,
   120  			Subsystem: subsystemGossip,
   121  			Name:      prefix + "gossipsub_warning_state_peers_total",
   122  			Help:      "number of peers in the warning state",
   123  		},
   124  	)
   125  
   126  	return gs
   127  }
   128  
   129  func (g *GossipSubScoreMetrics) OnOverallPeerScoreUpdated(score float64) {
   130  	g.peerScore.Observe(score)
   131  }
   132  
   133  func (g *GossipSubScoreMetrics) OnAppSpecificScoreUpdated(score float64) {
   134  	g.appSpecificScore.Observe(score)
   135  }
   136  
   137  func (g *GossipSubScoreMetrics) OnIPColocationFactorUpdated(factor float64) {
   138  	g.ipCollocationFactor.Observe(factor)
   139  }
   140  
   141  func (g *GossipSubScoreMetrics) OnBehaviourPenaltyUpdated(penalty float64) {
   142  	g.behaviourPenalty.Observe(penalty)
   143  }
   144  
   145  func (g *GossipSubScoreMetrics) OnTimeInMeshUpdated(topic channels.Topic, duration time.Duration) {
   146  	g.timeInMesh.WithLabelValues(string(topic)).Observe(duration.Seconds())
   147  }
   148  
   149  func (g *GossipSubScoreMetrics) OnFirstMessageDeliveredUpdated(topic channels.Topic, f float64) {
   150  	g.firstMessageDelivery.WithLabelValues(string(topic)).Observe(f)
   151  }
   152  
   153  func (g *GossipSubScoreMetrics) OnMeshMessageDeliveredUpdated(topic channels.Topic, f float64) {
   154  	g.meshMessageDelivery.WithLabelValues(string(topic)).Observe(f)
   155  }
   156  
   157  func (g *GossipSubScoreMetrics) OnInvalidMessageDeliveredUpdated(topic channels.Topic, f float64) {
   158  	g.invalidMessageDelivery.WithLabelValues(string(topic)).Observe(f)
   159  }
   160  
   161  func (g *GossipSubScoreMetrics) SetWarningStateCount(u uint) {
   162  	g.warningStateGauge.Set(float64(u))
   163  }