Skip to content

Commit

Permalink
reuse of geth flags and related code
Browse files Browse the repository at this point in the history
  • Loading branch information
r4f4ss authored and GrapeBaBa committed Oct 5, 2024
1 parent 8054d16 commit d831ad6
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 182 deletions.
28 changes: 14 additions & 14 deletions cmd/shisui/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,19 +81,19 @@ var (
utils.PortalLogLevelFlag,
}
metricsFlags = []cli.Flag{
utils.PortalMetricsEnabledFlag,
utils.PortalMetricsHTTPFlag,
utils.PortalMetricsPortFlag,
utils.PortalMetricsEnableInfluxDBFlag,
utils.PortalMetricsInfluxDBEndpointFlag,
utils.PortalMetricsInfluxDBDatabaseFlag,
utils.PortalMetricsInfluxDBUsernameFlag,
utils.PortalMetricsInfluxDBPasswordFlag,
utils.PortalMetricsInfluxDBTagsFlag,
utils.PortalMetricsEnableInfluxDBV2Flag,
utils.PortalMetricsInfluxDBTokenFlag,
utils.PortalMetricsInfluxDBBucketFlag,
utils.PortalMetricsInfluxDBOrganizationFlag,
utils.MetricsEnabledFlag,
utils.MetricsHTTPFlag,
utils.MetricsPortFlag,
utils.MetricsEnableInfluxDBFlag,
utils.MetricsInfluxDBEndpointFlag,
utils.MetricsInfluxDBDatabaseFlag,
utils.MetricsInfluxDBUsernameFlag,
utils.MetricsInfluxDBPasswordFlag,
utils.MetricsInfluxDBTagsFlag,
utils.MetricsEnableInfluxDBV2Flag,
utils.MetricsInfluxDBTokenFlag,
utils.MetricsInfluxDBBucketFlag,
utils.MetricsInfluxDBOrganizationFlag,
}
)

Expand All @@ -114,7 +114,7 @@ func shisui(ctx *cli.Context) error {
setDefaultLogger(ctx.Int(utils.PortalLogLevelFlag.Name))

// Start metrics export if enabled
utils.PortalSetupMetrics(ctx)
utils.SetupMetrics(ctx)

// Start system runtime metrics collection
go metrics.CollectProcessMetrics(3 * time.Second)
Expand Down
148 changes: 0 additions & 148 deletions cmd/utils/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -1026,93 +1026,6 @@ Please note that --` + MetricsHTTPFlag.Name + ` must be set to start the server.
Category: flags.PortalNetworkCategory,
Value: cli.NewStringSlice(portalwire.History.Name()),
}
// Metrics flags for shisui
PortalMetricsEnabledFlag = &cli.BoolFlag{
Name: "metrics",
Usage: "Enable metrics collection and reporting",
Category: flags.PortalNetworkMetricsCategory,
}
// MetricsHTTPFlag defines the endpoint for a stand-alone metrics HTTP endpoint.
// Since the pprof service enables sensitive/vulnerable behavior, this allows a user
// to enable a public-OK metrics endpoint without having to worry about ALSO exposing
// other profiling behavior or information.
PortalMetricsHTTPFlag = &cli.StringFlag{
Name: "metrics.addr",
Usage: `Enable stand-alone metrics HTTP server listening interface.`,
Category: flags.PortalNetworkMetricsCategory,
}
PortalMetricsPortFlag = &cli.IntFlag{
Name: "metrics.port",
Usage: `Metrics HTTP server listening port.
Please note that --` + MetricsHTTPFlag.Name + ` must be set to start the server.`,
Value: metrics.ShisuiDefaultConfig.Port,
Category: flags.PortalNetworkMetricsCategory,
}
PortalMetricsEnableInfluxDBFlag = &cli.BoolFlag{
Name: "metrics.influxdb",
Usage: "Enable metrics export/push to an external InfluxDB database",
Category: flags.PortalNetworkMetricsCategory,
}
PortalMetricsInfluxDBEndpointFlag = &cli.StringFlag{
Name: "metrics.influxdb.endpoint",
Usage: "InfluxDB API endpoint to report metrics to",
Value: metrics.ShisuiDefaultConfig.InfluxDBEndpoint,
Category: flags.PortalNetworkMetricsCategory,
}
PortalMetricsInfluxDBDatabaseFlag = &cli.StringFlag{
Name: "metrics.influxdb.database",
Usage: "InfluxDB database name to push reported metrics to",
Value: metrics.ShisuiDefaultConfig.InfluxDBDatabase,
Category: flags.PortalNetworkMetricsCategory,
}
PortalMetricsInfluxDBUsernameFlag = &cli.StringFlag{
Name: "metrics.influxdb.username",
Usage: "Username to authorize access to the database",
Value: metrics.ShisuiDefaultConfig.InfluxDBUsername,
Category: flags.PortalNetworkMetricsCategory,
}
PortalMetricsInfluxDBPasswordFlag = &cli.StringFlag{
Name: "metrics.influxdb.password",
Usage: "Password to authorize access to the database",
Value: metrics.ShisuiDefaultConfig.InfluxDBPassword,
Category: flags.PortalNetworkMetricsCategory,
}
// Tags are part of every measurement sent to InfluxDB. Queries on tags are faster in InfluxDB.
// For example `host` tag could be used so that we can group all nodes and average a measurement
// across all of them, but also so that we can select a specific node and inspect its measurements.
// https://docs.influxdata.com/influxdb/v1.4/concepts/key_concepts/#tag-key
PortalMetricsInfluxDBTagsFlag = &cli.StringFlag{
Name: "metrics.influxdb.tags",
Usage: "Comma-separated InfluxDB tags (key/values) attached to all measurements",
Value: metrics.ShisuiDefaultConfig.InfluxDBTags,
Category: flags.PortalNetworkMetricsCategory,
}

