Skip to content

Commit 7a5d1de

Browse files
committed
chore: Reduce code duplication for metadata metric families
1 parent 2e8c3bf commit 7a5d1de

File tree

3 files changed

+165
-210
lines changed

3 files changed

+165
-210
lines changed

internal/store/horizontalpodautoscaler.go

Lines changed: 4 additions & 106 deletions
Original file line numberDiff line numberDiff line change
@@ -44,17 +44,15 @@ func (m metricTargetType) String() string {
4444
}
4545

4646
var (
47-
descHorizontalPodAutoscalerAnnotationsName = "kube_horizontalpodautoscaler_annotations"
48-
descHorizontalPodAutoscalerAnnotationsHelp = "Kubernetes annotations converted to Prometheus labels."
49-
descHorizontalPodAutoscalerLabelsName = "kube_horizontalpodautoscaler_labels"
50-
descHorizontalPodAutoscalerLabelsHelp = "Kubernetes labels converted to Prometheus labels."
5147
descHorizontalPodAutoscalerLabelsDefaultLabels = []string{"namespace", "horizontalpodautoscaler"}
5248

5349
targetMetricLabels = []string{"metric_name", "metric_target_type"}
5450
)
5551

5652
func hpaMetricFamilies(allowAnnotationsList, allowLabelsList []string) []generator.FamilyGenerator {
57-
return []generator.FamilyGenerator{
53+
metadataFamilies := createMetadataMetricFamiliesGenerator(allowAnnotationsList, allowLabelsList, descHorizontalPodAutoscalerLabelsDefaultLabels, "kube_horizontalpodautoscaler", false)
54+
55+
return append(metadataFamilies,
5856
createHPAInfo(),
5957
createHPAMetaDataGeneration(),
6058
createHPASpecMaxReplicas(),
@@ -63,12 +61,8 @@ func hpaMetricFamilies(allowAnnotationsList, allowLabelsList []string) []generat
6361
createHPAStatusTargetMetric(),
6462
createHPAStatusCurrentReplicas(),
6563
createHPAStatusDesiredReplicas(),
66-
createHPAAnnotations(allowAnnotationsList),
67-
createHPALabels(allowLabelsList),
6864
createHPAStatusCondition(),
69-
createHPACreated(),
70-
createHPADeletionTimestamp(),
71-
}
65+
)
7266
}
7367

7468
func wrapHPAFunc(f func(*autoscaling.HorizontalPodAutoscaler) *metric.Family) func(interface{}) *metric.Family {
@@ -338,56 +332,6 @@ func createHPAStatusDesiredReplicas() generator.FamilyGenerator {
338332
)
339333
}
340334

