cmd/geth: move process metrics startup into utils.SetupMetrics

This commit is contained in:
Felix Lange 2024-12-10 12:18:34 +01:00
parent 7c60c1c845
commit 42c5c81bf0
3 changed files with 9 additions and 13 deletions

View file

@ -38,7 +38,6 @@ import (
"github.com/ethereum/go-ethereum/ethdb"
"github.com/ethereum/go-ethereum/internal/era"
"github.com/ethereum/go-ethereum/log"
"github.com/ethereum/go-ethereum/metrics"
"github.com/ethereum/go-ethereum/params"
"github.com/urfave/cli/v2"
)
@ -288,9 +287,6 @@ func importChain(ctx *cli.Context) error {
// Start metrics export if enabled
utils.SetupMetrics(&cfg.Metrics)
// Start system runtime metrics collection
go metrics.CollectProcessMetrics(3 * time.Second)
chain, db := utils.MakeChain(ctx, stack, false)
defer db.Close()

View file

@ -25,7 +25,6 @@ import (
"runtime"
"slices"
"strings"
"time"
"unicode"
"github.com/ethereum/go-ethereum/accounts"
@ -195,7 +194,6 @@ func makeFullNode(ctx *cli.Context) *node.Node {
// Start metrics export if enabled
utils.SetupMetrics(&cfg.Metrics)
go metrics.CollectProcessMetrics(3 * time.Second)
backend, eth := utils.RegisterEthService(stack, &cfg.Eth)

View file

@ -1969,12 +1969,15 @@ func RegisterFullSyncTester(stack *node.Node, eth *eth.Ethereum, target common.H
log.Info("Registered full-sync tester", "hash", target)
}
// SetupMetrics configures the metrics system.
func SetupMetrics(cfg *metrics.Config) {
if !cfg.Enabled {
return
}
metrics.Enable()
log.Info("Enabling metrics collection")
metrics.Enable()
// InfluxDB exporter.
var (
enableExport = cfg.EnableInfluxDB
enableExportV2 = cfg.EnableInfluxDBV2
@ -1991,22 +1994,18 @@ func SetupMetrics(cfg *metrics.Config) {
token = cfg.InfluxDBToken
bucket = cfg.InfluxDBBucket
organization = cfg.InfluxDBOrganization
tagsMap = SplitTagsFlag(cfg.InfluxDBTags)
)
if enableExport {
tagsMap := SplitTagsFlag(cfg.InfluxDBTags)
log.Info("Enabling metrics export to InfluxDB")
go influxdb.InfluxDBWithTags(metrics.DefaultRegistry, 10*time.Second, endpoint, database, username, password, "geth.", tagsMap)
} else if enableExportV2 {
tagsMap := SplitTagsFlag(cfg.InfluxDBTags)
log.Info("Enabling metrics export to InfluxDB (v2)")
go influxdb.InfluxDBV2WithTags(metrics.DefaultRegistry, 10*time.Second, endpoint, token, bucket, organization, "geth.", tagsMap)
}
// Expvar exporter.
if cfg.HTTP != "" {
address := net.JoinHostPort(cfg.HTTP, fmt.Sprintf("%d", cfg.Port))
log.Info("Enabling stand-alone metrics HTTP endpoint", "address", address)
@ -2014,6 +2013,9 @@ func SetupMetrics(cfg *metrics.Config) {
} else if cfg.HTTP == "" && cfg.Port != 0 {
log.Warn(fmt.Sprintf("--%s specified without --%s, metrics server will not start.", MetricsPortFlag.Name, MetricsHTTPFlag.Name))
}
// Enable system metrics collection.
go metrics.CollectProcessMetrics(3 * time.Second)
}
// SplitTagsFlag parses a comma-separated list of k=v metrics tags.