github.com/0xPolygon/supernets2-node@v0.0.0-20230711153321-2fe574524eaa/state/metrics/metrics.go (about)

     1  package metrics
     2  
     3  import (
     4  	"time"
     5  
     6  	"github.com/0xPolygon/supernets2-node/metrics"
     7  	"github.com/prometheus/client_golang/prometheus"
     8  )
     9  
    10  // CallerLabel is used to point which entity is the caller of a given function
    11  type CallerLabel string
    12  
    13  const (
    14  	// Prefix for the metrics of the state package.
    15  	Prefix = "state_"
    16  	// ExecutorProcessingTimeName is the name of the metric that shows the processing time in the executor.
    17  	ExecutorProcessingTimeName = Prefix + "executor_processing_time"
    18  	// CallerLabelName is the name of the label for the caller.
    19  	CallerLabelName = "caller"
    20  
    21  	// SequencerCallerLabel is used when sequencer is calling the function
    22  	SequencerCallerLabel CallerLabel = "sequencer"
    23  	// SynchronizerCallerLabel is used when synchronizer is calling the function
    24  	SynchronizerCallerLabel CallerLabel = "synchronizer"
    25  	// DiscardCallerLabel is used we want to skip measuring the execution time
    26  	DiscardCallerLabel CallerLabel = "discard"
    27  )
    28  
    29  // Register the metrics for the sequencer package.
    30  func Register() {
    31  	histogramVecs := []metrics.HistogramVecOpts{
    32  		{
    33  			HistogramOpts: prometheus.HistogramOpts{
    34  				Name: ExecutorProcessingTimeName,
    35  				Help: "[STATE] processing time in executor",
    36  			},
    37  			Labels: []string{CallerLabelName},
    38  		},
    39  	}
    40  
    41  	metrics.RegisterHistogramVecs(histogramVecs...)
    42  }
    43  
    44  // ExecutorProcessingTime observes the last processing time of the executor in the histogram vector by the provided elapsed time
    45  // and for the given label.
    46  func ExecutorProcessingTime(caller string, lastExecutionTime time.Duration) {
    47  	execTimeInSeconds := float64(lastExecutionTime) / float64(time.Second)
    48  	metrics.HistogramVecObserve(ExecutorProcessingTimeName, string(caller), execTimeInSeconds)
    49  }