cmd/geth: simple it

Signed-off-by: jsvisa <delweng@gmail.com>
This commit is contained in:
jsvisa 2023-09-13 12:32:45 +08:00
parent 3711a738bd
commit 18d0211b00
2 changed files with 52 additions and 54 deletions

View file

@ -258,9 +258,7 @@ func importChain(ctx *cli.Context) error {
utils.Fatalf("This command requires an argument.") utils.Fatalf("This command requires an argument.")
} }
// Start metrics export if enabled // Start metrics export if enabled
utils.SetupMetrics(ctx) utils.SetupMetricsFromCLI(ctx)
// Start system runtime metrics collection
go metrics.CollectProcessMetrics(3 * time.Second)
stack, _ := makeConfigNode(ctx) stack, _ := makeConfigNode(ctx)
defer stack.Close() defer stack.Close()

View file

@ -1982,65 +1982,65 @@ func RegisterFullSyncTester(stack *node.Node, eth *eth.Ethereum, path string) {
log.Info("Registered full-sync tester", "number", block.NumberU64(), "hash", block.Hash()) log.Info("Registered full-sync tester", "number", block.NumberU64(), "hash", block.Hash())
} }
func SetupMetrics(ctx *cli.Context) { func SetupMetricsFromCLI(ctx *cli.Context) {
if metrics.Enabled { if !metrics.Enabled {
log.Info("Enabling metrics collection") return
}
var ( log.Info("Enabling metrics collection")
enableExport = ctx.Bool(MetricsEnableInfluxDBFlag.Name)
enableExportV2 = ctx.Bool(MetricsEnableInfluxDBV2Flag.Name)
)
if enableExport || enableExportV2 { var (
CheckExclusive(ctx, MetricsEnableInfluxDBFlag, MetricsEnableInfluxDBV2Flag) enableExport = ctx.Bool(MetricsEnableInfluxDBFlag.Name)
enableExportV2 = ctx.Bool(MetricsEnableInfluxDBV2Flag.Name)
)
v1FlagIsSet := ctx.IsSet(MetricsInfluxDBUsernameFlag.Name) || if enableExport || enableExportV2 {
ctx.IsSet(MetricsInfluxDBPasswordFlag.Name) CheckExclusive(ctx, MetricsEnableInfluxDBFlag, MetricsEnableInfluxDBV2Flag)
v2FlagIsSet := ctx.IsSet(MetricsInfluxDBTokenFlag.Name) || v1FlagIsSet := ctx.IsSet(MetricsInfluxDBUsernameFlag.Name) ||
ctx.IsSet(MetricsInfluxDBOrganizationFlag.Name) || ctx.IsSet(MetricsInfluxDBPasswordFlag.Name)
ctx.IsSet(MetricsInfluxDBBucketFlag.Name)
if enableExport && v2FlagIsSet { v2FlagIsSet := ctx.IsSet(MetricsInfluxDBTokenFlag.Name) ||
Fatalf("Flags --influxdb.metrics.organization, --influxdb.metrics.token, --influxdb.metrics.bucket are only available for influxdb-v2") ctx.IsSet(MetricsInfluxDBOrganizationFlag.Name) ||
} else if enableExportV2 && v1FlagIsSet { ctx.IsSet(MetricsInfluxDBBucketFlag.Name)
Fatalf("Flags --influxdb.metrics.username, --influxdb.metrics.password are only available for influxdb-v1")
}
}
var ( if enableExport && v2FlagIsSet {
endpoint = ctx.String(MetricsInfluxDBEndpointFlag.Name) Fatalf("Flags --influxdb.metrics.organization, --influxdb.metrics.token, --influxdb.metrics.bucket are only available for influxdb-v2")
database = ctx.String(MetricsInfluxDBDatabaseFlag.Name) } else if enableExportV2 && v1FlagIsSet {
username = ctx.String(MetricsInfluxDBUsernameFlag.Name) Fatalf("Flags --influxdb.metrics.username, --influxdb.metrics.password are only available for influxdb-v1")
password = ctx.String(MetricsInfluxDBPasswordFlag.Name)
token = ctx.String(MetricsInfluxDBTokenFlag.Name)
bucket = ctx.String(MetricsInfluxDBBucketFlag.Name)
organization = ctx.String(MetricsInfluxDBOrganizationFlag.Name)
)
if enableExport {
tagsMap := SplitTagsFlag(ctx.String(MetricsInfluxDBTagsFlag.Name))
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(ctx.String(MetricsInfluxDBTagsFlag.Name))
log.Info("Enabling metrics export to InfluxDB (v2)")
go influxdb.InfluxDBV2WithTags(metrics.DefaultRegistry, 10*time.Second, endpoint, token, bucket, organization, "geth.", tagsMap)
}
if ctx.IsSet(MetricsHTTPFlag.Name) {
address := net.JoinHostPort(ctx.String(MetricsHTTPFlag.Name), fmt.Sprintf("%d", ctx.Int(MetricsPortFlag.Name)))
log.Info("Enabling stand-alone metrics HTTP endpoint", "address", address)
exp.Setup(address)
} else if ctx.IsSet(MetricsPortFlag.Name) {
log.Warn(fmt.Sprintf("--%s specified without --%s, metrics server will not start.", MetricsPortFlag.Name, MetricsHTTPFlag.Name))
} }
} }
var (
tagsMap = SplitTagsFlag(ctx.String(MetricsInfluxDBTagsFlag.Name))
endpoint = ctx.String(MetricsInfluxDBEndpointFlag.Name)
database = ctx.String(MetricsInfluxDBDatabaseFlag.Name)
username = ctx.String(MetricsInfluxDBUsernameFlag.Name)
password = ctx.String(MetricsInfluxDBPasswordFlag.Name)
token = ctx.String(MetricsInfluxDBTokenFlag.Name)
bucket = ctx.String(MetricsInfluxDBBucketFlag.Name)
organization = ctx.String(MetricsInfluxDBOrganizationFlag.Name)
)
if enableExport {
log.Info("Enabling metrics export to InfluxDB")
go influxdb.InfluxDBWithTags(metrics.DefaultRegistry, 10*time.Second, endpoint, database, username, password, "geth.", tagsMap)
} else if enableExportV2 {
log.Info("Enabling metrics export to InfluxDB (v2)")
go influxdb.InfluxDBV2WithTags(metrics.DefaultRegistry, 10*time.Second, endpoint, token, bucket, organization, "geth.", tagsMap)
}
if ctx.IsSet(MetricsHTTPFlag.Name) {
address := net.JoinHostPort(ctx.String(MetricsHTTPFlag.Name), fmt.Sprintf("%d", ctx.Int(MetricsPortFlag.Name)))
log.Info("Enabling stand-alone metrics HTTP endpoint", "address", address)
exp.Setup(address)
} else if ctx.IsSet(MetricsPortFlag.Name) {
log.Warn(fmt.Sprintf("--%s specified without --%s, metrics server will not start.", MetricsPortFlag.Name, MetricsHTTPFlag.Name))
}
// Start system runtime metrics collection
go metrics.CollectProcessMetrics(3 * time.Second)
} }
func SetupMetricsFromConfig(c *metrics.Config) { func SetupMetricsFromConfig(c *metrics.Config) {