From 3fbf5e05b21d2c84e92ebf23c86d43d269b9eb31 Mon Sep 17 00:00:00 2001 From: Alex Vanin Date: Tue, 1 Jun 2021 14:20:03 +0300 Subject: [PATCH] [#493] cmd/node: Add metrics section to config Signed-off-by: Alex Vanin --- cmd/neofs-node/config/metrics/config.go | 40 ++++++++++++++++++++ cmd/neofs-node/config/metrics/config_test.go | 37 ++++++++++++++++++ config/example/node.env | 5 ++- config/example/node.json | 4 ++ config/example/node.yaml | 3 ++ 5 files changed, 88 insertions(+), 1 deletion(-) create mode 100644 cmd/neofs-node/config/metrics/config.go create mode 100644 cmd/neofs-node/config/metrics/config_test.go diff --git a/cmd/neofs-node/config/metrics/config.go b/cmd/neofs-node/config/metrics/config.go new file mode 100644 index 000000000..17510ed30 --- /dev/null +++ b/cmd/neofs-node/config/metrics/config.go @@ -0,0 +1,40 @@ +package metricsconfig + +import ( + "time" + + "github.com/nspcc-dev/neofs-node/cmd/neofs-node/config" +) + +const ( + subsection = "metrics" + + ShutdownTimeoutDefault = 30 * time.Second + AddressDefault = "" +) + +// ShutdownTimeout returns value of "shutdown_timeout" config parameter +// from "metrics" section. +// +// Returns ShutdownTimeoutDefault if value is not set. +func ShutdownTimeout(c *config.Config) time.Duration { + v := config.DurationSafe(c.Sub(subsection), "shutdown_timeout") + if v != 0 { + return v + } + + return ShutdownTimeoutDefault +} + +// Address returns value of "address" config parameter +// from "metrics" section. +// +// Returns AddressDefault if value is not set. +func Address(c *config.Config) string { + v := config.StringSafe(c.Sub(subsection), "address") + if v != "" { + return v + } + + return AddressDefault +} diff --git a/cmd/neofs-node/config/metrics/config_test.go b/cmd/neofs-node/config/metrics/config_test.go new file mode 100644 index 000000000..92056ee5f --- /dev/null +++ b/cmd/neofs-node/config/metrics/config_test.go @@ -0,0 +1,37 @@ +package metricsconfig_test + +import ( + "testing" + "time" + + "github.com/nspcc-dev/neofs-node/cmd/neofs-node/config" + metricsconfig "github.com/nspcc-dev/neofs-node/cmd/neofs-node/config/metrics" + configtest "github.com/nspcc-dev/neofs-node/cmd/neofs-node/config/test" + "github.com/stretchr/testify/require" +) + +func TestMetricsSection(t *testing.T) { + t.Run("defaults", func(t *testing.T) { + to := metricsconfig.ShutdownTimeout(configtest.EmptyConfig()) + addr := metricsconfig.Address(configtest.EmptyConfig()) + + require.Equal(t, metricsconfig.ShutdownTimeoutDefault, to) + require.Equal(t, metricsconfig.AddressDefault, addr) + }) + + const path = "../../../../config/example/node" + + var fileConfigTest = func(c *config.Config) { + to := metricsconfig.ShutdownTimeout(c) + addr := metricsconfig.Address(c) + + require.Equal(t, 15*time.Second, to) + require.Equal(t, "127.0.0.1:9090", addr) + } + + configtest.ForEachFileType(path, fileConfigTest) + + t.Run("ENV", func(t *testing.T) { + configtest.ForEnvFileType(path, fileConfigTest) + }) +} diff --git a/config/example/node.env b/config/example/node.env index 15e7fc33e..d4c9c3711 100644 --- a/config/example/node.env +++ b/config/example/node.env @@ -1,4 +1,7 @@ NEOFS_LOGGER_LEVEL=debug NEOFS_PROFILER_ADDRESS=127.0.0.1:6060 -NEOFS_PROFILER_SHUTDOWN_TIMEOUT=15s \ No newline at end of file +NEOFS_PROFILER_SHUTDOWN_TIMEOUT=15s + +NEOFS_METRICS_ADDRESS=127.0.0.1:9090 +NEOFS_METRICS_SHUTDOWN_TIMEOUT=15s \ No newline at end of file diff --git a/config/example/node.json b/config/example/node.json index 875c6c37c..660037a13 100644 --- a/config/example/node.json +++ b/config/example/node.json @@ -5,5 +5,9 @@ "profiler": { "address": "127.0.0.1:6060", "shutdown_timeout": "15s" + }, + "metrics": { + "address": "127.0.0.1:9090", + "shutdown_timeout": "15s" } } diff --git a/config/example/node.yaml b/config/example/node.yaml index 36dbe164a..15318a4a4 100644 --- a/config/example/node.yaml +++ b/config/example/node.yaml @@ -3,3 +3,6 @@ logger: profiler: address: 127.0.0.1:6060 shutdown_timeout: 15s +metrics: + address: 127.0.0.1:9090 + shutdown_timeout: 15s