diff --git a/CHANGELOG.md b/CHANGELOG.md index f69f2d315..9507723a4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -36,7 +36,7 @@ - [\#346](https://github.com/cosmos/evm/pull/346) Add eth_createAccessList method and implementation - [\#502](https://github.com/cosmos/evm/pull/502) Add block time in derived logs. -- [\#588](https://github.com/cosmos/evm/pull/588) go-ethereum metrics are now available in Cosmos SDK's telemetry server at host:port/geth/metrics (default localhost:1317/geth/metrics). +- [\#633](https://github.com/cosmos/evm/pull/633) go-ethereum metrics are now emitted on a separate server. default address: 127.0.0.1:8100. ### STATE BREAKING diff --git a/contrib/images/evmd-env/Dockerfile b/contrib/images/evmd-env/Dockerfile index 3a570ed6a..d043dca02 100644 --- a/contrib/images/evmd-env/Dockerfile +++ b/contrib/images/evmd-env/Dockerfile @@ -22,7 +22,7 @@ RUN addgroup -g 1025 nonroot RUN adduser -D nonroot -u 1025 -G nonroot # Set up the runtime environment -EXPOSE 26656 26657 1317 9090 +EXPOSE 26656 26657 1317 9090 26660 8545 8100 STOPSIGNAL SIGTERM VOLUME /evmd WORKDIR /evmd diff --git a/evmd/cmd/evmd/cmd/testnet.go b/evmd/cmd/evmd/cmd/testnet.go index c90690cb0..a98dfc052 100644 --- a/evmd/cmd/evmd/cmd/testnet.go +++ b/evmd/cmd/evmd/cmd/testnet.go @@ -288,9 +288,10 @@ func initTestnetFiles( sdkGRPCPort = 9090 // evmGRPC = 9900 // TODO: maybe need this? idk. - evmJSONRPC = 8545 - evmJSONRPCWS = 8546 - evmJSONRPCMetrics = 6065 + evmJSONRPC = 8545 + evmJSONRPCWS = 8546 + evmJSONRPCMetrics = 6065 + evmGethMetricsPort = 8100 ) p2pPortStart := 26656 @@ -317,6 +318,7 @@ func initTestnetFiles( evmCfg.JSONRPC.Address = fmt.Sprintf("127.0.0.1:%d", evmJSONRPC+evmPortOffset) evmCfg.JSONRPC.MetricsAddress = fmt.Sprintf("127.0.0.1:%d", evmJSONRPCMetrics+evmPortOffset) evmCfg.JSONRPC.WsAddress = fmt.Sprintf("127.0.0.1:%d", evmJSONRPCWS+evmPortOffset) + evmCfg.EVM.GethMetricsAddress = fmt.Sprintf("127.0.0.1:%d", evmGethMetricsPort+evmPortOffset) } else { evmCfg.JSONRPC.WsAddress = fmt.Sprintf("0.0.0.0:%d", evmJSONRPCWS) evmCfg.JSONRPC.Address = fmt.Sprintf("0.0.0.0:%d", evmJSONRPC) diff --git a/go.mod b/go.mod index 4166dd6d3..2e66b7626 100644 --- a/go.mod +++ b/go.mod @@ -38,7 +38,6 @@ require ( github.com/onsi/ginkgo/v2 v2.23.4 github.com/onsi/gomega v1.38.0 github.com/pkg/errors v0.9.1 - github.com/prometheus/client_golang v1.22.0 github.com/rs/cors v1.11.1 github.com/spf13/cast v1.9.2 github.com/spf13/cobra v1.9.1 @@ -181,7 +180,6 @@ require ( github.com/klauspost/cpuid/v2 v2.2.10 // indirect github.com/kr/pretty v0.3.1 // indirect github.com/kr/text v0.2.0 // indirect - github.com/kylelemons/godebug v1.1.0 // indirect github.com/lib/pq v1.10.9 // indirect github.com/manifoldco/promptui v0.9.0 // indirect github.com/mattn/go-colorable v0.1.14 // indirect @@ -207,6 +205,7 @@ require ( github.com/pion/transport/v3 v3.0.1 // indirect github.com/planetscale/vtprotobuf v0.6.1-0.20240319094008-0393e58bdf10 // indirect github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 // indirect + github.com/prometheus/client_golang v1.22.0 // indirect github.com/prometheus/client_model v0.6.1 // indirect github.com/prometheus/common v0.63.0 // indirect github.com/prometheus/procfs v0.15.1 // indirect diff --git a/metrics/geth.go b/metrics/geth.go new file mode 100644 index 000000000..44c052639 --- /dev/null +++ b/metrics/geth.go @@ -0,0 +1,59 @@ +package metrics + +import ( + "context" + "errors" + "net/http" + "time" + + gethmetrics "github.com/ethereum/go-ethereum/metrics" + gethprom "github.com/ethereum/go-ethereum/metrics/prometheus" + + "cosmossdk.io/log" +) + +// StartGethMetricServer starts the geth metrics server on the specified address. +func StartGethMetricServer(ctx context.Context, log log.Logger, addr string) error { + mux := http.NewServeMux() + mux.Handle("/metrics", gethprom.Handler(gethmetrics.DefaultRegistry)) + + server := &http.Server{ + Addr: addr, + Handler: mux, + ReadTimeout: 10 * time.Second, + WriteTimeout: 10 * time.Second, + ReadHeaderTimeout: 10 * time.Second, + } + + errCh := make(chan error, 1) + + go func() { + log.Info("starting geth metrics server...", "address", addr) + errCh <- server.ListenAndServe() + }() + + // Start a blocking select to wait for an indication to stop the server or that + // the server failed to start properly. + select { + case <-ctx.Done(): + // The calling process canceled or closed the provided context, so we must + // gracefully stop the metrics server. + log.Info("stopping geth metrics server...", "address", addr) + + shutdownCtx, cancel := context.WithTimeout(context.Background(), 5*time.Second) + defer cancel() + + if err := server.Shutdown(shutdownCtx); err != nil { + log.Error("geth metrics server shutdown error", "err", err) + return err + } + return nil + + case err := <-errCh: + if err != nil && !errors.Is(err, http.ErrServerClosed) { + log.Error("failed to start geth metrics server", "err", err) + return err + } + return nil + } +} diff --git a/server/config/config.go b/server/config/config.go index a46df42f9..d8dd07bcf 100644 --- a/server/config/config.go +++ b/server/config/config.go @@ -3,6 +3,7 @@ package config import ( "errors" "fmt" + "net/netip" "path" "time" @@ -66,6 +67,9 @@ const ( // DefaultEVMMinTip is the default minimum priority fee for the mempool DefaultEVMMinTip = 0 + // DefaultGethMetricsAddress is the default port for the geth metrics server. + DefaultGethMetricsAddress = "127.0.0.1:8100" + // DefaultGasCap is the default cap on gas that can be used in eth_call/estimateGas DefaultGasCap uint64 = 25_000_000 @@ -145,6 +149,8 @@ type EVMConfig struct { EVMChainID uint64 `mapstructure:"evm-chain-id"` // MinTip defines the minimum priority fee for the mempool MinTip uint64 `mapstructure:"min-tip"` + // GethMetricsAddress is the address the geth metrics server will bind to. Default 127.0.0.1:8100 + GethMetricsAddress string `mapstructure:"geth-metrics-address"` } // JSONRPCConfig defines configuration for the EVM RPC server. @@ -213,6 +219,7 @@ func DefaultEVMConfig() *EVMConfig { EVMChainID: DefaultEVMChainID, EnablePreimageRecording: DefaultEnablePreimageRecording, MinTip: DefaultEVMMinTip, + GethMetricsAddress: DefaultGethMetricsAddress, } } @@ -222,6 +229,10 @@ func (c EVMConfig) Validate() error { return fmt.Errorf("invalid tracer type %s, available types: %v", c.Tracer, evmTracers) } + if _, err := netip.ParseAddrPort(c.GethMetricsAddress); err != nil { + return fmt.Errorf("invalid geth metrics address %q: %w", c.GethMetricsAddress, err) + } + return nil } diff --git a/server/config/toml.go b/server/config/toml.go index 8b61fdd3a..b805482a0 100644 --- a/server/config/toml.go +++ b/server/config/toml.go @@ -25,6 +25,9 @@ evm-chain-id = {{ .EVM.EVMChainID }} # MinTip defines the minimum priority fee for the mempool. min-tip = {{ .EVM.MinTip }} +# GethMetricsAddress defines the addr to bind the geth metrics server to. Default 127.0.0.1:8100. +geth-metrics-address = "{{ .EVM.GethMetricsAddress }}" + ############################################################################### ### JSON RPC Configuration ### ############################################################################### diff --git a/server/flags/flags.go b/server/flags/flags.go index db86ca3fa..6bbc61261 100644 --- a/server/flags/flags.go +++ b/server/flags/flags.go @@ -70,6 +70,7 @@ const ( EVMEnablePreimageRecording = "evm.cache-preimage" EVMChainID = "evm.evm-chain-id" EVMMinTip = "evm.min-tip" + EvmGethMetricsAddress = "evm.geth-metrics-address" ) // TLS flags diff --git a/server/start.go b/server/start.go index aa317693c..1a08fcb0d 100644 --- a/server/start.go +++ b/server/start.go @@ -9,9 +9,7 @@ import ( "path/filepath" "runtime/pprof" - gethmetrics "github.com/ethereum/go-ethereum/metrics" ethmetricsexp "github.com/ethereum/go-ethereum/metrics/exp" - gethprom "github.com/ethereum/go-ethereum/metrics/prometheus" "github.com/spf13/cobra" "golang.org/x/sync/errgroup" "google.golang.org/grpc" @@ -31,6 +29,7 @@ import ( dbm "github.com/cosmos/cosmos-db" "github.com/cosmos/evm/indexer" evmmempool "github.com/cosmos/evm/mempool" + evmmetrics "github.com/cosmos/evm/metrics" ethdebug "github.com/cosmos/evm/rpc/namespaces/ethereum/debug" cosmosevmserverconfig "github.com/cosmos/evm/server/config" srvflags "github.com/cosmos/evm/server/flags" @@ -222,6 +221,7 @@ which accepts a path for the resulting pprof file. cmd.Flags().Bool(srvflags.EVMEnablePreimageRecording, cosmosevmserverconfig.DefaultEnablePreimageRecording, "Enables tracking of SHA3 preimages in the EVM (not implemented yet)") //nolint:lll cmd.Flags().Uint64(srvflags.EVMChainID, cosmosevmserverconfig.DefaultEVMChainID, "the EIP-155 compatible replay protection chain ID") cmd.Flags().Uint64(srvflags.EVMMinTip, cosmosevmserverconfig.DefaultEVMMinTip, "the minimum priority fee for the mempool") + cmd.Flags().String(srvflags.EvmGethMetricsAddress, cosmosevmserverconfig.DefaultGethMetricsAddress, "the address to bind the geth metrics server to") cmd.Flags().String(srvflags.TLSCertPath, "", "the cert.pem file path for the server TLS configuration") cmd.Flags().String(srvflags.TLSKeyPath, "", "the key.pem file path for the server TLS configuration") @@ -504,7 +504,7 @@ func startInProcess(svrCtx *server.Context, clientCtx client.Context, opts Start defer grpcSrv.GracefulStop() } - startAPIServer(ctx, svrCtx, clientCtx, g, config.Config, app, grpcSrv, metrics) + startAPIServer(ctx, svrCtx, clientCtx, g, config.Config, app, grpcSrv, metrics, config.EVM.GethMetricsAddress) if config.JSONRPC.Enable { txApp, ok := app.(AppWithPendingTxStream) @@ -675,6 +675,7 @@ func startAPIServer( app types.Application, grpcSrv *grpc.Server, metrics *telemetry.Metrics, + gethMetricsAddress string, ) { if !svrCfg.API.Enable { return @@ -685,7 +686,9 @@ func startAPIServer( if svrCfg.Telemetry.Enabled { apiSrv.SetTelemetry(metrics) - apiSrv.Router.Handle("/geth/metrics", gethprom.Handler(gethmetrics.DefaultRegistry)) + g.Go(func() error { + return evmmetrics.StartGethMetricServer(ctx, svrCtx.Logger.With("server", "geth_metrics"), gethMetricsAddress) + }) } g.Go(func() error {