PortalMetricsEnableInfluxDBV2Flag = &cli.BoolFlag{
Name: "metrics.influxdbv2",
Usage: "Enable metrics export/push to an external InfluxDB v2 database",
Category: flags.PortalNetworkMetricsCategory,
}
PortalMetricsInfluxDBTokenFlag = &cli.StringFlag{
Name: "metrics.influxdb.token",
Usage: "Token to authorize access to the database (v2 only)",
Value: metrics.ShisuiDefaultConfig.InfluxDBToken,
Category: flags.PortalNetworkMetricsCategory,
}

PortalMetricsInfluxDBBucketFlag = &cli.StringFlag{
Name: "metrics.influxdb.bucket",
Usage: "InfluxDB bucket name to push reported metrics to (v2 only)",
Value: metrics.ShisuiDefaultConfig.InfluxDBBucket,
Category: flags.PortalNetworkMetricsCategory,
}

PortalMetricsInfluxDBOrganizationFlag = &cli.StringFlag{
Name: "metrics.influxdb.organization",
Usage: "InfluxDB organization name (v2 only)",
Value: metrics.ShisuiDefaultConfig.InfluxDBOrganization,
Category: flags.PortalNetworkMetricsCategory,
}
)

var (
Expand Down Expand Up @@ -2129,67 +2042,6 @@ func RegisterFullSyncTester(stack *node.Node, eth *eth.Ethereum, target common.H
log.Info("Registered full-sync tester", "hash", target)
}

func PortalSetupMetrics(ctx *cli.Context) {
if metrics.Enabled {
log.Info("Enabling metrics collection")

var (
enableExport = ctx.Bool(PortalMetricsEnableInfluxDBFlag.Name)
enableExportV2 = ctx.Bool(PortalMetricsEnableInfluxDBV2Flag.Name)
)

if enableExport || enableExportV2 {
CheckExclusive(ctx, PortalMetricsEnableInfluxDBFlag, PortalMetricsEnableInfluxDBV2Flag)

v1FlagIsSet := ctx.IsSet(PortalMetricsInfluxDBUsernameFlag.Name) ||
ctx.IsSet(PortalMetricsInfluxDBPasswordFlag.Name)

v2FlagIsSet := ctx.IsSet(PortalMetricsInfluxDBTokenFlag.Name) ||
ctx.IsSet(PortalMetricsInfluxDBOrganizationFlag.Name) ||
ctx.IsSet(PortalMetricsInfluxDBBucketFlag.Name)

if enableExport && v2FlagIsSet {
Fatalf("Flags --influxdb.metrics.organization, --influxdb.metrics.token, --influxdb.metrics.bucket are only available for influxdb-v2")
} else if enableExportV2 && v1FlagIsSet {
Fatalf("Flags --influxdb.metrics.username, --influxdb.metrics.password are only available for influxdb-v1")
}
}

var (
endpoint = ctx.String(PortalMetricsInfluxDBEndpointFlag.Name)
database = ctx.String(PortalMetricsInfluxDBDatabaseFlag.Name)
username = ctx.String(PortalMetricsInfluxDBUsernameFlag.Name)
password = ctx.String(PortalMetricsInfluxDBPasswordFlag.Name)

token = ctx.String(PortalMetricsInfluxDBTokenFlag.Name)
bucket = ctx.String(PortalMetricsInfluxDBBucketFlag.Name)
organization = ctx.String(PortalMetricsInfluxDBOrganizationFlag.Name)
)

if enableExport {
tagsMap := SplitTagsFlag(ctx.String(PortalMetricsInfluxDBTagsFlag.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(PortalMetricsInfluxDBTagsFlag.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(PortalMetricsHTTPFlag.Name) {
address := net.JoinHostPort(ctx.String(PortalMetricsHTTPFlag.Name), fmt.Sprintf("%d", ctx.Int(PortalMetricsPortFlag.Name)))
log.Info("Enabling stand-alone metrics HTTP endpoint", "address", address)
exp.Setup(address)
} else if ctx.IsSet(PortalMetricsPortFlag.Name) {
log.Warn(fmt.Sprintf("--%s specified without --%s, metrics server will not start.", PortalMetricsPortFlag.Name, PortalMetricsHTTPFlag.Name))
}
}
}

func SetupMetrics(ctx *cli.Context) {
if metrics.Enabled {
log.Info("Enabling metrics collection")
Expand Down
39 changes: 19 additions & 20 deletions internal/flags/categories.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,26 +19,25 @@ package flags
import "github.com/urfave/cli/v2"

const (
EthCategory = "ETHEREUM"
BeaconCategory = "BEACON CHAIN"
DevCategory = "DEVELOPER CHAIN"
StateCategory = "STATE HISTORY MANAGEMENT"
TxPoolCategory = "TRANSACTION POOL (EVM)"
BlobPoolCategory = "TRANSACTION POOL (BLOB)"
PerfCategory = "PERFORMANCE TUNING"
AccountCategory = "ACCOUNT"
APICategory = "API AND CONSOLE"
NetworkingCategory = "NETWORKING"
MinerCategory = "MINER"
GasPriceCategory = "GAS PRICE ORACLE"
VMCategory = "VIRTUAL MACHINE"
LoggingCategory = "LOGGING AND DEBUGGING"
MetricsCategory = "METRICS AND STATS"
MiscCategory = "MISC"
TestingCategory = "TESTING"
DeprecatedCategory = "ALIASED (deprecated)"
PortalNetworkCategory = "PORTAL NETWORK"
PortalNetworkMetricsCategory = "PORTAL NETWORK METRICS"
EthCategory = "ETHEREUM"
BeaconCategory = "BEACON CHAIN"
DevCategory = "DEVELOPER CHAIN"
StateCategory = "STATE HISTORY MANAGEMENT"
TxPoolCategory = "TRANSACTION POOL (EVM)"
BlobPoolCategory = "TRANSACTION POOL (BLOB)"
PerfCategory = "PERFORMANCE TUNING"
AccountCategory = "ACCOUNT"
APICategory = "API AND CONSOLE"
NetworkingCategory = "NETWORKING"
MinerCategory = "MINER"
GasPriceCategory = "GAS PRICE ORACLE"
VMCategory = "VIRTUAL MACHINE"
LoggingCategory = "LOGGING AND DEBUGGING"
MetricsCategory = "METRICS AND STATS"
MiscCategory = "MISC"
TestingCategory = "TESTING"
DeprecatedCategory = "ALIASED (deprecated)"
PortalNetworkCategory = "PORTAL NETWORK"
)

func init() {
Expand Down

0 comments on commit d831ad6

Please sign in to comment.