Skip to content

Commit

Permalink
feat: add database block count to prometheus to improve alert monitor…
Browse files Browse the repository at this point in the history
…ing (#74)

## Description

Fixes [BDU-542](https://forbole.atlassian.net/browse/BDU-542)

## Checklist
- [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.
  • Loading branch information
MonikaCat committed Sep 14, 2022
1 parent 00b8f68 commit 7d0a7c4
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 0 deletions.
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
}

0 comments on commit 7d0a7c4

Please sign in to comment.