2023-02-09 08:59:31 +00:00
|
|
|
package metrics
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
"sync"
|
|
|
|
|
2023-08-23 11:07:52 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-s3-gw/internal/logs"
|
2024-02-26 13:18:34 +00:00
|
|
|
"github.com/prometheus/client_golang/prometheus"
|
2023-04-10 08:40:58 +00:00
|
|
|
dto "github.com/prometheus/client_model/go"
|
2023-02-09 08:59:31 +00:00
|
|
|
"go.uber.org/zap"
|
|
|
|
)
|
|
|
|
|
|
|
|
type AppMetrics struct {
|
|
|
|
logger *zap.Logger
|
|
|
|
gate *GateMetrics
|
|
|
|
mu sync.RWMutex
|
|
|
|
enabled bool
|
|
|
|
}
|
|
|
|
|
2024-02-26 13:18:34 +00:00
|
|
|
type AppMetricsConfig struct {
|
|
|
|
Logger *zap.Logger
|
|
|
|
PoolStatistics StatisticScraper
|
2024-07-30 16:40:58 +00:00
|
|
|
TreeStatistic TreePoolStatistic
|
2024-02-26 13:18:34 +00:00
|
|
|
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
|
2023-02-09 08:59:31 +00:00
|
|
|
}
|
2024-02-26 13:18:34 +00:00
|
|
|
|
2023-02-09 08:59:31 +00:00
|
|
|
return &AppMetrics{
|
2024-02-26 13:18:34 +00:00
|
|
|
logger: cfg.Logger,
|
2024-07-30 16:40:58 +00:00
|
|
|
gate: NewGateMetrics(cfg.PoolStatistics, cfg.TreeStatistic, registry),
|
2024-02-26 13:18:34 +00:00
|
|
|
enabled: cfg.Enabled,
|
2023-02-09 08:59:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-11-20 12:05:04 +00:00
|
|
|
func (m *AppMetrics) DroppedLogsInc() {
|
|
|
|
m.gate.Logs.DroppedLogsInc()
|
|
|
|
}
|
|
|
|
|
2023-02-09 08:59:31 +00:00
|
|
|
func (m *AppMetrics) SetEnabled(enabled bool) {
|
|
|
|
if !enabled {
|
2023-08-23 11:07:52 +00:00
|
|
|
m.logger.Warn(logs.MetricsAreDisabled)
|
2023-02-09 08:59:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
m.mu.Lock()
|
|
|
|
m.enabled = enabled
|
|
|
|
m.mu.Unlock()
|
|
|
|
}
|
|
|
|
|
2023-04-07 14:28:21 +00:00
|
|
|
func (m *AppMetrics) State() *StateMetrics {
|
2023-02-09 08:59:31 +00:00
|
|
|
if !m.isEnabled() {
|
2023-04-07 14:28:21 +00:00
|
|
|
return nil
|
2023-02-09 08:59:31 +00:00
|
|
|
}
|
|
|
|
|
2023-04-07 14:28:21 +00:00
|
|
|
return m.gate.State
|
2023-02-09 08:59:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (m *AppMetrics) Shutdown() {
|
|
|
|
m.mu.Lock()
|
|
|
|
if m.enabled {
|
2023-04-17 13:15:11 +00:00
|
|
|
m.gate.State.SetHealth(HealthStatusShuttingDown)
|
2023-02-09 08:59:31 +00:00
|
|
|
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()
|
|
|
|
}
|
|
|
|
|
2024-02-26 13:18:34 +00:00
|
|
|
func (m *AppMetrics) UsersAPIStats() *UsersAPIStats {
|
2023-02-09 08:59:31 +00:00
|
|
|
if !m.isEnabled() {
|
2024-02-26 13:18:34 +00:00
|
|
|
return nil
|
2023-02-09 08:59:31 +00:00
|
|
|
}
|
2024-02-26 13:18:34 +00:00
|
|
|
return m.gate.Billing.apiStat
|
2023-02-09 08:59:31 +00:00
|
|
|
}
|
2023-04-07 14:28:21 +00:00
|
|
|
|
|
|
|
func (m *AppMetrics) Statistic() *APIStatMetrics {
|
|
|
|
if !m.isEnabled() {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return m.gate.Stats
|
|
|
|
}
|
2023-04-10 08:40:58 +00:00
|
|
|
|
|
|
|
func (m *AppMetrics) Gather() ([]*dto.MetricFamily, error) {
|
|
|
|
return m.gate.Gather()
|
|
|
|
}
|
2023-08-29 15:17:56 +00:00
|
|
|
|
|
|
|
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)
|
|
|
|
}
|