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

     1  package metrics
     2  
     3  import (
     4  	"github.com/prometheus/client_golang/prometheus"
     5  
     6  	"github.com/onflow/flow-go/module"
     7  )
     8  
     9  // AlspMetrics is a struct that contains all the metrics related to the ALSP module.
    10  // It implements the AlspMetrics interface.
    11  // AlspMetrics encapsulates the metrics collectors for the Application Layer Spam Prevention (ALSP) module, which
    12  // is part of the networking layer. ALSP is responsible to prevent spam attacks on the application layer messages that
    13  // appear to be valid for the networking layer but carry on a malicious intent on the application layer (i.e., Flow protocols).
    14  type AlspMetrics struct {
    15  	reportedMisbehaviorCount *prometheus.CounterVec
    16  }
    17  
    18  var _ module.AlspMetrics = (*AlspMetrics)(nil)
    19  
    20  // NewAlspMetrics creates a new AlspMetrics struct. It initializes the metrics collectors for the ALSP module.
    21  // Returns:
    22  // - a pointer to the AlspMetrics struct.
    23  func NewAlspMetrics() *AlspMetrics {
    24  	alsp := &AlspMetrics{}
    25  
    26  	alsp.reportedMisbehaviorCount = prometheus.NewCounterVec(
    27  		prometheus.CounterOpts{
    28  			Namespace: namespaceNetwork,
    29  			Subsystem: subsystemAlsp,
    30  			Name:      "reported_misbehavior_total",
    31  			Help:      "number of reported spamming misbehavior received by alsp",
    32  		}, []string{LabelChannel, LabelMisbehavior},
    33  	)
    34  
    35  	return alsp
    36  }
    37  
    38  // OnMisbehaviorReported is called when a misbehavior is reported by the application layer to ALSP.
    39  // An engine detecting a spamming-related misbehavior reports it to the ALSP module. It increases
    40  // the counter vector of reported misbehavior.
    41  // Args:
    42  // - channel: the channel on which the misbehavior was reported
    43  // - misbehaviorType: the type of misbehavior reported
    44  func (a *AlspMetrics) OnMisbehaviorReported(channel string, misbehaviorType string) {
    45  	a.reportedMisbehaviorCount.With(prometheus.Labels{
    46  		LabelChannel:     channel,
    47  		LabelMisbehavior: misbehaviorType,
    48  	}).Inc()
    49  }