cmd/geth: actually enable metrics when passed via toml

This commit is contained in:
lightclient 2024-11-21 20:57:29 +08:00
parent e3d61e6db0
commit 83b532c4da
No known key found for this signature in database
GPG Key ID: 75C916AFEE20183E
4 changed files with 31 additions and 39 deletions

View File

@ -282,14 +282,13 @@ func importChain(ctx *cli.Context) error {
if ctx.Args().Len() < 1 { if ctx.Args().Len() < 1 {
utils.Fatalf("This command requires an argument.") utils.Fatalf("This command requires an argument.")
} }
// Start metrics export if enabled stack, cfg := makeConfigNode(ctx)
utils.SetupMetrics(ctx)
// Start system runtime metrics collection
go metrics.CollectProcessMetrics(3 * time.Second)
stack, _ := makeConfigNode(ctx)
defer stack.Close() defer stack.Close()
// Start metrics export if enabled
utils.SetupMetrics(&cfg.Metrics)
go metrics.CollectProcessMetrics(3 * time.Second)
chain, db := utils.MakeChain(ctx, stack, false) chain, db := utils.MakeChain(ctx, stack, false)
defer db.Close() defer db.Close()

View File

@ -25,6 +25,7 @@ import (
"runtime" "runtime"
"slices" "slices"
"strings" "strings"
"time"
"unicode" "unicode"
"github.com/ethereum/go-ethereum/accounts" "github.com/ethereum/go-ethereum/accounts"
@ -192,6 +193,10 @@ func makeFullNode(ctx *cli.Context) *node.Node {
cfg.Eth.OverrideVerkle = &v cfg.Eth.OverrideVerkle = &v
} }
// Start metrics export if enabled
utils.SetupMetrics(&cfg.Metrics)
go metrics.CollectProcessMetrics(3 * time.Second)
backend, eth := utils.RegisterEthService(stack, &cfg.Eth) backend, eth := utils.RegisterEthService(stack, &cfg.Eth)
// Create gauge with geth system and build information // Create gauge with geth system and build information

View File

@ -34,7 +34,6 @@ import (
"github.com/ethereum/go-ethereum/internal/debug" "github.com/ethereum/go-ethereum/internal/debug"
"github.com/ethereum/go-ethereum/internal/flags" "github.com/ethereum/go-ethereum/internal/flags"
"github.com/ethereum/go-ethereum/log" "github.com/ethereum/go-ethereum/log"
"github.com/ethereum/go-ethereum/metrics"
"github.com/ethereum/go-ethereum/node" "github.com/ethereum/go-ethereum/node"
"go.uber.org/automaxprocs/maxprocs" "go.uber.org/automaxprocs/maxprocs"
@ -325,12 +324,6 @@ func prepare(ctx *cli.Context) {
ctx.Set(utils.CacheFlag.Name, strconv.Itoa(4096)) ctx.Set(utils.CacheFlag.Name, strconv.Itoa(4096))
} }
} }
// Start metrics export if enabled
utils.SetupMetrics(ctx)
// Start system runtime metrics collection
go metrics.CollectProcessMetrics(3 * time.Second)
} }
// geth is the main entry point into the system if no special subcommand is run. // geth is the main entry point into the system if no special subcommand is run.

View File

