2023-02-09 08:59:31 +00:00
|
|
|
package metrics
|
|
|
|
|
2023-04-07 14:28:21 +00:00
|
|
|
import (
|
|
|
|
"github.com/prometheus/client_golang/prometheus"
|
|
|
|
)
|
2023-02-09 08:59:31 +00:00
|
|
|
|
|
|
|
const stateSubsystem = "state"
|
|
|
|
|
2023-04-07 14:28:21 +00:00
|
|
|
const (
|
|
|
|
healthMetric = "health"
|
|
|
|
versionInfoMetric = "version_info"
|
|
|
|
)
|
|
|
|
|
2023-04-17 13:15:11 +00:00
|
|
|
// HealthStatus of the gate application.
|
|
|
|
type HealthStatus int32
|
|
|
|
|
|
|
|
const (
|
|
|
|
HealthStatusUndefined HealthStatus = 0
|
|
|
|
HealthStatusStarting HealthStatus = 1
|
|
|
|
HealthStatusReady HealthStatus = 2
|
|
|
|
HealthStatusShuttingDown HealthStatus = 3
|
|
|
|
)
|
|
|
|
|
2023-04-07 14:28:21 +00:00
|
|
|
type StateMetrics struct {
|
2023-02-09 08:59:31 +00:00
|
|
|
healthCheck prometheus.Gauge
|
2023-04-07 14:28:21 +00:00
|
|
|
versionInfo *prometheus.GaugeVec
|
2023-02-09 08:59:31 +00:00
|
|
|
}
|
|
|
|
|
2023-04-07 14:28:21 +00:00
|
|
|
func newStateMetrics() *StateMetrics {
|
|
|
|
return &StateMetrics{
|
2023-04-10 08:40:58 +00:00
|
|
|
healthCheck: mustNewGauge(appMetricsDesc[stateSubsystem][healthMetric]),
|
|
|
|
versionInfo: mustNewGaugeVec(appMetricsDesc[stateSubsystem][versionInfoMetric]),
|
2023-02-09 08:59:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-10 08:40:58 +00:00
|
|
|
func (m *StateMetrics) SetHealth(s HealthStatus) {
|
2023-04-07 14:28:21 +00:00
|
|
|
if m == nil {
|
|
|
|
return
|
|
|
|
}
|
2023-04-10 08:40:58 +00:00
|
|
|
m.healthCheck.Set(float64(s))
|
2023-02-09 08:59:31 +00:00
|
|
|
}
|
|
|
|
|
2023-04-10 08:40:58 +00:00
|
|
|
func (m *StateMetrics) SetVersion(ver string) {
|
2023-04-07 14:28:21 +00:00
|
|
|
if m == nil {
|
|
|
|
return
|
|
|
|
}
|
2023-04-10 08:40:58 +00:00
|
|
|
m.versionInfo.WithLabelValues(ver).Set(1)
|
2023-02-09 08:59:31 +00:00
|
|
|
}
|
|
|
|
|
2023-04-10 08:40:58 +00:00
|
|
|
func (m *StateMetrics) Describe(desc chan<- *prometheus.Desc) {
|
2023-04-07 14:28:21 +00:00
|
|
|
if m == nil {
|
|
|
|
return
|
|
|
|
}
|
2023-04-10 08:40:58 +00:00
|
|
|
|
|
|
|
m.healthCheck.Describe(desc)
|
|
|
|
m.versionInfo.Describe(desc)
|
2023-02-09 08:59:31 +00:00
|
|
|
}
|
2023-04-07 14:28:21 +00:00
|
|
|
|
2023-04-10 08:40:58 +00:00
|
|
|
func (m *StateMetrics) Collect(ch chan<- prometheus.Metric) {
|
2023-04-07 14:28:21 +00:00
|
|
|
if m == nil {
|
|
|
|
return
|
|
|
|
}
|
2023-04-10 08:40:58 +00:00
|
|
|
|
|
|
|
m.healthCheck.Collect(ch)
|
|
|
|
m.versionInfo.Collect(ch)
|
2023-04-07 14:28:21 +00:00
|
|
|
}
|