From cf018c2fab9b6ea3bb6aba52158ce11ffbd8b61f Mon Sep 17 00:00:00 2001 From: Denis Kirillov Date: Fri, 22 Jul 2022 11:30:57 +0300 Subject: [PATCH] [#175] Add health metric Signed-off-by: Denis Kirillov --- app.go | 13 +++++++++++++ metrics.go | 41 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 54 insertions(+) diff --git a/app.go b/app.go index 26201c6..edb3d31 100644 --- a/app.go +++ b/app.go @@ -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 } diff --git a/metrics.go b/metrics.go index 40491bc..4836d6b 100644 --- a/metrics.go +++ b/metrics.go @@ -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)) }