1/*
2Copyright 2016 The Kubernetes Authors.
3
4Licensed under the Apache License, Version 2.0 (the "License");
5you may not use this file except in compliance with the License.
6You may obtain a copy of the License at
7
8    http://www.apache.org/licenses/LICENSE-2.0
9
10Unless required by applicable law or agreed to in writing, software
11distributed under the License is distributed on an "AS IS" BASIS,
12WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13See the License for the specific language governing permissions and
14limitations under the License.
15*/
16
17// This file provides abstractions for setting the provider (e.g., prometheus)
18// of metrics.
19
20package cache
21
22import (
23	"sync"
24)
25
26// GaugeMetric represents a single numerical value that can arbitrarily go up
27// and down.
28type GaugeMetric interface {
29	Set(float64)
30}
31
32// CounterMetric represents a single numerical value that only ever
33// goes up.
34type CounterMetric interface {
35	Inc()
36}
37
38// SummaryMetric captures individual observations.
39type SummaryMetric interface {
40	Observe(float64)
41}
42
43type noopMetric struct{}
44
45func (noopMetric) Inc()            {}
46func (noopMetric) Dec()            {}
47func (noopMetric) Observe(float64) {}
48func (noopMetric) Set(float64)     {}
49
50// MetricsProvider generates various metrics used by the reflector.
51type 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
64type noopMetricsProvider struct{}
65
66func (noopMetricsProvider) NewListsMetric(name string) CounterMetric         { return noopMetric{} }
67func (noopMetricsProvider) NewListDurationMetric(name string) SummaryMetric  { return noopMetric{} }
68func (noopMetricsProvider) NewItemsInListMetric(name string) SummaryMetric   { return noopMetric{} }
69func (noopMetricsProvider) NewWatchesMetric(name string) CounterMetric       { return noopMetric{} }
70func (noopMetricsProvider) NewShortWatchesMetric(name string) CounterMetric  { return noopMetric{} }
71func (noopMetricsProvider) NewWatchDurationMetric(name string) SummaryMetric { return noopMetric{} }
72func (noopMetricsProvider) NewItemsInWatchMetric(name string) SummaryMetric  { return noopMetric{} }
73func (noopMetricsProvider) NewLastResourceVersionMetric(name string) GaugeMetric {
74	return noopMetric{}
75}
76
77var metricsFactory = struct {
78	metricsProvider MetricsProvider
79	setProviders    sync.Once
80}{
81	metricsProvider: noopMetricsProvider{},
82}
83
84// SetReflectorMetricsProvider sets the metrics provider
85func SetReflectorMetricsProvider(metricsProvider MetricsProvider) {
86	metricsFactory.setProviders.Do(func() {
87		metricsFactory.metricsProvider = metricsProvider
88	})
89}
90