-
Notifications
You must be signed in to change notification settings - Fork 127
/
leader_metrics.go
63 lines (49 loc) · 1.55 KB
/
leader_metrics.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
package leader
import (
"sync"
"github.com/prometheus/client_golang/prometheus"
"github.com/armadaproject/armada/internal/common/armadacontext"
"github.com/armadaproject/armada/internal/common/metrics"
)
var leaderStatusDesc = prometheus.NewDesc(
metrics.MetricPrefix+"scheduler_leader_status",
"Gauge of if the reporting system is leader, 0 indicates hot replica, 1 indicates leader.",
[]string{"name"}, nil,
)
type LeaderStatusMetricsCollector struct {
currentInstanceName string
isCurrentlyLeader bool
lock sync.Mutex
}
func NewLeaderStatusMetricsCollector(currentInstanceName string) *LeaderStatusMetricsCollector {
return &LeaderStatusMetricsCollector{
isCurrentlyLeader: false,
currentInstanceName: currentInstanceName,
lock: sync.Mutex{},
}
}
func (l *LeaderStatusMetricsCollector) onStartedLeading(*armadacontext.Context) {
l.lock.Lock()
defer l.lock.Unlock()
l.isCurrentlyLeader = true
}
func (l *LeaderStatusMetricsCollector) onStoppedLeading() {
l.lock.Lock()
defer l.lock.Unlock()
l.isCurrentlyLeader = false
}
func (l *LeaderStatusMetricsCollector) isLeading() bool {
l.lock.Lock()
defer l.lock.Unlock()
return l.isCurrentlyLeader
}
func (l *LeaderStatusMetricsCollector) Describe(desc chan<- *prometheus.Desc) {
desc <- leaderStatusDesc
}
func (l *LeaderStatusMetricsCollector) Collect(metrics chan<- prometheus.Metric) {
value := float64(0)
if l.isLeading() {
value = 1
}
metrics <- prometheus.MustNewConstMetric(leaderStatusDesc, prometheus.GaugeValue, value, l.currentInstanceName)
}