eth-statediff-service/pkg/prom/prom.go

129 lines
3.5 KiB
Go
Raw Normal View History

2021-10-21 16:57:44 +00:00
// VulcanizeDB
// Copyright © 2021 Vulcanize
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
package prom
import (
"time"
"github.com/jmoiron/sqlx"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto"
)
const statsSubsystem = "stats"
var (
metrics bool
2021-10-21 18:00:30 +00:00
lastLoadedHeight prometheus.Gauge
lastProcessedHeight prometheus.Gauge
tBlockLoad prometheus.Histogram
tBlockProcessing prometheus.Histogram
tStateProcessing prometheus.Histogram
tTxCommit prometheus.Histogram
2021-10-21 16:57:44 +00:00
)
2021-10-24 23:36:49 +00:00
const (
LOADED_HEIGHT = "loaded_height"
PROCESSED_HEIGHT = "processed_height"
T_BLOCK_LOAD = "t_block_load"
T_BLOCK_PROCESSING = "t_block_processing"
T_STATE_PROCESSING = "t_state_processing"
T_POSTGRES_TX_COMMIT = "t_postgres_tx_commit"
)
2021-10-21 16:57:44 +00:00
// Init module initialization
func Init() {
metrics = true
2021-10-21 18:00:30 +00:00
lastLoadedHeight = promauto.NewGauge(prometheus.GaugeOpts{
2021-10-21 16:57:44 +00:00
Namespace: namespace,
2021-10-24 23:36:49 +00:00
Name: LOADED_HEIGHT,
2021-10-21 18:00:30 +00:00
Help: "The last block that was loaded for processing",
2021-10-21 16:57:44 +00:00
})
2021-10-21 18:00:30 +00:00
lastProcessedHeight = promauto.NewGauge(prometheus.GaugeOpts{
2021-10-21 16:57:44 +00:00
Namespace: namespace,
2021-10-24 23:36:49 +00:00
Name: PROCESSED_HEIGHT,
2021-10-21 18:00:30 +00:00
Help: "The last block that was processed",
2021-10-21 16:57:44 +00:00
})
2021-10-21 18:00:30 +00:00
tBlockLoad = promauto.NewHistogram(prometheus.HistogramOpts{
2021-10-21 16:57:44 +00:00
Namespace: namespace,
Subsystem: statsSubsystem,
2021-10-24 23:36:49 +00:00
Name: T_BLOCK_LOAD,
2021-10-21 18:00:30 +00:00
Help: "Block loading time",
2021-10-21 16:57:44 +00:00
})
2021-10-21 18:00:30 +00:00
tBlockProcessing = promauto.NewHistogram(prometheus.HistogramOpts{
2021-10-21 16:57:44 +00:00
Namespace: namespace,
Subsystem: statsSubsystem,
2021-10-24 23:36:49 +00:00
Name: T_BLOCK_PROCESSING,
2021-10-21 18:00:30 +00:00
Help: "Block (header, uncles, txs, rcts, tx trie, rct trie) processing time",
2021-10-21 16:57:44 +00:00
})
2021-10-21 18:00:30 +00:00
tStateProcessing = promauto.NewHistogram(prometheus.HistogramOpts{
2021-10-21 16:57:44 +00:00
Namespace: namespace,
Subsystem: statsSubsystem,
2021-10-24 23:36:49 +00:00
Name: T_STATE_PROCESSING,
2021-10-21 18:00:30 +00:00
Help: "State (state trie, storage tries, and code) processing time",
2021-10-21 16:57:44 +00:00
})
2021-10-21 18:00:30 +00:00
tTxCommit = promauto.NewHistogram(prometheus.HistogramOpts{
2021-10-21 16:57:44 +00:00
Namespace: namespace,
Subsystem: statsSubsystem,
2021-10-24 23:36:49 +00:00
Name: T_POSTGRES_TX_COMMIT,
2021-10-21 18:00:30 +00:00
Help: "Postgres tx commit time",
2021-10-21 16:57:44 +00:00
})
}
// RegisterDBCollector create metric colletor for given connection
func RegisterDBCollector(name string, db *sqlx.DB) {
if metrics {
prometheus.Register(NewDBStatsCollector(name, db))
}
}
2021-10-21 18:00:30 +00:00
// SetLastLoadedHeight sets last loaded height
func SetLastLoadedHeight(height int64) {
2021-10-21 16:57:44 +00:00
if metrics {
2021-10-21 18:00:30 +00:00
lastLoadedHeight.Set(float64(height))
2021-10-21 16:57:44 +00:00
}
}
2021-10-21 18:00:30 +00:00
// SetLastProcessedHeight sets last processed height
func SetLastProcessedHeight(height int64) {
2021-10-21 16:57:44 +00:00
if metrics {
2021-10-21 18:00:30 +00:00
lastProcessedHeight.Set(float64(height))
2021-10-21 16:57:44 +00:00
}
}
// SetTimeMetric time metric observation
func SetTimeMetric(name string, t time.Duration) {
if !metrics {
return
}
tAsF64 := t.Seconds()
switch name {
2021-10-24 23:36:49 +00:00
case T_BLOCK_LOAD:
2021-10-21 18:00:30 +00:00
tBlockLoad.Observe(tAsF64)
2021-10-24 23:36:49 +00:00
case T_BLOCK_PROCESSING:
2021-10-21 18:00:30 +00:00
tBlockProcessing.Observe(tAsF64)
2021-10-24 23:36:49 +00:00
case T_STATE_PROCESSING:
2021-10-21 18:00:30 +00:00
tStateProcessing.Observe(tAsF64)
2021-10-24 23:36:49 +00:00
case T_POSTGRES_TX_COMMIT:
2021-10-21 18:00:30 +00:00
tTxCommit.Observe(tAsF64)
2021-10-21 16:57:44 +00:00
}
}