@ -1969,24 +1969,19 @@ func RegisterFullSyncTester(stack *node.Node, eth *eth.Ethereum, target common.H
log.Info("Registered full-sync tester", "hash", target) log.Info("Registered full-sync tester", "hash", target)
} }
func SetupMetrics(ctx *cli.Context) { func SetupMetrics(cfg *metrics.Config) {
if metrics.Enabled { if cfg.Enabled {
log.Info("Enabling metrics collection") log.Info("Enabling metrics collection")
var ( var (
enableExport = ctx.Bool(MetricsEnableInfluxDBFlag.Name) enableExport = cfg.EnableInfluxDB
enableExportV2 = ctx.Bool(MetricsEnableInfluxDBV2Flag.Name) enableExportV2 = cfg.EnableInfluxDBV2
) )
if enableExport && enableExportV2 {
Fatalf("Flags %v can't be used at the same time", strings.Join([]string{MetricsEnableInfluxDBFlag.Name, MetricsEnableInfluxDBV2Flag.Name}, ", "))
}
if enableExport || enableExportV2 { if enableExport || enableExportV2 {
CheckExclusive(ctx, MetricsEnableInfluxDBFlag, MetricsEnableInfluxDBV2Flag) v1FlagIsSet := cfg.InfluxDBUsername != "" || cfg.InfluxDBPassword != ""
v2FlagIsSet := cfg.InfluxDBToken != "" || cfg.InfluxDBOrganization != "" || cfg.InfluxDBBucket != ""
v1FlagIsSet := ctx.IsSet(MetricsInfluxDBUsernameFlag.Name) ||
ctx.IsSet(MetricsInfluxDBPasswordFlag.Name)
v2FlagIsSet := ctx.IsSet(MetricsInfluxDBTokenFlag.Name) ||
ctx.IsSet(MetricsInfluxDBOrganizationFlag.Name) ||
ctx.IsSet(MetricsInfluxDBBucketFlag.Name)
if enableExport && v2FlagIsSet { if enableExport && v2FlagIsSet {
Fatalf("Flags --influxdb.metrics.organization, --influxdb.metrics.token, --influxdb.metrics.bucket are only available for influxdb-v2") Fatalf("Flags --influxdb.metrics.organization, --influxdb.metrics.token, --influxdb.metrics.bucket are only available for influxdb-v2")
@ -1996,35 +1991,35 @@ func SetupMetrics(ctx *cli.Context) {
} }
var ( var (
endpoint = ctx.String(MetricsInfluxDBEndpointFlag.Name) endpoint = cfg.InfluxDBEndpoint
database = ctx.String(MetricsInfluxDBDatabaseFlag.Name) database = cfg.InfluxDBDatabase
username = ctx.String(MetricsInfluxDBUsernameFlag.Name) username = cfg.InfluxDBUsername
password = ctx.String(MetricsInfluxDBPasswordFlag.Name) password = cfg.InfluxDBPassword
token = ctx.String(MetricsInfluxDBTokenFlag.Name) token = cfg.InfluxDBToken
bucket = ctx.String(MetricsInfluxDBBucketFlag.Name) bucket = cfg.InfluxDBBucket
organization = ctx.String(MetricsInfluxDBOrganizationFlag.Name) organization = cfg.InfluxDBOrganization
) )
if enableExport { if enableExport {
tagsMap := SplitTagsFlag(ctx.String(MetricsInfluxDBTagsFlag.Name)) tagsMap := SplitTagsFlag(cfg.InfluxDBTags)
log.Info("Enabling metrics export to InfluxDB") log.Info("Enabling metrics export to InfluxDB")
go influxdb.InfluxDBWithTags(metrics.DefaultRegistry, 10*time.Second, endpoint, database, username, password, "geth.", tagsMap) go influxdb.InfluxDBWithTags(metrics.DefaultRegistry, 10*time.Second, endpoint, database, username, password, "geth.", tagsMap)
} else if enableExportV2 { } else if enableExportV2 {
tagsMap := SplitTagsFlag(ctx.String(MetricsInfluxDBTagsFlag.Name)) tagsMap := SplitTagsFlag(cfg.InfluxDBTags)
log.Info("Enabling metrics export to InfluxDB (v2)") log.Info("Enabling metrics export to InfluxDB (v2)")
go influxdb.InfluxDBV2WithTags(metrics.DefaultRegistry, 10*time.Second, endpoint, token, bucket, organization, "geth.", tagsMap) go influxdb.InfluxDBV2WithTags(metrics.DefaultRegistry, 10*time.Second, endpoint, token, bucket, organization, "geth.", tagsMap)
} }
if ctx.IsSet(MetricsHTTPFlag.Name) { if cfg.HTTP != "" {
address := net.JoinHostPort(ctx.String(MetricsHTTPFlag.Name), fmt.Sprintf("%d", ctx.Int(MetricsPortFlag.Name))) address := net.JoinHostPort(cfg.HTTP, fmt.Sprintf("%d", cfg.Port))
log.Info("Enabling stand-alone metrics HTTP endpoint", "address", address) log.Info("Enabling stand-alone metrics HTTP endpoint", "address", address)
exp.Setup(address) exp.Setup(address)
} else if ctx.IsSet(MetricsPortFlag.Name) { } else if cfg.HTTP == "" && cfg.Port != 0 {
log.Warn(fmt.Sprintf("--%s specified without --%s, metrics server will not start.", MetricsPortFlag.Name, MetricsHTTPFlag.Name)) log.Warn(fmt.Sprintf("--%s specified without --%s, metrics server will not start.", MetricsPortFlag.Name, MetricsHTTPFlag.Name))
} }
} }