Remove BPA from Makefile
[icn.git] / cmd / bpa-operator / vendor / k8s.io / client-go / tools / cache / reflector_metrics.go
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 type reflectorMetrics struct {
51         numberOfLists       CounterMetric
52         listDuration        SummaryMetric
53         numberOfItemsInList SummaryMetric
54
55         numberOfWatches      CounterMetric
56         numberOfShortWatches CounterMetric
57         watchDuration        SummaryMetric
58         numberOfItemsInWatch SummaryMetric
59
60         lastResourceVersion GaugeMetric
61 }
62
63 // MetricsProvider generates various metrics used by the reflector.
64 type MetricsProvider interface {
65         NewListsMetric(name string) CounterMetric
66         NewListDurationMetric(name string) SummaryMetric
67         NewItemsInListMetric(name string) SummaryMetric
68
69         NewWatchesMetric(name string) CounterMetric
70         NewShortWatchesMetric(name string) CounterMetric
71         NewWatchDurationMetric(name string) SummaryMetric
72         NewItemsInWatchMetric(name string) SummaryMetric
73
74         NewLastResourceVersionMetric(name string) GaugeMetric
75 }
76
77 type noopMetricsProvider struct{}
78
79 func (noopMetricsProvider) NewListsMetric(name string) CounterMetric         { return noopMetric{} }
80 func (noopMetricsProvider) NewListDurationMetric(name string) SummaryMetric  { return noopMetric{} }
81 func (noopMetricsProvider) NewItemsInListMetric(name string) SummaryMetric   { return noopMetric{} }
82 func (noopMetricsProvider) NewWatchesMetric(name string) CounterMetric       { return noopMetric{} }
83 func (noopMetricsProvider) NewShortWatchesMetric(name string) CounterMetric  { return noopMetric{} }
84 func (noopMetricsProvider) NewWatchDurationMetric(name string) SummaryMetric { return noopMetric{} }
85 func (noopMetricsProvider) NewItemsInWatchMetric(name string) SummaryMetric  { return noopMetric{} }
86 func (noopMetricsProvider) NewLastResourceVersionMetric(name string) GaugeMetric {
87         return noopMetric{}
88 }
89
90 var metricsFactory = struct {
91         metricsProvider MetricsProvider
92         setProviders    sync.Once
93 }{
94         metricsProvider: noopMetricsProvider{},
95 }
96
97 func newReflectorMetrics(name string) *reflectorMetrics {
98         var ret *reflectorMetrics
99         if len(name) == 0 {
100                 return ret
101         }
102         return &reflectorMetrics{
103                 numberOfLists:        metricsFactory.metricsProvider.NewListsMetric(name),
104                 listDuration:         metricsFactory.metricsProvider.NewListDurationMetric(name),
105                 numberOfItemsInList:  metricsFactory.metricsProvider.NewItemsInListMetric(name),
106                 numberOfWatches:      metricsFactory.metricsProvider.NewWatchesMetric(name),
107                 numberOfShortWatches: metricsFactory.metricsProvider.NewShortWatchesMetric(name),
108                 watchDuration:        metricsFactory.metricsProvider.NewWatchDurationMetric(name),
109                 numberOfItemsInWatch: metricsFactory.metricsProvider.NewItemsInWatchMetric(name),
110                 lastResourceVersion:  metricsFactory.metricsProvider.NewLastResourceVersionMetric(name),
111         }
112 }
113
114 // SetReflectorMetricsProvider sets the metrics provider
115 func SetReflectorMetricsProvider(metricsProvider MetricsProvider) {
116         metricsFactory.setProviders.Do(func() {
117                 metricsFactory.metricsProvider = metricsProvider
118         })
119 }