2 Copyright 2016 The Kubernetes Authors.
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
8 http://www.apache.org/licenses/LICENSE-2.0
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.
17 // This file provides abstractions for setting the provider (e.g., prometheus)
26 // GaugeMetric represents a single numerical value that can arbitrarily go up
28 type GaugeMetric interface {
32 // CounterMetric represents a single numerical value that only ever
34 type CounterMetric interface {
38 // SummaryMetric captures individual observations.
39 type SummaryMetric interface {
43 type noopMetric struct{}
45 func (noopMetric) Inc() {}
46 func (noopMetric) Dec() {}
47 func (noopMetric) Observe(float64) {}
48 func (noopMetric) Set(float64) {}
50 type reflectorMetrics struct {
51 numberOfLists CounterMetric
52 listDuration SummaryMetric
53 numberOfItemsInList SummaryMetric
55 numberOfWatches CounterMetric
56 numberOfShortWatches CounterMetric
57 watchDuration SummaryMetric
58 numberOfItemsInWatch SummaryMetric
60 lastResourceVersion GaugeMetric
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
69 NewWatchesMetric(name string) CounterMetric
70 NewShortWatchesMetric(name string) CounterMetric
71 NewWatchDurationMetric(name string) SummaryMetric
72 NewItemsInWatchMetric(name string) SummaryMetric
74 NewLastResourceVersionMetric(name string) GaugeMetric
77 type noopMetricsProvider struct{}
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 {
90 var metricsFactory = struct {
91 metricsProvider MetricsProvider
92 setProviders sync.Once
94 metricsProvider: noopMetricsProvider{},
97 func newReflectorMetrics(name string) *reflectorMetrics {
98 var ret *reflectorMetrics
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),
114 // SetReflectorMetricsProvider sets the metrics provider
115 func SetReflectorMetricsProvider(metricsProvider MetricsProvider) {
116 metricsFactory.setProviders.Do(func() {
117 metricsFactory.metricsProvider = metricsProvider