From 57a52df85a12e9d73d24698742a3a27c3a84102e Mon Sep 17 00:00:00 2001 From: Magic Cat <37407870+MonikaCat@users.noreply.github.com> Date: Wed, 14 Sep 2022 19:52:10 +0800 Subject: [PATCH] feat: add database block count to prometheus to improve alert monitoring (#74) Fixes [BDU-542](https://forbole.atlassian.net/browse/BDU-542) - [x] Targeted PR against correct branch. - [x] Linked to Github issue with discussion and accepted design OR link to spec that describes this work. - [ ] Wrote unit tests. - [x] Re-reviewed `Files changed` in the Github PR explorer. --- CHANGELOG.md | 3 ++- database/database.go | 3 +++ database/postgresql/postgresql.go | 11 +++++++++++ logging/prometheus.go | 13 +++++++++++++ parser/worker.go | 3 +++ 5 files changed, 32 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 1907a34a..dccb69d5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,8 +1,9 @@ ## Unreleased ### Changes +- ([\#74](https://github.com/forbole/juno/pull/74)) Added database block count to prometheus to improve alert monitoring +- ([\#75](https://github.com/forbole/juno/pull/75)) Allow modules to handle MsgExec inner messages - Updated cosmos/cosmos-sdk to `v0.45.8` - Use `sqlx` instead of `sql` while dealing with a PostgreSQL database -- ([\#75](https://github.com/forbole/juno/pull/75)) Allow modules to handle MsgExec inner messages ## v3.4.0 ### Changes diff --git a/database/database.go b/database/database.go index 6a02956e..65583d47 100644 --- a/database/database.go +++ b/database/database.go @@ -26,6 +26,9 @@ type Database interface { // NOTE. For each transaction inside txs, SaveTx will be called as well. SaveBlock(block *types.Block) error + // GetTotalBlocks returns total number of blocks stored in database. + GetTotalBlocks() int64 + // SaveTx will be called to save each transaction contained inside a block. // An error is returned if the operation fails. SaveTx(tx *types.Tx) error diff --git a/database/postgresql/postgresql.go b/database/postgresql/postgresql.go index 28be5c32..0b8f4fa8 100644 --- a/database/postgresql/postgresql.go +++ b/database/postgresql/postgresql.go @@ -125,6 +125,17 @@ VALUES ($1, $2, $3, $4, $5, $6) ON CONFLICT DO NOTHING` return err } +// GetTotalBlocks implements database.Database +func (db *Database) GetTotalBlocks() int64 { + var blockCount int64 + err := db.Sql.QueryRow(`SELECT count(*) FROM block;`).Scan(&blockCount) + if err != nil { + return 0 + } + + return blockCount +} + // SaveTx implements database.Database func (db *Database) SaveTx(tx *types.Tx) error { var partitionID int64 diff --git a/logging/prometheus.go b/logging/prometheus.go index b81f9117..725da8aa 100644 --- a/logging/prometheus.go +++ b/logging/prometheus.go @@ -37,6 +37,14 @@ var ErrorCount = prometheus.NewCounter( }, ) +var DbBlockCount = prometheus.NewGaugeVec( + prometheus.GaugeOpts{ + Name: "juno_db_total_blocks", + Help: "Total number of blocks in database.", + }, + []string{"total_blocks_in_db"}, +) + func init() { err := prometheus.Register(StartHeight) if err != nil { @@ -57,4 +65,9 @@ func init() { if err != nil { panic(err) } + + err = prometheus.Register(DbBlockCount) + if err != nil { + panic(err) + } } diff --git a/parser/worker.go b/parser/worker.go index 5c3a32ea..507b73dc 100644 --- a/parser/worker.go +++ b/parser/worker.go @@ -355,5 +355,8 @@ func (w Worker) ExportTxs(txs []*types.Tx) error { } } + totalBlocks := w.db.GetTotalBlocks() + logging.DbBlockCount.WithLabelValues("total_blocks_in_db").Set(float64(totalBlocks)) + return nil }