Skip to content
This repository was archived by the owner on Aug 19, 2025. It is now read-only.
Closed
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
105 changes: 105 additions & 0 deletions metrics/prometheus.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ var (
storageMutex sync.RWMutex
registerer prometheus.Registerer
gauges map[string]prometheus.Gauge
gaugeVecs map[string]*prometheus.GaugeVec
counters map[string]prometheus.Counter
counterVecs map[string]*prometheus.CounterVec
histograms map[string]prometheus.Histogram
Expand All @@ -28,6 +29,11 @@ type CounterVecOpts struct {
Labels []string
}

type GaugeVecOpts struct {
prometheus.GaugeOpts
Labels []string
}

// HistogramVecOpts holds options for the HistogramVec type.
type HistogramVecOpts struct {
prometheus.HistogramOpts
Expand All @@ -40,6 +46,7 @@ func Init() {
storageMutex = sync.RWMutex{}
registerer = prometheus.DefaultRegisterer
gauges = make(map[string]prometheus.Gauge)
gaugeVecs = make(map[string]*prometheus.GaugeVec)
counters = make(map[string]prometheus.Counter)
counterVecs = make(map[string]*prometheus.CounterVec)
histograms = make(map[string]prometheus.Histogram)
Expand All @@ -54,6 +61,62 @@ func Handler() http.Handler {
return promhttp.Handler()
}

// RegisterGaugeVecs registers the provided gauge vec metrics to the Prometheus
// registerer.
func RegisterGaugeVecs(opts ...GaugeVecOpts) {
if !initialized {
return
}

storageMutex.Lock()
defer storageMutex.Unlock()

for _, options := range opts {
registerGaugeVecIfNotExists(options)
}
}

// UnregisterGaugeVecs unregisters the provided gauge vec metrics from the Prometheus
// registerer.
func UnregisterGaugeVecs(names ...string) {
if !initialized {
return
}

storageMutex.Lock()
defer storageMutex.Unlock()

for _, name := range names {
unregisterGaugeVecIfExists(name)
}
}

// GaugeVec retrieves gauge ver metric by name
func GaugeVec(name string) (gaugeVec *prometheus.GaugeVec, exist bool) {
if !initialized {
return
}

storageMutex.RLock()
defer storageMutex.RUnlock()

gaugeVec, exist = gaugeVecs[name]

return gaugeVec, exist
}

// GaugeVecSet sets the value for gauge vec with the given name and label.
// name and label.
func GaugeVecSet(name string, label string, value float64) {
if !initialized {
return
}

if cv, ok := GaugeVec(name); ok {
cv.WithLabelValues(label).Add(value)
}
}

// RegisterGauges registers the provided gauge metrics to the Prometheus
// registerer.
func RegisterGauges(opts ...prometheus.GaugeOpts) {
Expand Down Expand Up @@ -468,6 +531,48 @@ func unregisterGaugeIfExists(name string) {
log.Debug("Gauge Metric successfully unregistered!")
}

// registerGaugeVecIfNotExists registers single gauge vec metric if not exists
func registerGaugeVecIfNotExists(opts GaugeVecOpts) {
log := log.WithFields("metricName", opts.Name)
if _, exist := gaugeVecs[opts.Name]; exist {
log.Warn("Gauge vec metric already exists.")
return
}

log.Debug("Creating Gauge Vec Metric...")
gauge := prometheus.NewGaugeVec(opts.GaugeOpts, opts.Labels)
log.Debugf("Gauge Vec Metric successfully created! Labels: %p", opts.ConstLabels)

log.Debug("Registering Gauge Vec Metric...")
registerer.MustRegister(gauge)
log.Debug("Gauge Vec Metric successfully registered!")

gaugeVecs[opts.Name] = gauge
}

// unregisterGaugeVecIfExists unregisters single gauge vec metric if exists
func unregisterGaugeVecIfExists(name string) {
var (
gauge *prometheus.GaugeVec
ok bool
)

log := log.WithFields("metricName", name)
if gauge, ok = gaugeVecs[name]; !ok {
log.Warn("Trying to delete non-existing Gauge metrics.")
return
}

log.Debug("Unregistering Gauge Vec Metric...")
ok = registerer.Unregister(gauge)
if !ok {
log.Error("Failed to unregister Gauge Vec Metric.")
return
}
delete(gauges, name)
log.Debug("Gauge Vec Metric successfully unregistered!")
}

// registerCounterIfNotExists registers single counter metric if not exists
func registerCounterIfNotExists(opts prometheus.CounterOpts) {
log := log.WithFields("metricName", opts.Name)
Expand Down
22 changes: 22 additions & 0 deletions sequencer/db_metric.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package sequencer

import (
"github.com/0xPolygonHermez/zkevm-node/log"
"github.com/0xPolygonHermez/zkevm-node/sequencer/metrics"
"time"
)

func (d *dbManager) countPendingTx() {
ticker := time.NewTicker(time.Second * 10)
for {
select {
case <-ticker.C:
transactions, err := d.txPool.CountPendingTransactions(d.ctx)
if err != nil {
log.Errorf("load pending tx from pool: %v", err)
continue
}
metrics.PendingTxCount(int(transactions))
}
}
}
1 change: 1 addition & 0 deletions sequencer/dbmanager.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ func newDBManager(ctx context.Context, config DBManagerCfg, txPool txPool, state
// Start stars the dbManager routines
func (d *dbManager) Start() {
go d.loadFromPool()
go d.countPendingTx()
go func() {
for {
time.Sleep(d.cfg.L2ReorgRetrievalInterval.Duration)
Expand Down
4 changes: 4 additions & 0 deletions sequencer/finalizer.go
Original file line number Diff line number Diff line change
Expand Up @@ -383,13 +383,17 @@ func (f *finalizer) finalizeBatches(ctx context.Context) {
log.Infof(metrics.GetLogStatistics().Summary())
metrics.GetLogStatistics().ResetStatistics()
metrics.GetLogStatistics().UpdateTimestamp(metrics.NewRound, time.Now())
metrics.BatchExecuteTime(metrics.BatchFinalizeTypeLabelDeadline, metrics.GetLogStatistics().GetStatistics(metrics.ProcessingTxCommit))
metrics.TrustBatchNum(f.batch.batchNumber - 1)
} else if f.isBatchFull() || f.isBatchAlmostFull() {
log.Infof("closing batch %d because it's almost full.", f.batch.batchNumber)
metrics.GetLogStatistics().SetTag(metrics.BatchCloseReason, "full")
f.finalizeBatch(ctx)
log.Infof(metrics.GetLogStatistics().Summary())
metrics.GetLogStatistics().ResetStatistics()
metrics.GetLogStatistics().UpdateTimestamp(metrics.NewRound, time.Now())
metrics.BatchExecuteTime(metrics.BatchFinalizeTypeLabelFullBatch, metrics.GetLogStatistics().GetStatistics(metrics.ProcessingTxCommit))
metrics.TrustBatchNum(f.batch.batchNumber - 1)
}

if err := ctx.Err(); err != nil {
Expand Down
1 change: 1 addition & 0 deletions sequencer/interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ type txPool interface {
DeleteTransactionByHash(ctx context.Context, hash common.Hash) error
MarkWIPTxsAsPending(ctx context.Context) error
GetNonWIPPendingTxs(ctx context.Context) ([]pool.Transaction, error)
CountPendingTransactions(ctx context.Context) (uint64, error)
UpdateTxStatus(ctx context.Context, hash common.Hash, newStatus pool.TxStatus, isWIP bool, failedReason *string) error
GetTxZkCountersByHash(ctx context.Context, hash common.Hash) (*state.ZKCounters, error)
UpdateTxWIPStatus(ctx context.Context, hash common.Hash, isWIP bool) error
Expand Down
2 changes: 2 additions & 0 deletions sequencer/metrics/logstatistics.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ type LogStatistics interface {
CumulativeValue(tag LogTag, value int64)
CumulativeTiming(tag LogTag, duration time.Duration)
SetTag(tag LogTag, value string)
GetTag(tag LogTag) string
GetStatistics(tag LogTag) int64
Summary() string
ResetStatistics()

Expand Down
8 changes: 8 additions & 0 deletions sequencer/metrics/logstatisticsimpl.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,14 @@ func (l *logStatisticsInstance) SetTag(tag LogTag, value string) {
l.tags[tag] = value
}

func (l *logStatisticsInstance) GetTag(tag LogTag) string {
return l.tags[tag]
}

func (l *logStatisticsInstance) GetStatistics(tag LogTag) int64 {
return l.statistics[tag]
}

func (l *logStatisticsInstance) UpdateTimestamp(tag LogTag, tm time.Time) {
l.timestamp[tag] = tm
}
Expand Down
50 changes: 50 additions & 0 deletions sequencer/metrics/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,20 @@ const (
SequenceRewardInMaticName = Prefix + "sequence_reward_in_matic"
// ProcessingTimeName is the name of the metric that shows the processing time.
ProcessingTimeName = Prefix + "processing_time"
// PendingTxCountName is the name of metric that shows the number of pending transactions.
PendingTxCountName = Prefix + "pending_tx_count"
// BatchExecuteTimeName is the name of the metric that shows the batch execution time.
BatchExecuteTimeName = Prefix + "batch_execute_time"
// TrustBatchNumName is the name of the metric that shows the trust batch num
TrustBatchNumName = Prefix + "trust_batch_num"
// WorkerPrefix is the prefix for the metrics of the worker.
WorkerPrefix = Prefix + "worker_"
// WorkerProcessingTimeName is the name of the metric that shows the worker processing time.
WorkerProcessingTimeName = WorkerPrefix + "processing_time"
// TxProcessedLabelName is the name of the label for the processed transactions.
TxProcessedLabelName = "status"
// BatchFinalizeTypeLabelName is the name of the label for the batch finalize type.
BatchFinalizeTypeLabelName = "batch_type"
)

// TxProcessedLabel represents the possible values for the
Expand All @@ -45,12 +53,20 @@ const (
TxProcessedLabelFailed TxProcessedLabel = "failed"
)

type BatchFinalizeTypeLabel string

const (
BatchFinalizeTypeLabelDeadline BatchFinalizeTypeLabel = "deadline"
BatchFinalizeTypeLabelFullBatch BatchFinalizeTypeLabel = "full_batch"
)

// Register the metrics for the sequencer package.
func Register() {
var (
counters []prometheus.CounterOpts
counterVecs []metrics.CounterVecOpts
gauges []prometheus.GaugeOpts
gaugeVecs []metrics.GaugeVecOpts
histograms []prometheus.HistogramOpts
)

Expand Down Expand Up @@ -88,6 +104,24 @@ func Register() {
Name: SequenceRewardInMaticName,
Help: "[SEQUENCER] reward for a sequence in Matic",
},
{
Name: PendingTxCountName,
Help: "[SEQUENCER] number of pending transactions",
},
{
Name: TrustBatchNumName,
Help: "[SEQUENCER] trust batch num",
},
}

gaugeVecs = []metrics.GaugeVecOpts{
{
GaugeOpts: prometheus.GaugeOpts{
Name: BatchExecuteTimeName,
Help: "[SEQUENCER] batch execution time",
},
Labels: []string{BatchFinalizeTypeLabelName},
},
}

histograms = []prometheus.HistogramOpts{
Expand All @@ -104,9 +138,25 @@ func Register() {
metrics.RegisterCounters(counters...)
metrics.RegisterCounterVecs(counterVecs...)
metrics.RegisterGauges(gauges...)
metrics.RegisterGaugeVecs(gaugeVecs...)
metrics.RegisterHistograms(histograms...)
}

// PendingTxCount sets the gauge to the given number of pending transactions.
func PendingTxCount(count int) {
metrics.GaugeSet(PendingTxCountName, float64(count))
}

// BatchExecuteTime sets the gauge vector to the given batch type and time.
func BatchExecuteTime(batchType BatchFinalizeTypeLabel, time int64) {
metrics.GaugeVecSet(BatchExecuteTimeName, string(batchType), float64(time))
}

// TrustBatchNum set the gauge to the given trust batch num
func TrustBatchNum(batchNum uint64) {
metrics.GaugeSet(TrustBatchNumName, float64(batchNum))
}

// AverageGasPrice sets the gauge to the given average gas price.
func AverageGasPrice(price float64) {
metrics.GaugeSet(GasPriceEstimatedAverageName, price)
Expand Down
26 changes: 26 additions & 0 deletions synchronizer/metrics/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,12 @@ const (

// ProcessTrustedBatchTimeName is the name of the label to process trusted batch.
ProcessTrustedBatchTimeName = Prefix + "process_trusted_batch_time"

// VirtualBatchNumName is the name of the metric virtual batch number
VirtualBatchNumName = Prefix + "virtual_batch_num"

// VerifiedBatchNumName is the name of the metric verified batch number
VerifiedBatchNumName = Prefix + "verified_batch_num"
)

// Register the metrics for the synchronizer package.
Expand Down Expand Up @@ -80,7 +86,27 @@ func Register() {
},
}

gauge := []prometheus.GaugeOpts{
{
Name: VirtualBatchNumName,
Help: "[SYNCHRONIZER] virtual batch num",
},
{
Name: VerifiedBatchNumName,
Help: "[SYNCHRONIZER] verified batch num",
},
}

metrics.RegisterHistograms(histograms...)
metrics.RegisterGauges(gauge...)
}

func VirtualBatchNum(batchNum uint64) {
metrics.GaugeSet(VirtualBatchNumName, float64(batchNum))
}

func VerifiedBatchNum(batchNum uint64) {
metrics.GaugeSet(VerifiedBatchNumName, float64(batchNum))
}

// InitializationTime observes the time initializing the synchronizer on the histogram.
Expand Down
2 changes: 2 additions & 0 deletions synchronizer/synchronizer.go
Original file line number Diff line number Diff line change
Expand Up @@ -1001,6 +1001,7 @@ func (s *ClientSynchronizer) processSequenceBatches(sequencedBatches []etherman.
log.Errorf("error storing virtualBatch. BatchNumber: %d, BlockNumber: %d, error: %v", virtualBatch.BatchNumber, blockNumber, err)
return err
}
metrics.VirtualBatchNum(virtualBatch.BatchNumber)
}
// Insert the sequence to allow the aggregator verify the sequence batches
seq := state.Sequence{
Expand Down Expand Up @@ -1251,6 +1252,7 @@ func (s *ClientSynchronizer) processTrustedVerifyBatches(lastVerifiedBatch ether
log.Errorf("error storing the verifiedB in processTrustedVerifyBatches. BlockNumber: %d, error: %v", lastVerifiedBatch.BlockNumber, err)
return err
}
metrics.VerifiedBatchNum(verifiedB.BatchNumber)
}
return nil
}
Expand Down