Skip to content

Commit 10555d4

Browse files
cmd/geth: dump config for metrics (#22083)
* cmd/geth: dump config * cmd/geth: dump config * cmd/geth: properly read config again * cmd/geth: override metrics if flags are set * cmd/geth: write metrics regardless if enabled * cmd/geth: renamed to metricsfromcliargs * metrics: add default configuration
1 parent 3981822 commit 10555d4

3 files changed

Lines changed: 92 additions & 10 deletions

File tree

‎cmd/geth/config.go‎

Lines changed: 40 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ import (
3030
"github.com/ethereum/go-ethereum/eth"
3131
"github.com/ethereum/go-ethereum/internal/ethapi"
3232
"github.com/ethereum/go-ethereum/log"
33+
"github.com/ethereum/go-ethereum/metrics"
3334
"github.com/ethereum/go-ethereum/node"
3435
"github.com/ethereum/go-ethereum/params"
3536
"github.com/naoina/toml"
@@ -88,6 +89,7 @@ type gethConfig struct {
8889
Shh whisperDeprecatedConfig
8990
Node node.Config
9091
Ethstats ethstatsConfig
92+
Metrics metrics.Config
9193
}
9294

9395
func loadConfig(file string, cfg *gethConfig) error {
@@ -119,8 +121,9 @@ func defaultNodeConfig() node.Config {
119121
func makeConfigNode(ctx *cli.Context) (*node.Node, gethConfig) {
120122
// Load defaults.
121123
cfg := gethConfig{
122-
Eth: eth.DefaultConfig,
123-
Node: defaultNodeConfig(),
124+
Eth: eth.DefaultConfig,
125+
Node: defaultNodeConfig(),
126+
Metrics: metrics.DefaultConfig,
124127
}
125128

126129
// Load config file.
@@ -133,7 +136,6 @@ func makeConfigNode(ctx *cli.Context) (*node.Node, gethConfig) {
133136
log.Warn("Deprecated whisper config detected. Whisper has been moved to github.com/ethereum/whisper")
134137
}
135138
}
136-
137139
// Apply flags.
138140
utils.SetNodeConfig(ctx, &cfg.Node)
139141
stack, err := node.New(&cfg.Node)
@@ -146,6 +148,8 @@ func makeConfigNode(ctx *cli.Context) (*node.Node, gethConfig) {
146148
}
147149
utils.SetShhConfig(ctx, stack)
148150

151+
applyMetricConfig(ctx, &cfg)
152+
149153
return stack, cfg
150154
}
151155

@@ -204,3 +208,36 @@ func dumpConfig(ctx *cli.Context) error {
204208

205209
return nil
206210
}
211+
212+
func applyMetricConfig(ctx *cli.Context, cfg *gethConfig) {
213+
if ctx.GlobalIsSet(utils.MetricsEnabledFlag.Name) {
214+
cfg.Metrics.Enabled = ctx.GlobalBool(utils.MetricsEnabledFlag.Name)
215+
}
216+
if ctx.GlobalIsSet(utils.MetricsEnabledExpensiveFlag.Name) {
217+
cfg.Metrics.EnabledExpensive = ctx.GlobalBool(utils.MetricsEnabledExpensiveFlag.Name)
218+
}
219+
if ctx.GlobalIsSet(utils.MetricsHTTPFlag.Name) {
220+
cfg.Metrics.HTTP = ctx.GlobalString(utils.MetricsHTTPFlag.Name)
221+
}
222+
if ctx.GlobalIsSet(utils.MetricsPortFlag.Name) {
223+
cfg.Metrics.Port = ctx.GlobalInt(utils.MetricsPortFlag.Name)
224+
}
225+
if ctx.GlobalIsSet(utils.MetricsEnableInfluxDBFlag.Name) {
226+
cfg.Metrics.EnableInfluxDB = ctx.GlobalBool(utils.MetricsEnableInfluxDBFlag.Name)
227+
}
228+
if ctx.GlobalIsSet(utils.MetricsInfluxDBEndpointFlag.Name) {
229+
cfg.Metrics.InfluxDBEndpoint = ctx.GlobalString(utils.MetricsInfluxDBEndpointFlag.Name)
230+
}
231+
if ctx.GlobalIsSet(utils.MetricsInfluxDBDatabaseFlag.Name) {
232+
cfg.Metrics.InfluxDBDatabase = ctx.GlobalString(utils.MetricsInfluxDBDatabaseFlag.Name)
233+
}
234+
if ctx.GlobalIsSet(utils.MetricsInfluxDBUsernameFlag.Name) {
235+
cfg.Metrics.InfluxDBUsername = ctx.GlobalString(utils.MetricsInfluxDBUsernameFlag.Name)
236+
}
237+
if ctx.GlobalIsSet(utils.MetricsInfluxDBPasswordFlag.Name) {
238+
cfg.Metrics.InfluxDBPassword = ctx.GlobalString(utils.MetricsInfluxDBPasswordFlag.Name)
239+
}
240+
if ctx.GlobalIsSet(utils.MetricsInfluxDBTagsFlag.Name) {
241+
cfg.Metrics.InfluxDBTags = ctx.GlobalString(utils.MetricsInfluxDBTagsFlag.Name)
242+
}
243+
}

‎cmd/utils/flags.go‎

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -684,12 +684,12 @@ var (
684684
MetricsHTTPFlag = cli.StringFlag{
685685
Name: "metrics.addr",
686686
Usage: "Enable stand-alone metrics HTTP server listening interface",
687-
Value: "127.0.0.1",
687+
Value: metrics.DefaultConfig.HTTP,
688688
}
689689
MetricsPortFlag = cli.IntFlag{
690690
Name: "metrics.port",
691691
Usage: "Metrics HTTP server listening port",
692-
Value: 6060,
692+
Value: metrics.DefaultConfig.Port,
693693
}
694694
MetricsEnableInfluxDBFlag = cli.BoolFlag{
695695
Name: "metrics.influxdb",
@@ -698,22 +698,22 @@ var (
698698
MetricsInfluxDBEndpointFlag = cli.StringFlag{
699699
Name: "metrics.influxdb.endpoint",
700700
Usage: "InfluxDB API endpoint to report metrics to",
701-
Value: "http://localhost:8086",
701+
Value: metrics.DefaultConfig.InfluxDBEndpoint,
702702
}
703703
MetricsInfluxDBDatabaseFlag = cli.StringFlag{
704704
Name: "metrics.influxdb.database",
705705
Usage: "InfluxDB database name to push reported metrics to",
706-
Value: "geth",
706+
Value: metrics.DefaultConfig.InfluxDBDatabase,
707707
}
708708
MetricsInfluxDBUsernameFlag = cli.StringFlag{
709709
Name: "metrics.influxdb.username",
710710
Usage: "Username to authorize access to the database",
711-
Value: "test",
711+
Value: metrics.DefaultConfig.InfluxDBUsername,
712712
}
713713
MetricsInfluxDBPasswordFlag = cli.StringFlag{
714714
Name: "metrics.influxdb.password",
715715
Usage: "Password to authorize access to the database",
716-
Value: "test",
716+
Value: metrics.DefaultConfig.InfluxDBPassword,
717717
}
718718
// Tags are part of every measurement sent to InfluxDB. Queries on tags are faster in InfluxDB.
719719
// For example `host` tag could be used so that we can group all nodes and average a measurement
@@ -722,7 +722,7 @@ var (
722722
MetricsInfluxDBTagsFlag = cli.StringFlag{
723723
Name: "metrics.influxdb.tags",
724724
Usage: "Comma-separated InfluxDB tags (key/values) attached to all measurements",
725-
Value: "host=localhost",
725+
Value: metrics.DefaultConfig.InfluxDBTags,
726726
}
727727
EWASMInterpreterFlag = cli.StringFlag{
728728
Name: "vm.ewasm",

‎metrics/config.go‎

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
// Copyright 2021 The go-ethereum Authors
2+
// This file is part of go-ethereum.
3+
//
4+
// The go-ethereum library is free software: you can redistribute it and/or modify
5+
// it under the terms of the GNU Lesser General Public License as published by
6+
// the Free Software Foundation, either version 3 of the License, or
7+
// (at your option) any later version.
8+
//
9+
// The go-ethereum library is distributed in the hope that it will be useful,
10+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
// GNU Lesser General Public License for more details.
13+
//
14+
// You should have received a copy of the GNU Lesser General Public License
15+
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
16+
17+
package metrics
18+
19+
// Config contains the configuration for the metric collection.
20+
type Config struct {
21+
Enabled bool `toml:",omitempty"`
22+
EnabledExpensive bool `toml:",omitempty"`
23+
HTTP string `toml:",omitempty"`
24+
Port int `toml:",omitempty"`
25+
EnableInfluxDB bool `toml:",omitempty"`
26+
InfluxDBEndpoint string `toml:",omitempty"`
27+
InfluxDBDatabase string `toml:",omitempty"`
28+
InfluxDBUsername string `toml:",omitempty"`
29+
InfluxDBPassword string `toml:",omitempty"`
30+
InfluxDBTags string `toml:",omitempty"`
31+
}
32+
33+
// DefaultConfig is the default config for metrics used in go-ethereum.
34+
var DefaultConfig = Config{
35+
Enabled: false,
36+
EnabledExpensive: false,
37+
HTTP: "127.0.0.1",
38+
Port: 6060,
39+
EnableInfluxDB: false,
40+
InfluxDBEndpoint: "http://localhost:8086",
41+
InfluxDBDatabase: "geth",
42+
InfluxDBUsername: "test",
43+
InfluxDBPassword: "test",
44+
InfluxDBTags: "host=localhost",
45+
}

0 commit comments

Comments
 (0)