...

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

Documentation: k8s.io/component-base/metrics/prometheus/controllers

     1  /*
     2  Copyright 2021 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 controllers
    18  
    19  import (
    20  	"sync"
    21  
    22  	k8smetrics "k8s.io/component-base/metrics"
    23  	"k8s.io/component-base/metrics/legacyregistry"
    24  )
    25  
    26  var (
    27  	once                    sync.Once
    28  	controllerInstanceCount = k8smetrics.NewGaugeVec(
    29  		&k8smetrics.GaugeOpts{
    30  			Name:           "running_managed_controllers",
    31  			Help:           "Indicates where instances of a controller are currently running",
    32  			StabilityLevel: k8smetrics.ALPHA,
    33  		},
    34  		[]string{"name", "manager"},
    35  	)
    36  )
    37  
    38  // ControllerManagerMetrics is a proxy to set controller manager specific metrics.
    39  type ControllerManagerMetrics struct {
    40  	manager string
    41  }
    42  
    43  // NewControllerManagerMetrics create a new ControllerManagerMetrics, with specific manager name.
    44  func NewControllerManagerMetrics(manager string) *ControllerManagerMetrics {
    45  	controllerMetrics := &ControllerManagerMetrics{
    46  		manager: manager,
    47  	}
    48  	return controllerMetrics
    49  }
    50  
    51  // Register controller manager metrics.
    52  func Register() {
    53  	once.Do(func() {
    54  		legacyregistry.MustRegister(controllerInstanceCount)
    55  	})
    56  }
    57  
    58  // ControllerStarted sets the controllerInstanceCount to 1.
    59  // These values use set instead of inc/dec to avoid accidentally double counting
    60  // a controller that starts but fails to properly signal when it crashes.
    61  func (a *ControllerManagerMetrics) ControllerStarted(name string) {
    62  	controllerInstanceCount.With(k8smetrics.Labels{"name": name, "manager": a.manager}).Set(float64(1))
    63  }
    64  
    65  // ControllerStopped sets the controllerInstanceCount to 0.
    66  func (a *ControllerManagerMetrics) ControllerStopped(name string) {
    67  	controllerInstanceCount.With(k8smetrics.Labels{"name": name, "manager": a.manager}).Set(float64(0))
    68  }
    69  

View as plain text