2023-02-01 17:06:36 +00:00
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" } )
2024-05-13 20:57:03 +00:00
leaderSlowpathCounter = prometheus . NewCounterVec ( prometheus . CounterOpts {
Name : "leader_election_slowpath_total" ,
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." ,
} , [ ] string { "name" } )
2023-02-01 17:06:36 +00:00
)
func init ( ) {
Registry . MustRegister ( leaderGauge )
leaderelection . SetProvider ( leaderelectionMetricsProvider { } )
}
type leaderelectionMetricsProvider struct { }
2024-05-13 20:57:03 +00:00
func ( leaderelectionMetricsProvider ) NewLeaderMetric ( ) leaderelection . LeaderMetric {
return leaderElectionPrometheusAdapter { }
2023-02-01 17:06:36 +00:00
}
2024-05-13 20:57:03 +00:00
type leaderElectionPrometheusAdapter struct { }
func ( s leaderElectionPrometheusAdapter ) On ( name string ) {
leaderGauge . WithLabelValues ( name ) . Set ( 1.0 )
2023-02-01 17:06:36 +00:00
}
2024-05-13 20:57:03 +00:00
func ( s leaderElectionPrometheusAdapter ) Off ( name string ) {
leaderGauge . WithLabelValues ( name ) . Set ( 0.0 )
2023-02-01 17:06:36 +00:00
}
2024-05-13 20:57:03 +00:00
func ( leaderElectionPrometheusAdapter ) SlowpathExercised ( name string ) {
leaderSlowpathCounter . WithLabelValues ( name ) . Inc ( )
2023-02-01 17:06:36 +00:00
}