Skip to content

Commit da0c289

Browse files
Fix panicing on extra labels on deprecated metrics (#110)
1 parent 5dacc78 commit da0c289

File tree

1 file changed

+19
-28
lines changed

1 file changed

+19
-28
lines changed

status/controller.go

Lines changed: 19 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ func (c *Controller[T]) reconcile(ctx context.Context, req reconcile.Request, o
148148
MetricLabelName: req.Name,
149149
})
150150
if deletionTS, ok := c.terminatingObjects.Load(req); ok {
151-
c.observeHistogram(c.TerminationDuration, TerminationDuration, time.Since(deletionTS.(*metav1.Time).Time).Seconds(), c.toAdditionalMetricLabels(o))
151+
c.observeHistogram(c.TerminationDuration, TerminationDuration, time.Since(deletionTS.(*metav1.Time).Time).Seconds(), map[string]string{}, c.toAdditionalMetricLabels(o))
152152
}
153153
if finalizers, ok := c.observedFinalizers.LoadAndDelete(req); ok {
154154
for _, finalizer := range finalizers.([]string) {
@@ -169,10 +169,10 @@ func (c *Controller[T]) reconcile(ctx context.Context, req reconcile.Request, o
169169
}
170170

171171
if o.GetDeletionTimestamp() != nil {
172-
c.setGaugeMetric(c.TerminationCurrentTimeSeconds, TerminationCurrentTimeSeconds, time.Since(o.GetDeletionTimestamp().Time).Seconds(), lo.Assign(map[string]string{
172+
c.setGaugeMetric(c.TerminationCurrentTimeSeconds, TerminationCurrentTimeSeconds, time.Since(o.GetDeletionTimestamp().Time).Seconds(), map[string]string{
173173
MetricLabelNamespace: req.Namespace,
174174
MetricLabelName: req.Name,
175-
}, c.toAdditionalMetricLabels(o)))
175+
}, c.toAdditionalMetricLabels(o))
176176
c.terminatingObjects.Store(req, o.GetDeletionTimestamp())
177177
}
178178

@@ -185,32 +185,32 @@ func (c *Controller[T]) reconcile(ctx context.Context, req reconcile.Request, o
185185
c.observedConditions.Store(req, currentConditions)
186186

187187
for _, condition := range o.GetConditions() {
188-
c.setGaugeMetric(c.ConditionCount, ConditionCount, 1, lo.Assign(map[string]string{
188+
c.setGaugeMetric(c.ConditionCount, ConditionCount, 1, map[string]string{
189189
MetricLabelNamespace: req.Namespace,
190190
MetricLabelName: req.Name,
191191
pmetrics.LabelType: condition.Type,
192192
MetricLabelConditionStatus: string(condition.Status),
193193
pmetrics.LabelReason: condition.Reason,
194-
}, c.toAdditionalMetricLabels(o)))
195-
c.setGaugeMetric(c.ConditionCurrentStatusSeconds, ConditionCurrentStatusSeconds, time.Since(condition.LastTransitionTime.Time).Seconds(), lo.Assign(map[string]string{
194+
}, c.toAdditionalMetricLabels(o))
195+
c.setGaugeMetric(c.ConditionCurrentStatusSeconds, ConditionCurrentStatusSeconds, time.Since(condition.LastTransitionTime.Time).Seconds(), map[string]string{
196196
MetricLabelNamespace: req.Namespace,
197197
MetricLabelName: req.Name,
198198
pmetrics.LabelType: condition.Type,
199199
MetricLabelConditionStatus: string(condition.Status),
200200
pmetrics.LabelReason: condition.Reason,
201-
}, c.toAdditionalMetricLabels(o)))
201+
}, c.toAdditionalMetricLabels(o))
202202
}
203203

204204
for _, observedCondition := range observedConditions.List() {
205205
if currentCondition := currentConditions.Get(observedCondition.Type); currentCondition == nil || currentCondition.Status != observedCondition.Status {
206-
c.deleteGaugeMetric(c.ConditionCount, ConditionCount, map[string]string{
206+
c.deletePartialMatchGaugeMetric(c.ConditionCount, ConditionCount, map[string]string{
207207
MetricLabelNamespace: req.Namespace,
208208
MetricLabelName: req.Name,
209209
pmetrics.LabelType: observedCondition.Type,
210210
MetricLabelConditionStatus: string(observedCondition.Status),
211211
pmetrics.LabelReason: observedCondition.Reason,
212212
})
213-
c.deleteGaugeMetric(c.ConditionCurrentStatusSeconds, ConditionCurrentStatusSeconds, map[string]string{
213+
c.deletePartialMatchGaugeMetric(c.ConditionCurrentStatusSeconds, ConditionCurrentStatusSeconds, map[string]string{
214214
MetricLabelNamespace: req.Namespace,
215215
MetricLabelName: req.Name,
216216
pmetrics.LabelType: observedCondition.Type,
@@ -241,19 +241,19 @@ func (c *Controller[T]) reconcile(ctx context.Context, req reconcile.Request, o
241241
continue
242242
}
243243
// A condition transitions if it either didn't exist before or it has changed
244-
c.incCounterMetric(c.ConditionTransitionsTotal, ConditionTransitionsTotal, lo.Assign(map[string]string{
244+
c.incCounterMetric(c.ConditionTransitionsTotal, ConditionTransitionsTotal, map[string]string{
245245
pmetrics.LabelType: condition.Type,
246246
MetricLabelConditionStatus: string(condition.Status),
247247
pmetrics.LabelReason: condition.Reason,
248-
}, c.toAdditionalMetricLabels(o)))
248+
}, c.toAdditionalMetricLabels(o))
249249
if observedCondition == nil {
250250
continue
251251
}
252252
duration := condition.LastTransitionTime.Time.Sub(observedCondition.LastTransitionTime.Time).Seconds()
253-
c.observeHistogram(c.ConditionDuration, ConditionDuration, duration, lo.Assign(map[string]string{
253+
c.observeHistogram(c.ConditionDuration, ConditionDuration, duration, map[string]string{
254254
pmetrics.LabelType: observedCondition.Type,
255255
MetricLabelConditionStatus: string(observedCondition.Status),
256-
}, c.toAdditionalMetricLabels(o)))
256+
}, c.toAdditionalMetricLabels(o))
257257
c.eventRecorder.Event(o, v1.EventTypeNormal, condition.Type, fmt.Sprintf("Status condition transitioned, Type: %s, Status: %s -> %s, Reason: %s%s",
258258
condition.Type,
259259
observedCondition.Status,
@@ -265,33 +265,24 @@ func (c *Controller[T]) reconcile(ctx context.Context, req reconcile.Request, o
265265
return reconcile.Result{RequeueAfter: time.Second * 10}, nil
266266
}
267267

268-
func (c *Controller[T]) incCounterMetric(current pmetrics.CounterMetric, deprecated pmetrics.CounterMetric, labels map[string]string) {
269-
current.Inc(labels)
268+
func (c *Controller[T]) incCounterMetric(current pmetrics.CounterMetric, deprecated pmetrics.CounterMetric, labels, additionalLabels map[string]string) {
269+
current.Inc(lo.Assign(labels, additionalLabels))
270270
if c.emitDeprecatedMetrics {
271271
labels[pmetrics.LabelKind] = c.gvk.Kind
272272
labels[pmetrics.LabelGroup] = c.gvk.Group
273273
deprecated.Inc(labels)
274274
}
275275
}
276276

277-
func (c *Controller[T]) setGaugeMetric(current pmetrics.GaugeMetric, deprecated pmetrics.GaugeMetric, value float64, labels map[string]string) {
278-
current.Set(value, labels)
277+
func (c *Controller[T]) setGaugeMetric(current pmetrics.GaugeMetric, deprecated pmetrics.GaugeMetric, value float64, labels, additionalLabels map[string]string) {
278+
current.Set(value, lo.Assign(labels, additionalLabels))
279279
if c.emitDeprecatedMetrics {
280280
labels[pmetrics.LabelKind] = c.gvk.Kind
281281
labels[pmetrics.LabelGroup] = c.gvk.Group
282282
deprecated.Set(value, labels)
283283
}
284284
}
285285

286-
func (c *Controller[T]) deleteGaugeMetric(current pmetrics.GaugeMetric, deprecated pmetrics.GaugeMetric, labels map[string]string) {
287-
current.DeletePartialMatch(labels)
288-
if c.emitDeprecatedMetrics {
289-
labels[pmetrics.LabelKind] = c.gvk.Kind
290-
labels[pmetrics.LabelGroup] = c.gvk.Group
291-
deprecated.Delete(labels)
292-
}
293-
}
294-
295286
func (c *Controller[T]) deletePartialMatchGaugeMetric(current pmetrics.GaugeMetric, deprecated pmetrics.GaugeMetric, labels map[string]string) {
296287
current.DeletePartialMatch(labels)
297288
if c.emitDeprecatedMetrics {
@@ -301,8 +292,8 @@ func (c *Controller[T]) deletePartialMatchGaugeMetric(current pmetrics.GaugeMetr
301292
}
302293
}
303294

304-
func (c *Controller[T]) observeHistogram(current pmetrics.ObservationMetric, deprecated pmetrics.ObservationMetric, value float64, labels map[string]string) {
305-
current.Observe(value, labels)
295+
func (c *Controller[T]) observeHistogram(current pmetrics.ObservationMetric, deprecated pmetrics.ObservationMetric, value float64, labels, additionalLabels map[string]string) {
296+
current.Observe(value, lo.Assign(labels, additionalLabels))
306297
if c.emitDeprecatedMetrics {
307298
labels[pmetrics.LabelKind] = c.gvk.Kind
308299
labels[pmetrics.LabelGroup] = c.gvk.Group

0 commit comments

Comments
 (0)