...

Source file src/k8s.io/client-go/tools/cache/reflector_metrics.go

Documentation: k8s.io/client-go/tools/cache

     1  /*
     2  Copyright 2016 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  // This file provides abstractions for setting the provider (e.g., prometheus)
    18  // of metrics.
    19  
    20  package cache
    21  
    22  import (
    23  	"sync"
    24  )
    25  
    26  // GaugeMetric represents a single numerical value that can arbitrarily go up
    27  // and down.
    28  type GaugeMetric interface {
    29  	Set(float64)
    30  }
    31  
    32  // CounterMetric represents a single numerical value that only ever
    33  // goes up.
    34  type CounterMetric interface {
    35  	Inc()
    36  }
    37  
    38  // SummaryMetric captures individual observations.
    39  type SummaryMetric interface {
    40  	Observe(float64)
    41  }
    42  
    43  type noopMetric struct{}
    44  
    45  func (noopMetric) Inc()            {}
    46  func (noopMetric) Dec()            {}
    47  func (noopMetric) Observe(float64) {}
    48  func (noopMetric) Set(float64)     {}
    49  
    50  // MetricsProvider generates various metrics used by the reflector.
    51  type MetricsProvider interface {
    52  	NewListsMetric(name string) CounterMetric
    53  	NewListDurationMetric(name string) SummaryMetric
    54  	NewItemsInListMetric(name string) SummaryMetric
    55  
    56  	NewWatchesMetric(name string) CounterMetric
    57  	NewShortWatchesMetric(name string) CounterMetric
    58  	NewWatchDurationMetric(name string) SummaryMetric
    59  	NewItemsInWatchMetric(name string) SummaryMetric
    60  
    61  	NewLastResourceVersionMetric(name string) GaugeMetric
    62  }
    63  
    64  type noopMetricsProvider struct{}
    65  
    66  func (noopMetricsProvider) NewListsMetric(name string) CounterMetric         { return noopMetric{} }
    67  func (noopMetricsProvider) NewListDurationMetric(name string) SummaryMetric  { return noopMetric{} }
    68  func (noopMetricsProvider) NewItemsInListMetric(name string) SummaryMetric   { return noopMetric{} }
    69  func (noopMetricsProvider) NewWatchesMetric(name string) CounterMetric       { return noopMetric{} }
    70  func (noopMetricsProvider) NewShortWatchesMetric(name string) CounterMetric  { return noopMetric{} }
    71  func (noopMetricsProvider) NewWatchDurationMetric(name string) SummaryMetric { return noopMetric{} }
    72  func (noopMetricsProvider) NewItemsInWatchMetric(name string) SummaryMetric  { return noopMetric{} }
    73  func (noopMetricsProvider) NewLastResourceVersionMetric(name string) GaugeMetric {
    74  	return noopMetric{}
    75  }
    76  
    77  var metricsFactory = struct {
    78  	metricsProvider MetricsProvider
    79  	setProviders    sync.Once
    80  }{
    81  	metricsProvider: noopMetricsProvider{},
    82  }
    83  
    84  // SetReflectorMetricsProvider sets the metrics provider
    85  func SetReflectorMetricsProvider(metricsProvider MetricsProvider) {
    86  	metricsFactory.setProviders.Do(func() {
    87  		metricsFactory.metricsProvider = metricsProvider
    88  	})
    89  }
    90  

View as plain text