Skip to content

Commit d991225

Browse files
authored
✨ Enable metrics of clientgo leader election (#1901)
* tweak comment of the workqueue metrics file * enable metrics of clientgo leader election
1 parent 262268f commit d991225

File tree

4 files changed

+47
-2
lines changed

4 files changed

+47
-2
lines changed

pkg/manager/internal.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,9 @@ type controllerManager struct {
144144
// webhookServer if unset, and Add() it to controllerManager.
145145
webhookServerOnce sync.Once
146146

147+
// leaderElectionID is the name of the resource that leader election
148+
// will use for holding the leader lock.
149+
leaderElectionID string
147150
// leaseDuration is the duration that non-leader candidates will
148151
// wait to force acquire leadership.
149152
leaseDuration time.Duration
@@ -637,6 +640,7 @@ func (cm *controllerManager) startLeaderElection(ctx context.Context) (err error
637640
},
638641
},
639642
ReleaseOnCancel: cm.leaderElectionReleaseOnCancel,
643+
Name: cm.leaderElectionID,
640644
})
641645
if err != nil {
642646
return err

pkg/manager/manager.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -439,6 +439,7 @@ func New(config *rest.Config, options Options) (Manager, error) {
439439
certDir: options.CertDir,
440440
tlsOpts: options.TLSOpts,
441441
webhookServer: options.WebhookServer,
442+
leaderElectionID: options.LeaderElectionID,
442443
leaseDuration: *options.LeaseDuration,
443444
renewDeadline: *options.RenewDeadline,
444445
retryPeriod: *options.RetryPeriod,

pkg/metrics/leaderelection.go

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package metrics
2+
3+
import (
4+
"github.com/prometheus/client_golang/prometheus"
5+
"k8s.io/client-go/tools/leaderelection"
6+
)
7+
8+
// This file is copied and adapted from k8s.io/component-base/metrics/prometheus/clientgo/leaderelection
9+
// which registers metrics to the k8s legacy Registry. We require very
10+
// similar functionality, but must register metrics to a different Registry.
11+
12+
var (
13+
leaderGauge = prometheus.NewGaugeVec(prometheus.GaugeOpts{
14+
Name: "leader_election_master_status",
15+
Help: "Gauge of if the reporting system is master of the relevant lease, 0 indicates backup, 1 indicates master. 'name' is the string used to identify the lease. Please make sure to group by name.",
16+
}, []string{"name"})
17+
)
18+
19+
func init() {
20+
Registry.MustRegister(leaderGauge)
21+
leaderelection.SetProvider(leaderelectionMetricsProvider{})
22+
}
23+
24+
type leaderelectionMetricsProvider struct{}
25+
26+
func (leaderelectionMetricsProvider) NewLeaderMetric() leaderelection.SwitchMetric {
27+
return &switchAdapter{gauge: leaderGauge}
28+
}
29+
30+
type switchAdapter struct {
31+
gauge *prometheus.GaugeVec
32+
}
33+
34+
func (s *switchAdapter) On(name string) {
35+
s.gauge.WithLabelValues(name).Set(1.0)
36+
}
37+
38+
func (s *switchAdapter) Off(name string) {
39+
s.gauge.WithLabelValues(name).Set(0.0)
40+
}

pkg/metrics/workqueue.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@ import (
2121
"k8s.io/client-go/util/workqueue"
2222
)
2323

24-
// This file is copied and adapted from k8s.io/kubernetes/pkg/util/workqueue/prometheus
25-
// which registers metrics to the default prometheus Registry. We require very
24+
// This file is copied and adapted from k8s.io/component-base/metrics/prometheus/workqueue
25+
// which registers metrics to the k8s legacy Registry. We require very
2626
// similar functionality, but must register metrics to a different Registry.
2727

2828
// Metrics subsystem and all keys used by the workqueue.

0 commit comments

Comments
 (0)