...

Source file src/k8s.io/component-base/metrics/prometheus/clientgo/leaderelection/metrics.go

Documentation: k8s.io/component-base/metrics/prometheus/clientgo/leaderelection

     1  /*
     2  Copyright 2018 The Kubernetes Authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package leaderelection
    18  
    19  import (
    20  	"k8s.io/client-go/tools/leaderelection"
    21  	k8smetrics "k8s.io/component-base/metrics"
    22  	"k8s.io/component-base/metrics/legacyregistry"
    23  )
    24  
    25  var (
    26  	leaderGauge = k8smetrics.NewGaugeVec(&k8smetrics.GaugeOpts{
    27  		Name:           "leader_election_master_status",
    28  		StabilityLevel: k8smetrics.ALPHA,
    29  		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.",
    30  	}, []string{"name"})
    31  	// A cumulative counter should be sufficient to get a rough ratio of slow path
    32  	// exercised given the leader election frequency is specified explicitly. So that
    33  	// to avoid the overhead to report a counter exercising fastpath.
    34  	leaderSlowpathCounter = k8smetrics.NewCounterVec(&k8smetrics.CounterOpts{
    35  		Name:           "leader_election_slowpath_total",
    36  		StabilityLevel: k8smetrics.ALPHA,
    37  		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.",
    38  	}, []string{"name"})
    39  )
    40  
    41  func init() {
    42  	legacyregistry.MustRegister(leaderGauge)
    43  	legacyregistry.MustRegister(leaderSlowpathCounter)
    44  	leaderelection.SetProvider(prometheusMetricsProvider{})
    45  }
    46  
    47  type prometheusMetricsProvider struct{}
    48  
    49  func (prometheusMetricsProvider) NewLeaderMetric() leaderelection.LeaderMetric {
    50  	return &leaderAdapter{gauge: leaderGauge, counter: leaderSlowpathCounter}
    51  }
    52  
    53  type leaderAdapter struct {
    54  	gauge   *k8smetrics.GaugeVec
    55  	counter *k8smetrics.CounterVec
    56  }
    57  
    58  func (s *leaderAdapter) On(name string) {
    59  	s.gauge.WithLabelValues(name).Set(1.0)
    60  }
    61  
    62  func (s *leaderAdapter) Off(name string) {
    63  	s.gauge.WithLabelValues(name).Set(0.0)
    64  }
    65  
    66  func (s *leaderAdapter) SlowpathExercised(name string) {
    67  	s.counter.WithLabelValues(name).Inc()
    68  }
    69  

View as plain text