...
1
16
17 package metrics
18
19 import (
20 "context"
21
22 "github.com/blang/semver/v4"
23 "github.com/prometheus/client_golang/prometheus"
24
25 "k8s.io/component-base/version"
26 )
27
28
29
30 type Gauge struct {
31 GaugeMetric
32 *GaugeOpts
33 lazyMetric
34 selfCollector
35 }
36
37 var _ GaugeMetric = &Gauge{}
38 var _ Registerable = &Gauge{}
39 var _ kubeCollector = &Gauge{}
40
41
42
43
44 func NewGauge(opts *GaugeOpts) *Gauge {
45 opts.StabilityLevel.setDefaults()
46
47 kc := &Gauge{
48 GaugeOpts: opts,
49 lazyMetric: lazyMetric{stabilityLevel: opts.StabilityLevel},
50 }
51 kc.setPrometheusGauge(noop)
52 kc.lazyInit(kc, BuildFQName(opts.Namespace, opts.Subsystem, opts.Name))
53 return kc
54 }
55
56
57 func (g *Gauge) setPrometheusGauge(gauge prometheus.Gauge) {
58 g.GaugeMetric = gauge
59 g.initSelfCollection(gauge)
60 }
61
62
63 func (g *Gauge) DeprecatedVersion() *semver.Version {
64 return parseSemver(g.GaugeOpts.DeprecatedVersion)
65 }
66
67
68
69 func (g *Gauge) initializeMetric() {
70 g.GaugeOpts.annotateStabilityLevel()
71
72 g.setPrometheusGauge(prometheus.NewGauge(g.GaugeOpts.toPromGaugeOpts()))
73 }
74
75
76
77 func (g *Gauge) initializeDeprecatedMetric() {
78 g.GaugeOpts.markDeprecated()
79 g.initializeMetric()
80 }
81
82
83 func (g *Gauge) WithContext(ctx context.Context) GaugeMetric {
84 return g.GaugeMetric
85 }
86
87
88
89 type GaugeVec struct {
90 *prometheus.GaugeVec
91 *GaugeOpts
92 lazyMetric
93 originalLabels []string
94 }
95
96 var _ GaugeVecMetric = &GaugeVec{}
97 var _ Registerable = &GaugeVec{}
98 var _ kubeCollector = &GaugeVec{}
99
100
101
102
103
104 func NewGaugeVec(opts *GaugeOpts, labels []string) *GaugeVec {
105 opts.StabilityLevel.setDefaults()
106
107 fqName := BuildFQName(opts.Namespace, opts.Subsystem, opts.Name)
108 allowListLock.RLock()
109 if allowList, ok := labelValueAllowLists[fqName]; ok {
110 opts.LabelValueAllowLists = allowList
111 }
112 allowListLock.RUnlock()
113
114 cv := &GaugeVec{
115 GaugeVec: noopGaugeVec,
116 GaugeOpts: opts,
117 originalLabels: labels,
118 lazyMetric: lazyMetric{stabilityLevel: opts.StabilityLevel},
119 }
120 cv.lazyInit(cv, fqName)
121 return cv
122 }
123
124
125 func (v *GaugeVec) DeprecatedVersion() *semver.Version {
126 return parseSemver(v.GaugeOpts.DeprecatedVersion)
127 }
128
129
130
131 func (v *GaugeVec) initializeMetric() {
132 v.GaugeOpts.annotateStabilityLevel()
133 v.GaugeVec = prometheus.NewGaugeVec(v.GaugeOpts.toPromGaugeOpts(), v.originalLabels)
134 }
135
136
137
138 func (v *GaugeVec) initializeDeprecatedMetric() {
139 v.GaugeOpts.markDeprecated()
140 v.initializeMetric()
141 }
142
143 func (v *GaugeVec) WithLabelValuesChecked(lvs ...string) (GaugeMetric, error) {
144 if !v.IsCreated() {
145 if v.IsHidden() {
146 return noop, nil
147 }
148 return noop, errNotRegistered
149 }
150 if v.LabelValueAllowLists != nil {
151 v.LabelValueAllowLists.ConstrainToAllowedList(v.originalLabels, lvs)
152 }
153 elt, err := v.GaugeVec.GetMetricWithLabelValues(lvs...)
154 return elt, err
155 }
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172 func (v *GaugeVec) WithLabelValues(lvs ...string) GaugeMetric {
173 ans, err := v.WithLabelValuesChecked(lvs...)
174 if err == nil || ErrIsNotRegistered(err) {
175 return ans
176 }
177 panic(err)
178 }
179
180 func (v *GaugeVec) WithChecked(labels map[string]string) (GaugeMetric, error) {
181 if !v.IsCreated() {
182 if v.IsHidden() {
183 return noop, nil
184 }
185 return noop, errNotRegistered
186 }
187 if v.LabelValueAllowLists != nil {
188 v.LabelValueAllowLists.ConstrainLabelMap(labels)
189 }
190 elt, err := v.GaugeVec.GetMetricWith(labels)
191 return elt, err
192 }
193
194
195
196
197
198 func (v *GaugeVec) With(labels map[string]string) GaugeMetric {
199 ans, err := v.WithChecked(labels)
200 if err == nil || ErrIsNotRegistered(err) {
201 return ans
202 }
203 panic(err)
204 }
205
206
207
208
209
210
211
212
213 func (v *GaugeVec) Delete(labels map[string]string) bool {
214 if !v.IsCreated() {
215 return false
216 }
217 return v.GaugeVec.Delete(labels)
218 }
219
220
221 func (v *GaugeVec) Reset() {
222 if !v.IsCreated() {
223 return
224 }
225
226 v.GaugeVec.Reset()
227 }
228
229 func newGaugeFunc(opts *GaugeOpts, function func() float64, v semver.Version) GaugeFunc {
230 g := NewGauge(opts)
231
232 if !g.Create(&v) {
233 return nil
234 }
235
236 return prometheus.NewGaugeFunc(g.GaugeOpts.toPromGaugeOpts(), function)
237 }
238
239
240
241
242
243
244
245 func NewGaugeFunc(opts *GaugeOpts, function func() float64) GaugeFunc {
246 v := parseVersion(version.Get())
247
248 return newGaugeFunc(opts, function, v)
249 }
250
251
252 func (v *GaugeVec) WithContext(ctx context.Context) *GaugeVecWithContext {
253 return &GaugeVecWithContext{
254 ctx: ctx,
255 GaugeVec: v,
256 }
257 }
258
259 func (v *GaugeVec) InterfaceWithContext(ctx context.Context) GaugeVecMetric {
260 return v.WithContext(ctx)
261 }
262
263
264 type GaugeVecWithContext struct {
265 *GaugeVec
266 ctx context.Context
267 }
268
269
270 func (vc *GaugeVecWithContext) WithLabelValues(lvs ...string) GaugeMetric {
271 return vc.GaugeVec.WithLabelValues(lvs...)
272 }
273
274
275 func (vc *GaugeVecWithContext) With(labels map[string]string) GaugeMetric {
276 return vc.GaugeVec.With(labels)
277 }
278
View as plain text