...

Source file src/sigs.k8s.io/controller-runtime/pkg/metrics/leaderelection.go

Documentation: sigs.k8s.io/controller-runtime/pkg/metrics

     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  	leaderSlowpathCounter = prometheus.NewCounterVec(prometheus.CounterOpts{
    19  		Name: "leader_election_slowpath_total",
    20  		Help: "Total number of slow path exercised in renewing leader leases. 'name' is the string used to identify the lease. Please make sure to group by name.",
    21  	}, []string{"name"})
    22  )
    23  
    24  func init() {
    25  	Registry.MustRegister(leaderGauge)
    26  	leaderelection.SetProvider(leaderelectionMetricsProvider{})
    27  }
    28  
    29  type leaderelectionMetricsProvider struct{}
    30  
    31  func (leaderelectionMetricsProvider) NewLeaderMetric() leaderelection.LeaderMetric {
    32  	return leaderElectionPrometheusAdapter{}
    33  }
    34  
    35  type leaderElectionPrometheusAdapter struct{}
    36  
    37  func (s leaderElectionPrometheusAdapter) On(name string) {
    38  	leaderGauge.WithLabelValues(name).Set(1.0)
    39  }
    40  
    41  func (s leaderElectionPrometheusAdapter) Off(name string) {
    42  	leaderGauge.WithLabelValues(name).Set(0.0)
    43  }
    44  
    45  func (leaderElectionPrometheusAdapter) SlowpathExercised(name string) {
    46  	leaderSlowpathCounter.WithLabelValues(name).Inc()
    47  }
    48  

View as plain text