...

Source file src/k8s.io/kube-aggregator/pkg/controllers/status/metrics.go

Documentation: k8s.io/kube-aggregator/pkg/controllers/status

     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 apiserver
    18  
    19  import (
    20  	"sync"
    21  
    22  	"k8s.io/component-base/metrics"
    23  )
    24  
    25  /*
    26   * By default, all the following metrics are defined as falling under
    27   * ALPHA stability level https://github.com/kubernetes/enhancements/blob/master/keps/sig-instrumentation/1209-metrics-stability/kubernetes-control-plane-metrics-stability.md#stability-classes)
    28   *
    29   * Promoting the stability level of the metric is a responsibility of the component owner, since it
    30   * involves explicitly acknowledging support for the metric across multiple releases, in accordance with
    31   * the metric stability policy.
    32   */
    33  var (
    34  	unavailableGaugeDesc = metrics.NewDesc(
    35  		"aggregator_unavailable_apiservice",
    36  		"Gauge of APIServices which are marked as unavailable broken down by APIService name.",
    37  		[]string{"name"},
    38  		nil,
    39  		metrics.ALPHA,
    40  		"",
    41  	)
    42  )
    43  
    44  type availabilityMetrics struct {
    45  	unavailableCounter *metrics.CounterVec
    46  
    47  	*availabilityCollector
    48  }
    49  
    50  func newAvailabilityMetrics() *availabilityMetrics {
    51  	return &availabilityMetrics{
    52  		unavailableCounter: metrics.NewCounterVec(
    53  			&metrics.CounterOpts{
    54  				Name:           "aggregator_unavailable_apiservice_total",
    55  				Help:           "Counter of APIServices which are marked as unavailable broken down by APIService name and reason.",
    56  				StabilityLevel: metrics.ALPHA,
    57  			},
    58  			[]string{"name", "reason"},
    59  		),
    60  		availabilityCollector: newAvailabilityCollector(),
    61  	}
    62  }
    63  
    64  // Register registers apiservice availability metrics.
    65  func (m *availabilityMetrics) Register(
    66  	registrationFunc func(metrics.Registerable) error,
    67  	customRegistrationFunc func(metrics.StableCollector) error,
    68  ) error {
    69  	err := registrationFunc(m.unavailableCounter)
    70  	if err != nil {
    71  		return err
    72  	}
    73  
    74  	err = customRegistrationFunc(m.availabilityCollector)
    75  	if err != nil {
    76  		return err
    77  	}
    78  
    79  	return nil
    80  }
    81  
    82  // UnavailableCounter returns a counter to track apiservices marked as unavailable.
    83  func (m *availabilityMetrics) UnavailableCounter(apiServiceName, reason string) metrics.CounterMetric {
    84  	return m.unavailableCounter.WithLabelValues(apiServiceName, reason)
    85  }
    86  
    87  type availabilityCollector struct {
    88  	metrics.BaseStableCollector
    89  
    90  	mtx            sync.RWMutex
    91  	availabilities map[string]bool
    92  }
    93  
    94  // Check if apiServiceStatusCollector implements necessary interface.
    95  var _ metrics.StableCollector = &availabilityCollector{}
    96  
    97  func newAvailabilityCollector() *availabilityCollector {
    98  	return &availabilityCollector{
    99  		availabilities: make(map[string]bool),
   100  	}
   101  }
   102  
   103  // DescribeWithStability implements the metrics.StableCollector interface.
   104  func (c *availabilityCollector) DescribeWithStability(ch chan<- *metrics.Desc) {
   105  	ch <- unavailableGaugeDesc
   106  }
   107  
   108  // CollectWithStability implements the metrics.StableCollector interface.
   109  func (c *availabilityCollector) CollectWithStability(ch chan<- metrics.Metric) {
   110  	c.mtx.RLock()
   111  	defer c.mtx.RUnlock()
   112  
   113  	for apiServiceName, isAvailable := range c.availabilities {
   114  		gaugeValue := 1.0
   115  		if isAvailable {
   116  			gaugeValue = 0.0
   117  		}
   118  		ch <- metrics.NewLazyConstMetric(
   119  			unavailableGaugeDesc,
   120  			metrics.GaugeValue,
   121  			gaugeValue,
   122  			apiServiceName,
   123  		)
   124  	}
   125  }
   126  
   127  // SetAPIServiceAvailable sets the given apiservice availability gauge to available.
   128  func (c *availabilityCollector) SetAPIServiceAvailable(apiServiceKey string) {
   129  	c.setAPIServiceAvailability(apiServiceKey, true)
   130  }
   131  
   132  // SetAPIServiceUnavailable sets the given apiservice availability gauge to unavailable.
   133  func (c *availabilityCollector) SetAPIServiceUnavailable(apiServiceKey string) {
   134  	c.setAPIServiceAvailability(apiServiceKey, false)
   135  }
   136  
   137  func (c *availabilityCollector) setAPIServiceAvailability(apiServiceKey string, availability bool) {
   138  	c.mtx.Lock()
   139  	defer c.mtx.Unlock()
   140  
   141  	c.availabilities[apiServiceKey] = availability
   142  }
   143  
   144  // ForgetAPIService removes the availability gauge of the given apiservice.
   145  func (c *availabilityCollector) ForgetAPIService(apiServiceKey string) {
   146  	c.mtx.Lock()
   147  	defer c.mtx.Unlock()
   148  
   149  	delete(c.availabilities, apiServiceKey)
   150  }
   151  

View as plain text