From 71e369d2417206431ddcfc0318fa50b18b0d774c Mon Sep 17 00:00:00 2001 From: Mike Spreitzer Date: Mon, 31 Oct 2022 17:12:54 -0700 Subject: [PATCH] Handle nil case correctly Also add test for that case and another. Kubernetes-commit: 114f276d68c2350e8ddfbf42188184d4f4978ba1 --- metrics/timing_histogram.go | 3 +++ metrics/timing_histogram_test.go | 43 ++++++++++++++++++++++++++++++++ 2 files changed, 46 insertions(+) diff --git a/metrics/timing_histogram.go b/metrics/timing_histogram.go index 793a049f..a0f0b253 100644 --- a/metrics/timing_histogram.go +++ b/metrics/timing_histogram.go @@ -177,6 +177,9 @@ func (v *TimingHistogramVec) WithLabelValuesChecked(lvs ...string) (GaugeMetric, v.LabelValueAllowLists.ConstrainToAllowedList(v.originalLabels, lvs) } ops, err := v.TimingHistogramVec.GetMetricWithLabelValues(lvs...) + if err != nil { + return noop, err + } return ops.(GaugeMetric), err } diff --git a/metrics/timing_histogram_test.go b/metrics/timing_histogram_test.go index 60703d9c..7ea98898 100644 --- a/metrics/timing_histogram_test.go +++ b/metrics/timing_histogram_test.go @@ -440,3 +440,46 @@ func BenchmarkTimingHistogramVecEltFetched(b *testing.B) { x = (x + i) % 60 } } + +func TestUnregisteredVec(t *testing.T) { + hv := NewTestableTimingHistogramVec(time.Now, &TimingHistogramOpts{ + Namespace: "testns", + Subsystem: "testsubsys", + Name: "testhist", + Help: "Me", + Buckets: []float64{1, 2, 4, 8, 16}, + InitialValue: 3, + }, + []string{"label1", "label2"}) + gauge, err := hv.WithLabelValuesChecked("v1", "v2") + if gauge != noop { + t.Errorf("Expected noop but got %#+v", gauge) + } + if !ErrIsNotRegistered(err) { + t.Errorf("Expected errNotRegistered but got err=%v", err) + } +} + +func TestBadValues(t *testing.T) { + hv := NewTestableTimingHistogramVec(time.Now, &TimingHistogramOpts{ + Namespace: "testns", + Subsystem: "testsubsys", + Name: "testhist", + Help: "Me", + Buckets: []float64{1, 2, 4, 8, 16}, + InitialValue: 3, + }, + []string{"label1", "label2"}) + registry := NewKubeRegistry() + registry.MustRegister(hv) + gauge, err := hv.WithLabelValuesChecked("v1") + if gauge != noop { + t.Errorf("Expected noop but got %#+v", gauge) + } + if err == nil { + t.Error("Expected an error but got nil") + } + if ErrIsNotRegistered(err) { + t.Error("Expected an error other than errNotRegistered but got that one") + } +}