341-
func createHPAAnnotations(allowAnnotationsList []string) generator.FamilyGenerator {
342-
return *generator.NewFamilyGeneratorWithStability(
343-
descHorizontalPodAutoscalerAnnotationsName,
344-
descHorizontalPodAutoscalerAnnotationsHelp,
345-
metric.Gauge,
346-
basemetrics.ALPHA,
347-
"",
348-
wrapHPAFunc(func(a *autoscaling.HorizontalPodAutoscaler) *metric.Family {
349-
if len(allowAnnotationsList) == 0 {
350-
return &metric.Family{}
351-
}
352-
annotationKeys, annotationValues := createPrometheusLabelKeysValues("annotation", a.Annotations, allowAnnotationsList)
353-
return &metric.Family{
354-
Metrics: []*metric.Metric{
355-
{
356-
LabelKeys: annotationKeys,
357-
LabelValues: annotationValues,
358-
Value: 1,
359-
},
360-
},
361-
}
362-
}),
363-
)
364-
}
365-
366-
func createHPALabels(allowLabelsList []string) generator.FamilyGenerator {
367-
return *generator.NewFamilyGeneratorWithStability(
368-
descHorizontalPodAutoscalerLabelsName,
369-
descHorizontalPodAutoscalerLabelsHelp,
370-
metric.Gauge,
371-
basemetrics.STABLE,
372-
"",
373-
wrapHPAFunc(func(a *autoscaling.HorizontalPodAutoscaler) *metric.Family {
374-
if len(allowLabelsList) == 0 {
375-
return &metric.Family{}
376-
}
377-
labelKeys, labelValues := createPrometheusLabelKeysValues("label", a.Labels, allowLabelsList)
378-
return &metric.Family{
379-
Metrics: []*metric.Metric{
380-
{
381-
LabelKeys: labelKeys,
382-
LabelValues: labelValues,
383-
Value: 1,
384-
},
385-
},
386-
}
387-
}),
388-
)
389-
}
390-
391335
func createHPAStatusCondition() generator.FamilyGenerator {
392336
return *generator.NewFamilyGeneratorWithStability(
393337
"kube_horizontalpodautoscaler_status_condition",
@@ -415,49 +359,3 @@ func createHPAStatusCondition() generator.FamilyGenerator {
415359
}),
416360
)
417361
}
418-
419-
func createHPACreated() generator.FamilyGenerator {
420-
return *generator.NewFamilyGeneratorWithStability(
421-
"kube_horizontalpodautoscaler_created",
422-
"Unix creation timestamp",
423-
metric.Gauge,
424-
basemetrics.ALPHA,
425-
"",
426-
wrapHPAFunc(func(a *autoscaling.HorizontalPodAutoscaler) *metric.Family {
427-
ms := []*metric.Metric{}
428-
429-
if !a.CreationTimestamp.IsZero() {
430-
ms = append(ms, &metric.Metric{
431-
Value: float64(a.CreationTimestamp.Unix()),
432-
})
433-
}
434-
435-
return &metric.Family{
436-
Metrics: ms,
437-
}
438-
}),
439-
)
440-
}
441-
442-
func createHPADeletionTimestamp() generator.FamilyGenerator {
443-
return *generator.NewFamilyGeneratorWithStability(
444-
"kube_horizontalpodautoscaler_deletion_timestamp",
445-
"Unix deletion timestamp",
446-
metric.Gauge,
447-
basemetrics.ALPHA,
448-
"",
449-
wrapHPAFunc(func(a *autoscaling.HorizontalPodAutoscaler) *metric.Family {
450-
ms := []*metric.Metric{}
451-
452-
if !a.DeletionTimestamp.IsZero() {
453-
ms = append(ms, &metric.Metric{
454-
Value: float64(a.DeletionTimestamp.Unix()),
455-
})
456-
}
457-
458-
return &metric.Family{
459-
Metrics: ms,
460-
}
461-
}),
462-
)
463-
}

internal/store/metadata.go

