diff --git a/cmd/opera/launcher/metrics/metrics.go b/cmd/opera/launcher/metrics/metrics.go index 6249b4954..bab964d68 100644 --- a/cmd/opera/launcher/metrics/metrics.go +++ b/cmd/opera/launcher/metrics/metrics.go @@ -3,28 +3,46 @@ package metrics import ( "os" "path/filepath" + "sync" "sync/atomic" + "time" "github.com/ethereum/go-ethereum/log" "github.com/ethereum/go-ethereum/metrics" ) -var ( - // TODO: refactor it - dbDir atomic.Value - dbSizeMetric = metrics.NewRegisteredFunctionalGauge("db_size", nil, measureDbDir) -) +var once sync.Once func SetDataDir(datadir string) { - dbDir.Store(datadir) + once.Do(func() { + go measureDbDir("db_size", datadir) + }) } -func measureDbDir() (size int64) { - datadir, ok := dbDir.Load().(string) - if !ok || datadir == "" || datadir == "inmemory" { - return +func measureDbDir(name, datadir string) { + var ( + dbSize atomic.Int64 + gauge metrics.Gauge + rescan = (len(datadir) > 0 && datadir != "inmemory") + ) + for { + time.Sleep(time.Second) + + if rescan { + size := sizeOfDir(datadir) + dbSize.Store(size) + } + + if gauge == nil { + gauge = metrics.NewRegisteredFunctionalGauge(name, nil, func() int64 { + return dbSize.Load() + }) + } + + if !rescan { + break + } } - return sizeOfDir(datadir) } func sizeOfDir(dir string) (size int64) { diff --git a/demo/start.sh b/demo/start.sh index 5ecc72bcc..763c6a529 100755 --- a/demo/start.sh +++ b/demo/start.sh @@ -25,6 +25,7 @@ do --nat extip:127.0.0.1 \ --http --http.addr="127.0.0.1" --http.port=${RPCP} --http.corsdomain="*" --http.api="eth,debug,net,admin,web3,personal,txpool,ftm,dag" \ --ws --ws.addr="127.0.0.1" --ws.port=${WSP} --ws.origins="*" --ws.api="eth,debug,net,admin,web3,personal,txpool,ftm,dag" \ + --metrics --metrics.addr=127.0.0.1 --metrics.port=$(($RPCP+1100)) \ --verbosity=3 --tracing >> opera$i.log 2>&1)& echo -e "\tnode$i ok"