instrumenting the applicationfeature/txsel-l1frozenqueue-acccreation
@ -0,0 +1,192 @@ |
|||
package metric |
|||
|
|||
import ( |
|||
"time" |
|||
|
|||
"github.com/hermeznetwork/hermez-node/log" |
|||
"github.com/prometheus/client_golang/prometheus" |
|||
) |
|||
|
|||
type ( |
|||
// Metric represents the metric type
|
|||
Metric string |
|||
) |
|||
|
|||
const ( |
|||
namespaceError = "error" |
|||
namespaceSync = "synchronizer" |
|||
namespaceTxSelector = "txselector" |
|||
namespaceAPI = "api" |
|||
) |
|||
|
|||
var ( |
|||
// Errors errors count metric.
|
|||
Errors = prometheus.NewCounterVec( |
|||
prometheus.CounterOpts{ |
|||
Namespace: namespaceError, |
|||
Name: "errors", |
|||
Help: "", |
|||
}, []string{"error"}) |
|||
|
|||
// WaitServerProof duration time to get the calculated
|
|||
// proof from the server.
|
|||
WaitServerProof = prometheus.NewHistogramVec( |
|||
prometheus.HistogramOpts{ |
|||
Namespace: namespaceSync, |
|||
Name: "wait_server_proof", |
|||
Help: "", |
|||
}, []string{"batch_number", "pipeline_number"}) |
|||
|
|||
// Reorgs block reorg count
|
|||
Reorgs = prometheus.NewCounter( |
|||
prometheus.CounterOpts{ |
|||
Namespace: namespaceSync, |
|||
Name: "reorgs", |
|||
Help: "", |
|||
}) |
|||
|
|||
// LastBlockNum last block synced
|
|||
LastBlockNum = prometheus.NewGauge( |
|||
prometheus.GaugeOpts{ |
|||
Namespace: namespaceSync, |
|||
Name: "synced_last_block_num", |
|||
Help: "", |
|||
}) |
|||
|
|||
// EthLastBlockNum last eth block synced
|
|||
EthLastBlockNum = prometheus.NewGauge( |
|||
prometheus.GaugeOpts{ |
|||
Namespace: namespaceSync, |
|||
Name: "eth_last_block_num", |
|||
Help: "", |
|||
}) |
|||
|
|||
// LastBatchNum last batch synced
|
|||
LastBatchNum = prometheus.NewGauge( |
|||
prometheus.GaugeOpts{ |
|||
Namespace: namespaceSync, |
|||
Name: "synced_last_batch_num", |
|||
Help: "", |
|||
}) |
|||
|
|||
// EthLastBatchNum last eth batch synced
|
|||
EthLastBatchNum = prometheus.NewGauge( |
|||
prometheus.GaugeOpts{ |
|||
Namespace: namespaceSync, |
|||
Name: "eth_last_batch_num", |
|||
Help: "", |
|||
}) |
|||
|
|||
// GetL2TxSelection L2 tx selection count
|
|||
GetL2TxSelection = prometheus.NewCounter( |
|||
prometheus.CounterOpts{ |
|||
Namespace: namespaceTxSelector, |
|||
Name: "get_l2_txselection_total", |
|||
Help: "", |
|||
}) |
|||
|
|||
// GetL1L2TxSelection L1L2 tx selection count
|
|||
GetL1L2TxSelection = prometheus.NewCounter( |
|||
prometheus.CounterOpts{ |
|||
Namespace: namespaceTxSelector, |
|||
Name: "get_l1_l2_txselection_total", |
|||
Help: "", |
|||
}) |
|||
|
|||
// SelectedL1CoordinatorTxs selected L1 coordinator tx count
|
|||
SelectedL1CoordinatorTxs = prometheus.NewGauge( |
|||
prometheus.GaugeOpts{ |
|||
Namespace: namespaceTxSelector, |
|||
Name: "selected_l1_coordinator_txs", |
|||
Help: "", |
|||
}) |
|||
|
|||
// SelectedL1UserTxs selected L1 user tx count
|
|||
SelectedL1UserTxs = prometheus.NewGauge( |
|||
prometheus.GaugeOpts{ |
|||
Namespace: namespaceTxSelector, |
|||
Name: "selected_l1_user_txs", |
|||
Help: "", |
|||
}) |
|||
|
|||
// SelectedL2Txs selected L2 tx count
|
|||
SelectedL2Txs = prometheus.NewGauge( |
|||
prometheus.GaugeOpts{ |
|||
Namespace: namespaceTxSelector, |
|||
Name: "selected_l2_txs", |
|||
Help: "", |
|||
}) |
|||
|
|||
// DiscardedL2Txs discarded L2 tx count
|
|||
DiscardedL2Txs = prometheus.NewGauge( |
|||
prometheus.GaugeOpts{ |
|||
Namespace: namespaceTxSelector, |
|||
Name: "discarded_l2_txs", |
|||
Help: "", |
|||
}) |
|||
) |
|||
|
|||
func init() { |
|||
if err := registerCollectors(); err != nil { |
|||
log.Error(err) |
|||
} |
|||
} |
|||
func registerCollectors() error { |
|||
if err := registerCollector(Errors); err != nil { |
|||
return err |
|||
} |
|||
if err := registerCollector(WaitServerProof); err != nil { |
|||
return err |
|||
} |
|||
if err := registerCollector(Reorgs); err != nil { |
|||
return err |
|||
} |
|||
if err := registerCollector(LastBlockNum); err != nil { |
|||
return err |
|||
} |
|||
if err := registerCollector(LastBatchNum); err != nil { |
|||
return err |
|||
} |
|||
if err := registerCollector(EthLastBlockNum); err != nil { |
|||
return err |
|||
} |
|||
if err := registerCollector(EthLastBatchNum); err != nil { |
|||
return err |
|||
} |
|||
if err := registerCollector(GetL2TxSelection); err != nil { |
|||
return err |
|||
} |
|||
if err := registerCollector(GetL1L2TxSelection); err != nil { |
|||
return err |
|||
} |
|||
if err := registerCollector(SelectedL1CoordinatorTxs); err != nil { |
|||
return err |
|||
} |
|||
if err := registerCollector(SelectedL1UserTxs); err != nil { |
|||
return err |
|||
} |
|||
return registerCollector(DiscardedL2Txs) |
|||
} |
|||
|
|||
func registerCollector(collector prometheus.Collector) error { |
|||
err := prometheus.Register(collector) |
|||
if err != nil { |
|||
if _, ok := err.(prometheus.AlreadyRegisteredError); !ok { |
|||
return err |
|||
} |
|||
} |
|||
return nil |
|||
} |
|||
|
|||
// MeasureDuration measure the method execution duration
|
|||
// and save it into a histogram metric
|
|||
func MeasureDuration(histogram *prometheus.HistogramVec, start time.Time, lvs ...string) { |
|||
duration := time.Since(start) |
|||
histogram.WithLabelValues(lvs...).Observe(float64(duration.Milliseconds())) |
|||
} |
|||
|
|||
// CollectError collect the error message and increment
|
|||
// the error count
|
|||
func CollectError(err error) { |
|||
Errors.With(map[string]string{"error": err.Error()}).Inc() |
|||
} |
@ -0,0 +1,78 @@ |
|||
package metric |
|||
|
|||
import ( |
|||
"strconv" |
|||
"time" |
|||
|
|||
"github.com/gin-gonic/gin" |
|||
"github.com/prometheus/client_golang/prometheus" |
|||
) |
|||
|
|||
const ( |
|||
favicon = "/favicon.ico" |
|||
) |
|||
|
|||
// Prometheus contains the metrics gathered by the instance and its path
|
|||
type Prometheus struct { |
|||
reqCnt *prometheus.CounterVec |
|||
reqDur *prometheus.HistogramVec |
|||
} |
|||
|
|||
// NewPrometheus generates a new set of metrics with a certain subsystem name
|
|||
func NewPrometheus() (*Prometheus, error) { |
|||
reqCnt := prometheus.NewCounterVec( |
|||
prometheus.CounterOpts{ |
|||
Namespace: namespaceAPI, |
|||
Name: "requests_total", |
|||
Help: "How many HTTP requests processed, partitioned by status code and HTTP method", |
|||
}, |
|||
[]string{"code", "method", "path"}, |
|||
) |
|||
if err := registerCollector(reqCnt); err != nil { |
|||
return nil, err |
|||
} |
|||
reqDur := prometheus.NewHistogramVec( |
|||
prometheus.HistogramOpts{ |
|||
Namespace: namespaceAPI, |
|||
Name: "request_duration_seconds", |
|||
Help: "The HTTP request latencies in seconds", |
|||
}, |
|||
[]string{"code", "method", "path"}, |
|||
) |
|||
if err := registerCollector(reqDur); err != nil { |
|||
return nil, err |
|||
} |
|||
return &Prometheus{ |
|||
reqCnt: reqCnt, |
|||
reqDur: reqDur, |
|||
}, nil |
|||
} |
|||
|
|||
// PrometheusMiddleware creates the prometheus collector and
|
|||
// defines status handler function for the middleware
|
|||
func PrometheusMiddleware() (gin.HandlerFunc, error) { |
|||
p, err := NewPrometheus() |
|||
if err != nil { |
|||
return nil, err |
|||
} |
|||
return p.Middleware(), nil |
|||
} |
|||
|
|||
// Middleware defines status handler function for middleware
|
|||
func (p *Prometheus) Middleware() gin.HandlerFunc { |
|||
return func(c *gin.Context) { |
|||
if c.Request.URL.Path == favicon { |
|||
c.Next() |
|||
return |
|||
} |
|||
start := time.Now() |
|||
c.Next() |
|||
|
|||
status := strconv.Itoa(c.Writer.Status()) |
|||
elapsed := float64(time.Since(start)) / float64(time.Second) |
|||
fullPath := c.FullPath() |
|||
|
|||
p.reqDur.WithLabelValues(status, c.Request.Method, fullPath).Observe(elapsed) |
|||
p.reqCnt.WithLabelValues(status, c.Request.Method, fullPath).Inc() |
|||
} |
|||
} |
@ -1,44 +0,0 @@ |
|||
package synchronizer |
|||
|
|||
import "github.com/prometheus/client_golang/prometheus" |
|||
|
|||
var ( |
|||
metricReorgsCount = prometheus.NewCounter( |
|||
prometheus.CounterOpts{ |
|||
Name: "sync_reorgs", |
|||
Help: "", |
|||
}, |
|||
) |
|||
metricSyncedLastBlockNum = prometheus.NewGauge( |
|||
prometheus.GaugeOpts{ |
|||
Name: "sync_synced_last_block_num", |
|||
Help: "", |
|||
}, |
|||
) |
|||
metricEthLastBlockNum = prometheus.NewGauge( |
|||
prometheus.GaugeOpts{ |
|||
Name: "sync_eth_last_block_num", |
|||
Help: "", |
|||
}, |
|||
) |
|||
metricSyncedLastBatchNum = prometheus.NewGauge( |
|||
prometheus.GaugeOpts{ |
|||
Name: "sync_synced_last_batch_num", |
|||
Help: "", |
|||
}, |
|||
) |
|||
metricEthLastBatchNum = prometheus.NewGauge( |
|||
prometheus.GaugeOpts{ |
|||
Name: "sync_eth_last_batch_num", |
|||
Help: "", |
|||
}, |
|||
) |
|||
) |
|||
|
|||
func init() { |
|||
prometheus.MustRegister(metricReorgsCount) |
|||
prometheus.MustRegister(metricSyncedLastBlockNum) |
|||
prometheus.MustRegister(metricEthLastBlockNum) |
|||
prometheus.MustRegister(metricSyncedLastBatchNum) |
|||
prometheus.MustRegister(metricEthLastBatchNum) |
|||
} |
@ -1,53 +0,0 @@ |
|||
package txselector |
|||
|
|||
import "github.com/prometheus/client_golang/prometheus" |
|||
|
|||
var ( |
|||
metricGetL2TxSelection = prometheus.NewCounter( |
|||
prometheus.CounterOpts{ |
|||
Name: "txsel_get_l2_txselecton_total", |
|||
Help: "", |
|||
}, |
|||
) |
|||
metricGetL1L2TxSelection = prometheus.NewCounter( |
|||
prometheus.CounterOpts{ |
|||
Name: "txsel_get_l1_l2_txselecton_total", |
|||
Help: "", |
|||
}, |
|||
) |
|||
|
|||
metricSelectedL1CoordinatorTxs = prometheus.NewGauge( |
|||
prometheus.GaugeOpts{ |
|||
Name: "txsel_selected_l1_coordinator_txs", |
|||
Help: "", |
|||
}, |
|||
) |
|||
metricSelectedL1UserTxs = prometheus.NewGauge( |
|||
prometheus.GaugeOpts{ |
|||
Name: "txsel_selected_l1_user_txs", |
|||
Help: "", |
|||
}, |
|||
) |
|||
metricSelectedL2Txs = prometheus.NewGauge( |
|||
prometheus.GaugeOpts{ |
|||
Name: "txsel_selected_l2_txs", |
|||
Help: "", |
|||
}, |
|||
) |
|||
metricDiscardedL2Txs = prometheus.NewGauge( |
|||
prometheus.GaugeOpts{ |
|||
Name: "txsel_discarded_l2_txs", |
|||
Help: "", |
|||
}, |
|||
) |
|||
) |
|||
|
|||
func init() { |
|||
prometheus.MustRegister(metricGetL2TxSelection) |
|||
prometheus.MustRegister(metricGetL1L2TxSelection) |
|||
|
|||
prometheus.MustRegister(metricSelectedL1CoordinatorTxs) |
|||
prometheus.MustRegister(metricSelectedL1UserTxs) |
|||
prometheus.MustRegister(metricSelectedL2Txs) |
|||
prometheus.MustRegister(metricDiscardedL2Txs) |
|||
} |