Skip to content

Commit 65a17c0

Browse files
authored
metrics: add support for enabling metrics from env vars (#28118)
1 parent 909dd4a commit 65a17c0

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

metrics/metrics.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@ import (
99
"os"
1010
"runtime/metrics"
1111
"runtime/pprof"
12+
"strconv"
1213
"strings"
14+
"syscall"
1315
"time"
1416

1517
"github.com/ethereum/go-ethereum/log"
@@ -30,13 +32,35 @@ var EnabledExpensive = false
3032
// enablerFlags is the CLI flag names to use to enable metrics collections.
3133
var enablerFlags = []string{"metrics"}
3234

35+
// enablerEnvVars is the env var names to use to enable metrics collections.
36+
var enablerEnvVars = []string{"GETH_METRICS"}
37+
3338
// expensiveEnablerFlags is the CLI flag names to use to enable metrics collections.
3439
var expensiveEnablerFlags = []string{"metrics.expensive"}
3540

41+
// expensiveEnablerEnvVars is the env var names to use to enable metrics collections.
42+
var expensiveEnablerEnvVars = []string{"GETH_METRICS_EXPENSIVE"}
43+
3644
// Init enables or disables the metrics system. Since we need this to run before
3745
// any other code gets to create meters and timers, we'll actually do an ugly hack
3846
// and peek into the command line args for the metrics flag.
3947
func init() {
48+
for _, enabler := range enablerEnvVars {
49+
if val, found := syscall.Getenv(enabler); found && !Enabled {
50+
if enable, _ := strconv.ParseBool(val); enable { // ignore error, flag parser will choke on it later
51+
log.Info("Enabling metrics collection")
52+
Enabled = true
53+
}
54+
}
55+
}
56+
for _, enabler := range expensiveEnablerEnvVars {
57+
if val, found := syscall.Getenv(enabler); found && !EnabledExpensive {
58+
if enable, _ := strconv.ParseBool(val); enable { // ignore error, flag parser will choke on it later
59+
log.Info("Enabling expensive metrics collection")
60+
EnabledExpensive = true
61+
}
62+
}
63+
}
4064
for _, arg := range os.Args {
4165
flag := strings.TrimLeft(arg, "-")
4266

0 commit comments

Comments
 (0)