From f3a43f947261a0d7b96628c6c059b89e180acc5a Mon Sep 17 00:00:00 2001 From: Shivam Sharma Date: Fri, 12 Nov 2021 13:13:28 +0530 Subject: [PATCH] Added Prometheus to New CLI: fixes --- command/server/config.go | 14 +++------- command/server/server.go | 57 ++++++++++++++++++++++++++-------------- 2 files changed, 40 insertions(+), 31 deletions(-) diff --git a/command/server/config.go b/command/server/config.go index 34fa13cf51..dd3f5dce95 100644 --- a/command/server/config.go +++ b/command/server/config.go @@ -88,9 +88,6 @@ type Config struct { // GRPC has the grpc server related settings GRPC *GRPCConfig - - // Prometheus Server - Prometheus *PrometheusConfig `hcl:"prometheus,optional"` } type P2PConfig struct { @@ -239,14 +236,6 @@ type GRPCConfig struct { Addr string } -type PrometheusConfig struct { - // Enabled selects whether the api is enabled - Enabled bool `hcl:"enabled,optional"` - - // Host is the address to bind the api - address string `hcl:"host,optional"` -} - type APIConfig struct { // Enabled selects whether the api is enabled Enabled bool `hcl:"enabled,optional"` @@ -286,6 +275,9 @@ type TelemetryConfig struct { // InfluxDB has the influxdb related settings InfluxDB *InfluxDBConfig `hcl:"influx,block"` + + // Prometheus Address + PrometheusAddr string `hcl:"prometheus-addr,optional"` } type InfluxDBConfig struct { diff --git a/command/server/server.go b/command/server/server.go index 5981f0cf48..2c06804cc6 100644 --- a/command/server/server.go +++ b/command/server/server.go @@ -20,6 +20,7 @@ import ( "github.com/ethereum/go-ethereum/metrics" "github.com/ethereum/go-ethereum/metrics/exp" "github.com/ethereum/go-ethereum/metrics/influxdb" + "github.com/ethereum/go-ethereum/metrics/prometheus" "github.com/ethereum/go-ethereum/node" "github.com/fjl/memsize/memsizeui" "github.com/mattn/go-colorable" @@ -50,11 +51,6 @@ func NewServer(config *Config) (*Server, error) { return nil, err } - // start the Prometheus Server - if err := srv.StartPrometheus(config.Prometheus.address, config.Prometheus.Enabled); err != nil { - return nil, err - } - // create the node/stack nodeCfg, err := config.buildNode() if err != nil { @@ -159,6 +155,42 @@ func (s *Server) setupMetrics(config *TelemetryConfig) error { // Start system runtime metrics collection go metrics.CollectProcessMetrics(3 * time.Second) + // Hook go-metrics into expvar on any /debug/metrics request, load all vars + // from the registry into expvar, and execute regular expvar handler. + + if len(config.PrometheusAddr) != 0 { + + prometheusMux := http.NewServeMux() + + // this would cause a panic: + // panic: http: multiple registrations for /debug/vars + // http.HandleFunc("/debug/vars", e.expHandler) + // haven't found an elegant way, so just use a different endpoint + + prometheusMux.HandleFunc("/debug/metrics", func(w http.ResponseWriter, r *http.Request) { + exp.ExpHandler(metrics.DefaultRegistry) + }) + + prometheusMux.HandleFunc("/debug/metrics/prometheus", func(w http.ResponseWriter, r *http.Request) { + prometheus.Handler(metrics.DefaultRegistry) + }) + + promServer := &http.Server{ + Addr: config.PrometheusAddr, + Handler: prometheusMux, + ReadTimeout: 10 * time.Second, + WriteTimeout: 10 * time.Second, + MaxHeaderBytes: 1 << 20, + } + + go func() { + if err := promServer.ListenAndServe(); err != nil { + log.Error("Failure in running Prometheus server", "err", err) + } + }() + + } + return nil } @@ -182,21 +214,6 @@ func (s *Server) setupGRPCServer(addr string) error { } func (s *Server) StartPrometheus(address string, enabled bool) error { - // Hook go-metrics into expvar on any /debug/metrics request, load all vars - // from the registry into expvar, and execute regular expvar handler. - if enabled { - exp.Exp(metrics.DefaultRegistry) - } - - http.Handle("/memsize/", http.StripPrefix("/memsize", &Memsize)) - - log.Info("Starting pprof server", "addr", fmt.Sprintf("http://%s/debug/pprof", address)) - - go func() { - if err := http.ListenAndServe(address, nil); err != nil { - log.Error("Failure in running pprof server", "err", err) - } - }() return nil }