...

Source file src/k8s.io/kubernetes/pkg/controller/podautoscaler/monitor/monitor.go

Documentation: k8s.io/kubernetes/pkg/controller/podautoscaler/monitor

     1  /*
     2  Copyright 2023 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 monitor
    18  
    19  import (
    20  	"time"
    21  
    22  	v2 "k8s.io/api/autoscaling/v2"
    23  )
    24  
    25  type ActionLabel string
    26  type ErrorLabel string
    27  
    28  const (
    29  	ActionLabelScaleUp   ActionLabel = "scale_up"
    30  	ActionLabelScaleDown ActionLabel = "scale_down"
    31  	ActionLabelNone      ActionLabel = "none"
    32  
    33  	// ErrorLabelSpec represents an error due to an invalid spec of HPA object.
    34  	ErrorLabelSpec ErrorLabel = "spec"
    35  	// ErrorLabelInternal represents an error from an internal computation or communication with other component.
    36  	ErrorLabelInternal ErrorLabel = "internal"
    37  	ErrorLabelNone     ErrorLabel = "none"
    38  )
    39  
    40  // Monitor records some metrics so that people can monitor HPA controller.
    41  type Monitor interface {
    42  	ObserveReconciliationResult(action ActionLabel, err ErrorLabel, duration time.Duration)
    43  	ObserveMetricComputationResult(action ActionLabel, err ErrorLabel, duration time.Duration, metricType v2.MetricSourceType)
    44  }
    45  
    46  type monitor struct{}
    47  
    48  func New() Monitor {
    49  	return &monitor{}
    50  }
    51  
    52  // ObserveReconciliationResult observes some metrics from a reconciliation result.
    53  func (r *monitor) ObserveReconciliationResult(action ActionLabel, err ErrorLabel, duration time.Duration) {
    54  	reconciliationsTotal.WithLabelValues(string(action), string(err)).Inc()
    55  	reconciliationsDuration.WithLabelValues(string(action), string(err)).Observe(duration.Seconds())
    56  }
    57  
    58  // ObserveMetricComputationResult observes some metrics from a metric computation result.
    59  func (r *monitor) ObserveMetricComputationResult(action ActionLabel, err ErrorLabel, duration time.Duration, metricType v2.MetricSourceType) {
    60  	metricComputationTotal.WithLabelValues(string(action), string(err), string(metricType)).Inc()
    61  	metricComputationDuration.WithLabelValues(string(action), string(err), string(metricType)).Observe(duration.Seconds())
    62  }
    63  

View as plain text