Skip to content

Commit

Permalink
enforce metric cardinality check to counter metric.
Browse files Browse the repository at this point in the history
  • Loading branch information
YoyinZyc committed Mar 3, 2021
1 parent a44bb76 commit b81e2d1
Show file tree
Hide file tree
Showing 2 changed files with 91 additions and 1 deletion.
15 changes: 14 additions & 1 deletion staging/src/k8s.io/component-base/metrics/counter.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,13 +112,20 @@ type CounterVec struct {
func NewCounterVec(opts *CounterOpts, labels []string) *CounterVec {
opts.StabilityLevel.setDefaults()

fqName := BuildFQName(opts.Namespace, opts.Subsystem, opts.Name)
allowListLock.RLock()
if allowList, ok := labelValueAllowLists[fqName]; ok {
opts.LabelValueAllowLists = allowList
}
allowListLock.RUnlock()

cv := &CounterVec{
CounterVec: noopCounterVec,
CounterOpts: opts,
originalLabels: labels,
lazyMetric: lazyMetric{},
}
cv.lazyInit(cv, BuildFQName(opts.Namespace, opts.Subsystem, opts.Name))
cv.lazyInit(cv, fqName)
return cv
}

Expand Down Expand Up @@ -158,6 +165,9 @@ func (v *CounterVec) WithLabelValues(lvs ...string) CounterMetric {
if !v.IsCreated() {
return noop // return no-op counter
}
if v.LabelValueAllowLists != nil {
v.LabelValueAllowLists.ConstrainToAllowedList(v.originalLabels, lvs)
}
return v.CounterVec.WithLabelValues(lvs...)
}

Expand All @@ -169,6 +179,9 @@ func (v *CounterVec) With(labels map[string]string) CounterMetric {
if !v.IsCreated() {
return noop // return no-op counter
}
if v.LabelValueAllowLists != nil {
v.LabelValueAllowLists.ConstrainLabelMap(labels)
}
return v.CounterVec.With(labels)
}

Expand Down
77 changes: 77 additions & 0 deletions staging/src/k8s.io/component-base/metrics/counter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -208,3 +208,80 @@ func TestCounterVec(t *testing.T) {
})
}
}

func TestCounterWithLabelValueAllowList(t *testing.T) {
labelAllowValues := map[string]string{
"namespace_subsystem_metric_test_name,label_a": "allowed",
}
labels := []string{"label_a", "label_b"}
opts := &CounterOpts{
Namespace: "namespace",
Name: "metric_test_name",
Subsystem: "subsystem",
}
var tests = []struct {
desc string
labelValues [][]string
expectMetricValues map[string]int
}{
{
desc: "Test no unexpected input",
labelValues: [][]string{{"allowed", "b1"}, {"allowed", "b2"}},
expectMetricValues: map[string]int{
"allowed b1": 1,
"allowed b2": 1,
},
},
{
desc: "Test unexpected input",
labelValues: [][]string{{"allowed", "b1"}, {"not_allowed", "b1"}},
expectMetricValues: map[string]int{
"allowed b1": 1,
"unexpected b1": 1,
},
},
}

for _, test := range tests {
t.Run(test.desc, func(t *testing.T) {
SetLabelAllowListFromCLI(labelAllowValues)
registry := newKubeRegistry(apimachineryversion.Info{
Major: "1",
Minor: "15",
GitVersion: "v1.15.0-alpha-1.12345",
})
c := NewCounterVec(opts, labels)
registry.MustRegister(c)

for _, lv := range test.labelValues {
c.WithLabelValues(lv...).Inc()
}
mfs, err := registry.Gather()
assert.Nil(t, err, "Gather failed %v", err)

for _, mf := range mfs {
if *mf.Name != BuildFQName(opts.Namespace, opts.Subsystem, opts.Name) {
continue
}
mfMetric := mf.GetMetric()

for _, m := range mfMetric {
var aValue, bValue string
for _, l := range m.Label {
if *l.Name == "label_a" {
aValue = *l.Value
}
if *l.Name == "label_b" {
bValue = *l.Value
}
}
labelValuePair := aValue + " " + bValue
expectedValue, ok := test.expectMetricValues[labelValuePair]
assert.True(t, ok, "Got unexpected label values, lable_a is %v, label_b is %v", aValue, bValue)
actualValue := int(m.GetCounter().GetValue())
assert.Equalf(t, expectedValue, actualValue, "Got %v, wanted %v as the count while setting label_a to %v and label b to %v", actualValue, expectedValue, aValue, bValue)
}
}
})
}
}

0 comments on commit b81e2d1

Please sign in to comment.