github.com/0xPolygon/supernets2-node@v0.0.0-20230711153321-2fe574524eaa/sequencer/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  const (
    11  	// Prefix for the metrics of the sequencer package.
    12  	Prefix = "sequencer_"
    13  	// SequencesSentToL1CountName is the name of the metric that counts the sequences sent to L1.
    14  	SequencesSentToL1CountName = Prefix + "sequences_sent_to_L1_count"
    15  	// GasPriceEstimatedAverageName is the name of the metric that shows the average estimated gas price.
    16  	GasPriceEstimatedAverageName = Prefix + "gas_price_estimated_average"
    17  	// TxProcessedName is the name of the metric that counts the processed transactions.
    18  	TxProcessedName = Prefix + "transaction_processed"
    19  	// SequencesOversizedDataErrorName is the name of the metric that counts the sequences with oversized data error.
    20  	SequencesOversizedDataErrorName = Prefix + "sequences_oversized_data_error"
    21  	// EthToMaticPriceName is the name of the metric that shows the Ethereum to Matic price.
    22  	EthToMaticPriceName = Prefix + "eth_to_matic_price"
    23  	// SequenceRewardInMaticName is the name of the metric that shows the reward in Matic of a sequence.
    24  	SequenceRewardInMaticName = Prefix + "sequence_reward_in_matic"
    25  	// ProcessingTimeName is the name of the metric that shows the processing time.
    26  	ProcessingTimeName = Prefix + "processing_time"
    27  	// WorkerPrefix is the prefix for the metrics of the worker.
    28  	WorkerPrefix = Prefix + "worker_"
    29  	// WorkerProcessingTimeName is the name of the metric that shows the worker processing time.
    30  	WorkerProcessingTimeName = WorkerPrefix + "processing_time"
    31  	// TxProcessedLabelName is the name of the label for the processed transactions.
    32  	TxProcessedLabelName = "status"
    33  )
    34  
    35  // TxProcessedLabel represents the possible values for the
    36  // `sequencer_transaction_processed` metric `type` label.
    37  type TxProcessedLabel string
    38  
    39  const (
    40  	// TxProcessedLabelSuccessful represents a successful transaction
    41  	TxProcessedLabelSuccessful TxProcessedLabel = "successful"
    42  	// TxProcessedLabelInvalid represents an invalid transaction
    43  	TxProcessedLabelInvalid TxProcessedLabel = "invalid"
    44  	// TxProcessedLabelFailed represents a failed transaction
    45  	TxProcessedLabelFailed TxProcessedLabel = "failed"
    46  )
    47  
    48  // Register the metrics for the sequencer package.
    49  func Register() {
    50  	var (
    51  		counters    []prometheus.CounterOpts
    52  		counterVecs []metrics.CounterVecOpts
    53  		gauges      []prometheus.GaugeOpts
    54  		histograms  []prometheus.HistogramOpts
    55  	)
    56  
    57  	counters = []prometheus.CounterOpts{
    58  		{
    59  			Name: SequencesSentToL1CountName,
    60  			Help: "[SEQUENCER] total count of sequences sent to L1",
    61  		},
    62  		{
    63  			Name: SequencesOversizedDataErrorName,
    64  			Help: "[SEQUENCER] total count of sequences with oversized data error",
    65  		},
    66  	}
    67  
    68  	counterVecs = []metrics.CounterVecOpts{
    69  		{
    70  			CounterOpts: prometheus.CounterOpts{
    71  				Name: TxProcessedName,
    72  				Help: "[SEQUENCER] number of transactions processed",
    73  			},
    74  			Labels: []string{TxProcessedLabelName},
    75  		},
    76  	}
    77  
    78  	gauges = []prometheus.GaugeOpts{
    79  		{
    80  			Name: GasPriceEstimatedAverageName,
    81  			Help: "[SEQUENCER] average gas price estimated",
    82  		},
    83  		{
    84  			Name: EthToMaticPriceName,
    85  			Help: "[SEQUENCER] eth to matic price",
    86  		},
    87  		{
    88  			Name: SequenceRewardInMaticName,
    89  			Help: "[SEQUENCER] reward for a sequence in Matic",
    90  		},
    91  	}
    92  
    93  	histograms = []prometheus.HistogramOpts{
    94  		{
    95  			Name: ProcessingTimeName,
    96  			Help: "[SEQUENCER] processing time",
    97  		},
    98  		{
    99  			Name: WorkerProcessingTimeName,
   100  			Help: "[SEQUENCER] worker processing time",
   101  		},
   102  	}
   103  
   104  	metrics.RegisterCounters(counters...)
   105  	metrics.RegisterCounterVecs(counterVecs...)
   106  	metrics.RegisterGauges(gauges...)
   107  	metrics.RegisterHistograms(histograms...)
   108  }
   109  
   110  // AverageGasPrice sets the gauge to the given average gas price.
   111  func AverageGasPrice(price float64) {
   112  	metrics.GaugeSet(GasPriceEstimatedAverageName, price)
   113  }
   114  
   115  // SequencesSentToL1 increases the counter by the provided number of sequences
   116  // sent to L1.
   117  func SequencesSentToL1(numSequences float64) {
   118  	metrics.CounterAdd(SequencesSentToL1CountName, numSequences)
   119  }
   120  
   121  // TxProcessed increases the counter vector by the provided transactions count
   122  // and for the given label.
   123  func TxProcessed(status TxProcessedLabel, count float64) {
   124  	metrics.CounterVecAdd(TxProcessedName, string(status), count)
   125  }
   126  
   127  // SequencesOvesizedDataError increases the counter for sequences that
   128  // encounter a OversizedData error.
   129  func SequencesOvesizedDataError() {
   130  	metrics.CounterInc(SequencesOversizedDataErrorName)
   131  }
   132  
   133  // EthToMaticPrice sets the gauge for the Ethereum to Matic price.
   134  func EthToMaticPrice(price float64) {
   135  	metrics.GaugeSet(EthToMaticPriceName, price)
   136  }
   137  
   138  // SequenceRewardInMatic sets the gauge for the reward in Matic of a sequence.
   139  func SequenceRewardInMatic(reward float64) {
   140  	metrics.GaugeSet(SequenceRewardInMaticName, reward)
   141  }
   142  
   143  // ProcessingTime observes the last processing time on the histogram.
   144  func ProcessingTime(lastProcessTime time.Duration) {
   145  	execTimeInSeconds := float64(lastProcessTime) / float64(time.Second)
   146  	metrics.HistogramObserve(ProcessingTimeName, execTimeInSeconds)
   147  }
   148  
   149  // WorkerProcessingTime observes the last processing time on the histogram.
   150  func WorkerProcessingTime(lastProcessTime time.Duration) {
   151  	execTimeInSeconds := float64(lastProcessTime) / float64(time.Second)
   152  	metrics.HistogramObserve(WorkerProcessingTimeName, execTimeInSeconds)
   153  }