forked from TrueCloudLab/frostfs-node
[#493] Refactor serving of prometheus and pprof services
Rename `util/profiler` package to `httputil` and refactor it: * simplify utility HTTP server; * make more generic server's parameters in order to remove `viper.Viper` dependency; * use single constructor for creating the pprof and prometheus servers; * replace `enabled` config value with empty-check of the network address. Signed-off-by: Leonard Lyubich <leonard@nspcc.ru>
This commit is contained in:
parent
6339f1a468
commit
8d17dab86e
15 changed files with 295 additions and 304 deletions
|
@ -40,7 +40,6 @@ import (
|
|||
"github.com/nspcc-dev/neofs-node/pkg/services/util/response"
|
||||
util2 "github.com/nspcc-dev/neofs-node/pkg/util"
|
||||
"github.com/nspcc-dev/neofs-node/pkg/util/logger"
|
||||
"github.com/nspcc-dev/neofs-node/pkg/util/profiler"
|
||||
"github.com/panjf2000/ants/v2"
|
||||
"github.com/pkg/errors"
|
||||
"github.com/spf13/viper"
|
||||
|
@ -55,13 +54,12 @@ const (
|
|||
cfgLogLevel = "logger.level"
|
||||
|
||||
// pprof keys
|
||||
cfgProfilerEnable = "pprof.enabled"
|
||||
cfgProfilerAddr = "pprof.address"
|
||||
cfgProfilerTTL = "pprof.shutdown_ttl"
|
||||
cfgProfilerAddr = "profiler.address"
|
||||
cfgProfilerShutdownTimeout = "profiler.shutdown_timeout"
|
||||
|
||||
// metrics keys
|
||||
cfgMetricsEnable = "metrics.enabled"
|
||||
cfgMetricsAddr = "metrics.address"
|
||||
cfgMetricsAddr = "metrics.address"
|
||||
cfgMetricsShutdownTimeout = "metrics.shutdown_timeout"
|
||||
|
||||
// config keys for cfgNodeInfo
|
||||
cfgNodeKey = "node.key"
|
||||
|
@ -203,10 +201,6 @@ type cfg struct {
|
|||
|
||||
cfgObject cfgObject
|
||||
|
||||
profiler profiler.Profiler
|
||||
|
||||
metricsServer profiler.Metrics
|
||||
|
||||
metricsCollector *metrics.StorageMetrics
|
||||
|
||||
workers []worker
|
||||
|
@ -438,7 +432,7 @@ func initCfg(path string) *cfg {
|
|||
},
|
||||
}
|
||||
|
||||
if viperCfg.GetBool(cfgMetricsEnable) {
|
||||
if c.viper.GetString(cfgMetricsAddr) != "" {
|
||||
c.metricsCollector = metrics.NewStorageMetrics()
|
||||
}
|
||||
|
||||
|
@ -496,12 +490,9 @@ func defaultConfiguration(v *viper.Viper) {
|
|||
|
||||
v.SetDefault(cfgLogLevel, "info")
|
||||
|
||||
v.SetDefault(cfgProfilerEnable, false)
|
||||
v.SetDefault(cfgProfilerAddr, ":6060")
|
||||
v.SetDefault(cfgProfilerTTL, "30s")
|
||||
v.SetDefault(cfgProfilerShutdownTimeout, "30s")
|
||||
|
||||
v.SetDefault(cfgMetricsEnable, false)
|
||||
v.SetDefault(cfgMetricsAddr, ":9090")
|
||||
v.SetDefault(cfgMetricsShutdownTimeout, "30s")
|
||||
|
||||
v.SetDefault(cfgGCQueueSize, 1000)
|
||||
v.SetDefault(cfgGCQueueTick, "5s")
|
||||
|
|
|
@ -60,12 +60,10 @@ func initApp(c *cfg) {
|
|||
}
|
||||
|
||||
func bootUp(c *cfg) {
|
||||
serveProfiler(c)
|
||||
serveGRPC(c)
|
||||
bootstrapNode(c)
|
||||
startWorkers(c)
|
||||
startBlockTimers(c)
|
||||
serveMetrics(c)
|
||||
}
|
||||
|
||||
func wait(c *cfg) {
|
||||
|
|
|
@ -1,17 +1,40 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"github.com/nspcc-dev/neofs-node/pkg/util/profiler"
|
||||
"context"
|
||||
|
||||
httputil "github.com/nspcc-dev/neofs-node/pkg/util/http"
|
||||
"github.com/prometheus/client_golang/prometheus/promhttp"
|
||||
"go.uber.org/zap"
|
||||
)
|
||||
|
||||
func initMetrics(c *cfg) {
|
||||
if c.metricsCollector != nil {
|
||||
c.metricsServer = profiler.NewMetrics(c.log, c.viper)
|
||||
addr := c.viper.GetString(cfgMetricsAddr)
|
||||
if addr == "" {
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
func serveMetrics(c *cfg) {
|
||||
if c.metricsServer != nil {
|
||||
c.metricsServer.Start(c.ctx)
|
||||
}
|
||||
var prm httputil.Prm
|
||||
|
||||
prm.Address = addr
|
||||
prm.Handler = promhttp.Handler()
|
||||
|
||||
srv := httputil.New(prm,
|
||||
httputil.WithShutdownTimeout(
|
||||
c.viper.GetDuration(cfgMetricsShutdownTimeout),
|
||||
),
|
||||
)
|
||||
|
||||
c.workers = append(c.workers, newWorkerFromFunc(func(context.Context) {
|
||||
fatalOnErr(srv.Serve())
|
||||
}))
|
||||
|
||||
c.closers = append(c.closers, func() {
|
||||
err := srv.Shutdown()
|
||||
if err != nil {
|
||||
c.log.Debug("could not shutdown metrics server",
|
||||
zap.String("error", err.Error()),
|
||||
)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
|
|
@ -1,15 +1,39 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"github.com/nspcc-dev/neofs-node/pkg/util/profiler"
|
||||
"context"
|
||||
|
||||
httputil "github.com/nspcc-dev/neofs-node/pkg/util/http"
|
||||
"go.uber.org/zap"
|
||||
)
|
||||
|
||||
func initProfiler(c *cfg) {
|
||||
c.profiler = profiler.NewProfiler(c.log, c.viper)
|
||||
}
|
||||
|
||||
func serveProfiler(c *cfg) {
|
||||
if c.profiler != nil {
|
||||
c.profiler.Start(c.ctx)
|
||||
addr := c.viper.GetString(cfgProfilerAddr)
|
||||
if addr == "" {
|
||||
return
|
||||
}
|
||||
|
||||
var prm httputil.Prm
|
||||
|
||||
prm.Address = addr
|
||||
prm.Handler = httputil.Handler()
|
||||
|
||||
srv := httputil.New(prm,
|
||||
httputil.WithShutdownTimeout(
|
||||
c.viper.GetDuration(cfgProfilerShutdownTimeout),
|
||||
),
|
||||
)
|
||||
|
||||
c.workers = append(c.workers, newWorkerFromFunc(func(context.Context) {
|
||||
fatalOnErr(srv.Serve())
|
||||
}))
|
||||
|
||||
c.closers = append(c.closers, func() {
|
||||
err := srv.Shutdown()
|
||||
if err != nil {
|
||||
c.log.Debug("could not shutdown pprof server",
|
||||
zap.String("error", err.Error()),
|
||||
)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue