Skip to content

Commit

Permalink
✨ Enable metrics of clientgo leader election (#1901)
Browse files Browse the repository at this point in the history
* tweak comment of the workqueue metrics file

* enable metrics of clientgo leader election
  • Loading branch information
zqzten committed Dec 1, 2022
1 parent 262268f commit d991225
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 2 deletions.
4 changes: 4 additions & 0 deletions pkg/manager/internal.go
Expand Up @@ -144,6 +144,9 @@ type controllerManager struct {
// webhookServer if unset, and Add() it to controllerManager.
webhookServerOnce sync.Once

// leaderElectionID is the name of the resource that leader election
// will use for holding the leader lock.
leaderElectionID string
// leaseDuration is the duration that non-leader candidates will
// wait to force acquire leadership.
leaseDuration time.Duration
Expand Down Expand Up @@ -637,6 +640,7 @@ func (cm *controllerManager) startLeaderElection(ctx context.Context) (err error
},
},
ReleaseOnCancel: cm.leaderElectionReleaseOnCancel,
Name: cm.leaderElectionID,
})
if err != nil {
return err
Expand Down
1 change: 1 addition & 0 deletions pkg/manager/manager.go
Expand Up @@ -439,6 +439,7 @@ func New(config *rest.Config, options Options) (Manager, error) {
certDir: options.CertDir,
tlsOpts: options.TLSOpts,
webhookServer: options.WebhookServer,
leaderElectionID: options.LeaderElectionID,
leaseDuration: *options.LeaseDuration,
renewDeadline: *options.RenewDeadline,
retryPeriod: *options.RetryPeriod,
Expand Down
40 changes: 40 additions & 0 deletions pkg/metrics/leaderelection.go
@@ -0,0 +1,40 @@
package metrics

import (
"github.com/prometheus/client_golang/prometheus"
"k8s.io/client-go/tools/leaderelection"
)

// This file is copied and adapted from k8s.io/component-base/metrics/prometheus/clientgo/leaderelection
// which registers metrics to the k8s legacy Registry. We require very
// similar functionality, but must register metrics to a different Registry.

var (
leaderGauge = prometheus.NewGaugeVec(prometheus.GaugeOpts{
Name: "leader_election_master_status",
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.",
}, []string{"name"})
)

func init() {
Registry.MustRegister(leaderGauge)
leaderelection.SetProvider(leaderelectionMetricsProvider{})
}

type leaderelectionMetricsProvider struct{}

func (leaderelectionMetricsProvider) NewLeaderMetric() leaderelection.SwitchMetric {
return &switchAdapter{gauge: leaderGauge}
}

type switchAdapter struct {
gauge *prometheus.GaugeVec
}

func (s *switchAdapter) On(name string) {
s.gauge.WithLabelValues(name).Set(1.0)
}

func (s *switchAdapter) Off(name string) {
s.gauge.WithLabelValues(name).Set(0.0)
}
4 changes: 2 additions & 2 deletions pkg/metrics/workqueue.go
Expand Up @@ -21,8 +21,8 @@ import (
"k8s.io/client-go/util/workqueue"
)

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

// Metrics subsystem and all keys used by the workqueue.
Expand Down

0 comments on commit d991225

Please sign in to comment.