Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add metrics #61

Merged
merged 17 commits into from
Aug 10, 2023
41 changes: 23 additions & 18 deletions app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,21 @@ package app
import (
"encoding/json"
"fmt"
"gorm.io/driver/mysql"
"gorm.io/gorm"

"github.com/bnb-chain/greenfield-challenger/attest"

"github.com/bnb-chain/greenfield-challenger/config"
"github.com/bnb-chain/greenfield-challenger/db/dao"
"github.com/bnb-chain/greenfield-challenger/db/model"
"github.com/bnb-chain/greenfield-challenger/executor"
"github.com/bnb-chain/greenfield-challenger/logging"
"github.com/bnb-chain/greenfield-challenger/metrics"
"github.com/bnb-chain/greenfield-challenger/monitor"
"github.com/bnb-chain/greenfield-challenger/submitter"
"github.com/bnb-chain/greenfield-challenger/verifier"
"github.com/bnb-chain/greenfield-challenger/vote"
"github.com/bnb-chain/greenfield-challenger/wiper"
"github.com/spf13/viper"
"gorm.io/driver/mysql"
"gorm.io/gorm"
)

type App struct {
Expand All @@ -30,6 +29,7 @@ type App struct {
voteCollator *vote.VoteCollator
txSubmitter *submitter.TxSubmitter
attestMonitor *attest.AttestMonitor
metricService *metrics.MetricService
dbWiper *wiper.DBWiper
}

Expand All @@ -44,7 +44,7 @@ func NewApp(cfg *config.Config) *App {

db, err := gorm.Open(mysql.Open(dbPath), &gorm.Config{})

//only for debug purpose
// only for debug purpose
db = db.Debug()

if err != nil {
Expand All @@ -58,12 +58,13 @@ func NewApp(cfg *config.Config) *App {
dbConfig.SetMaxIdleConns(cfg.DBConfig.MaxIdleConns)
dbConfig.SetMaxOpenConns(cfg.DBConfig.MaxOpenConns)

if cfg.DBConfig.DebugMode {
err = ResetDB(db, &model.Block{}, &model.Event{}, &model.Vote{})
if err != nil {
logging.Logger.Errorf("reset db error, err=%+v", err.Error())
}
}
// For clearing database during debugging
//if cfg.DBConfig.DebugMode {
// err = ResetDB(db, &model.Block{}, &model.Event{}, &model.Vote{})
// if err != nil {
// logging.Logger.Errorf("reset db error, err=%+v", err.Error())
// }
//}

model.InitBlockTable(db)
model.InitEventTable(db)
Expand All @@ -76,23 +77,25 @@ func NewApp(cfg *config.Config) *App {

executor := executor.NewExecutor(cfg)

metricService := metrics.NewMetricService(cfg)

monitorDataHandler := monitor.NewDataHandler(daoManager)
monitor := monitor.NewMonitor(executor, monitorDataHandler)
monitor := monitor.NewMonitor(executor, monitorDataHandler, metricService)

verifierDataHandler := verifier.NewDataHandler(daoManager)
hashVerifier := verifier.NewHashVerifier(cfg, executor, cfg.GreenfieldConfig.DeduplicationInterval, verifierDataHandler)
hashVerifier := verifier.NewHashVerifier(cfg, executor, cfg.GreenfieldConfig.DeduplicationInterval, verifierDataHandler, metricService)

signer := vote.NewVoteSigner(executor.BlsPrivKey)
voteDataHandler := vote.NewDataHandler(daoManager, executor)
voteCollector := vote.NewVoteCollector(cfg, executor, voteDataHandler)
voteBroadcaster := vote.NewVoteBroadcaster(cfg, signer, executor, voteDataHandler)
voteCollator := vote.NewVoteCollator(cfg, signer, executor, voteDataHandler)
voteCollector := vote.NewVoteCollector(cfg, executor, voteDataHandler, metricService)
voteBroadcaster := vote.NewVoteBroadcaster(cfg, signer, executor, voteDataHandler, metricService)
voteCollator := vote.NewVoteCollator(cfg, signer, executor, voteDataHandler, metricService)

txDataHandler := submitter.NewDataHandler(daoManager, executor)
txSubmitter := submitter.NewTxSubmitter(cfg, executor, txDataHandler)
txSubmitter := submitter.NewTxSubmitter(cfg, executor, txDataHandler, metricService)

attestDataHandler := attest.NewDataHandler(daoManager)
attestMonitor := attest.NewAttestMonitor(executor, attestDataHandler)
attestMonitor := attest.NewAttestMonitor(executor, attestDataHandler, metricService)

dbWiper := wiper.NewDBWiper(daoManager)

Expand All @@ -105,6 +108,7 @@ func NewApp(cfg *config.Config) *App {
voteCollator: voteCollator,
attestMonitor: attestMonitor,
txSubmitter: txSubmitter,
metricService: metricService,
dbWiper: dbWiper,
}
}
Expand All @@ -119,6 +123,7 @@ func (a *App) Start() {
go a.voteBroadcaster.BroadcastVotesLoop()
go a.voteCollator.CollateVotesLoop()
go a.attestMonitor.UpdateAttestedChallengeIdLoop()
go a.metricService.Start()
a.txSubmitter.SubmitTransactionLoop()
}

Expand Down
7 changes: 6 additions & 1 deletion attest/attest_monitor.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"sync"
"time"

"github.com/bnb-chain/greenfield-challenger/metrics"

"github.com/bnb-chain/greenfield-challenger/db/model"
"github.com/bnb-chain/greenfield-challenger/executor"
"github.com/bnb-chain/greenfield-challenger/logging"
Expand All @@ -14,14 +16,16 @@ type AttestMonitor struct {
mtx sync.RWMutex
attestedChallengeIds map[uint64]bool // used to save the last attested challenge id
dataProvider DataProvider
metricService *metrics.MetricService
}

func NewAttestMonitor(executor *executor.Executor, dataProvider DataProvider) *AttestMonitor {
func NewAttestMonitor(executor *executor.Executor, dataProvider DataProvider, metricService *metrics.MetricService) *AttestMonitor {
return &AttestMonitor{
executor: executor,
mtx: sync.RWMutex{},
attestedChallengeIds: make(map[uint64]bool, 0),
dataProvider: dataProvider,
metricService: metricService,
}
}

Expand Down Expand Up @@ -78,4 +82,5 @@ func (a *AttestMonitor) updateEventStatus(challengeId uint64) {
if err != nil {
logging.Logger.Errorf("update attested event status error, err=%s", err.Error())
}
a.metricService.IncAttestedChallenges()
}
15 changes: 14 additions & 1 deletion config/config.go
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
package config

import (
"cosmossdk.io/math"
"encoding/json"
"fmt"
"os"

"cosmossdk.io/math"
)

type Config struct {
GreenfieldConfig GreenfieldConfig `json:"greenfield_config"`
LogConfig LogConfig `json:"log_config"`
AlertConfig AlertConfig `json:"alert_config"`
DBConfig DBConfig `json:"db_config"`
MetricsConfig MetricsConfig `json:"metrics_config"`
}

type GreenfieldConfig struct {
Expand Down Expand Up @@ -127,10 +129,21 @@ func (cfg *DBConfig) Validate() {
}
}

type MetricsConfig struct {
Port uint16 `json:"port"`
}

func (cfg *MetricsConfig) Validate() {
if cfg.Port <= 0 || cfg.Port > 65535 {
panic("port should be within (0, 65535]")
}
}

func (cfg *Config) Validate() {
cfg.LogConfig.Validate()
cfg.DBConfig.Validate()
cfg.GreenfieldConfig.Validate()
cfg.MetricsConfig.Validate()
}

func ParseConfigFromJson(content string) *Config {
Expand Down
2 changes: 1 addition & 1 deletion executor/executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -321,11 +321,11 @@ func (e *Executor) GetStorageProviderEndpoint(address string) (string, error) {
return "", err
}
res, err := client.GetStorageProviderInfo(context.Background(), spAddr)
logging.Logger.Infof("response res.endpoint %s", res.Endpoint)
if err != nil {
logging.Logger.Errorf("executor failed to query storage provider %s, err=%+v", address, err.Error())
return "", err
}
logging.Logger.Infof("response res.endpoint %s", res.Endpoint)

return res.Endpoint, nil
}
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ require (
github.com/ory/dockertest v3.3.5+incompatible
github.com/panjf2000/ants/v2 v2.7.3
github.com/pkg/errors v0.9.1
github.com/prometheus/client_golang v1.15.0
github.com/prysmaticlabs/prysm v0.0.0-20220124113610-e26cde5e091b
github.com/spf13/pflag v1.0.5
github.com/spf13/viper v1.15.0
Expand Down Expand Up @@ -121,7 +122,6 @@ require (
github.com/pelletier/go-toml/v2 v2.0.7 // indirect
github.com/petermattis/goid v0.0.0-20230317030725-371a4b8eda08 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/prometheus/client_golang v1.15.0 // indirect
github.com/prometheus/client_model v0.3.0 // indirect
github.com/prometheus/common v0.42.0 // indirect
github.com/prometheus/procfs v0.9.0 // indirect
Expand Down
2 changes: 1 addition & 1 deletion logging/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (

var (
// Logger instance for quick declarative logging levels
Logger = logging.MustGetLogger("inscription-challenger")
Logger = logging.MustGetLogger("greenfield-challenger")
// log levels that are available
levels = map[string]logging.Level{
"CRITICAL": logging.CRITICAL,
Expand Down
Loading