...

Source file src/k8s.io/apiextensions-apiserver/pkg/apiserver/validation/metrics.go

Documentation: k8s.io/apiextensions-apiserver/pkg/apiserver/validation

     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 validation
    18  
    19  import (
    20  	"time"
    21  
    22  	"k8s.io/component-base/metrics"
    23  	"k8s.io/component-base/metrics/legacyregistry"
    24  )
    25  
    26  const (
    27  	namespace = "apiextensions_apiserver"
    28  	subsystem = "validation"
    29  )
    30  
    31  // Interface to stub for tests
    32  type ValidationMetrics interface {
    33  	ObserveRatchetingTime(d time.Duration)
    34  }
    35  
    36  var Metrics ValidationMetrics = &validationMetrics{
    37  	RatchetingTime: metrics.NewHistogram(&metrics.HistogramOpts{
    38  		Namespace:      namespace,
    39  		Subsystem:      subsystem,
    40  		Name:           "ratcheting_seconds",
    41  		Help:           "Time for comparison of old to new for the purposes of CRDValidationRatcheting during an UPDATE in seconds.",
    42  		StabilityLevel: metrics.ALPHA,
    43  		// Start 0.01ms with the last bucket being [~2.5s, +Inf)
    44  		Buckets: metrics.ExponentialBuckets(0.00001, 4, 10),
    45  	}),
    46  }
    47  
    48  func init() {
    49  	legacyregistry.MustRegister(Metrics.(*validationMetrics).RatchetingTime)
    50  }
    51  
    52  type validationMetrics struct {
    53  	RatchetingTime *metrics.Histogram
    54  }
    55  
    56  // ObserveRatchetingTime records the time spent on ratcheting
    57  func (m *validationMetrics) ObserveRatchetingTime(d time.Duration) {
    58  	m.RatchetingTime.Observe(d.Seconds())
    59  }
    60  
    61  // Reset resets the metrics. This is meant to be used for testing. Panics
    62  // if the metrics cannot be re-registered. Returns all the reset metrics
    63  func (m *validationMetrics) Reset() []metrics.Registerable {
    64  	m.RatchetingTime = metrics.NewHistogram(m.RatchetingTime.HistogramOpts)
    65  	return []metrics.Registerable{m.RatchetingTime}
    66  }
    67  

View as plain text