2023-02-09 08:59:31 +00:00
|
|
|
package metrics
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
"sync"
|
|
|
|
|
|
|
|
"go.uber.org/zap"
|
|
|
|
)
|
|
|
|
|
|
|
|
type AppMetrics struct {
|
|
|
|
logger *zap.Logger
|
|
|
|
gate *GateMetrics
|
|
|
|
mu sync.RWMutex
|
|
|
|
enabled bool
|
|
|
|
}
|
|
|
|
|
2023-04-07 14:28:21 +00:00
|
|
|
func NewAppMetrics(logger *zap.Logger, poolStatistics StatisticScraper, enabled bool) *AppMetrics {
|
2023-02-09 08:59:31 +00:00
|
|
|
if !enabled {
|
|
|
|
logger.Warn("metrics are disabled")
|
|
|
|
}
|
|
|
|
return &AppMetrics{
|
|
|
|
logger: logger,
|
|
|
|
gate: NewGateMetrics(poolStatistics),
|
|
|
|
enabled: enabled,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *AppMetrics) SetEnabled(enabled bool) {
|
|
|
|
if !enabled {
|
|
|
|
m.logger.Warn("metrics are disabled")
|
|
|
|
}
|
|
|
|
|
|
|
|
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()
|
|
|
|
}
|
|
|
|
|
2023-04-07 14:28:21 +00:00
|
|
|
func (m *AppMetrics) Update(user, bucket, cnrID string, reqType RequestType, in, out uint64) {
|
2023-02-09 08:59:31 +00:00
|
|
|
if !m.isEnabled() {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
m.gate.Billing.apiStat.Update(user, bucket, cnrID, reqType, in, out)
|
|
|
|
}
|
2023-04-07 14:28:21 +00:00
|
|
|
|
|
|
|
func (m *AppMetrics) Statistic() *APIStatMetrics {
|
|
|
|
if !m.isEnabled() {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return m.gate.Stats
|
|
|
|
}
|