Skip to content

Commit

Permalink
metrics: peek into toml to decide enabling
Browse files Browse the repository at this point in the history
  • Loading branch information
lightclient committed Nov 22, 2024
1 parent f6971a7 commit 3fe6879
Showing 1 changed file with 23 additions and 3 deletions.
26 changes: 23 additions & 3 deletions metrics/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"time"

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

// Enabled is checked by the constructor functions for all of the
Expand All @@ -37,17 +38,35 @@ func init() {
for _, enabler := range enablerEnvVars {
if val, found := syscall.Getenv(enabler); found && !Enabled {
if enable, _ := strconv.ParseBool(val); enable { // ignore error, flag parser will choke on it later
log.Info("Enabling metrics collection")
Enabled = true
}
}
}
for _, arg := range os.Args {
for i, arg := range os.Args {
flag := strings.TrimLeft(arg, "-")

// If geth is being configured by toml, try to peak into it.
if !Enabled && strings.HasPrefix(flag, "config") {
file := strings.TrimSpace(os.Args[i+1])
f, err := os.Open(file)
if err != nil {
continue
}
defer f.Close()

var cfg map[string]map[string]any
if err := toml.NewDecoder(f).Decode(&cfg); err != nil {
continue
}
if m, ok := cfg["Metrics"]; ok {
if v, ok := m["Enabled"].(bool); ok && v {
Enabled = true
}
}
}

for _, enabler := range enablerFlags {
if !Enabled && flag == enabler {
log.Info("Enabling metrics collection")
Enabled = true
}
}
Expand Down Expand Up @@ -129,6 +148,7 @@ func readRuntimeStats(v *runtimeStats) {
func CollectProcessMetrics(refresh time.Duration) {
// Short circuit if the metrics system is disabled
if !Enabled {
log.Warn("Nah I don't wanna do metrics")
return
}

Expand Down

0 comments on commit 3fe6879

Please sign in to comment.