Skip to content

Commit

Permalink
Append basic prometheus metrics (#60)
Browse files Browse the repository at this point in the history
Closes #58.
  • Loading branch information
roman-khimov authored Sep 11, 2024
2 parents 0d9bfee + 0527402 commit 068bde9
Show file tree
Hide file tree
Showing 7 changed files with 248 additions and 40 deletions.
24 changes: 20 additions & 4 deletions cmd/neofs-oauthz/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ type (
cfg *viper.Viper
webServer *http.Server
webDone chan struct{}

gateMetrics *gateMetrics
}

// App is an interface for the main gateway function.
Expand Down Expand Up @@ -65,16 +67,28 @@ func WithConfig(c *viper.Viper) Option {
func newApp(ctx context.Context, opt ...Option) App {
var err error
a := &app{
log: zap.L(),
cfg: viper.GetViper(),
webServer: new(http.Server),
webDone: make(chan struct{}),
log: zap.L(),
cfg: viper.GetViper(),
webServer: new(http.Server),
webDone: make(chan struct{}),
gateMetrics: newGateMetrics(),
}

for i := range opt {
opt[i](a)
}

a.gateMetrics.SetAppVersion(Version)

prometheusService := newPrometheus(
a.log,
a.cfg.GetBool(cfgPrometheusEnabled),
a.cfg.GetString(cfgPrometheusAddress),
)

services := newServices([]*service{prometheusService})
services.RunServices()

key, err := a.getKey()
if err != nil {
a.log.Fatal("failed to get neofs credentials", zap.Error(err))
Expand Down Expand Up @@ -293,6 +307,8 @@ func (a *app) Serve(ctx context.Context) {
myHandler.HandleFunc("/callback", authenticator.Callback)
a.webServer.Handler = myHandler

a.gateMetrics.SetServiceStarted()

a.webServer.Addr = a.authCfg.Host
if a.authCfg.TLSEnabled {
a.log.Info("running web server (TLS-enabled)", zap.String("address", a.webServer.Addr))
Expand Down
80 changes: 80 additions & 0 deletions cmd/neofs-oauthz/app_metrics.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
package main

import (
"net/http"

"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
"go.uber.org/zap"
)

const (
namespace = "neofs_oauthz"
)

type (
// gateMetrics is a metrics collection.
gateMetrics struct {
stateMetrics
}

stateMetrics struct {
up prometheus.Gauge
gwVersion *prometheus.GaugeVec
}
)

// newGateMetrics creates new metrics for the app.
func newGateMetrics() *gateMetrics {
stateMetric := newStateMetrics()
stateMetric.register()

return &gateMetrics{
stateMetrics: *stateMetric,
}
}

func newStateMetrics() *stateMetrics {
return &stateMetrics{
up: prometheus.NewGauge(prometheus.GaugeOpts{
Namespace: namespace,
Name: "up",
Help: "App is up and running",
}),
gwVersion: prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Help: "App version",
Name: "version",
Namespace: namespace,
},
[]string{"version"},
),
}
}

func (m stateMetrics) register() {
prometheus.MustRegister(m.up)
prometheus.MustRegister(m.gwVersion)
}

// SetServiceStarted updates the `up` metric with the value 1.
func (m stateMetrics) SetServiceStarted() {
m.up.Set(1.0)
}

// newPrometheus creates a new service for gathering prometheus metrics.
func newPrometheus(log *zap.Logger, enabled bool, address string) *service {
return newService(
&http.Server{
Addr: address,
Handler: promhttp.Handler(),
},
enabled,
log.With(zap.String("service", "Prometheus")),
)
}

// SetAppVersion increments the app version metric counter for the specified version label.
func (g *gateMetrics) SetAppVersion(ver string) {
g.gwVersion.WithLabelValues(ver).Add(1)
}
3 changes: 3 additions & 0 deletions cmd/neofs-oauthz/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,9 @@ const (
cfgOauthEndpointTokenFmt = "oauth.%s.endpoint.token"
cfgRedirectURL = "redirect.url"
callbackURLFmt = "%scallback"

cfgPrometheusEnabled = "prometheus.enabled"
cfgPrometheusAddress = "prometheus.address"
)

var ignore = map[string]struct{}{
Expand Down
91 changes: 91 additions & 0 deletions cmd/neofs-oauthz/service.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
package main

import (
"context"
"errors"
"net/http"
"time"

"go.uber.org/zap"
)

const defaultShutdownTimeout = 15 * time.Second

type (
// service serves metrics.
service struct {
*http.Server
enabled bool
log *zap.Logger
}

// services is a collection for services which can be started in background.
services struct {
services []*service
}
)

// newService is a constructor for service.
func newService(
server *http.Server,
enabled bool,
log *zap.Logger,
) *service {
return &service{
Server: server,
enabled: enabled,
log: log,
}
}

// Start runs http service with the exposed endpoint on the configured port.
func (ms *service) Start() {
if !ms.enabled {
ms.log.Info("service hasn't started since it's disabled")
}

ms.log.Info("service is running", zap.String("endpoint", ms.Addr))

if err := ms.ListenAndServe(); err != nil {
if !errors.Is(err, http.ErrServerClosed) {
ms.log.Warn("service couldn't start on configured port", zap.Error(err))
}
}
}

// ShutDown stops the service.
func (ms *service) ShutDown(ctx context.Context) {
ms.log.Info("shutting down service", zap.String("endpoint", ms.Addr))

if err := ms.Shutdown(ctx); err != nil {
ms.log.Panic("can't shut down service", zap.Error(err))
}
}

// newServices is a constructor for services.
func newServices(servioceList []*service) *services {
return &services{
services: servioceList,
}
}

// RunServices function runs all services.
func (x *services) RunServices() {
for _, s := range x.services {
go s.Start()
}
}

// StopServices function is shutting down all services.
func (x *services) StopServices() {
ctx, cancel := shutdownContext()
defer cancel()

for _, s := range x.services {
go s.ShutDown(ctx)
}
}

func shutdownContext() (context.Context, context.CancelFunc) {
return context.WithTimeout(context.Background(), defaultShutdownTimeout)
}
4 changes: 4 additions & 0 deletions config/config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -46,3 +46,7 @@ bearer_cookie_name: "Bearer"
connect_timeout: 30s
request_timeout: 15s
rebalance_timer: 15s

prometheus:
enabled: true
address: localhost:9986
20 changes: 14 additions & 6 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ go 1.22
require (
github.com/nspcc-dev/neo-go v0.106.3
github.com/nspcc-dev/neofs-sdk-go v1.0.0-rc.12
github.com/prometheus/client_golang v1.20.3
github.com/spf13/pflag v1.0.5
github.com/spf13/viper v1.19.0
go.uber.org/zap v1.27.0
Expand All @@ -13,22 +14,29 @@ require (

require (
github.com/antlr/antlr4/runtime/Go/antlr/v4 v4.0.0-20221202181307-76fa05c21b12 // indirect
github.com/beorn7/perks v1.0.1 // indirect
github.com/cespare/xxhash/v2 v2.3.0 // indirect
github.com/cpuguy83/go-md2man/v2 v2.0.4 // indirect
github.com/decred/dcrd/dcrec/secp256k1/v4 v4.2.0 // indirect
github.com/fsnotify/fsnotify v1.7.0 // indirect
github.com/golang/protobuf v1.5.3 // indirect
github.com/google/uuid v1.6.0 // indirect
github.com/hashicorp/golang-lru/v2 v2.0.7 // indirect
github.com/hashicorp/hcl v1.0.0 // indirect
github.com/klauspost/compress v1.17.9 // indirect
github.com/magiconair/properties v1.8.7 // indirect
github.com/mitchellh/mapstructure v1.5.0 // indirect
github.com/mr-tron/base58 v1.2.0 // indirect
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect
github.com/nspcc-dev/go-ordered-json v0.0.0-20240301084351-0246b013f8b2 // indirect
github.com/nspcc-dev/hrw/v2 v2.0.1 // indirect
github.com/nspcc-dev/neofs-api-go/v2 v2.14.1-0.20240305074711-35bc78d84dc4 // indirect
github.com/nspcc-dev/rfc6979 v0.2.1 // indirect
github.com/nspcc-dev/tzhash v1.7.2 // indirect
github.com/pelletier/go-toml/v2 v2.2.2 // indirect
github.com/prometheus/client_model v0.6.1 // indirect
github.com/prometheus/common v0.55.0 // indirect
github.com/prometheus/procfs v0.15.1 // indirect
github.com/russross/blackfriday/v2 v2.1.0 // indirect
github.com/sagikazarmark/locafero v0.4.0 // indirect
github.com/sagikazarmark/slog-shim v0.1.0 // indirect
Expand All @@ -40,15 +48,15 @@ require (
github.com/urfave/cli/v2 v2.27.2 // indirect
github.com/xrash/smetrics v0.0.0-20240312152122-5f08fbb34913 // indirect
go.uber.org/multierr v1.11.0 // indirect
golang.org/x/crypto v0.21.0 // indirect
golang.org/x/crypto v0.24.0 // indirect
golang.org/x/exp v0.0.0-20240222234643-814bf88cf225 // indirect
golang.org/x/net v0.23.0 // indirect
golang.org/x/sys v0.18.0 // indirect
golang.org/x/term v0.18.0 // indirect
golang.org/x/text v0.14.0 // indirect
golang.org/x/net v0.26.0 // indirect
golang.org/x/sys v0.22.0 // indirect
golang.org/x/term v0.21.0 // indirect
golang.org/x/text v0.16.0 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20240314234333-6e1732d8331c // indirect
google.golang.org/grpc v1.62.1 // indirect
google.golang.org/protobuf v1.33.0 // indirect
google.golang.org/protobuf v1.34.2 // indirect
gopkg.in/ini.v1 v1.67.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
Loading

0 comments on commit 068bde9

Please sign in to comment.