Skip to content

Commit

Permalink
don't show zero 'db_size' on start
Browse files Browse the repository at this point in the history
  • Loading branch information
rus-alex committed Feb 22, 2023
1 parent 5bfeed5 commit cb20db7
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 17 deletions.
39 changes: 22 additions & 17 deletions cmd/opera/launcher/metrics/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,40 +3,45 @@ package metrics
import (
"os"
"path/filepath"
"sync"
"sync/atomic"
"time"

"github.com/ethereum/go-ethereum/log"
"github.com/ethereum/go-ethereum/metrics"
)

var (
dbDir atomic.Value
dbSize atomic.Int64
dbSizeMetric = metrics.NewRegisteredFunctionalGauge("db_size", nil, func() int64 {
return dbSize.Load()
})
)
var once sync.Once

func SetDataDir(datadir string) {
was := dbDir.Swap(datadir)
if was != nil {
panic("SetDataDir() only once!")
}
go measureDbDir()
once.Do(func() {
go measureDbDir("db_size", datadir)
})
}

func measureDbDir() {
func measureDbDir(name, datadir string) {
var (
dbSize atomic.Int64
gauge metrics.Gauge
rescan = (len(datadir) > 0 && datadir != "inmemory")
)
for {
time.Sleep(time.Second)

datadir, ok := dbDir.Load().(string)
if !ok || len(datadir) == 0 || datadir == "inmemory" {
dbSize.Store(0)
} else {
if rescan {
size := sizeOfDir(datadir)
dbSize.Store(size)
}

if gauge == nil {
gauge = metrics.NewRegisteredFunctionalGauge(name, nil, func() int64 {
return dbSize.Load()
})
}

if !rescan {
break
}
}
}

Expand Down
1 change: 1 addition & 0 deletions demo/start.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down

0 comments on commit cb20db7

Please sign in to comment.