Marina Biryukova
2981a47e99
All checks were successful
/ DCO (pull_request) Successful in 1m20s
/ Vulncheck (pull_request) Successful in 1m54s
/ Builds (1.20) (pull_request) Successful in 2m22s
/ Builds (1.21) (pull_request) Successful in 2m8s
/ Lint (pull_request) Successful in 4m32s
/ Tests (1.20) (pull_request) Successful in 2m27s
/ Tests (1.21) (pull_request) Successful in 2m13s
Signed-off-by: Marina Biryukova <m.biryukova@yadro.com>
115 lines
2 KiB
Go
115 lines
2 KiB
Go
package metrics
|
|
|
|
import (
|
|
"net/http"
|
|
"sync"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-s3-gw/internal/logs"
|
|
"github.com/prometheus/client_golang/prometheus"
|
|
dto "github.com/prometheus/client_model/go"
|
|
"go.uber.org/zap"
|
|
)
|
|
|
|
type AppMetrics struct {
|
|
logger *zap.Logger
|
|
gate *GateMetrics
|
|
mu sync.RWMutex
|
|
enabled bool
|
|
}
|
|
|
|
type AppMetricsConfig struct {
|
|
Logger *zap.Logger
|
|
PoolStatistics StatisticScraper
|
|
Registerer prometheus.Registerer
|
|
Enabled bool
|
|
}
|
|
|
|
func NewAppMetrics(cfg AppMetricsConfig) *AppMetrics {
|
|
if !cfg.Enabled {
|
|
cfg.Logger.Warn(logs.MetricsAreDisabled)
|
|
}
|
|
|
|
registry := cfg.Registerer
|
|
if registry == nil {
|
|
registry = prometheus.DefaultRegisterer
|
|
}
|
|
|
|
return &AppMetrics{
|
|
logger: cfg.Logger,
|
|
gate: NewGateMetrics(cfg.PoolStatistics, registry),
|
|
enabled: cfg.Enabled,
|
|
}
|
|
}
|
|
|
|
func (m *AppMetrics) SetEnabled(enabled bool) {
|
|
if !enabled {
|
|
m.logger.Warn(logs.MetricsAreDisabled)
|
|
}
|
|
|
|
m.mu.Lock()
|
|
m.enabled = enabled
|
|
m.mu.Unlock()
|
|
}
|
|
|
|
func (m *AppMetrics) State() *StateMetrics {
|
|
if !m.isEnabled() {
|
|
return nil
|
|
}
|
|
|
|
return m.gate.State
|
|
}
|
|
|
|
func (m *AppMetrics) Shutdown() {
|
|
m.mu.Lock()
|
|
if m.enabled {
|
|
m.gate.State.SetHealth(HealthStatusShuttingDown)
|
|
m.enabled = false
|
|
}
|
|
m.gate.Unregister()
|
|
m.mu.Unlock()
|
|
}
|
|
|
|
func (m *AppMetrics) isEnabled() bool {
|
|
m.mu.RLock()
|
|
defer m.mu.RUnlock()
|
|
return m.enabled
|
|
}
|
|
|
|
func (m *AppMetrics) Handler() http.Handler {
|
|
return m.gate.Handler()
|
|
}
|
|
|
|
func (m *AppMetrics) UsersAPIStats() *UsersAPIStats {
|
|
if !m.isEnabled() {
|
|
return nil
|
|
}
|
|
return m.gate.Billing.apiStat
|
|
}
|
|
|
|
func (m *AppMetrics) Statistic() *APIStatMetrics {
|
|
if !m.isEnabled() {
|
|
return nil
|
|
}
|
|
|
|
return m.gate.Stats
|
|
}
|
|
|
|
func (m *AppMetrics) Gather() ([]*dto.MetricFamily, error) {
|
|
return m.gate.Gather()
|
|
}
|
|
|
|
func (m *AppMetrics) MarkHealthy(endpoint string) {
|
|
if !m.isEnabled() {
|
|
return
|
|
}
|
|
|
|
m.gate.HTTPServer.MarkHealthy(endpoint)
|
|
}
|
|
|
|
func (m *AppMetrics) MarkUnhealthy(endpoint string) {
|
|
if !m.isEnabled() {
|
|
return
|
|
}
|
|
|
|
m.gate.HTTPServer.MarkUnhealthy(endpoint)
|
|
}
|