Skip to content

Commit

Permalink
Merge pull request #113494 from MikeSpreitzer/fix-nil-cast
Browse files Browse the repository at this point in the history
Handle nil case correctly in TimingHistogramVec::WithLabelValuesChecked

Kubernetes-commit: db1ee6091f3b98dc99974dfbb7c756ba23724610
  • Loading branch information
k8s-publishing-bot committed Nov 4, 2022
2 parents 4e0e608 + 71e369d commit 23eee02
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 8 deletions.
8 changes: 4 additions & 4 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
)
8 changes: 4 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down
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 23eee02

Please sign in to comment.