Skip to content

Commit

Permalink
feat: sp services add metrics (#211)
Browse files Browse the repository at this point in the history
* feat: sp service adds metrics

* fix: add metrics config

* refactor: refactor metrics service

* fix: update go.mod

* fix: fix config toml template

* chore: coding adjustments

* fix: golang ci lint error

---------

Co-authored-by: DylanYong <dylan.y@nodereal.io>
Co-authored-by: joeylichang <joeycli0919@gmail.com>
  • Loading branch information
3 people authored Mar 23, 2023
1 parent f693c65 commit 6fcfe19
Show file tree
Hide file tree
Showing 24 changed files with 729 additions and 80 deletions.
2 changes: 1 addition & 1 deletion cmd/conf/conf.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ var ConfigUploadCmd = &cli.Command{
utils.DBUserFlag,
utils.DBPasswordFlag,
utils.DBAddressFlag,
utils.DBDataBaseFlag,
utils.DBDatabaseFlag,
},
Category: "CONFIG COMMANDS",
Description: `
Expand Down
21 changes: 20 additions & 1 deletion cmd/storage_provider/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/bnb-chain/greenfield-storage-provider/model"
"github.com/bnb-chain/greenfield-storage-provider/pkg/lifecycle"
"github.com/bnb-chain/greenfield-storage-provider/pkg/log"
"github.com/bnb-chain/greenfield-storage-provider/pkg/metrics"
"github.com/bnb-chain/greenfield-storage-provider/service/blocksyncer"
"github.com/bnb-chain/greenfield-storage-provider/service/challenge"
"github.com/bnb-chain/greenfield-storage-provider/service/downloader"
Expand All @@ -23,7 +24,7 @@ import (
"github.com/bnb-chain/greenfield-storage-provider/service/uploader"
)

// initLog init global log level and log path.
// initLog initializes global log level and log path.
func initLog(ctx *cli.Context, cfg *config.StorageProviderConfig) error {
if cfg.LogCfg == nil {
cfg.LogCfg = config.DefaultLogConfig
Expand All @@ -45,6 +46,24 @@ func initLog(ctx *cli.Context, cfg *config.StorageProviderConfig) error {
return nil
}

// initMetrics initializes global metrics.
func initMetrics(ctx *cli.Context, cfg *config.StorageProviderConfig) error {
if cfg == nil {
cfg.MetricsCfg = config.DefaultMetricsConfig
}
if ctx.IsSet(utils.MetricsEnabledFlag.Name) {
cfg.MetricsCfg.Enabled = ctx.Bool(utils.MetricsEnabledFlag.Name)
}
if ctx.IsSet(utils.MetricsHTTPFlag.Name) {
cfg.MetricsCfg.HTTPAddress = ctx.String(utils.MetricsHTTPFlag.Name)
}
if cfg.MetricsCfg.Enabled {
slc := lifecycle.NewServiceLifecycle()
slc.RegisterServices(metrics.NewMetrics(cfg.MetricsCfg))
}
return nil
}

// initService init service instance by name and config.
func initService(serviceName string, cfg *config.StorageProviderConfig) (server lifecycle.Service, err error) {
switch serviceName {
Expand Down
31 changes: 26 additions & 5 deletions cmd/storage_provider/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,20 +24,31 @@ var (

var app *cli.App

// flags that configure the storage provider
var (
// flags that configure the storage provider
spFlags = []cli.Flag{
configFlags = []cli.Flag{
utils.ConfigFileFlag,
utils.ConfigRemoteFlag,
utils.ServerFlag,
}

dbFlags = []cli.Flag{
utils.DBUserFlag,
utils.DBPasswordFlag,
utils.DBAddressFlag,
utils.DBDataBaseFlag,
utils.DBDatabaseFlag,
}

logFlags = []cli.Flag{
utils.LogLevelFlag,
utils.LogPathFlag,
utils.LogStdOutputFlag,
}

metricsFlags = []cli.Flag{
utils.MetricsEnabledFlag,
utils.MetricsHTTPFlag,
}
)

func init() {
Expand All @@ -46,7 +57,12 @@ func init() {
app.Usage = appUsage
app.Action = storageProvider
app.HideVersion = true
app.Flags = append(app.Flags, spFlags...)
app.Flags = utils.MergeFlags(
configFlags,
dbFlags,
logFlags,
metricsFlags,
)
app.Commands = []*cli.Command{
// config category commands
conf.ConfigDumpCmd,
Expand Down Expand Up @@ -85,6 +101,7 @@ func makeConfig(ctx *cli.Context) (*config.StorageProviderConfig, error) {
} else if ctx.IsSet(utils.ConfigFileFlag.Name) {
cfg = config.LoadConfig(ctx.String(utils.ConfigFileFlag.Name))
}

// override the services to be started by flag
if ctx.IsSet(utils.ServerFlag.Name) {
services := util.SplitByComma(ctx.String(utils.ServerFlag.Name))
Expand All @@ -94,6 +111,10 @@ func makeConfig(ctx *cli.Context) (*config.StorageProviderConfig, error) {
if err := initLog(ctx, cfg); err != nil {
return nil, err
}
// init metrics
if err := initMetrics(ctx, cfg); err != nil {
return nil, err
}
return cfg, nil
}

Expand All @@ -113,7 +134,7 @@ func storageProvider(ctx *cli.Context) error {
log.Errorw("failed to init service", "service", serviceName, "error", err)
os.Exit(1)
}
log.Debugw("success to init service ", "service", serviceName)
log.Debugw("succeed to init service ", "service", serviceName)
// register service to lifecycle.
slc.RegisterServices(service)
}
Expand Down
4 changes: 2 additions & 2 deletions cmd/utils/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ func MakeSPDB(ctx *cli.Context, spDBCfg *storeconfig.SQLDBConfig) (*sqldb.SpDBIm
if ctx.IsSet(ctx.String(DBAddressFlag.Name)) {
spDBCfg.Address = ctx.String(DBAddressFlag.Name)
}
if ctx.IsSet(ctx.String(DBDataBaseFlag.Name)) {
spDBCfg.Database = ctx.String(DBDataBaseFlag.Name)
if ctx.IsSet(ctx.String(DBDatabaseFlag.Name)) {
spDBCfg.Database = ctx.String(DBDatabaseFlag.Name)
}
return sqldb.NewSpDB(spDBCfg)
}
107 changes: 76 additions & 31 deletions cmd/utils/flags.go
Original file line number Diff line number Diff line change
@@ -1,62 +1,107 @@
package utils

import (
"github.com/bnb-chain/greenfield-storage-provider/config"
"github.com/urfave/cli/v2"

"github.com/bnb-chain/greenfield-storage-provider/model"
)

const (
ConfigCategory = "SP CONFIG"
LoggingCategory = "LOGGING AND DEBUGGING"
MetricsCategory = "METRICS AND STATS"
DatabaseCategory = "DATABASE"
)

var (
ConfigFileFlag = &cli.StringFlag{
Name: "config",
Aliases: []string{"c"},
Usage: "Config file path for uploading to db",
Value: "./config.toml",
Name: "config",
Category: ConfigCategory,
Aliases: []string{"c"},
Usage: "Config file path for uploading to db",
Value: "./config.toml",
}
ConfigRemoteFlag = &cli.BoolFlag{
Name: "config.remote",
Name: "config.remote",
Category: ConfigCategory,
Usage: "Flag load config from remote db,if 'config.remote' be set, the db.user, " +
"db.password and db.address flags are needed, otherwise use the default value",
}
ServerFlag = &cli.StringFlag{
Name: "server",
Aliases: []string{"service", "s"},
Usage: "Services to be started list, eg -server gateway,uploader,receiver... ",
Name: "server",
Category: ConfigCategory,
Aliases: []string{"service", "svc"},
Usage: "Services to be started list, e.g. -server gateway, uploader, receiver...",
}

// database flags
DBUserFlag = &cli.StringFlag{
Name: "db.user",
Usage: "DB user name",
EnvVars: []string{model.SpDBUser},
Name: "db.user",
Category: DatabaseCategory,
Usage: "DB user name",
EnvVars: []string{model.SpDBUser},
}
DBPasswordFlag = &cli.StringFlag{
Name: "db.password",
Usage: "DB user password",
EnvVars: []string{model.SpDBPasswd},
Name: "db.password",
Category: DatabaseCategory,
Usage: "DB user password",
EnvVars: []string{model.SpDBPasswd},
}
DBAddressFlag = &cli.StringFlag{
Name: "db.address",
Usage: "DB listen address",
EnvVars: []string{model.SpDBAddress},
Value: "localhost:3306",
Name: "db.address",
Category: DatabaseCategory,
Usage: "DB listen address",
EnvVars: []string{model.SpDBAddress},
Value: config.DefaultSQLDBConfig.Address,
}
DBDataBaseFlag = &cli.StringFlag{
Name: "db.database",
Usage: "DB database name",
EnvVars: []string{model.SpDBDataBase},
Value: "localhost:3306",
DBDatabaseFlag = &cli.StringFlag{
Name: "db.database",
Category: DatabaseCategory,
Usage: "DB database name",
EnvVars: []string{model.SpDBDataBase},
Value: config.DefaultSQLDBConfig.Database,
}

// log flags
LogLevelFlag = &cli.StringFlag{
Name: "log.level",
Usage: "log level",
Value: "info",
Name: "log.level",
Category: LoggingCategory,
Usage: "log level",
Value: "info",
}
LogPathFlag = &cli.StringFlag{
Name: "log.path",
Usage: "log output file path",
Value: "./gnfd-sp.log",
Name: "log.path",
Category: LoggingCategory,
Usage: "log output file path",
Value: config.DefaultLogConfig.Path,
}
LogStdOutputFlag = &cli.BoolFlag{
Name: "log.std",
Usage: "log output standard io",
Name: "log.std",
Category: LoggingCategory,
Usage: "log output standard io",
}

// Metrics flags
MetricsEnabledFlag = &cli.BoolFlag{
Name: "metrics",
Category: MetricsCategory,
Usage: "Enable metrics collection and reporting",
Value: config.DefaultMetricsConfig.Enabled,
}
MetricsHTTPFlag = &cli.StringFlag{
Name: "metrics.addr",
Category: MetricsCategory,
Usage: "Enable stand-alone metrics HTTP server listening address",
Value: config.DefaultMetricsConfig.HTTPAddress,
}
)

// MergeFlags merges the given flag slices.
func MergeFlags(groups ...[]cli.Flag) []cli.Flag {
var ret []cli.Flag
for _, group := range groups {
ret = append(ret, group...)
}
return ret
}
9 changes: 9 additions & 0 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (

"github.com/bnb-chain/greenfield-storage-provider/model"
gnfd "github.com/bnb-chain/greenfield-storage-provider/pkg/greenfield"
"github.com/bnb-chain/greenfield-storage-provider/pkg/metrics"
"github.com/bnb-chain/greenfield-storage-provider/service/blocksyncer"
"github.com/bnb-chain/greenfield-storage-provider/service/signer"
"github.com/bnb-chain/greenfield-storage-provider/store/config"
Expand All @@ -32,6 +33,7 @@ type StorageProviderConfig struct {
BlockSyncerCfg *blocksyncer.Config
P2PCfg *p2p.NodeConfig
LogCfg *LogConfig
MetricsCfg *metrics.MetricsConfig
}

// JSONMarshal marshal the StorageProviderConfig to json format
Expand Down Expand Up @@ -88,6 +90,7 @@ var DefaultStorageProviderConfig = &StorageProviderConfig{
BlockSyncerCfg: DefaultBlockSyncerConfig,
P2PCfg: DefaultP2PConfig,
LogCfg: DefaultLogConfig,
MetricsCfg: DefaultMetricsConfig,
}

// DefaultSQLDBConfig defines the default configuration of SQL DB
Expand Down Expand Up @@ -123,6 +126,12 @@ var DefaultBlockSyncerConfig = &blocksyncer.Config{
Dsn: "localhost:3308",
}

// DefaultMetricsConfig defines the default config of Metrics service
var DefaultMetricsConfig = &metrics.MetricsConfig{
Enabled: false,
HTTPAddress: model.MetricsHTTPAddress,
}

type LogConfig struct {
Level string
Path string
Expand Down
4 changes: 4 additions & 0 deletions config/config_template.toml
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,10 @@ P2PPrivateKey = ""
Bootstrap = []
PingPeriod = 2

[MetricsCfg]
Enabled = false
HTTPAddress = "localhost:9833"

[LogCfg]
Level = "debug"
Path = "./gnfd-sp.log"
2 changes: 1 addition & 1 deletion config/subconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ func (cfg *StorageProviderConfig) MakeMetadataServiceConfig() (*metadata.Metadat
if _, ok := cfg.ListenAddress[model.MetadataService]; ok {
mCfg.GRPCAddress = cfg.ListenAddress[model.MetadataService]
} else {
return nil, fmt.Errorf("missing meta data gRPC address configuration for meta data service")
return nil, fmt.Errorf("missing metadata gRPC address configuration for meta data service")
}
return mCfg, nil
}
Expand Down
7 changes: 4 additions & 3 deletions deployment/localup/localup.sh
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,9 @@ generate_env() {
done
}

###############################################################
# make sp config.toml real according env.info/db.info/sp.info #
###############################################################
##################################################################
# make sp config.toml real according to env.info/db.info/sp.info #
##################################################################
make_config() {
index=0
for sp_dir in ${workspace}/${SP_DEPLOY_DIR}/* ; do
Expand All @@ -83,6 +83,7 @@ make_config() {
sed -i -e "s/9733/$(($cur_port+733))/g" config.toml
sed -i -e "s/9833/$(($cur_port+833))/g" config.toml
sed -i -e "s/9933/$(($cur_port+933))/g" config.toml
sed -i -e "s/24036/$(($cur_port+4036))/g" config.toml
sed -i -e "s/SpOperatorAddress = \".*\"/SpOperatorAddress = \"${OPERATOR_ADDRESS}\"/g" config.toml
sed -i -e "s/OperatorPrivateKey = \".*\"/OperatorPrivateKey = \"${OPERATOR_PRIVATE_KEY}\"/g" config.toml
sed -i -e "s/FundingPrivateKey = \".*\"/FundingPrivateKey = \"${FUNDING_PRIVATE_KEY}\"/g" config.toml
Expand Down
Loading

0 comments on commit 6fcfe19

Please sign in to comment.