diff --git a/cmd/harmony/main.go b/cmd/harmony/main.go index 69a837d1a..5df0c41cc 100644 --- a/cmd/harmony/main.go +++ b/cmd/harmony/main.go @@ -505,7 +505,7 @@ func setupNodeAndRun(hc harmonyconfig.HarmonyConfig) { } go func() { - core.WritePreimagesMetricsIntoPrometheus(currentNode.Blockchain().ChainDb(), currentNode.Consensus.UpdatePreimageGenerationMetrics) + core.WritePreimagesMetricsIntoPrometheus(currentNode.Blockchain(), currentNode.Consensus.UpdatePreimageGenerationMetrics) }() go listenOSSigAndShutDown(currentNode) diff --git a/consensus/metrics.go b/consensus/metrics.go index 9d4dcee0a..4603050de 100644 --- a/consensus/metrics.go +++ b/consensus/metrics.go @@ -25,7 +25,14 @@ var ( Help: "the last block for which pre-image generation ran locally", }, ) - + verifiedPreimagesGauge = prometheus.NewGauge( + prometheus.GaugeOpts{ + Namespace: "hmy", + Subsystem: "blockchain", + Name: "verified_preimages", + Help: "the number of verified preimages", + }, + ) lastPreimageImportGauge = prometheus.NewGauge( prometheus.GaugeOpts{ Namespace: "hmy", @@ -129,7 +136,7 @@ func (consensus *Consensus) UpdateLeaderMetrics(numCommits float64, blockNum flo consensusCounterVec.With(prometheus.Labels{"consensus": "num_commits"}).Add(numCommits) consensusGaugeVec.With(prometheus.Labels{"consensus": "num_commits"}).Set(numCommits) } -func (consensus *Consensus) UpdatePreimageGenerationMetrics(preimageStart, preimageEnd, lastPreimageImport uint64) { +func (consensus *Consensus) UpdatePreimageGenerationMetrics(preimageStart, preimageEnd, lastPreimageImport, verifiedAddresses uint64) { if lastPreimageImport > 0 { lastPreimageImportGauge.Set(float64(lastPreimageImport)) } @@ -139,6 +146,9 @@ func (consensus *Consensus) UpdatePreimageGenerationMetrics(preimageStart, preim if preimageEnd > 0 { preimageEndGauge.Set(float64(preimageEnd)) } + if verifiedAddresses > 0 { + verifiedPreimagesGauge.Set(float64(verifiedAddresses)) + } } // AddPubkeyMetrics add the list of blskeys to prometheus metrics diff --git a/core/preimages.go b/core/preimages.go index d99ac351c..57dd59419 100644 --- a/core/preimages.go +++ b/core/preimages.go @@ -6,11 +6,11 @@ import ( "io" "os" "strconv" + "time" "github.com/ethereum/go-ethereum/common" ethCommon "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/crypto" - "github.com/ethereum/go-ethereum/ethdb" "github.com/harmony-one/harmony/block" "github.com/harmony-one/harmony/core/rawdb" "github.com/harmony-one/harmony/core/state" @@ -339,10 +339,25 @@ func VerifyPreimages(header *block.Header, chain BlockChain) (uint64, error) { return existingPreimages, nil } -func WritePreimagesMetricsIntoPrometheus(dbReader ethdb.Database, sendMetrics func(preimageStart, preimageEnd, lastPreimageImport uint64)) { - lastImport, _ := rawdb.ReadPreimageImportBlock(dbReader) - startBlock, _ := rawdb.ReadPreImageStartBlock(dbReader) - endBlock, _ := rawdb.ReadPreImageEndBlock(dbReader) +func WritePreimagesMetricsIntoPrometheus(chain BlockChain, sendMetrics func(preimageStart, preimageEnd, lastPreimageImport, verifiedAddresses uint64)) { + if chain.ShardID() < 2 { + return + } + + ticker := time.NewTicker(time.Minute * 5) + dbReader := chain.ChainDb() + + for { + select { + case <-ticker.C: + lastImport, _ := rawdb.ReadPreimageImportBlock(dbReader) + startBlock, _ := rawdb.ReadPreImageStartBlock(dbReader) + endBlock, _ := rawdb.ReadPreImageEndBlock(dbReader) + + chain.CurrentBlock().NumberU64() + verify, _ := VerifyPreimages(chain.CurrentBlock().Header(), chain) - sendMetrics(startBlock, endBlock, lastImport) + sendMetrics(startBlock, endBlock, lastImport, verify) + } + } }