network: add Pprof metrics

Since we have some perf issues from time to time it is good to have pprof debugger. Disabled by default.
This commit is contained in:
Vsevolod Brekelov 2019-12-04 09:48:32 +03:00
parent e4db0e5db4
commit 0085831ec5
13 changed files with 111 additions and 45 deletions

View file

@ -280,12 +280,14 @@ func startServer(ctx *cli.Context) error {
server := network.NewServer(serverConfig, chain)
rpcServer := rpc.NewServer(chain, cfg.ApplicationConfiguration.RPC, server)
errChan := make(chan error)
monitoring := metrics.NewMetricsService(cfg.ApplicationConfiguration.Monitoring)
prometheus := metrics.NewPrometheusService(cfg.ApplicationConfiguration.Prometheus)
pprof := metrics.NewPprofService(cfg.ApplicationConfiguration.Pprof)
go chain.Run()
go server.Start(errChan)
go rpcServer.Start(errChan)
go monitoring.Start()
go prometheus.Start()
go pprof.Start()
fmt.Println(logo())
fmt.Println(server.UserAgent)
@ -304,7 +306,8 @@ Main:
if serverErr := rpcServer.Shutdown(); serverErr != nil {
shutdownErr = errors.Wrap(serverErr, "Error encountered whilst shutting down server")
}
monitoring.ShutDown()
prometheus.ShutDown()
pprof.ShutDown()
chain.Close()
break Main
}
@ -317,17 +320,20 @@ Main:
return nil
}
// configureAddresses sets up addresses for RPC and Monitoring depending from the provided config.
// In case RPC or Monitoring Address provided each of them will use it.
// In case global Address (of the node) provided and RPC/Monitoring don't have configured addresses they will
// use global one. So Node and RPC and Monitoring will run on one address.
// configureAddresses sets up addresses for RPC, Prometheus and Pprof depending from the provided config.
// In case RPC or Prometheus or Pprof Address provided each of them will use it.
// In case global Address (of the node) provided and RPC/Prometheus/Pprof don't have configured addresses they will
// use global one. So Node and RPC and Prometheus and Pprof will run on one address.
func configureAddresses(cfg config.ApplicationConfiguration) {
if cfg.Address != "" {
if cfg.RPC.Address == "" {
cfg.RPC.Address = cfg.Address
}
if cfg.Monitoring.Address == "" {
cfg.Monitoring.Address = cfg.Address
if cfg.Prometheus.Address == "" {
cfg.Prometheus.Address = cfg.Address
}
if cfg.Pprof.Address == "" {
cfg.Pprof.Address = cfg.Address
}
}
}

View file

@ -76,7 +76,8 @@ type (
MaxPeers int `yaml:"MaxPeers"`
AttemptConnPeers int `yaml:"AttemptConnPeers"`
MinPeers int `yaml:"MinPeers"`
Monitoring metrics.PrometheusConfig `yaml:"Monitoring"`
Prometheus metrics.Config `yaml:"Prometheus"`
Pprof metrics.Config `yaml:"Pprof"`
RPC RPCConfig `yaml:"RPC"`
UnlockWallet WalletConfig `yaml:"UnlockWallet"`
}

View file

@ -57,6 +57,9 @@ ApplicationConfiguration:
Enabled: true
EnableCORSWorkaround: false
Port: 10332
Monitoring:
Prometheus:
Enabled: true
Port: 2112
Pprof:
Enabled: false
Port: 2113

View file

@ -48,9 +48,12 @@ ApplicationConfiguration:
Enabled: true
EnableCORSWorkaround: false
Port: 30336
Monitoring:
Prometheus:
Enabled: true
Port: 20004
Pprof:
Enabled: false
Port: 20014
UnlockWallet:
Path: "6PYRXVwHSqFSukL3CuXxdQ75VmsKpjeLgQLEjt83FrtHf1gCVphHzdD4nc"
Password: "four"

View file

@ -48,9 +48,12 @@ ApplicationConfiguration:
Enabled: true
EnableCORSWorkaround: false
Port: 30333
Monitoring:
Prometheus:
Enabled: true
Port: 20001
Pprof:
Enabled: false
Port: 20011
UnlockWallet:
Path: "6PYLmjBYJ4wQTCEfqvnznGJwZeW9pfUcV5m5oreHxqryUgqKpTRAFt9L8Y"
Password: "one"

View file

@ -48,9 +48,12 @@ ApplicationConfiguration:
Enabled: true
EnableCORSWorkaround: false
Port: 30335
Monitoring:
Prometheus:
Enabled: true
Port: 20003
Pprof:
Enabled: false
Port: 20013
UnlockWallet:
Path: "6PYX86vYiHfUbpD95hfN1xgnvcSxy5skxfWYKu3ztjecxk6ikYs2kcWbeh"
Password: "three"

View file

@ -48,9 +48,12 @@ ApplicationConfiguration:
Enabled: true
EnableCORSWorkaround: false
Port: 30334
Monitoring:
Prometheus:
Enabled: true
Port: 20002
Pprof:
Enabled: false
Port: 20012
UnlockWallet:
Path: "6PYXHjPaNvW8YknSXaKsTWjf9FRxo1s4naV2jdmSQEgzaqKGX368rndN3L"
Password: "two"

View file

@ -48,6 +48,9 @@ ApplicationConfiguration:
Enabled: true
EnableCORSWorkaround: false
Port: 20331
Monitoring:
Prometheus:
Enabled: true
Port: 2112
Pprof:
Enabled: false
Port: 2113

View file

@ -57,6 +57,9 @@ ApplicationConfiguration:
Enabled: true
EnableCORSWorkaround: false
Port: 20332
Monitoring:
Prometheus:
Enabled: true
Port: 2112
Pprof:
Enabled: false
Port: 2113

View file

@ -47,6 +47,9 @@ ApplicationConfiguration:
Enabled: true
EnableCORSWorkaround: false
Port: 20332
Monitoring:
Prometheus:
Enabled: false #since it's not useful for unit tests.
Port: 2112
Pprof:
Enabled: false #since it's not useful for unit tests.
Port: 2113

View file

@ -4,57 +4,47 @@ import (
"context"
"net/http"
"github.com/prometheus/client_golang/prometheus/promhttp"
log "github.com/sirupsen/logrus"
)
// Service serves metrics provided by prometheus.
// Service serves metrics.
type Service struct {
*http.Server
config PrometheusConfig
config Config
serviceType string
}
// PrometheusConfig config for Prometheus used for monitoring.
// Additional information about Prometheus could be found here: https://prometheus.io/docs/guides/go-application.
type PrometheusConfig struct {
// Config config used for monitoring.
type Config struct {
Enabled bool `yaml:"Enabled"`
Address string `yaml:"Address"`
Port string `yaml:"Port"`
}
// NewMetricsService created new service for gathering metrics.
func NewMetricsService(cfg PrometheusConfig) *Service {
return &Service{
&http.Server{
Addr: cfg.Address + ":" + cfg.Port,
Handler: promhttp.Handler(),
}, cfg,
}
}
// Start runs http service with exposed `/metrics` endpoint on configured port.
// Start runs http service with exposed endpoint on configured port.
func (ms *Service) Start() {
if ms.config.Enabled {
log.WithFields(log.Fields{
"endpoint": ms.Addr,
"service": ms.serviceType,
}).Info("service running")
err := ms.ListenAndServe()
if err != nil {
log.WithFields(log.Fields{
"endpoint": ms.Addr,
}).Info("metrics service up and running")
} else {
log.Warn("metrics service couldn't start on configured port")
if err != nil && err != http.ErrServerClosed {
log.Warnf("%s service couldn't start on configured port", ms.serviceType)
}
} else {
log.Infof("metrics service hasn't started since it's disabled")
log.Infof("%s service hasn't started since it's disabled", ms.serviceType)
}
}
// ShutDown stops service.
func (ms *Service) ShutDown() {
log.WithFields(log.Fields{
"endpoint": ms.config.Port,
}).Info("shutting down monitoring-service")
"endpoint": ms.Addr,
"service": ms.serviceType,
}).Info("shutting down service")
err := ms.Shutdown(context.Background())
if err != nil {
log.Fatal("can't shut down monitoring service")
log.Fatalf("can't shut down %s service", ms.serviceType)
}
}

View file

@ -0,0 +1,25 @@
package metrics
import (
"net/http"
"net/http/pprof"
)
// PprofService https://golang.org/pkg/net/http/pprof/.
type PprofService Service
// NewPprofService created new service for gathering pprof metrics.
func NewPprofService(cfg Config) *Service {
handler := http.NewServeMux()
handler.HandleFunc("/debug/pprof", pprof.Index)
handler.HandleFunc("/debug/pprof/cmdline", pprof.Cmdline)
handler.HandleFunc("/debug/pprof/profile", pprof.Profile)
handler.HandleFunc("/debug/pprof/symbol", pprof.Symbol)
handler.HandleFunc("/debug/pprof/trace", pprof.Trace)
return &Service{
&http.Server{
Addr: cfg.Address + ":" + cfg.Port,
Handler: handler,
}, cfg, "Pprof",
}
}

View file

@ -0,0 +1,20 @@
package metrics
import (
"net/http"
"github.com/prometheus/client_golang/prometheus/promhttp"
)
// PrometheusService https://prometheus.io/docs/guides/go-application.
type PrometheusService Service
// NewPrometheusService creates new service for gathering prometheus metrics.
func NewPrometheusService(cfg Config) *Service {
return &Service{
&http.Server{
Addr: cfg.Address + ":" + cfg.Port,
Handler: promhttp.Handler(),
}, cfg, "Prometheus",
}
}