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

metrics: handle values from config file #565

Merged
merged 3 commits into from
Oct 20, 2022
Merged
Show file tree
Hide file tree
Changes from all 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
10 changes: 10 additions & 0 deletions internal/cli/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,12 @@ func (s *Server) Stop() {
}

func (s *Server) setupMetrics(config *TelemetryConfig, serviceName string) error {
// Check the global metrics if they're matching with the provided config
if metrics.Enabled != config.Enabled || metrics.EnabledExpensive != config.Expensive {
log.Warn("Metric misconfiguration, some of them might not be visible")
}

// Update the values anyways (for services which don't need immediate attention)
metrics.Enabled = config.Enabled
metrics.EnabledExpensive = config.Expensive

Expand All @@ -267,6 +273,10 @@ func (s *Server) setupMetrics(config *TelemetryConfig, serviceName string) error

log.Info("Enabling metrics collection")

if metrics.EnabledExpensive {
log.Info("Enabling expensive metrics collection")
}

// influxdb
if v1Enabled, v2Enabled := config.InfluxDB.V1Enabled, config.InfluxDB.V2Enabled; v1Enabled || v2Enabled {
if v1Enabled && v2Enabled {
Expand Down
53 changes: 49 additions & 4 deletions metrics/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
"strings"
"time"

"github.com/ethereum/go-ethereum/log"
"github.com/BurntSushi/toml"
)

// Enabled is checked by the constructor functions for all of the
Expand All @@ -32,26 +32,71 @@ var enablerFlags = []string{"metrics"}
// expensiveEnablerFlags is the CLI flag names to use to enable metrics collections.
var expensiveEnablerFlags = []string{"metrics.expensive"}

// configFlag is the CLI flag name to use to start node by providing a toml based config
var configFlag = "config"

// Init enables or disables the metrics system. Since we need this to run before
// any other code gets to create meters and timers, we'll actually do an ugly hack
// and peek into the command line args for the metrics flag.
func init() {
for _, arg := range os.Args {
var configFile string

for i := 0; i < len(os.Args); i++ {
arg := os.Args[i]

flag := strings.TrimLeft(arg, "-")

// check for existence of `config` flag
if flag == configFlag && i < len(os.Args)-1 {
configFile = strings.TrimLeft(os.Args[i+1], "-") // find the value of flag
}

for _, enabler := range enablerFlags {
if !Enabled && flag == enabler {
log.Info("Enabling metrics collection")
Enabled = true
}
}

for _, enabler := range expensiveEnablerFlags {
if !EnabledExpensive && flag == enabler {
log.Info("Enabling expensive metrics collection")
EnabledExpensive = true
}
}
}

// Update the global metrics value, if they're provided in the config file
updateMetricsFromConfig(configFile)
}

func updateMetricsFromConfig(path string) {
// Don't act upon any errors here. They're already taken into
// consideration when the toml config file will be parsed in the cli.
data, err := os.ReadFile(path)
tomlData := string(data)

if err != nil {
return
}

// Create a minimal config to decode
type TelemetryConfig struct {
Enabled bool `hcl:"metrics,optional" toml:"metrics,optional"`
Expensive bool `hcl:"expensive,optional" toml:"expensive,optional"`
}

type CliConfig struct {
Telemetry *TelemetryConfig `hcl:"telemetry,block" toml:"telemetry,block"`
}

conf := &CliConfig{}

if _, err := toml.Decode(tomlData, &conf); err != nil || conf == nil {
return
}

// We have the values now, update them
Enabled = conf.Telemetry.Enabled
EnabledExpensive = conf.Telemetry.Expensive
}

// CollectProcessMetrics periodically collects various metrics about the running
Expand Down