Skip to content

Commit

Permalink
Handle nil case correctly
Browse files Browse the repository at this point in the history
Also add test for that case and another.

Kubernetes-commit: 114f276d68c2350e8ddfbf42188184d4f4978ba1
  • Loading branch information
MikeSpreitzer authored and k8s-publishing-bot committed Nov 1, 2022
1 parent 4e0e608 commit 71e369d
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 0 deletions.
3 changes: 3 additions & 0 deletions metrics/timing_histogram.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
43 changes: 43 additions & 0 deletions metrics/timing_histogram_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
}

0 comments on commit 71e369d

Please sign in to comment.