Skip to content

Commit 3c94af7

Browse files
authored
Merge pull request kubernetes#118299 from rexagod/kep-2305
KEP-2305: introduce allow-metric-labels-manifest
2 parents ae02d1c + b8336f3 commit 3c94af7

File tree

5 files changed

+45
-5
lines changed

5 files changed

+45
-5
lines changed

staging/src/k8s.io/component-base/go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ require (
2424
go.opentelemetry.io/otel/trace v1.10.0
2525
go.uber.org/zap v1.19.0
2626
golang.org/x/sys v0.10.0
27+
gopkg.in/yaml.v2 v2.4.0
2728
k8s.io/apimachinery v0.0.0
2829
k8s.io/client-go v0.0.0
2930
k8s.io/klog/v2 v2.100.1
@@ -77,7 +78,6 @@ require (
7778
google.golang.org/grpc v1.54.0 // indirect
7879
google.golang.org/protobuf v1.31.0 // indirect
7980
gopkg.in/inf.v0 v0.9.1 // indirect
80-
gopkg.in/yaml.v2 v2.4.0 // indirect
8181
gopkg.in/yaml.v3 v3.0.1 // indirect
8282
k8s.io/api v0.0.0 // indirect
8383
k8s.io/kube-openapi v0.0.0-20230905202853-d090da108d2f // indirect

staging/src/k8s.io/component-base/metrics/metric.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ func (r *lazyMetric) Create(version *semver.Version) bool {
166166
if deprecatedV != nil {
167167
dv = deprecatedV.String()
168168
}
169-
registeredMetrics.WithLabelValues(string(sl), dv).Inc()
169+
registeredMetricsTotal.WithLabelValues(string(sl), dv).Inc()
170170
return r.IsCreated()
171171
}
172172

staging/src/k8s.io/component-base/metrics/options.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ type Options struct {
3131
ShowHiddenMetricsForVersion string
3232
DisabledMetrics []string
3333
AllowListMapping map[string]string
34+
AllowListMappingManifest string
3435
}
3536

3637
// NewOptions returns default metrics options
@@ -81,6 +82,10 @@ func (o *Options) AddFlags(fs *pflag.FlagSet) {
8182
"The map from metric-label to value allow-list of this label. The key's format is <MetricName>,<LabelName>. "+
8283
"The value's format is <allowed_value>,<allowed_value>..."+
8384
"e.g. metric1,label1='v1,v2,v3', metric1,label2='v1,v2,v3' metric2,label1='v1,v2,v3'.")
85+
fs.StringVar(&o.AllowListMappingManifest, "allow-metric-labels-manifest", o.AllowListMappingManifest,
86+
"The path to the manifest file that contains the allow-list mapping. "+
87+
"The format of the file is the same as the flag --allow-metric-labels. "+
88+
"Note that the flag --allow-metric-labels will override the manifest file.")
8489
}
8590

8691
// Apply applies parameters into global configuration of metrics.
@@ -97,6 +102,8 @@ func (o *Options) Apply() {
97102
}
98103
if o.AllowListMapping != nil {
99104
SetLabelAllowListFromCLI(o.AllowListMapping)
105+
} else if len(o.AllowListMappingManifest) > 0 {
106+
SetLabelAllowListFromManifest(o.AllowListMappingManifest)
100107
}
101108
}
102109

@@ -122,7 +129,7 @@ func validateAllowMetricLabel(allowListMapping map[string]string) error {
122129
for k := range allowListMapping {
123130
reg := regexp.MustCompile(metricNameRegex + `,` + labelRegex)
124131
if reg.FindString(k) != k {
125-
return fmt.Errorf("--allow-metric-labels must has a list of kv pair with format `metricName:labelName=labelValue, labelValue,...`")
132+
return fmt.Errorf("--allow-metric-labels must have a list of kv pair with format `metricName:labelName=labelValue, labelValue,...`")
126133
}
127134
}
128135
return nil

staging/src/k8s.io/component-base/metrics/opts.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,18 @@ package metrics
1818

1919
import (
2020
"fmt"
21+
"os"
22+
"path/filepath"
2123
"strings"
2224
"sync"
2325
"time"
2426

2527
"github.com/prometheus/client_golang/prometheus"
28+
"gopkg.in/yaml.v2"
29+
2630
"k8s.io/apimachinery/pkg/util/sets"
2731
promext "k8s.io/component-base/metrics/prometheusextension"
32+
"k8s.io/klog/v2"
2833
)
2934

3035
var (
@@ -319,6 +324,7 @@ func (allowList *MetricLabelAllowList) ConstrainToAllowedList(labelNameList, lab
319324
if allowValues, ok := allowList.labelToAllowList[name]; ok {
320325
if !allowValues.Has(value) {
321326
labelValueList[index] = "unexpected"
327+
cardinalityEnforcementUnexpectedCategorizationsTotal.Inc()
322328
}
323329
}
324330
}
@@ -329,6 +335,7 @@ func (allowList *MetricLabelAllowList) ConstrainLabelMap(labels map[string]strin
329335
if allowValues, ok := allowList.labelToAllowList[name]; ok {
330336
if !allowValues.Has(value) {
331337
labels[name] = "unexpected"
338+
cardinalityEnforcementUnexpectedCategorizationsTotal.Inc()
332339
}
333340
}
334341
}
@@ -354,3 +361,20 @@ func SetLabelAllowListFromCLI(allowListMapping map[string]string) {
354361
}
355362
}
356363
}
364+
365+
func SetLabelAllowListFromManifest(manifest string) {
366+
allowListLock.Lock()
367+
defer allowListLock.Unlock()
368+
allowListMapping := make(map[string]string)
369+
data, err := os.ReadFile(filepath.Clean(manifest))
370+
if err != nil {
371+
klog.Errorf("Failed to read allow list manifest: %v", err)
372+
return
373+
}
374+
err = yaml.Unmarshal(data, &allowListMapping)
375+
if err != nil {
376+
klog.Errorf("Failed to parse allow list manifest: %v", err)
377+
return
378+
}
379+
SetLabelAllowListFromCLI(allowListMapping)
380+
}

staging/src/k8s.io/component-base/metrics/registry.go

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ var (
3737
registriesLock sync.RWMutex
3838
disabledMetrics = map[string]struct{}{}
3939

40-
registeredMetrics = NewCounterVec(
40+
registeredMetricsTotal = NewCounterVec(
4141
&CounterOpts{
4242
Name: "registered_metrics_total",
4343
Help: "The count of registered metrics broken by stability level and deprecation version.",
@@ -61,6 +61,14 @@ var (
6161
StabilityLevel: BETA,
6262
},
6363
)
64+
65+
cardinalityEnforcementUnexpectedCategorizationsTotal = NewCounter(
66+
&CounterOpts{
67+
Name: "cardinality_enforcement_unexpected_categorizations_total",
68+
Help: "The count of unexpected categorizations during cardinality enforcement.",
69+
StabilityLevel: ALPHA,
70+
},
71+
)
6472
)
6573

6674
// shouldHide be used to check if a specific metric with deprecated version should be hidden
@@ -379,7 +387,8 @@ func NewKubeRegistry() KubeRegistry {
379387
}
380388

381389
func (r *kubeRegistry) RegisterMetaMetrics() {
382-
r.MustRegister(registeredMetrics)
390+
r.MustRegister(registeredMetricsTotal)
383391
r.MustRegister(disabledMetricsTotal)
384392
r.MustRegister(hiddenMetricsTotal)
393+
r.MustRegister(cardinalityEnforcementUnexpectedCategorizationsTotal)
385394
}

0 commit comments

Comments
 (0)