[#200] Unregister metrics on shutdown to fix test

Signed-off-by: Denis Kirillov <denis@nspcc.ru>
This commit is contained in:
Denis Kirillov 2022-09-09 09:57:48 +03:00 committed by Kirillov Denis
parent ce84dc7068
commit 82eba97505
5 changed files with 28 additions and 6 deletions

12
app.go
View file

@ -67,6 +67,7 @@ type (
GateMetricsProvider interface { GateMetricsProvider interface {
SetHealth(int32) SetHealth(int32)
Unregister()
} }
) )
@ -243,6 +244,16 @@ func (m *gateMetrics) SetHealth(status int32) {
m.provider.SetHealth(status) 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 { func remove(list []string, element string) []string {
for i, item := range list { for i, item := range list {
if item == element { if item == element {
@ -366,6 +377,7 @@ LOOP:
a.log.Info("shutting down web server", zap.Error(a.webServer.Shutdown())) a.log.Info("shutting down web server", zap.Error(a.webServer.Shutdown()))
a.metrics.Shutdown()
a.stopServices() a.stopServices()
close(a.webDone) close(a.webDone)

View file

@ -13,7 +13,6 @@ import (
"path" "path"
"strconv" "strconv"
"strings" "strings"
"sync/atomic"
"time" "time"
"unicode" "unicode"
"unicode/utf8" "unicode/utf8"
@ -28,6 +27,7 @@ import (
oid "github.com/nspcc-dev/neofs-sdk-go/object/id" oid "github.com/nspcc-dev/neofs-sdk-go/object/id"
"github.com/nspcc-dev/neofs-sdk-go/pool" "github.com/nspcc-dev/neofs-sdk-go/pool"
"github.com/valyala/fasthttp" "github.com/valyala/fasthttp"
"go.uber.org/atomic"
"go.uber.org/zap" "go.uber.org/zap"
) )

View file

@ -61,7 +61,7 @@ func TestIntegration(t *testing.T) {
ctx, cancel2 := context.WithCancel(rootCtx) ctx, cancel2 := context.WithCancel(rootCtx)
aioContainer := createDockerContainer(ctx, t, aioImage+version) aioContainer := createDockerContainer(ctx, t, aioImage+version)
cancel := runServer() server, cancel := runServer()
clientPool := getPool(ctx, t, key) clientPool := getPool(ctx, t, key)
CID, err := createContainer(ctx, t, clientPool, ownerID, version) CID, err := createContainer(ctx, t, clientPool, ownerID, version)
require.NoError(t, err, 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) }) t.Run("get zip "+version, func(t *testing.T) { getZip(ctx, t, clientPool, ownerID, CID, version) })
cancel() cancel()
server.Wait()
err = aioContainer.Terminate(ctx) err = aioContainer.Terminate(ctx)
require.NoError(t, err) require.NoError(t, err)
cancel2() cancel2()
} }
} }
func runServer() context.CancelFunc { func runServer() (App, context.CancelFunc) {
cancelCtx, cancel := context.WithCancel(context.Background()) cancelCtx, cancel := context.WithCancel(context.Background())
v := getDefaultConfig() v := getDefaultConfig()
@ -86,7 +87,7 @@ func runServer() context.CancelFunc {
application := newApp(cancelCtx, WithConfig(v), WithLogger(l, lvl)) application := newApp(cancelCtx, WithConfig(v), WithLogger(l, lvl))
go application.Serve(cancelCtx) 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) { func simplePut(ctx context.Context, t *testing.T, p *pool.Pool, CID cid.ID, version string) {

View file

@ -63,6 +63,11 @@ func NewGateMetrics(p *pool.Pool) *GateMetrics {
} }
} }
func (g *GateMetrics) Unregister() {
g.stateMetrics.unregister()
prometheus.Unregister(&g.poolMetricsCollector)
}
func newStateMetrics() *stateMetrics { func newStateMetrics() *stateMetrics {
return &stateMetrics{ return &stateMetrics{
healthCheck: prometheus.NewGauge(prometheus.GaugeOpts{ healthCheck: prometheus.NewGauge(prometheus.GaugeOpts{
@ -78,6 +83,10 @@ func (m stateMetrics) register() {
prometheus.MustRegister(m.healthCheck) prometheus.MustRegister(m.healthCheck)
} }
func (m stateMetrics) unregister() {
prometheus.Unregister(m.healthCheck)
}
func (m stateMetrics) SetHealth(s int32) { func (m stateMetrics) SetHealth(s int32) {
m.healthCheck.Set(float64(s)) m.healthCheck.Set(float64(s))
} }
@ -160,7 +169,7 @@ func (m *poolMetricsCollector) Collect(ch chan<- prometheus.Metric) {
m.requestDuration.Collect(ch) 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.overallErrors.Describe(descs)
m.overallNodeErrors.Describe(descs) m.overallNodeErrors.Describe(descs)
m.overallNodeRequests.Describe(descs) m.overallNodeRequests.Describe(descs)

View file

@ -6,7 +6,6 @@ import (
"fmt" "fmt"
"io" "io"
"strconv" "strconv"
"sync/atomic"
"time" "time"
"github.com/nspcc-dev/neofs-http-gw/resolver" "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/pool"
"github.com/nspcc-dev/neofs-sdk-go/user" "github.com/nspcc-dev/neofs-sdk-go/user"
"github.com/valyala/fasthttp" "github.com/valyala/fasthttp"
"go.uber.org/atomic"
"go.uber.org/zap" "go.uber.org/zap"
) )