|
| 1 | +/* |
| 2 | +Copyright The Kubernetes Authors. |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +package metrics |
| 18 | + |
| 19 | +import ( |
| 20 | + "sync" |
| 21 | + |
| 22 | + . "github.com/onsi/ginkgo/v2" |
| 23 | + . "github.com/onsi/gomega" |
| 24 | + "github.com/prometheus/client_golang/prometheus" |
| 25 | + "k8s.io/apimachinery/pkg/util/sets" |
| 26 | +) |
| 27 | + |
| 28 | +type fakeGauge struct { |
| 29 | + prometheus.Gauge |
| 30 | + inc func() |
| 31 | +} |
| 32 | + |
| 33 | +func (f *fakeGauge) Inc() { f.inc() } |
| 34 | + |
| 35 | +type fakeGaugeVec struct { |
| 36 | + gauges map[string]int |
| 37 | + mu sync.Mutex |
| 38 | +} |
| 39 | + |
| 40 | +func (f *fakeGaugeVec) WithLabelValues(lvs ...string) prometheus.Gauge { |
| 41 | + f.mu.Lock() |
| 42 | + defer f.mu.Unlock() |
| 43 | + |
| 44 | + key := "" |
| 45 | + for _, lv := range lvs { |
| 46 | + key += lv + "|" |
| 47 | + } |
| 48 | + if _, ok := f.gauges[key]; !ok { |
| 49 | + f.gauges[key] = 0 |
| 50 | + } |
| 51 | + return &fakeGauge{inc: func() { |
| 52 | + f.mu.Lock() |
| 53 | + f.gauges[key]++ |
| 54 | + f.mu.Unlock() |
| 55 | + }} |
| 56 | +} |
| 57 | + |
| 58 | +var _ = Describe("depthWithPriorityMetric", func() { |
| 59 | + Describe("CardinalityLimit", func() { |
| 60 | + type testCase struct { |
| 61 | + numUniquePriorities int |
| 62 | + expectedKeyCount int |
| 63 | + expectedCardinalityExceededVal int |
| 64 | + } |
| 65 | + |
| 66 | + DescribeTable("should respect cardinality limits", |
| 67 | + func(tc testCase) { |
| 68 | + fakeVec := &fakeGaugeVec{gauges: make(map[string]int)} |
| 69 | + m := &depthWithPriorityMetric{ |
| 70 | + depth: fakeVec, |
| 71 | + lvs: []string{"test", "test"}, |
| 72 | + observedPriorities: sets.Set[int]{}, |
| 73 | + } |
| 74 | + |
| 75 | + wg := &sync.WaitGroup{} |
| 76 | + wg.Add(tc.numUniquePriorities) |
| 77 | + for i := 1; i <= tc.numUniquePriorities; i++ { |
| 78 | + go func() { |
| 79 | + m.Inc(i) |
| 80 | + wg.Done() |
| 81 | + }() |
| 82 | + } |
| 83 | + wg.Wait() |
| 84 | + |
| 85 | + Expect(fakeVec.gauges).To(HaveLen(tc.expectedKeyCount)) |
| 86 | + |
| 87 | + placeholderKey := "test|test|" + priorityCardinalityExceededPlaceholder + "|" |
| 88 | + Expect(fakeVec.gauges[placeholderKey]).To(Equal(tc.expectedCardinalityExceededVal)) |
| 89 | + }, |
| 90 | + Entry("under limit does not use placeholder", testCase{ |
| 91 | + numUniquePriorities: 10, |
| 92 | + expectedKeyCount: 10, |
| 93 | + expectedCardinalityExceededVal: 0, |
| 94 | + }), |
| 95 | + Entry("at limit does not use placeholder", testCase{ |
| 96 | + numUniquePriorities: 25, |
| 97 | + expectedKeyCount: 25, |
| 98 | + expectedCardinalityExceededVal: 0, |
| 99 | + }), |
| 100 | + Entry("exceeding limit uses placeholder", testCase{ |
| 101 | + numUniquePriorities: 26, |
| 102 | + expectedKeyCount: 26, |
| 103 | + expectedCardinalityExceededVal: 1, |
| 104 | + }), |
| 105 | + Entry("well over limit uses placeholder for all excess", testCase{ |
| 106 | + numUniquePriorities: 30, |
| 107 | + expectedKeyCount: 26, |
| 108 | + expectedCardinalityExceededVal: 5, |
| 109 | + }), |
| 110 | + ) |
| 111 | + }) |
| 112 | + |
| 113 | + It("same priority many adds does not trigger cardinality limit", func() { |
| 114 | + fakeVec := &fakeGaugeVec{gauges: make(map[string]int)} |
| 115 | + m := &depthWithPriorityMetric{ |
| 116 | + depth: fakeVec, |
| 117 | + lvs: []string{"test", "test"}, |
| 118 | + observedPriorities: sets.Set[int]{}, |
| 119 | + } |
| 120 | + |
| 121 | + wg := &sync.WaitGroup{} |
| 122 | + wg.Add(200) |
| 123 | + for range 200 { |
| 124 | + go func() { |
| 125 | + m.Inc(1) |
| 126 | + wg.Done() |
| 127 | + }() |
| 128 | + } |
| 129 | + wg.Wait() |
| 130 | + |
| 131 | + Expect(fakeVec.gauges).To(HaveLen(1)) |
| 132 | + Expect(fakeVec.gauges["test|test|1|"]).To(Equal(200)) |
| 133 | + }) |
| 134 | +}) |
0 commit comments