Lines changed: 158 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,158 @@
1+
/*
2+
Copyright 2025 The Kubernetes Authors All rights reserved.
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 store
18+
19+
import (
20+
basemetrics "k8s.io/component-base/metrics"
21+
22+
metaapi "k8s.io/apimachinery/pkg/api/meta"
23+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
24+
25+
"k8s.io/kube-state-metrics/v2/pkg/metric"
26+
27+
generator "k8s.io/kube-state-metrics/v2/pkg/metric_generator"
28+
)
29+
30+
var (
31+
descAnnotationsHelp = "Kubernetes annotations converted to Prometheus labels."
32+
descLabelsHelp = "Kubernetes labels converted to Prometheus labels."
33+
descCreationHelp = "Unix creation timestamp"
34+
descDeletionHelp = "Unix deletion timestamp"
35+
)
36+
37+
// createMetadataMetricFamiliesGenerator provides metadata metrics for all resources
38+
func createMetadataMetricFamiliesGenerator(allowAnnotationsList, allowLabelsList, descLabelsDefaultLabels []string, descPrefixName string, withUID bool) []generator.FamilyGenerator {
39+
wrapFunc := wrapMetadataFunc
40+
if withUID == true {
41+
wrapFunc = wrapMetadataWithUIDFunc
42+
}
43+
44+
return []generator.FamilyGenerator{
45+
*generator.NewFamilyGeneratorWithStability(
46+
descPrefixName+"_created",
47+
descCreationHelp,
48+
metric.Gauge,
49+
basemetrics.STABLE,
50+
"",
51+
wrapFunc(func(t *metav1.ObjectMeta) *metric.Family {
52+
ms := []*metric.Metric{}
53+
54+
if !t.CreationTimestamp.IsZero() {
55+
ms = append(ms, &metric.Metric{
56+
Value: float64(t.CreationTimestamp.Unix()),
57+
})
58+
}
59+
60+
return &metric.Family{
61+
Metrics: ms,
62+
}
63+
}, descLabelsDefaultLabels),
64+
),
65+
*generator.NewFamilyGeneratorWithStability(
66+
descPrefixName+"_deletion_timestamp",
67+
descDeletionHelp,
68+
metric.Gauge,
69+
basemetrics.ALPHA,
70+
"",
71+
wrapFunc(func(t *metav1.ObjectMeta) *metric.Family {
72+
ms := []*metric.Metric{}
73+
74+
if t.DeletionTimestamp != nil && !t.DeletionTimestamp.IsZero() {
75+
ms = append(ms, &metric.Metric{
76+
Value: float64(t.DeletionTimestamp.Unix()),
77+
})
78+
}
79+
80+
return &metric.Family{
81+
Metrics: ms,
82+
}
83+
}, descLabelsDefaultLabels),
84+
),
85+
86+
*generator.NewFamilyGeneratorWithStability(
87+
descPrefixName+"_annotations",
88+
descAnnotationsHelp,
89+
metric.Gauge,
90+
basemetrics.ALPHA,
91+
"",
92+
wrapFunc(func(t *metav1.ObjectMeta) *metric.Family {
93+
if len(allowAnnotationsList) == 0 {
94+
return &metric.Family{}
95+
}
96+
annotationKeys, annotationValues := createPrometheusLabelKeysValues("annotation", t.Annotations, allowAnnotationsList)
97+
return &metric.Family{
98+
Metrics: []*metric.Metric{
99+
{
100+
LabelKeys: annotationKeys,
101+
LabelValues: annotationValues,
102+
Value: 1,
103+
},
104+
}}
105+
}, descLabelsDefaultLabels),
106+
),
107+
*generator.NewFamilyGeneratorWithStability(
108+
descPrefixName+"_labels",
109+
descLabelsHelp,
110+
metric.Gauge,
111+
basemetrics.STABLE,
112+
"",
113+
wrapFunc(func(t *metav1.ObjectMeta) *metric.Family {
114+
if len(allowLabelsList) == 0 {
115+
return &metric.Family{}
116+
}
117+
labelKeys, labelValues := createPrometheusLabelKeysValues("label", t.Labels, allowLabelsList)
118+
return &metric.Family{
119+
Metrics: []*metric.Metric{
120+
{
121+
LabelKeys: labelKeys,
122+
LabelValues: labelValues,
123+
Value: 1,
124+
},
125+
}}
126+
127+
}, descLabelsDefaultLabels),
128+
),
129+
}
130+
}
131+
132+
func wrapMetadataFunc(f func(*metav1.ObjectMeta) *metric.Family, defaultLabels []string) func(any) *metric.Family {
133+
return func(obj any) *metric.Family {
134+
o := obj.(metav1.Object)
135+
objectMeta := metaapi.AsPartialObjectMetadata(o).ObjectMeta
136+
metricFamily := f(&objectMeta)
137+
138+
for _, m := range metricFamily.Metrics {
139+
m.LabelKeys, m.LabelValues = mergeKeyValues(defaultLabels, []string{o.GetNamespace(), o.GetName()}, m.LabelKeys, m.LabelValues)
140+
}
141+
142+
return metricFamily
143+
}
144+
}
145+
146+
func wrapMetadataWithUIDFunc(f func(*metav1.ObjectMeta) *metric.Family, defaultLabels []string) func(any) *metric.Family {
147+
return func(obj any) *metric.Family {
148+
o := obj.(metav1.Object)
149+
objectMeta := metaapi.AsPartialObjectMetadata(o).ObjectMeta
150+
metricFamily := f(&objectMeta)
151+
152+
for _, m := range metricFamily.Metrics {
153+
m.LabelKeys, m.LabelValues = mergeKeyValues(defaultLabels, []string{o.GetNamespace(), o.GetName(), string(o.GetUID())}, m.LabelKeys, m.LabelValues)
154+
}
155+
156+
return metricFamily
157+
}
158+
}

0 commit comments

Comments
 (0)