github.com/looshlee/beatles@v0.0.0-20220727174639-742810ab631c/pkg/metrics/interfaces.go (about) 1 // Copyright 2019 Authors of Cilium 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package metrics 16 17 import ( 18 "github.com/prometheus/client_golang/prometheus" 19 dto "github.com/prometheus/client_model/go" 20 ) 21 22 type CounterVec interface { 23 WithLabelValues(lvls ...string) prometheus.Counter 24 GetMetricWithLabelValues(lvs ...string) (prometheus.Counter, error) 25 With(labels prometheus.Labels) prometheus.Counter 26 prometheus.Collector 27 } 28 29 type GaugeVec interface { 30 WithLabelValues(lvls ...string) prometheus.Gauge 31 prometheus.Collector 32 } 33 34 var ( 35 NoOpMetric prometheus.Metric = &metric{} 36 NoOpCollector prometheus.Collector = &collector{} 37 38 NoOpCounter prometheus.Counter = &counter{NoOpMetric, NoOpCollector} 39 NoOpCounterVec CounterVec = &counterVec{NoOpCollector} 40 NoOpObserver prometheus.Observer = &observer{} 41 NoOpObserverVec prometheus.ObserverVec = &observerVec{NoOpCollector} 42 NoOpGauge prometheus.Gauge = &gauge{NoOpMetric, NoOpCollector} 43 NoOpGaugeVec GaugeVec = &gaugeVec{NoOpCollector} 44 ) 45 46 // Metric 47 48 type metric struct{} 49 50 // *WARNING*: Desc returns nil so do not register this metric into prometheus 51 // default register. 52 func (m *metric) Desc() *prometheus.Desc { return nil } 53 func (m *metric) Write(*dto.Metric) error { return nil } 54 55 // Collector 56 57 type collector struct{} 58 59 func (c *collector) Describe(chan<- *prometheus.Desc) {} 60 func (c *collector) Collect(chan<- prometheus.Metric) {} 61 62 // Counter 63 64 type counter struct { 65 prometheus.Metric 66 prometheus.Collector 67 } 68 69 func (cv *counter) Add(float64) {} 70 func (cv *counter) Inc() {} 71 72 // CounterVec 73 74 type counterVec struct{ prometheus.Collector } 75 76 func (cv *counterVec) WithLabelValues(lvls ...string) prometheus.Counter { return NoOpCounter } 77 78 func (cv *counterVec) GetMetricWithLabelValues(lvs ...string) (prometheus.Counter, error) { 79 return NoOpCounter, nil 80 } 81 82 func (cv *counterVec) With(labels prometheus.Labels) prometheus.Counter { return NoOpCounter } 83 84 // Observer 85 86 type observer struct{} 87 88 func (o *observer) Observe(float64) {} 89 90 // ObserverVec 91 92 type observerVec struct { 93 prometheus.Collector 94 } 95 96 func (ov *observerVec) GetMetricWith(prometheus.Labels) (prometheus.Observer, error) { 97 return NoOpObserver, nil 98 } 99 func (ov *observerVec) GetMetricWithLabelValues(lvs ...string) (prometheus.Observer, error) { 100 return NoOpObserver, nil 101 } 102 103 func (ov *observerVec) With(prometheus.Labels) prometheus.Observer { return NoOpObserver } 104 func (ov *observerVec) WithLabelValues(...string) prometheus.Observer { return NoOpObserver } 105 106 func (ov *observerVec) CurryWith(prometheus.Labels) (prometheus.ObserverVec, error) { 107 return NoOpObserverVec, nil 108 } 109 func (ov *observerVec) MustCurryWith(prometheus.Labels) prometheus.ObserverVec { 110 return NoOpObserverVec 111 } 112 113 // Gauge 114 115 type gauge struct { 116 prometheus.Metric 117 prometheus.Collector 118 } 119 120 func (g *gauge) Set(float64) {} 121 func (g *gauge) Inc() {} 122 func (g *gauge) Dec() {} 123 func (g *gauge) Add(float64) {} 124 func (g *gauge) Sub(float64) {} 125 func (g *gauge) SetToCurrentTime() {} 126 127 // GaugeVec 128 129 type gaugeVec struct { 130 prometheus.Collector 131 } 132 133 func (gv *gaugeVec) WithLabelValues(lvls ...string) prometheus.Gauge { 134 return NoOpGauge 135 }