arnaucube
arnaucube/hermez-nodePublic Self-hosted repository
hermez-node/metric/metric.go
metric/metric.goRaw
  1package metric
  2
  3import (
  4	"time"
  5
  6	"github.com/hermeznetwork/hermez-node/log"
  7	"github.com/prometheus/client_golang/prometheus"
  8)
  9
 10type (
 11	// Metric represents the metric type
 12	Metric string
 13)
 14
 15const (
 16	namespaceError      = "error"
 17	namespaceSync       = "synchronizer"
 18	namespaceTxSelector = "txselector"
 19	namespaceAPI        = "api"
 20)
 21
 22var (
 23	// Errors errors count metric.
 24	Errors = prometheus.NewCounterVec(
 25		prometheus.CounterOpts{
 26			Namespace: namespaceError,
 27			Name:      "errors",
 28			Help:      "",
 29		}, []string{"error"})
 30
 31	// WaitServerProof duration time to get the calculated
 32	// proof from the server.
 33	WaitServerProof = prometheus.NewHistogramVec(
 34		prometheus.HistogramOpts{
 35			Namespace: namespaceSync,
 36			Name:      "wait_server_proof",
 37			Help:      "",
 38		}, []string{"batch_number", "pipeline_number"})
 39
 40	// Reorgs block reorg count
 41	Reorgs = prometheus.NewCounter(
 42		prometheus.CounterOpts{
 43			Namespace: namespaceSync,
 44			Name:      "reorgs",
 45			Help:      "",
 46		})
 47
 48	// LastBlockNum last block synced
 49	LastBlockNum = prometheus.NewGauge(
 50		prometheus.GaugeOpts{
 51			Namespace: namespaceSync,
 52			Name:      "synced_last_block_num",
 53			Help:      "",
 54		})
 55
 56	// EthLastBlockNum last eth block synced
 57	EthLastBlockNum = prometheus.NewGauge(
 58		prometheus.GaugeOpts{
 59			Namespace: namespaceSync,
 60			Name:      "eth_last_block_num",
 61			Help:      "",
 62		})
 63
 64	// LastBatchNum last batch synced
 65	LastBatchNum = prometheus.NewGauge(
 66		prometheus.GaugeOpts{
 67			Namespace: namespaceSync,
 68			Name:      "synced_last_batch_num",
 69			Help:      "",
 70		})
 71
 72	// EthLastBatchNum last eth batch synced
 73	EthLastBatchNum = prometheus.NewGauge(
 74		prometheus.GaugeOpts{
 75			Namespace: namespaceSync,
 76			Name:      "eth_last_batch_num",
 77			Help:      "",
 78		})
 79
 80	// GetL2TxSelection L2 tx selection count
 81	GetL2TxSelection = prometheus.NewCounter(
 82		prometheus.CounterOpts{
 83			Namespace: namespaceTxSelector,
 84			Name:      "get_l2_txselection_total",
 85			Help:      "",
 86		})
 87
 88	// GetL1L2TxSelection L1L2 tx selection count
 89	GetL1L2TxSelection = prometheus.NewCounter(
 90		prometheus.CounterOpts{
 91			Namespace: namespaceTxSelector,
 92			Name:      "get_l1_l2_txselection_total",
 93			Help:      "",
 94		})
 95
 96	// SelectedL1CoordinatorTxs selected L1 coordinator tx count
 97	SelectedL1CoordinatorTxs = prometheus.NewGauge(
 98		prometheus.GaugeOpts{
 99			Namespace: namespaceTxSelector,
100			Name:      "selected_l1_coordinator_txs",
101			Help:      "",
102		})
103
104	// SelectedL1UserTxs selected L1 user tx count
105	SelectedL1UserTxs = prometheus.NewGauge(
106		prometheus.GaugeOpts{
107			Namespace: namespaceTxSelector,
108			Name:      "selected_l1_user_txs",
109			Help:      "",
110		})
111
112	// SelectedL2Txs selected L2 tx count
113	SelectedL2Txs = prometheus.NewGauge(
114		prometheus.GaugeOpts{
115			Namespace: namespaceTxSelector,
116			Name:      "selected_l2_txs",
117			Help:      "",
118		})
119
120	// DiscardedL2Txs discarded L2 tx count
121	DiscardedL2Txs = prometheus.NewGauge(
122		prometheus.GaugeOpts{
123			Namespace: namespaceTxSelector,
124			Name:      "discarded_l2_txs",
125			Help:      "",
126		})
127)
128
129func init() {
130	if err := registerCollectors(); err != nil {
131		log.Error(err)
132	}
133}
134func registerCollectors() error {
135	if err := registerCollector(Errors); err != nil {
136		return err
137	}
138	if err := registerCollector(WaitServerProof); err != nil {
139		return err
140	}
141	if err := registerCollector(Reorgs); err != nil {
142		return err
143	}
144	if err := registerCollector(LastBlockNum); err != nil {
145		return err
146	}
147	if err := registerCollector(LastBatchNum); err != nil {
148		return err
149	}
150	if err := registerCollector(EthLastBlockNum); err != nil {
151		return err
152	}
153	if err := registerCollector(EthLastBatchNum); err != nil {
154		return err
155	}
156	if err := registerCollector(GetL2TxSelection); err != nil {
157		return err
158	}
159	if err := registerCollector(GetL1L2TxSelection); err != nil {
160		return err
161	}
162	if err := registerCollector(SelectedL1CoordinatorTxs); err != nil {
163		return err
164	}
165	if err := registerCollector(SelectedL1UserTxs); err != nil {
166		return err
167	}
168	return registerCollector(DiscardedL2Txs)
169}
170
171func registerCollector(collector prometheus.Collector) error {
172	err := prometheus.Register(collector)
173	if err != nil {
174		if _, ok := err.(prometheus.AlreadyRegisteredError); !ok {
175			return err
176		}
177	}
178	return nil
179}
180
181// MeasureDuration measure the method execution duration
182// and save it into a histogram metric
183func MeasureDuration(histogram *prometheus.HistogramVec, start time.Time, lvs ...string) {
184	duration := time.Since(start)
185	histogram.WithLabelValues(lvs...).Observe(float64(duration.Milliseconds()))
186}
187
188// CollectError collect the error message and increment
189// the error count
190func CollectError(err error) {
191	Errors.With(map[string]string{"error": err.Error()}).Inc()
192}