From ac46d1a11fc06ca751a223a9d4b45b9021c1365b Mon Sep 17 00:00:00 2001 From: Evgenii Stratonikov Date: Wed, 13 Jul 2022 12:17:37 +0300 Subject: [PATCH] [#1602] config: Enable `metrics` and `profiler` services with a flag Signed-off-by: Evgenii Stratonikov --- CHANGELOG.md | 2 ++ cmd/neofs-ir/main.go | 15 +++++++++------ cmd/neofs-node/config.go | 4 +++- cmd/neofs-node/config/metrics/config.go | 10 +++++++++- cmd/neofs-node/config/metrics/config_test.go | 2 ++ cmd/neofs-node/config/profiler/config.go | 10 +++++++++- cmd/neofs-node/config/profiler/config_test.go | 2 ++ cmd/neofs-node/metrics.go | 5 ++--- cmd/neofs-node/pprof.go | 6 +++--- config/example/ir.env | 2 ++ config/example/ir.yaml | 2 ++ config/example/node.env | 2 ++ config/example/node.json | 2 ++ config/example/node.yaml | 2 ++ config/mainnet/config.yml | 1 + config/testnet/config.yml | 1 + 16 files changed, 53 insertions(+), 15 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 119461d1d..a09734dbe 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,8 @@ Changelog for NeoFS Node ### Added +- Profiler and metrics services now should be enabled with a separate flag + ### Changed - Require SG members to be unique (#1490) diff --git a/cmd/neofs-ir/main.go b/cmd/neofs-ir/main.go index bc79049c8..8122613a8 100644 --- a/cmd/neofs-ir/main.go +++ b/cmd/neofs-ir/main.go @@ -62,7 +62,7 @@ func main() { intErr := make(chan error) // internal inner ring errors - httpServers := initHTTPServers(cfg) + httpServers := initHTTPServers(cfg, log) innerRing, err := innerring.New(ctx, log, cfg, intErr) exitErr(err) @@ -107,23 +107,26 @@ func main() { log.Info("application stopped") } -func initHTTPServers(cfg *viper.Viper) []*httputil.Server { +func initHTTPServers(cfg *viper.Viper, log *logger.Logger) []*httputil.Server { items := []struct { + msg string cfgPrefix string handler func() http.Handler }{ - {"profiler", httputil.Handler}, - {"metrics", promhttp.Handler}, + {"pprof", "profiler", httputil.Handler}, + {"prometheus", "metrics", promhttp.Handler}, } httpServers := make([]*httputil.Server, 0, len(items)) for _, item := range items { - addr := cfg.GetString(item.cfgPrefix + ".address") - if addr == "" { + if !cfg.GetBool(item.cfgPrefix + ".enabled") { + log.Info(item.msg + " is disabled, skip") continue } + addr := cfg.GetString(item.cfgPrefix + ".address") + var prm httputil.Prm prm.Address = addr diff --git a/cmd/neofs-node/config.go b/cmd/neofs-node/config.go index ee7d0abd7..f35cdef65 100644 --- a/cmd/neofs-node/config.go +++ b/cmd/neofs-node/config.go @@ -321,9 +321,11 @@ func initCfg(path string) *cfg { user.IDFromKey(&c.ownerIDFromKey, key.PrivateKey.PublicKey) - if metricsconfig.Address(c.appCfg) != "" { + if metricsconfig.Enabled(c.appCfg) { c.metricsCollector = metrics.NewStorageMetrics() netState.metrics = c.metricsCollector + } else { + c.log.Info("prometheus metrics are disabled") } c.onShutdown(c.clientCache.CloseAll) // clean up connections diff --git a/cmd/neofs-node/config/metrics/config.go b/cmd/neofs-node/config/metrics/config.go index 9d24c7479..d18b97c03 100644 --- a/cmd/neofs-node/config/metrics/config.go +++ b/cmd/neofs-node/config/metrics/config.go @@ -13,9 +13,17 @@ const ( ShutdownTimeoutDefault = 30 * time.Second // AddressDefault is a default value for metrics HTTP service endpoint. - AddressDefault = "" + AddressDefault = "localhost:9090" ) +// Enabled returns the value of "enabled" config parameter +// from "metrics" section. +// +// Returns false if the value is missing or invalid. +func Enabled(c *config.Config) bool { + return config.BoolSafe(c.Sub(subsection), "enabled") +} + // ShutdownTimeout returns the value of "shutdown_timeout" config parameter // from "metrics" section. // diff --git a/cmd/neofs-node/config/metrics/config_test.go b/cmd/neofs-node/config/metrics/config_test.go index 3cdaa12fa..2a041a248 100644 --- a/cmd/neofs-node/config/metrics/config_test.go +++ b/cmd/neofs-node/config/metrics/config_test.go @@ -17,6 +17,7 @@ func TestMetricsSection(t *testing.T) { require.Equal(t, metricsconfig.ShutdownTimeoutDefault, to) require.Equal(t, metricsconfig.AddressDefault, addr) + require.False(t, metricsconfig.Enabled(configtest.EmptyConfig())) }) const path = "../../../../config/example/node" @@ -27,6 +28,7 @@ func TestMetricsSection(t *testing.T) { require.Equal(t, 15*time.Second, to) require.Equal(t, "localhost:9090", addr) + require.True(t, metricsconfig.Enabled(c)) } configtest.ForEachFileType(path, fileConfigTest) diff --git a/cmd/neofs-node/config/profiler/config.go b/cmd/neofs-node/config/profiler/config.go index 4d565c60d..5e0ddab84 100644 --- a/cmd/neofs-node/config/profiler/config.go +++ b/cmd/neofs-node/config/profiler/config.go @@ -13,9 +13,17 @@ const ( ShutdownTimeoutDefault = 30 * time.Second // AddressDefault is a default value for profiler HTTP service endpoint. - AddressDefault = "" + AddressDefault = "localhost:6060" ) +// Enabled returns the value of "enabled" config parameter +// from "profiler" section. +// +// Returns false if the value is missing or invalid. +func Enabled(c *config.Config) bool { + return config.BoolSafe(c.Sub(subsection), "enabled") +} + // ShutdownTimeout returns the value of "shutdown_timeout" config parameter // from "profiler" section. // diff --git a/cmd/neofs-node/config/profiler/config_test.go b/cmd/neofs-node/config/profiler/config_test.go index 4a773dd14..2a5bd292d 100644 --- a/cmd/neofs-node/config/profiler/config_test.go +++ b/cmd/neofs-node/config/profiler/config_test.go @@ -17,6 +17,7 @@ func TestProfilerSection(t *testing.T) { require.Equal(t, profilerconfig.ShutdownTimeoutDefault, to) require.Equal(t, profilerconfig.AddressDefault, addr) + require.False(t, profilerconfig.Enabled(configtest.EmptyConfig())) }) const path = "../../../../config/example/node" @@ -27,6 +28,7 @@ func TestProfilerSection(t *testing.T) { require.Equal(t, 15*time.Second, to) require.Equal(t, "localhost:6060", addr) + require.True(t, profilerconfig.Enabled(c)) } configtest.ForEachFileType(path, fileConfigTest) diff --git a/cmd/neofs-node/metrics.go b/cmd/neofs-node/metrics.go index 04275d626..65ac37a2a 100644 --- a/cmd/neofs-node/metrics.go +++ b/cmd/neofs-node/metrics.go @@ -10,14 +10,13 @@ import ( ) func initMetrics(c *cfg) { - addr := metricsconfig.Address(c.appCfg) - if addr == "" { + if !metricsconfig.Enabled(c.appCfg) { return } var prm httputil.Prm - prm.Address = addr + prm.Address = metricsconfig.Address(c.appCfg) prm.Handler = promhttp.Handler() srv := httputil.New(prm, diff --git a/cmd/neofs-node/pprof.go b/cmd/neofs-node/pprof.go index 727de5489..a64fa8574 100644 --- a/cmd/neofs-node/pprof.go +++ b/cmd/neofs-node/pprof.go @@ -9,14 +9,14 @@ import ( ) func initProfiler(c *cfg) { - addr := profilerconfig.Address(c.appCfg) - if addr == "" { + if !profilerconfig.Enabled(c.appCfg) { + c.log.Info("pprof is disabled") return } var prm httputil.Prm - prm.Address = addr + prm.Address = profilerconfig.Address(c.appCfg) prm.Handler = httputil.Handler() srv := httputil.New(prm, diff --git a/config/example/ir.env b/config/example/ir.env index ab86ebc25..833b353c1 100644 --- a/config/example/ir.env +++ b/config/example/ir.env @@ -82,9 +82,11 @@ NEOFS_IR_CONTRACTS_ALPHABET_DOBRO=e6122b65d45c8feeb04455d67814394c147ed4d1 NEOFS_IR_CONTRACTS_ALPHABET_YEST=cdbca5cb5d48a4472923844d0e3ee6328cf86d38 NEOFS_IR_CONTRACTS_ALPHABET_ZHIVETE=f584699bc2ff457d339fb09f16217042c1a42101 +NEOFS_IR_PROFILER_ENABLED=true NEOFS_IR_PROFILER_ADDRESS=localhost:6060 NEOFS_IR_PROFILER_SHUTDOWN_TIMEOUT=30s +NEOFS_IR_METRICS_ENABLED=true NEOFS_IR_METRICS_ADDRESS=localhost:9090 NEOFS_IR_METRICS_SHUTDOWN_TIMEOUT=30s diff --git a/config/example/ir.yaml b/config/example/ir.yaml index 74b4eb0f2..3c2a5dbf6 100644 --- a/config/example/ir.yaml +++ b/config/example/ir.yaml @@ -124,10 +124,12 @@ contracts: zhivete: f584699bc2ff457d339fb09f16217042c1a42101 # Optional: override address of zhivete contract in sidechain profiler: + enabled: true address: localhost:6060 # Endpoint for application pprof profiling; disabled by default shutdown_timeout: 30s # Timeout for profiling HTTP server graceful shutdown metrics: + enabled: true address: localhost:9090 # Endpoint for application prometheus metrics; disabled by default shutdown_timeout: 30s # Timeout for metrics HTTP server graceful shutdown diff --git a/config/example/node.env b/config/example/node.env index f2492bbf0..3c674ee71 100644 --- a/config/example/node.env +++ b/config/example/node.env @@ -1,8 +1,10 @@ NEOFS_LOGGER_LEVEL=debug +NEOFS_PROFILER_ENABLED=true NEOFS_PROFILER_ADDRESS=localhost:6060 NEOFS_PROFILER_SHUTDOWN_TIMEOUT=15s +NEOFS_METRICS_ENABLED=true NEOFS_METRICS_ADDRESS=localhost:9090 NEOFS_METRICS_SHUTDOWN_TIMEOUT=15s diff --git a/config/example/node.json b/config/example/node.json index 6d5cb103a..569aff46f 100644 --- a/config/example/node.json +++ b/config/example/node.json @@ -3,10 +3,12 @@ "level": "debug" }, "profiler": { + "enabled": true, "address": "localhost:6060", "shutdown_timeout": "15s" }, "metrics": { + "enabled": true, "address": "localhost:9090", "shutdown_timeout": "15s" }, diff --git a/config/example/node.yaml b/config/example/node.yaml index 311138b36..889d1cb28 100644 --- a/config/example/node.yaml +++ b/config/example/node.yaml @@ -2,10 +2,12 @@ logger: level: debug # logger level: one of "debug", "info" (default), "warn", "error", "dpanic", "panic", "fatal" profiler: + enabled: true address: localhost:6060 # endpoint for Node profiling shutdown_timeout: 15s # timeout for profiling HTTP server graceful shutdown metrics: + enabled: true address: localhost:9090 # endpoint for Node metrics shutdown_timeout: 15s # timeout for metrics HTTP server graceful shutdown diff --git a/config/mainnet/config.yml b/config/mainnet/config.yml index be6739a2c..c8863adc6 100644 --- a/config/mainnet/config.yml +++ b/config/mainnet/config.yml @@ -40,6 +40,7 @@ logger: level: info metrics: + enabled: true address: localhost:9090 shutdown_timeout: 15s diff --git a/config/testnet/config.yml b/config/testnet/config.yml index 0697eec85..a419410f8 100644 --- a/config/testnet/config.yml +++ b/config/testnet/config.yml @@ -24,6 +24,7 @@ node: attribute_1: User-Agent:NeoFS\/0.27 metrics: + enabled: true address: localhost:9090 shutdown_timeout: 15s