From dc59dc991bd10c908feb160c1dd0ada6649e860b Mon Sep 17 00:00:00 2001 From: Roman Khimov Date: Fri, 8 Jul 2022 19:10:46 +0300 Subject: [PATCH] config: move metrics.Config into config.BasicService Config package should be as lightweight as possible and now it depends on the whole metrics package just to get one structure from it. --- cli/server/server_test.go | 5 ++--- pkg/config/application_config.go | 5 ++--- pkg/config/basic_service.go | 8 ++++++++ pkg/network/metrics/metrics.go | 10 ++-------- pkg/network/metrics/pprof.go | 3 ++- pkg/network/metrics/prometheus.go | 3 ++- 6 files changed, 18 insertions(+), 16 deletions(-) create mode 100644 pkg/config/basic_service.go diff --git a/cli/server/server_test.go b/cli/server/server_test.go index 30d7edb6c..741a0ced6 100644 --- a/cli/server/server_test.go +++ b/cli/server/server_test.go @@ -10,7 +10,6 @@ import ( "github.com/nspcc-dev/neo-go/pkg/config" "github.com/nspcc-dev/neo-go/pkg/config/netmode" "github.com/nspcc-dev/neo-go/pkg/core/storage" - "github.com/nspcc-dev/neo-go/pkg/network/metrics" "github.com/nspcc-dev/neo-go/pkg/rpc" "github.com/stretchr/testify/require" "github.com/urfave/cli" @@ -317,7 +316,7 @@ func TestConfigureAddresses(t *testing.T) { t.Run("custom Pprof address", func(t *testing.T) { cfg := &config.ApplicationConfiguration{ Address: defaultAddress, - Pprof: metrics.Config{ + Pprof: config.BasicService{ Address: customAddress, }, } @@ -330,7 +329,7 @@ func TestConfigureAddresses(t *testing.T) { t.Run("custom Prometheus address", func(t *testing.T) { cfg := &config.ApplicationConfiguration{ Address: defaultAddress, - Prometheus: metrics.Config{ + Prometheus: config.BasicService{ Address: customAddress, }, } diff --git a/pkg/config/application_config.go b/pkg/config/application_config.go index 35dced09f..40322a15a 100644 --- a/pkg/config/application_config.go +++ b/pkg/config/application_config.go @@ -2,7 +2,6 @@ package config import ( "github.com/nspcc-dev/neo-go/pkg/core/storage" - "github.com/nspcc-dev/neo-go/pkg/network/metrics" "github.com/nspcc-dev/neo-go/pkg/rpc" ) @@ -19,8 +18,8 @@ type ApplicationConfiguration struct { NodePort uint16 `yaml:"NodePort"` PingInterval int64 `yaml:"PingInterval"` PingTimeout int64 `yaml:"PingTimeout"` - Pprof metrics.Config `yaml:"Pprof"` - Prometheus metrics.Config `yaml:"Prometheus"` + Pprof BasicService `yaml:"Pprof"` + Prometheus BasicService `yaml:"Prometheus"` ProtoTickInterval int64 `yaml:"ProtoTickInterval"` Relay bool `yaml:"Relay"` RPC rpc.Config `yaml:"RPC"` diff --git a/pkg/config/basic_service.go b/pkg/config/basic_service.go new file mode 100644 index 000000000..3e2137b96 --- /dev/null +++ b/pkg/config/basic_service.go @@ -0,0 +1,8 @@ +package config + +// BasicService is used for simple services like Pprof or Prometheus monitoring. +type BasicService struct { + Enabled bool `yaml:"Enabled"` + Address string `yaml:"Address"` + Port string `yaml:"Port"` +} diff --git a/pkg/network/metrics/metrics.go b/pkg/network/metrics/metrics.go index 088a8477e..071c1fbeb 100644 --- a/pkg/network/metrics/metrics.go +++ b/pkg/network/metrics/metrics.go @@ -4,24 +4,18 @@ import ( "context" "net/http" + "github.com/nspcc-dev/neo-go/pkg/config" "go.uber.org/zap" ) // Service serves metrics. type Service struct { *http.Server - config Config + config config.BasicService log *zap.Logger serviceType string } -// Config config used for monitoring. -type Config struct { - Enabled bool `yaml:"Enabled"` - Address string `yaml:"Address"` - Port string `yaml:"Port"` -} - // Start runs http service with the exposed endpoint on the configured port. func (ms *Service) Start() { if ms.config.Enabled { diff --git a/pkg/network/metrics/pprof.go b/pkg/network/metrics/pprof.go index 887acc0ea..6c3bbb202 100644 --- a/pkg/network/metrics/pprof.go +++ b/pkg/network/metrics/pprof.go @@ -4,6 +4,7 @@ import ( "net/http" "net/http/pprof" + "github.com/nspcc-dev/neo-go/pkg/config" "go.uber.org/zap" ) @@ -11,7 +12,7 @@ import ( type PprofService Service // NewPprofService creates a new service for gathering pprof metrics. -func NewPprofService(cfg Config, log *zap.Logger) *Service { +func NewPprofService(cfg config.BasicService, log *zap.Logger) *Service { if log == nil { return nil } diff --git a/pkg/network/metrics/prometheus.go b/pkg/network/metrics/prometheus.go index c02bc50fb..b00efc8b3 100644 --- a/pkg/network/metrics/prometheus.go +++ b/pkg/network/metrics/prometheus.go @@ -3,6 +3,7 @@ package metrics import ( "net/http" + "github.com/nspcc-dev/neo-go/pkg/config" "github.com/prometheus/client_golang/prometheus/promhttp" "go.uber.org/zap" ) @@ -11,7 +12,7 @@ import ( type PrometheusService Service // NewPrometheusService creates a new service for gathering prometheus metrics. -func NewPrometheusService(cfg Config, log *zap.Logger) *Service { +func NewPrometheusService(cfg config.BasicService, log *zap.Logger) *Service { if log == nil { return nil }