[#175] Add health metric

Signed-off-by: Denis Kirillov <denis@nspcc.ru>
This commit is contained in:
Denis Kirillov 2022-07-22 11:30:57 +03:00 committed by Alex Vanin
parent dd50c4ed55
commit cf018c2fab
2 changed files with 54 additions and 0 deletions

13
app.go
View file

@ -31,6 +31,7 @@ type (
webServer *fasthttp.Server
webDone chan struct{}
resolver *resolver.ContainerResolver
metrics GateMetricsProvider
}
// App is an interface for the main gateway function.
@ -41,6 +42,10 @@ type (
// Option is an application option.
Option func(a *app)
GateMetricsProvider interface {
SetHealth(int32)
}
)
// WithLogger returns Option to set a specific logger.
@ -151,6 +156,10 @@ func newApp(ctx context.Context, opt ...Option) App {
a.log.Info("container resolver is disabled")
}
if a.cfg.GetBool(cmdMetrics) {
a.metrics = newGateMetrics()
}
return a
}
@ -231,6 +240,10 @@ func getKeyFromWallet(w *wallet.Wallet, addrStr string, password *string) (*ecds
func (a *app) Wait() {
a.log.Info("starting application", zap.String("app_name", "neofs-http-gw"), zap.String("version", Version))
if a.metrics != nil {
a.metrics.SetHealth(1)
}
<-a.webDone // wait for web-server to be stopped
}

View file

@ -9,6 +9,47 @@ import (
"go.uber.org/zap"
)
const (
namespace = "neofs_http_gw"
stateSubsystem = "state"
)
type GateMetrics struct {
stateMetrics
}
type stateMetrics struct {
healthCheck prometheus.Gauge
}
func newGateMetrics() *GateMetrics {
stateMetric := newStateMetrics()
stateMetric.register()
return &GateMetrics{
stateMetrics: stateMetric,
}
}
func newStateMetrics() stateMetrics {
return stateMetrics{
healthCheck: prometheus.NewGauge(prometheus.GaugeOpts{
Namespace: namespace,
Subsystem: stateSubsystem,
Name: "health",
Help: "Current HTTP gateway state",
}),
}
}
func (m stateMetrics) register() {
prometheus.MustRegister(m.healthCheck)
}
func (m stateMetrics) SetHealth(s int32) {
m.healthCheck.Set(float64(s))
}
func attachMetrics(r *router.Router, l *zap.Logger) {
r.GET("/metrics/", metricsHandler(prometheus.DefaultGatherer, l))
}