-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Append basic prometheus metrics (#60)
Closes #58.
- Loading branch information
Showing
7 changed files
with
248 additions
and
40 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.