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(metrics): Monitor and publish metrics to Prometheus. #1437

Merged
merged 11 commits into from
Mar 10, 2021
1 change: 1 addition & 0 deletions chain/gssmr/config.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
[global]
basepath = "~/.gossamer/gssmr"
log = "info"
metrics-port = 9876

[log]
core = ""
Expand Down
3 changes: 3 additions & 0 deletions chain/gssmr/defaults.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ var (
// DefaultBasePath Default node base directory path
DefaultBasePath = string("~/.gossamer/gssmr")

// DefaultMetricsPort is the metrics server port
DefaultMetricsPort = uint32(9876)

// DefaultLvl is the default log level
DefaultLvl = log.LvlInfo

Expand Down
1 change: 1 addition & 0 deletions chain/ksmcc/config.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
[global]
basepath = "~/.gossamer/ksmcc"
log = "info"
metrics-port = 9876

[log]
core = ""
Expand Down
3 changes: 3 additions & 0 deletions chain/ksmcc/defaults.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ var (
// DefaultBasePath Default node base directory path
DefaultBasePath = string("~/.gossamer/ksmcc")

// DefaultMetricsPort is the metrics server port
DefaultMetricsPort = uint32(9876)

// DefaultLvl is the default log level
DefaultLvl = log.LvlInfo

Expand Down
9 changes: 9 additions & 0 deletions cmd/gossamer/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -399,6 +399,8 @@ func setDotGlobalConfig(ctx *cli.Context, tomlCfg *ctoml.Config, cfg *dot.Global
if tomlCfg.Global.LogLvl != "" {
cfg.LogLvl, _ = log.LvlFromString(tomlCfg.Global.LogLvl)
}

cfg.MetricsPort = tomlCfg.Global.MetricsPort
}

// check --name flag and update node configuration
Expand Down Expand Up @@ -427,6 +429,13 @@ func setDotGlobalConfig(ctx *cli.Context, tomlCfg *ctoml.Config, cfg *dot.Global
cfg.LogLvl = lvl
}

cfg.PublishMetrics = ctx.Bool("publish-metrics")

// check --metrics-port flag and update node configuration
if metricsPort := ctx.GlobalUint(MetricsPortFlag.Name); metricsPort != 0 {
cfg.MetricsPort = uint32(metricsPort)
}

logger.Debug(
"global configuration",
"name", cfg.Name,
Expand Down
106 changes: 74 additions & 32 deletions cmd/gossamer/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,54 +136,90 @@ func TestGlobalConfigFromFlags(t *testing.T) {
[]string{"config"},
[]interface{}{testCfgFile.Name()},
dot.GlobalConfig{
Name: testCfg.Global.Name,
ID: testCfg.Global.ID,
BasePath: testCfg.Global.BasePath,
LogLvl: log.LvlInfo,
Name: testCfg.Global.Name,
ID: testCfg.Global.ID,
BasePath: testCfg.Global.BasePath,
LogLvl: log.LvlInfo,
PublishMetrics: testCfg.Global.PublishMetrics,
MetricsPort: testCfg.Global.MetricsPort,
},
},
{
"Test gossamer --chain",
[]string{"config", "chain"},
[]interface{}{testCfgFile.Name(), "ksmcc"},
dot.GlobalConfig{
Name: dot.KsmccConfig().Global.Name,
ID: "ksmcc",
BasePath: dot.KsmccConfig().Global.BasePath,
LogLvl: log.LvlInfo,
Name: dot.KsmccConfig().Global.Name,
ID: "ksmcc",
BasePath: dot.KsmccConfig().Global.BasePath,
LogLvl: log.LvlInfo,
PublishMetrics: testCfg.Global.PublishMetrics,
MetricsPort: testCfg.Global.MetricsPort,
},
},
{
"Test gossamer --name",
[]string{"config", "name"},
[]interface{}{testCfgFile.Name(), "test_name"},
dot.GlobalConfig{
Name: "test_name",
ID: testCfg.Global.ID,
BasePath: testCfg.Global.BasePath,
LogLvl: log.LvlInfo,
Name: "test_name",
ID: testCfg.Global.ID,
BasePath: testCfg.Global.BasePath,
LogLvl: log.LvlInfo,
PublishMetrics: testCfg.Global.PublishMetrics,
MetricsPort: testCfg.Global.MetricsPort,
},
},
{
"Test gossamer --basepath",
[]string{"config", "basepath"},
[]interface{}{testCfgFile.Name(), "test_basepath"},
dot.GlobalConfig{
Name: testCfg.Global.Name,
ID: testCfg.Global.ID,
BasePath: "test_basepath",
LogLvl: log.LvlInfo,
Name: testCfg.Global.Name,
ID: testCfg.Global.ID,
BasePath: "test_basepath",
LogLvl: log.LvlInfo,
PublishMetrics: testCfg.Global.PublishMetrics,
MetricsPort: testCfg.Global.MetricsPort,
},
},
{
"Test gossamer --roles",
[]string{"config", "roles"},
[]interface{}{testCfgFile.Name(), "1"},
dot.GlobalConfig{
Name: testCfg.Global.Name,
ID: testCfg.Global.ID,
BasePath: testCfg.Global.BasePath,
LogLvl: log.LvlInfo,
Name: testCfg.Global.Name,
ID: testCfg.Global.ID,
BasePath: testCfg.Global.BasePath,
LogLvl: log.LvlInfo,
PublishMetrics: testCfg.Global.PublishMetrics,
MetricsPort: testCfg.Global.MetricsPort,
},
},
{
"Test gossamer --publish-metrics",
[]string{"config", "publish-metrics"},
[]interface{}{testCfgFile.Name(), true},
dot.GlobalConfig{
Name: testCfg.Global.Name,
ID: testCfg.Global.ID,
BasePath: testCfg.Global.BasePath,
LogLvl: log.LvlInfo,
PublishMetrics: true,
MetricsPort: testCfg.Global.MetricsPort,
},
},
{
"Test gossamer --metrics-port",
[]string{"config", "metrics-port"},
[]interface{}{testCfgFile.Name(), "9871"},
dot.GlobalConfig{
Name: testCfg.Global.Name,
ID: testCfg.Global.ID,
BasePath: testCfg.Global.BasePath,
LogLvl: log.LvlInfo,
PublishMetrics: testCfg.Global.PublishMetrics,
MetricsPort: uint32(9871),
},
},
}
Expand Down Expand Up @@ -621,10 +657,12 @@ func TestUpdateConfigFromGenesisJSON(t *testing.T) {

expected := &dot.Config{
Global: dot.GlobalConfig{
Name: testCfg.Global.Name,
ID: testCfg.Global.ID,
BasePath: testCfg.Global.BasePath,
LogLvl: testCfg.Global.LogLvl,
Name: testCfg.Global.Name,
ID: testCfg.Global.ID,
BasePath: testCfg.Global.BasePath,
LogLvl: testCfg.Global.LogLvl,
PublishMetrics: testCfg.Global.PublishMetrics,
MetricsPort: testCfg.Global.MetricsPort,
},
Log: dot.LogConfig{
CoreLvl: log.LvlInfo,
Expand Down Expand Up @@ -671,10 +709,12 @@ func TestUpdateConfigFromGenesisJSON_Default(t *testing.T) {

expected := &dot.Config{
Global: dot.GlobalConfig{
Name: testCfg.Global.Name,
ID: testCfg.Global.ID,
BasePath: testCfg.Global.BasePath,
LogLvl: testCfg.Global.LogLvl,
Name: testCfg.Global.Name,
ID: testCfg.Global.ID,
BasePath: testCfg.Global.BasePath,
LogLvl: testCfg.Global.LogLvl,
PublishMetrics: testCfg.Global.PublishMetrics,
MetricsPort: testCfg.Global.MetricsPort,
},
Log: dot.LogConfig{
CoreLvl: log.LvlInfo,
Expand Down Expand Up @@ -720,10 +760,12 @@ func TestUpdateConfigFromGenesisData(t *testing.T) {

expected := &dot.Config{
Global: dot.GlobalConfig{
Name: testCfg.Global.Name,
ID: testCfg.Global.ID,
BasePath: testCfg.Global.BasePath,
LogLvl: testCfg.Global.LogLvl,
Name: testCfg.Global.Name,
ID: testCfg.Global.ID,
BasePath: testCfg.Global.BasePath,
LogLvl: testCfg.Global.LogLvl,
PublishMetrics: testCfg.Global.PublishMetrics,
MetricsPort: testCfg.Global.MetricsPort,
},
Log: dot.LogConfig{
CoreLvl: log.LvlInfo,
Expand Down
9 changes: 5 additions & 4 deletions cmd/gossamer/export.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,10 +79,11 @@ func dotConfigToToml(dcfg *dot.Config) *ctoml.Config {
cfg := &ctoml.Config{}

cfg.Global = ctoml.GlobalConfig{
Name: dcfg.Global.Name,
ID: dcfg.Global.ID,
BasePath: dcfg.Global.BasePath,
LogLvl: dcfg.Global.LogLvl.String(),
Name: dcfg.Global.Name,
ID: dcfg.Global.ID,
BasePath: dcfg.Global.BasePath,
LogLvl: dcfg.Global.LogLvl.String(),
MetricsPort: dcfg.Global.MetricsPort,
}

cfg.Log = ctoml.LogConfig{
Expand Down
10 changes: 6 additions & 4 deletions cmd/gossamer/export_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,12 @@ func TestExportCommand(t *testing.T) {
[]interface{}{testConfig, genFile.Name(), testDir, testName, log.LvlInfo.String(), "true"},
&dot.Config{
Global: dot.GlobalConfig{
Name: testName,
ID: testCfg.Global.ID,
BasePath: testCfg.Global.BasePath,
LogLvl: log.LvlInfo,
Name: testName,
ID: testCfg.Global.ID,
BasePath: testCfg.Global.BasePath,
LogLvl: log.LvlInfo,
PublishMetrics: testCfg.Global.PublishMetrics,
MetricsPort: testCfg.Global.MetricsPort,
},
Log: dot.LogConfig{
CoreLvl: log.LvlInfo,
Expand Down
16 changes: 16 additions & 0 deletions cmd/gossamer/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,18 @@ var (
Name: "memprof",
Usage: "File to write memory profile to",
}

// PublishMetricsFlag publishes node metrics to prometheus.
PublishMetricsFlag = cli.BoolFlag{
Name: "publish-metrics",
Usage: "Publish node metrics",
}

// MetricsPortFlag set metric listen port
MetricsPortFlag = cli.StringFlag{
Name: "metrics-port",
Usage: "Set metric listening port ",
}
)

// Initialization-only flags
Expand Down Expand Up @@ -263,6 +275,10 @@ var (
WSFlag,
WSExternalFlag,
WSPortFlag,

// metrics flag
PublishMetricsFlag,
MetricsPortFlag,
}
)

Expand Down
1 change: 0 additions & 1 deletion cmd/gossamer/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ import (
"github.com/ChainSafe/gossamer/dot"
"github.com/ChainSafe/gossamer/lib/keystore"
"github.com/ChainSafe/gossamer/lib/utils"

log "github.com/ChainSafe/log15"
"github.com/urfave/cli"
)
Expand Down
10 changes: 6 additions & 4 deletions cmd/gossamer/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,10 +88,12 @@ func newTestConfig(t *testing.T) *dot.Config {

cfg := &dot.Config{
Global: dot.GlobalConfig{
Name: dot.GssmrConfig().Global.Name,
ID: dot.GssmrConfig().Global.ID,
BasePath: dir,
LogLvl: log.LvlInfo,
Name: dot.GssmrConfig().Global.Name,
ID: dot.GssmrConfig().Global.ID,
BasePath: dir,
LogLvl: log.LvlInfo,
PublishMetrics: dot.GssmrConfig().Global.PublishMetrics,
MetricsPort: dot.GssmrConfig().Global.MetricsPort,
},
Log: dot.LogConfig{
CoreLvl: log.LvlInfo,
Expand Down
28 changes: 16 additions & 12 deletions dot/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,12 @@ type Config struct {

// GlobalConfig is used for every node command
type GlobalConfig struct {
Name string
ID string
BasePath string
LogLvl log.Lvl
Name string
ID string
BasePath string
LogLvl log.Lvl
PublishMetrics bool
MetricsPort uint32
}

// LogConfig represents the log levels for individual packages
Expand Down Expand Up @@ -128,10 +130,11 @@ func networkServiceEnabled(cfg *Config) bool {
func GssmrConfig() *Config {
return &Config{
Global: GlobalConfig{
Name: gssmr.DefaultName,
ID: gssmr.DefaultID,
BasePath: gssmr.DefaultBasePath,
LogLvl: gssmr.DefaultLvl,
Name: gssmr.DefaultName,
ID: gssmr.DefaultID,
BasePath: gssmr.DefaultBasePath,
LogLvl: gssmr.DefaultLvl,
MetricsPort: gssmr.DefaultMetricsPort,
},
Log: LogConfig{
CoreLvl: gssmr.DefaultLvl,
Expand Down Expand Up @@ -175,10 +178,11 @@ func GssmrConfig() *Config {
func KsmccConfig() *Config {
return &Config{
Global: GlobalConfig{
Name: ksmcc.DefaultName,
ID: ksmcc.DefaultID,
BasePath: ksmcc.DefaultBasePath,
LogLvl: ksmcc.DefaultLvl,
Name: ksmcc.DefaultName,
ID: ksmcc.DefaultID,
BasePath: ksmcc.DefaultBasePath,
LogLvl: ksmcc.DefaultLvl,
MetricsPort: ksmcc.DefaultMetricsPort,
},
Log: LogConfig{
CoreLvl: ksmcc.DefaultLvl,
Expand Down
9 changes: 5 additions & 4 deletions dot/config/toml/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,11 @@ type Config struct {

// GlobalConfig is to marshal/unmarshal toml global config vars
type GlobalConfig struct {
Name string `toml:"name,omitempty"`
ID string `toml:"id,omitempty"`
BasePath string `toml:"basepath,omitempty"`
LogLvl string `toml:"log,omitempty"`
Name string `toml:"name,omitempty"`
ID string `toml:"id,omitempty"`
BasePath string `toml:"basepath,omitempty"`
LogLvl string `toml:"log,omitempty"`
MetricsPort uint32 `toml:"metrics-port,omitempty"`
}

// LogConfig represents the log levels for individual packages
Expand Down
Loading