From 85896f91b7ad148e0794dbe59d2482265dfea4db Mon Sep 17 00:00:00 2001 From: prathamesh0 Date: Tue, 19 Jul 2022 15:00:01 +0530 Subject: [PATCH] Expose database stats in metrics --- README.md | 8 +++++--- cmd/util.go | 12 +++++++++--- environments/config.toml | 1 + pkg/prom/db_stats_collector.go | 22 +++++++++++----------- pkg/prom/prom.go | 3 +-- 5 files changed, 27 insertions(+), 19 deletions(-) diff --git a/README.md b/README.md index d056099..26088a3 100644 --- a/README.md +++ b/README.md @@ -109,6 +109,7 @@ An example config file: http = true # PROM_HTTP httpAddr = "localhost" # PROM_HTTP_ADDR httpPort = "8889" # PROM_HTTP_PORT + dbStats = true # PROM_DB_STATS [ethereum] # node info @@ -155,7 +156,7 @@ An example config file: ### `serve` -* To serve state diffs over RPC: +* To serve the statediff RPC API: ```bash ./eth-statediff-service serve --config= @@ -167,7 +168,7 @@ An example config file: ./eth-statediff-service serve --config environments/config.toml ``` -* Available RPC methods are: +* Available RPC methods: * `statediff_stateTrieAt()` * `statediff_streamCodeAndCodeHash()` * `statediff_stateDiffAt()` @@ -185,7 +186,8 @@ An example config file: * This is done by turning "prerun" on in the config (`statediff.prerun = true`) and defining ranges and params in the `prerun` section of the config. * Set the range using `prerun.start` and `prerun.stop`. Use `prerun.ranges` if prerun on more than one range is required. - * Currently, `prerun.params.includeTD` must be set to `true`. + +* NOTE: Currently, `params.includeTD` must be set to / passed as `true`. ## Monitoring diff --git a/cmd/util.go b/cmd/util.go index 63870f3..bc4329f 100644 --- a/cmd/util.go +++ b/cmd/util.go @@ -6,12 +6,13 @@ import ( "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/params" "github.com/ethereum/go-ethereum/statediff" - gethsd "github.com/ethereum/go-ethereum/statediff" ind "github.com/ethereum/go-ethereum/statediff/indexer" + "github.com/ethereum/go-ethereum/statediff/indexer/shared" "github.com/ethereum/go-ethereum/trie" "github.com/spf13/viper" sd "github.com/vulcanize/eth-statediff-service/pkg" + "github.com/vulcanize/eth-statediff-service/pkg/prom" ) type blockRange [2]uint64 @@ -78,11 +79,16 @@ func createStateDiffService() (sd.StateDiffService, error) { if err != nil { logWithCommand.Fatal(err) } + logWithCommand.Info("Creating statediff indexer") - _, indexer, err := ind.NewStateDiffIndexer(context.Background(), chainConf, nodeInfo, conf) + db, indexer, err := ind.NewStateDiffIndexer(context.Background(), chainConf, nodeInfo, conf) if err != nil { logWithCommand.Fatal(err) } + if conf.Type() == shared.POSTGRES && viper.GetBool("prom.dbStats") { + prom.RegisterDBCollector(viper.GetString("database.name"), db) + } + logWithCommand.Info("Creating statediff service") sdConf := sd.Config{ ServiceWorkers: viper.GetUint("statediff.serviceWorkers"), @@ -97,7 +103,7 @@ func setupPreRunRanges() []sd.RangeRequest { if !viper.GetBool("statediff.prerun") { return nil } - preRunParams := gethsd.Params{ + preRunParams := statediff.Params{ IntermediateStateNodes: viper.GetBool("prerun.params.intermediateStateNodes"), IntermediateStorageNodes: viper.GetBool("prerun.params.intermediateStorageNodes"), IncludeBlock: viper.GetBool("prerun.params.includeBlock"), diff --git a/environments/config.toml b/environments/config.toml index 05ab100..3a5dd94 100644 --- a/environments/config.toml +++ b/environments/config.toml @@ -47,6 +47,7 @@ http = true httpAddr = "0.0.0.0" httpPort = 9100 + dbStats = false [ethereum] nodeID = "" diff --git a/pkg/prom/db_stats_collector.go b/pkg/prom/db_stats_collector.go index 3dc1223..6979918 100644 --- a/pkg/prom/db_stats_collector.go +++ b/pkg/prom/db_stats_collector.go @@ -17,9 +17,9 @@ package prom import ( - "database/sql" - "github.com/prometheus/client_golang/prometheus" + + "github.com/ethereum/go-ethereum/statediff/indexer/database/sql" ) const ( @@ -29,7 +29,7 @@ const ( // DBStatsGetter is an interface that gets sql.DBStats. type DBStatsGetter interface { - Stats() sql.DBStats + Stats() sql.Stats } // DBStatsCollector implements the prometheus.Collector interface. @@ -122,41 +122,41 @@ func (c DBStatsCollector) Collect(ch chan<- prometheus.Metric) { ch <- prometheus.MustNewConstMetric( c.maxOpenDesc, prometheus.GaugeValue, - float64(stats.MaxOpenConnections), + float64(stats.MaxOpen()), ) ch <- prometheus.MustNewConstMetric( c.openDesc, prometheus.GaugeValue, - float64(stats.OpenConnections), + float64(stats.Open()), ) ch <- prometheus.MustNewConstMetric( c.inUseDesc, prometheus.GaugeValue, - float64(stats.InUse), + float64(stats.InUse()), ) ch <- prometheus.MustNewConstMetric( c.idleDesc, prometheus.GaugeValue, - float64(stats.Idle), + float64(stats.Idle()), ) ch <- prometheus.MustNewConstMetric( c.waitedForDesc, prometheus.CounterValue, - float64(stats.WaitCount), + float64(stats.WaitCount()), ) ch <- prometheus.MustNewConstMetric( c.blockedSecondsDesc, prometheus.CounterValue, - stats.WaitDuration.Seconds(), + stats.WaitDuration().Seconds(), ) ch <- prometheus.MustNewConstMetric( c.closedMaxIdleDesc, prometheus.CounterValue, - float64(stats.MaxIdleClosed), + float64(stats.MaxIdleClosed()), ) ch <- prometheus.MustNewConstMetric( c.closedMaxLifetimeDesc, prometheus.CounterValue, - float64(stats.MaxLifetimeClosed), + float64(stats.MaxLifetimeClosed()), ) } diff --git a/pkg/prom/prom.go b/pkg/prom/prom.go index 9c8d0a5..795ccff 100644 --- a/pkg/prom/prom.go +++ b/pkg/prom/prom.go @@ -19,7 +19,6 @@ package prom import ( "time" - "github.com/jmoiron/sqlx" "github.com/prometheus/client_golang/prometheus" "github.com/prometheus/client_golang/prometheus/promauto" ) @@ -123,7 +122,7 @@ func Init() { } // RegisterDBCollector create metric collector for given connection -func RegisterDBCollector(name string, db *sqlx.DB) { +func RegisterDBCollector(name string, db DBStatsGetter) { if metrics { prometheus.Register(NewDBStatsCollector(name, db)) }