Skip to content

Commit 233ab4e

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

File tree

4 files changed

+163
-210
lines changed

4 files changed

+163
-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", wrapMetadataFunc)
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: 155 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,155 @@
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, w func(func(*metav1.ObjectMeta) *metric.Family, []string) func(any) *metric.Family) []generator.FamilyGenerator {
39+
wrapFunc := w
40+
41+
return []generator.FamilyGenerator{
42+
*generator.NewFamilyGeneratorWithStability(
43+
descPrefixName+"_created",
44+
descCreationHelp,
45+
metric.Gauge,
46+
basemetrics.STABLE,
47+
"",
48+
wrapFunc(func(t *metav1.ObjectMeta) *metric.Family {
49+
ms := []*metric.Metric{}
50+
51+
if !t.CreationTimestamp.IsZero() {
52+
ms = append(ms, &metric.Metric{
53+
Value: float64(t.CreationTimestamp.Unix()),
54+
})
55+
}
56+
57+
return &metric.Family{
58+
Metrics: ms,
59+
}
60+
}, descLabelsDefaultLabels),
61+
),
62+
*generator.NewFamilyGeneratorWithStability(
63+
descPrefixName+"_deletion_timestamp",
64+
descDeletionHelp,
65+
metric.Gauge,
66+
basemetrics.ALPHA,
67+
"",
68+
wrapFunc(func(t *metav1.ObjectMeta) *metric.Family {
69+
ms := []*metric.Metric{}
70+
71+
if t.DeletionTimestamp != nil && !t.DeletionTimestamp.IsZero() {
72+
ms = append(ms, &metric.Metric{
73+
Value: float64(t.DeletionTimestamp.Unix()),
74+
})
75+
}
76+
77+
return &metric.Family{
78+
Metrics: ms,
79+
}
80+
}, descLabelsDefaultLabels),
81+
),
82+
83+
*generator.NewFamilyGeneratorWithStability(
84+
descPrefixName+"_annotations",
85+
descAnnotationsHelp,
86+
metric.Gauge,
87+
basemetrics.ALPHA,
88+
"",
89+
wrapFunc(func(t *metav1.ObjectMeta) *metric.Family {
90+
if len(allowAnnotationsList) == 0 {
91+
return &metric.Family{}
92+
}
93+
annotationKeys, annotationValues := createPrometheusLabelKeysValues("annotation", t.Annotations, allowAnnotationsList)
94+
return &metric.Family{
95+
Metrics: []*metric.Metric{
96+
{
97+
LabelKeys: annotationKeys,
98+
LabelValues: annotationValues,
99+
Value: 1,
100+
},
101+
}}
102+
}, descLabelsDefaultLabels),
103+
),
104+
*generator.NewFamilyGeneratorWithStability(
105+
descPrefixName+"_labels",
106+
descLabelsHelp,
107+
metric.Gauge,
108+
basemetrics.STABLE,
109+
"",
110+
wrapFunc(func(t *metav1.ObjectMeta) *metric.Family {
111+
if len(allowLabelsList) == 0 {
112+
return &metric.Family{}
113+
}
114+
labelKeys, labelValues := createPrometheusLabelKeysValues("label", t.Labels, allowLabelsList)
115+
return &metric.Family{
116+
Metrics: []*metric.Metric{
117+
{
118+
LabelKeys: labelKeys,
119+
LabelValues: labelValues,
120+
Value: 1,
121+
},
122+
}}
123+
124+
}, descLabelsDefaultLabels),
125+
),
126+
}
127+
}
128+
129+
func wrapMetadataFunc(f func(*metav1.ObjectMeta) *metric.Family, defaultLabels []string) func(any) *metric.Family {
130+
return func(obj any) *metric.Family {
131+
o := obj.(metav1.Object)
132+
objectMeta := metaapi.AsPartialObjectMetadata(o).ObjectMeta
133+
metricFamily := f(&objectMeta)
134+
135+
for _, m := range metricFamily.Metrics {
136+
m.LabelKeys, m.LabelValues = mergeKeyValues(defaultLabels, []string{o.GetNamespace(), o.GetName()}, m.LabelKeys, m.LabelValues)
137+
}
138+
139+
return metricFamily
140+
}
141+
}
142+
143+
func wrapMetadataWithUIDFunc(f func(*metav1.ObjectMeta) *metric.Family, defaultLabels []string) func(any) *metric.Family {
144+
return func(obj any) *metric.Family {
145+
o := obj.(metav1.Object)
146+
objectMeta := metaapi.AsPartialObjectMetadata(o).ObjectMeta
147+
metricFamily := f(&objectMeta)
148+
149+
for _, m := range metricFamily.Metrics {
150+
m.LabelKeys, m.LabelValues = mergeKeyValues(defaultLabels, []string{o.GetNamespace(), o.GetName(), string(o.GetUID())}, m.LabelKeys, m.LabelValues)
151+
}
152+
153+
return metricFamily
154+
}
155+
}

0 commit comments

Comments
 (0)