Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add database block count to prometheus to improve alert monitoring #74

Merged
Merged
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
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## Unreleased
### Changes
- ([\#74](https://github.com/forbole/juno/pull/74)) Added database block count to prometheus to improve alert monitoring

## v3.4.0
### Changes
- ([\#71](https://github.com/forbole/juno/pull/71)) Retry RPC client connection upon failure instead of panic
Expand Down
3 changes: 3 additions & 0 deletions database/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
11 changes: 11 additions & 0 deletions database/postgresql/postgresql.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
13 changes: 13 additions & 0 deletions logging/prometheus.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -57,4 +65,9 @@ func init() {
if err != nil {
panic(err)
}

err = prometheus.Register(DbBlockCount)
if err != nil {
panic(err)
}
}
3 changes: 3 additions & 0 deletions parser/worker.go
Original file line number Diff line number Diff line change
Expand Up @@ -332,5 +332,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
}