From 82eba97505c7a4c360d11fb022395a9e5f124c81 Mon Sep 17 00:00:00 2001 From: Denis Kirillov Date: Fri, 9 Sep 2022 09:57:48 +0300 Subject: [PATCH] [#200] Unregister metrics on shutdown to fix test Signed-off-by: Denis Kirillov --- app.go | 12 ++++++++++++ downloader/download.go | 2 +- integration_test.go | 7 ++++--- metrics/metrics.go | 11 ++++++++++- uploader/upload.go | 2 +- 5 files changed, 28 insertions(+), 6 deletions(-) diff --git a/app.go b/app.go index c180353..39dfa9f 100644 --- a/app.go +++ b/app.go @@ -67,6 +67,7 @@ type ( GateMetricsProvider interface { SetHealth(int32) + Unregister() } ) @@ -243,6 +244,16 @@ func (m *gateMetrics) SetHealth(status int32) { m.provider.SetHealth(status) } +func (m *gateMetrics) Shutdown() { + m.mu.Lock() + if m.enabled { + m.provider.SetHealth(0) + m.enabled = false + } + m.provider.Unregister() + m.mu.Unlock() +} + func remove(list []string, element string) []string { for i, item := range list { if item == element { @@ -366,6 +377,7 @@ LOOP: a.log.Info("shutting down web server", zap.Error(a.webServer.Shutdown())) + a.metrics.Shutdown() a.stopServices() close(a.webDone) diff --git a/downloader/download.go b/downloader/download.go index 09b0f4a..97ef42f 100644 --- a/downloader/download.go +++ b/downloader/download.go @@ -13,7 +13,6 @@ import ( "path" "strconv" "strings" - "sync/atomic" "time" "unicode" "unicode/utf8" @@ -28,6 +27,7 @@ import ( oid "github.com/nspcc-dev/neofs-sdk-go/object/id" "github.com/nspcc-dev/neofs-sdk-go/pool" "github.com/valyala/fasthttp" + "go.uber.org/atomic" "go.uber.org/zap" ) diff --git a/integration_test.go b/integration_test.go index 2f4cd12..0da6f5e 100644 --- a/integration_test.go +++ b/integration_test.go @@ -61,7 +61,7 @@ func TestIntegration(t *testing.T) { ctx, cancel2 := context.WithCancel(rootCtx) aioContainer := createDockerContainer(ctx, t, aioImage+version) - cancel := runServer() + server, cancel := runServer() clientPool := getPool(ctx, t, key) CID, err := createContainer(ctx, t, clientPool, ownerID, version) require.NoError(t, err, version) @@ -72,13 +72,14 @@ func TestIntegration(t *testing.T) { t.Run("get zip "+version, func(t *testing.T) { getZip(ctx, t, clientPool, ownerID, CID, version) }) cancel() + server.Wait() err = aioContainer.Terminate(ctx) require.NoError(t, err) cancel2() } } -func runServer() context.CancelFunc { +func runServer() (App, context.CancelFunc) { cancelCtx, cancel := context.WithCancel(context.Background()) v := getDefaultConfig() @@ -86,7 +87,7 @@ func runServer() context.CancelFunc { application := newApp(cancelCtx, WithConfig(v), WithLogger(l, lvl)) go application.Serve(cancelCtx) - return cancel + return application, cancel } func simplePut(ctx context.Context, t *testing.T, p *pool.Pool, CID cid.ID, version string) { diff --git a/metrics/metrics.go b/metrics/metrics.go index 52b5e88..4520557 100644 --- a/metrics/metrics.go +++ b/metrics/metrics.go @@ -63,6 +63,11 @@ func NewGateMetrics(p *pool.Pool) *GateMetrics { } } +func (g *GateMetrics) Unregister() { + g.stateMetrics.unregister() + prometheus.Unregister(&g.poolMetricsCollector) +} + func newStateMetrics() *stateMetrics { return &stateMetrics{ healthCheck: prometheus.NewGauge(prometheus.GaugeOpts{ @@ -78,6 +83,10 @@ func (m stateMetrics) register() { prometheus.MustRegister(m.healthCheck) } +func (m stateMetrics) unregister() { + prometheus.Unregister(m.healthCheck) +} + func (m stateMetrics) SetHealth(s int32) { m.healthCheck.Set(float64(s)) } @@ -160,7 +169,7 @@ func (m *poolMetricsCollector) Collect(ch chan<- prometheus.Metric) { m.requestDuration.Collect(ch) } -func (m poolMetricsCollector) Describe(descs chan<- *prometheus.Desc) { +func (m *poolMetricsCollector) Describe(descs chan<- *prometheus.Desc) { m.overallErrors.Describe(descs) m.overallNodeErrors.Describe(descs) m.overallNodeRequests.Describe(descs) diff --git a/uploader/upload.go b/uploader/upload.go index 0784249..d2cabb4 100644 --- a/uploader/upload.go +++ b/uploader/upload.go @@ -6,7 +6,6 @@ import ( "fmt" "io" "strconv" - "sync/atomic" "time" "github.com/nspcc-dev/neofs-http-gw/resolver" @@ -19,6 +18,7 @@ import ( "github.com/nspcc-dev/neofs-sdk-go/pool" "github.com/nspcc-dev/neofs-sdk-go/user" "github.com/valyala/fasthttp" + "go.uber.org/atomic" "go.uber.org/zap" )