metrics: config validate

Signed-off-by: jsvisa <delweng@gmail.com>
This commit is contained in:
jsvisa 2023-09-13 11:48:49 +08:00
parent 766272ff8c
commit ec9ca1c7dc

View file

@ -16,6 +16,10 @@
package metrics package metrics
import (
"errors"
)
// Config contains the configuration for the metric collection. // Config contains the configuration for the metric collection.
type Config struct { type Config struct {
Enabled bool `toml:",omitempty"` Enabled bool `toml:",omitempty"`
@ -35,6 +39,20 @@ type Config struct {
InfluxDBOrganization string `toml:",omitempty"` InfluxDBOrganization string `toml:",omitempty"`
} }
func (c *Config) Validate() error {
if c.EnableInfluxDB || c.EnableInfluxDBV2 {
v1FlagIsSet := c.InfluxDBUsername != "" || c.InfluxDBPassword != ""
v2FlagIsSet := c.InfluxDBToken != "" || c.InfluxDBOrganization != "" || c.InfluxDBBucket != ""
if c.EnableInfluxDB && v2FlagIsSet {
return errors.New("Flags --influxdb.metrics.organization, --influxdb.metrics.token, --influxdb.metrics.bucket are only available for influxdb-v2")
} else if c.EnableInfluxDBV2 && v1FlagIsSet {
return errors.New("Flags --influxdb.metrics.username, --influxdb.metrics.password are only available for influxdb-v1")
}
}
return nil
}
// DefaultConfig is the default config for metrics used in go-ethereum. // DefaultConfig is the default config for metrics used in go-ethereum.
var DefaultConfig = Config{ var DefaultConfig = Config{
Enabled: false, Enabled: false,