diff --git a/go.mod b/go.mod index ec2a8b12..3a3d5a6d 100644 --- a/go.mod +++ b/go.mod @@ -25,7 +25,7 @@ require ( go.uber.org/zap v1.19.0 golang.org/x/sys v0.1.0 k8s.io/apimachinery v0.0.0-20221103075033-9e85d3af4ae2 - k8s.io/client-go v0.0.0-20221103075600-d576a3570dbe + k8s.io/client-go v0.0.0-20221104075609-cbe28cf468c4 k8s.io/klog/v2 v2.80.1 k8s.io/utils v0.0.0-20220922133306-665eaaec4324 sigs.k8s.io/json v0.0.0-20220713155537-f223a00ba0e2 @@ -77,14 +77,14 @@ require ( gopkg.in/yaml.v2 v2.4.0 // indirect gopkg.in/yaml.v3 v3.0.1 // indirect gotest.tools/v3 v3.0.3 // indirect - k8s.io/api v0.0.0-20221103075246-5448eb39ae20 // indirect + k8s.io/api v0.0.0-20221104075248-4bad65698270 // indirect k8s.io/kube-openapi v0.0.0-20221012153701-172d655c2280 // indirect sigs.k8s.io/structured-merge-diff/v4 v4.2.3 // indirect sigs.k8s.io/yaml v1.3.0 // indirect ) replace ( - k8s.io/api => k8s.io/api v0.0.0-20221103075246-5448eb39ae20 + k8s.io/api => k8s.io/api v0.0.0-20221104075248-4bad65698270 k8s.io/apimachinery => k8s.io/apimachinery v0.0.0-20221103075033-9e85d3af4ae2 - k8s.io/client-go => k8s.io/client-go v0.0.0-20221103075600-d576a3570dbe + k8s.io/client-go => k8s.io/client-go v0.0.0-20221104075609-cbe28cf468c4 ) diff --git a/go.sum b/go.sum index ef327a2a..9380333f 100644 --- a/go.sum +++ b/go.sum @@ -661,12 +661,12 @@ honnef.co/go/tools v0.0.0-20190523083050-ea95bdfd59fc/go.mod h1:rf3lG4BRIbNafJWh honnef.co/go/tools v0.0.1-2019.2.3/go.mod h1:a3bituU0lyd329TUQxRnasdCoJDkEUEAqEt0JzvZhAg= honnef.co/go/tools v0.0.1-2020.1.3/go.mod h1:X/FiERA/W4tHapMX5mGpAtMSVEeEUOyHaw9vFzvIQ3k= honnef.co/go/tools v0.0.1-2020.1.4/go.mod h1:X/FiERA/W4tHapMX5mGpAtMSVEeEUOyHaw9vFzvIQ3k= -k8s.io/api v0.0.0-20221103075246-5448eb39ae20 h1:L4yzu0YYenWmYNm1WG/jJy/6ioJxAlgDewudcjz0uUE= -k8s.io/api v0.0.0-20221103075246-5448eb39ae20/go.mod h1:G/3aN5AoXC2S2FQkBj1R024v+pPQBOzaaOCf3qJdj+A= +k8s.io/api v0.0.0-20221104075248-4bad65698270 h1:KC6Bw3yZDigxwsAHhJ0gUZftgbCPq0vWpHWS5JxWA/E= +k8s.io/api v0.0.0-20221104075248-4bad65698270/go.mod h1:O7KYltqKbaDavU20bYboF3FKNWyQen8XmDYgb8+Kat0= k8s.io/apimachinery v0.0.0-20221103075033-9e85d3af4ae2 h1:0nhI6fiyouN4H8MXOcMcCOybGhw4FgxwQbadTKPIRlA= k8s.io/apimachinery v0.0.0-20221103075033-9e85d3af4ae2/go.mod h1:zSkBXgO5G/dSQOe256tx5Yo2OJytojpY3bsXu/4/ZJE= -k8s.io/client-go v0.0.0-20221103075600-d576a3570dbe h1:Ht6eCnwrndb2gMiPG2fW5gppS2GU0AqeadKgpqBnmWI= -k8s.io/client-go v0.0.0-20221103075600-d576a3570dbe/go.mod h1:L+YjBlIC5U+lsvpFH3jgXB9bX/n0xqA1qv0sRoDF5MM= +k8s.io/client-go v0.0.0-20221104075609-cbe28cf468c4 h1:q4puzEPtKi0taly494tfk+0+/lDjyOEbA8m5I0YW7OU= +k8s.io/client-go v0.0.0-20221104075609-cbe28cf468c4/go.mod h1:6yG/j168ypuNbupkyHQbUUxd7IOtqrZeCOHBlNOneMs= k8s.io/klog/v2 v2.80.1 h1:atnLQ121W371wYYFawwYx1aEY2eUfs4l3J72wtgAwV4= k8s.io/klog/v2 v2.80.1/go.mod h1:y1WjHnz7Dj687irZUWR/WLkLc5N1YHtjLdmgWjndZn0= k8s.io/kube-openapi v0.0.0-20221012153701-172d655c2280 h1:+70TFaan3hfJzs+7VK2o+OGxg8HsuBr/5f6tVAjDu6E= 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") + } +}