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
50type 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.
64type 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
77type noopMetricsProvider struct{}
78
79func (noopMetricsProvider) NewListsMetric(name string) CounterMetric         { return noopMetric{} }
80func (noopMetricsProvider) NewListDurationMetric(name string) SummaryMetric  { return noopMetric{} }
81func (noopMetricsProvider) NewItemsInListMetric(name string) SummaryMetric   { return noopMetric{} }
82func (noopMetricsProvider) NewWatchesMetric(name string) CounterMetric       { return noopMetric{} }
83func (noopMetricsProvider) NewShortWatchesMetric(name string) CounterMetric  { return noopMetric{} }
84func (noopMetricsProvider) NewWatchDurationMetric(name string) SummaryMetric { return noopMetric{} }
85func (noopMetricsProvider) NewItemsInWatchMetric(name string) SummaryMetric  { return noopMetric{} }
86func (noopMetricsProvider) NewLastResourceVersionMetric(name string) GaugeMetric {
87	return noopMetric{}
88}
89
90var metricsFactory = struct {
91	metricsProvider MetricsProvider
92	setProviders    sync.Once
93}{
94	metricsProvider: noopMetricsProvider{},
95}
96
97// SetReflectorMetricsProvider sets the metrics provider
98func SetReflectorMetricsProvider(metricsProvider MetricsProvider) {
99	metricsFactory.setProviders.Do(func() {
100		metricsFactory.metricsProvider = metricsProvider
101	})
102}
103