Skip to content

Commit

Permalink
Merge pull request #426 from Fantom-foundation/fix/db_size-metric
Browse files Browse the repository at this point in the history
fix of db_size metric calc
  • Loading branch information
uprendis authored Feb 23, 2023
2 parents 406d46f + cb20db7 commit c7f7f6d
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 11 deletions.
40 changes: 29 additions & 11 deletions cmd/opera/launcher/metrics/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
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 c7f7f6d

Please sign in to comment.