mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-08-16 17:03:46 +00:00
Added Prometheus to New CLI: fixes
This commit is contained in:
parent
fc58176e80
commit
f3a43f9472
2 changed files with 40 additions and 31 deletions
|
|
@ -88,9 +88,6 @@ type Config struct {
|
||||||
|
|
||||||
// GRPC has the grpc server related settings
|
// GRPC has the grpc server related settings
|
||||||
GRPC *GRPCConfig
|
GRPC *GRPCConfig
|
||||||
|
|
||||||
// Prometheus Server
|
|
||||||
Prometheus *PrometheusConfig `hcl:"prometheus,optional"`
|
|
||||||
}
|
}
|
||||||
|
|
||||||
type P2PConfig struct {
|
type P2PConfig struct {
|
||||||
|
|
@ -239,14 +236,6 @@ type GRPCConfig struct {
|
||||||
Addr string
|
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 {
|
type APIConfig struct {
|
||||||
// Enabled selects whether the api is enabled
|
// Enabled selects whether the api is enabled
|
||||||
Enabled bool `hcl:"enabled,optional"`
|
Enabled bool `hcl:"enabled,optional"`
|
||||||
|
|
@ -286,6 +275,9 @@ type TelemetryConfig struct {
|
||||||
|
|
||||||
// InfluxDB has the influxdb related settings
|
// InfluxDB has the influxdb related settings
|
||||||
InfluxDB *InfluxDBConfig `hcl:"influx,block"`
|
InfluxDB *InfluxDBConfig `hcl:"influx,block"`
|
||||||
|
|
||||||
|
// Prometheus Address
|
||||||
|
PrometheusAddr string `hcl:"prometheus-addr,optional"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type InfluxDBConfig struct {
|
type InfluxDBConfig struct {
|
||||||
|
|
|
||||||
|
|
@ -20,6 +20,7 @@ import (
|
||||||
"github.com/ethereum/go-ethereum/metrics"
|
"github.com/ethereum/go-ethereum/metrics"
|
||||||
"github.com/ethereum/go-ethereum/metrics/exp"
|
"github.com/ethereum/go-ethereum/metrics/exp"
|
||||||
"github.com/ethereum/go-ethereum/metrics/influxdb"
|
"github.com/ethereum/go-ethereum/metrics/influxdb"
|
||||||
|
"github.com/ethereum/go-ethereum/metrics/prometheus"
|
||||||
"github.com/ethereum/go-ethereum/node"
|
"github.com/ethereum/go-ethereum/node"
|
||||||
"github.com/fjl/memsize/memsizeui"
|
"github.com/fjl/memsize/memsizeui"
|
||||||
"github.com/mattn/go-colorable"
|
"github.com/mattn/go-colorable"
|
||||||
|
|
@ -50,11 +51,6 @@ func NewServer(config *Config) (*Server, error) {
|
||||||
return nil, err
|
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
|
// create the node/stack
|
||||||
nodeCfg, err := config.buildNode()
|
nodeCfg, err := config.buildNode()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
@ -159,6 +155,42 @@ func (s *Server) setupMetrics(config *TelemetryConfig) error {
|
||||||
// Start system runtime metrics collection
|
// Start system runtime metrics collection
|
||||||
go metrics.CollectProcessMetrics(3 * time.Second)
|
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
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -182,21 +214,6 @@ func (s *Server) setupGRPCServer(addr string) error {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Server) StartPrometheus(address string, enabled bool) 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
|
return nil
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue