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

cmd/geth: dump config for metrics #22083

Merged
merged 7 commits into from
Jan 18, 2021
Merged
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 67 additions & 3 deletions cmd/geth/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,19 @@ type ethstatsConfig struct {
URL string `toml:",omitempty"`
}

type metricConfig struct {
Enabled bool `toml:",omitempty"`
EnabledExpensive bool `toml:",omitempty"`
HTTP string `toml:",omitempty"`
Port int `toml:",omitempty"`
EnableInfluxDB bool `toml:",omitempty"`
InfluxDBEndpoint string `toml:",omitempty"`
InfluxDBDatabase string `toml:",omitempty"`
InfluxDBUsername string `toml:",omitempty"`
InfluxDBPassword string `toml:",omitempty"`
InfluxDBTags string `toml:",omitempty"`
}

// whisper has been deprecated, but clients out there might still have [Shh]
// in their config, which will crash. Cut them some slack by keeping the
// config, and displaying a message that those config switches are ineffectual.
Expand All @@ -88,6 +101,7 @@ type gethConfig struct {
Shh whisperDeprecatedConfig
Node node.Config
Ethstats ethstatsConfig
Metrics metricConfig
}

func loadConfig(file string, cfg *gethConfig) error {
Expand Down Expand Up @@ -115,12 +129,28 @@ func defaultNodeConfig() node.Config {
return cfg
}

func defaultMetricConfig(ctx *cli.Context) metricConfig {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps this should be metricsFromCliArgs.. When I saw defaultMetricConfig being used before, I intuitively though that it just initialized some defaults, and didn't grokk that it actually read the cli arguments

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, that's the thing with this strange logic, we need to read the metrics twice.
First we need to read the "default" metrics and apply them s.th. if we don't override them in the load config, they are set to the default values.
Then, after reading the config, we need to read the metrics again (this time only the specially set metrics) to overwrite the config file configs with the configs from cli.

So basically we do:

  • read config from cli regardless of whether they are set (reads defaults)
  • read config from file (overwrites the default configs if applicable)
  • read only the set configs from cli (overwrites the file configs)

I'll change the naming!

return metricConfig{
Enabled: ctx.GlobalBool(utils.MetricsEnabledFlag.Name),
EnabledExpensive: ctx.GlobalBool(utils.MetricsEnabledExpensiveFlag.Name),
HTTP: ctx.GlobalString(utils.MetricsHTTPFlag.Name),
Port: ctx.GlobalInt(utils.MetricsPortFlag.Name),
EnableInfluxDB: ctx.GlobalBool(utils.MetricsEnableInfluxDBFlag.Name),
InfluxDBEndpoint: ctx.GlobalString(utils.MetricsInfluxDBEndpointFlag.Name),
InfluxDBDatabase: ctx.GlobalString(utils.MetricsInfluxDBDatabaseFlag.Name),
InfluxDBUsername: ctx.GlobalString(utils.MetricsInfluxDBUsernameFlag.Name),
InfluxDBPassword: ctx.GlobalString(utils.MetricsInfluxDBPasswordFlag.Name),
InfluxDBTags: ctx.GlobalString(utils.MetricsInfluxDBTagsFlag.Name),
}
}

// makeConfigNode loads geth configuration and creates a blank node instance.
func makeConfigNode(ctx *cli.Context) (*node.Node, gethConfig) {
// Load defaults.
cfg := gethConfig{
Eth: eth.DefaultConfig,
Node: defaultNodeConfig(),
Eth: eth.DefaultConfig,
Node: defaultNodeConfig(),
Metrics: defaultMetricConfig(ctx),
}

// Load config file.
Expand All @@ -133,7 +163,6 @@ func makeConfigNode(ctx *cli.Context) (*node.Node, gethConfig) {
log.Warn("Deprecated whisper config detected. Whisper has been moved to github.com/ethereum/whisper")
}
}

// Apply flags.
utils.SetNodeConfig(ctx, &cfg.Node)
stack, err := node.New(&cfg.Node)
Expand All @@ -146,6 +175,8 @@ func makeConfigNode(ctx *cli.Context) (*node.Node, gethConfig) {
}
utils.SetShhConfig(ctx, stack)

applyMetricConfig(ctx, &cfg)

return stack, cfg
}

Expand Down Expand Up @@ -204,3 +235,36 @@ func dumpConfig(ctx *cli.Context) error {

return nil
}

func applyMetricConfig(ctx *cli.Context, cfg *gethConfig) {
if ctx.GlobalIsSet(utils.MetricsEnabledFlag.Name) {
cfg.Metrics.Enabled = ctx.GlobalBool(utils.MetricsEnabledFlag.Name)
}
if ctx.GlobalIsSet(utils.MetricsEnabledExpensiveFlag.Name) {
cfg.Metrics.EnabledExpensive = ctx.GlobalBool(utils.MetricsEnabledExpensiveFlag.Name)
}
if ctx.GlobalIsSet(utils.MetricsHTTPFlag.Name) {
cfg.Metrics.HTTP = ctx.GlobalString(utils.MetricsHTTPFlag.Name)
}
if ctx.GlobalIsSet(utils.MetricsPortFlag.Name) {
cfg.Metrics.Port = ctx.GlobalInt(utils.MetricsPortFlag.Name)
}
if ctx.GlobalIsSet(utils.MetricsEnableInfluxDBFlag.Name) {
cfg.Metrics.EnableInfluxDB = ctx.GlobalBool(utils.MetricsEnableInfluxDBFlag.Name)
}
if ctx.GlobalIsSet(utils.MetricsInfluxDBEndpointFlag.Name) {
cfg.Metrics.InfluxDBEndpoint = ctx.GlobalString(utils.MetricsInfluxDBEndpointFlag.Name)
}
if ctx.GlobalIsSet(utils.MetricsInfluxDBDatabaseFlag.Name) {
cfg.Metrics.InfluxDBDatabase = ctx.GlobalString(utils.MetricsInfluxDBDatabaseFlag.Name)
}
if ctx.GlobalIsSet(utils.MetricsInfluxDBUsernameFlag.Name) {
cfg.Metrics.InfluxDBUsername = ctx.GlobalString(utils.MetricsInfluxDBUsernameFlag.Name)
}
if ctx.GlobalIsSet(utils.MetricsInfluxDBPasswordFlag.Name) {
cfg.Metrics.InfluxDBPassword = ctx.GlobalString(utils.MetricsInfluxDBPasswordFlag.Name)
}
if ctx.GlobalIsSet(utils.MetricsInfluxDBTagsFlag.Name) {
cfg.Metrics.InfluxDBTags = ctx.GlobalString(utils.MetricsInfluxDBTagsFlag.Name)
}
}